Skip to content

Instantly share code, notes, and snippets.

@x42
Created November 14, 2021 14:49
Show Gist options
  • Save x42/09c4ff482a6b728198c8ff6aa02bdc66 to your computer and use it in GitHub Desktop.
Save x42/09c4ff482a6b728198c8ff6aa02bdc66 to your computer and use it in GitHub Desktop.
// g++ -o /tmp/sf_out sf_out.cc -Wall -lsndfile
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sndfile.h>
int main (int argc, char** argv)
{
SF_INFO _info;
memset (&_info, 0, sizeof (_info));
_info.channels = 1;
_info.samplerate = 48000;
_info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
uint32_t n_samples = _info.samplerate * 4;
uint32_t bpm = 120;
uint32_t samples_per_beat = _info.samplerate * 60 / bpm;
float* out = (float*) calloc (n_samples, sizeof (float));
uint32_t p = 0;
do {
out[p] = 1.0;
p += samples_per_beat;
} while (p < n_samples);
SNDFILE* sf_out = sf_open ("/tmp/out.wav", SFM_WRITE, &_info);
sf_write_float (sf_out, out, n_samples);
free (out);
sf_close (sf_out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment