--- /dev/null
+#include "colon3sampler.h"
+/* technically ive written this before but i want to redo it */
+
+#define HEADLEN 44
+#define FMTLEN 16
+
+static int write_blank_header(char *out_filename);
+static int append_wave(char *out_filename, int16_t wave[], int w_B);
+
+int write_wave(char *out_filename, int16_t wave[], int w_B)
+{
+ write_blank_header(out_filename);
+ append_wave(out_filename, wave, w_B);
+ return 1;
+};
+
+static int write_blank_header(char *fn)
+{
+ FILE *fp;
+ fp = fopen(fn, "wb");
+ struct __attribute__((packed)) header {
+ const char riff[4];
+ const uint32_t riff_len;
+ const char wave[4];
+ const char fmt_[4];
+ const uint32_t fmt__len;
+ const uint16_t format_tag;
+ const uint16_t channels;
+ const uint32_t sampling_rate;
+ const uint32_t bytes_per_second;
+ const uint16_t block_align;
+ const uint16_t bits_per_sample;
+ const uint16_t data[4];
+ const uint32_t data_size;
+ };
+ const struct header wavheader = {
+ {'R', 'I', 'F', 'F'},
+ HEADLEN-8,
+ {'W', 'A', 'V', 'E'},
+ {'f', 'm', 't', ' '},
+ FMTLEN,
+ 1,
+ 1,
+ 44100,
+ 88200,
+ 2,
+ 16,
+ {'d', 'a', 't', 'a'},
+ 0
+ };
+ int b_w = fwrite(&wavheader, 1, HEADLEN, fp);
+ if (b_w < HEADLEN) goto error;
+ fclose(fp);
+ return 1;
+error:
+ printf("oh no error");
+ return 0;
+};
+
+static int append_wave(char *fn, int16_t w[], int w_B)
+{
+ if (fn == NULL) return 0;
+ FILE *fp;
+ fp = fopen(fn, "r+b"); /* not append since that writes to the end of
+ the file only, not write since that removes
+ the file. b does nothing on Linux/any POSIX
+ conforming system but on Windows is needed.
+ otherwise it won't work */
+ /* Not going to actually read how much the length is since i'm just
+ * overwriting it. It might be faster to combine this and the header
+ * part, actually... */
+
+ fseek(fp, 4, SEEK_SET); /* Filesize - 8 */
+ const uint32_t riffsize = HEADLEN + w_B - 8;
+ fwrite(&riffsize, 4, 1, fp);
+
+ fseek(fp, 40, SEEK_SET); /* Wave size */
+ const uint32_t wavesize = (uint32_t)w_B;
+ fwrite(&wavesize, 4, 1, fp);
+
+ /* should be right at the start of the wave :) */
+ int w_i = fwrite(w, 2, w_B, fp); /* w_B 2byte items */
+ if (w_i < w_B) goto error;
+ fclose(fp);
+ return 1;
+error:
+ printf("error with appending wave");
+ return 0;
+};