Sawuare's Site

Wafer

Wafer is a library for writing 8-bit PCM WAVE files. It is written in C and consists of a single header: wafer.h. It is part of Misk.

Here is an example of using Wafer to write an audio file that has 1 channel and 22050 samples per second:

#include "wafer.h"

#define Q 8160
#define L 256

int main(void) {
  uint8_t audio[L * L];

  for (int y = 0; y < L; ++y)
    for (int x = 0; x < L; ++x)
      audio[y * L + x] = ((Q ^ x) * x & (Q ^ y) * y) & Q ? 0 : 255;

  wafer_file *file = wafer_open("wafer-example.wav");
  wafer_set_channels(file, 1);
  wafer_set_samples_per_sec(file, 22050);
  wafer_write_metadata(file);
  wafer_write_data(audio, sizeof audio, file);
  wafer_close(file);
}

Here is the audio file, which is very loud:

Fun fact: Each sample in the audio file corresponds to a pixel in the following HPQ image. From top to bottom and left to right, a white pixel corresponds to a sample with amplitude 255, and a black pixel corresponds to a sample with amplitude 0:

p32 q8160