Skip to content

Instantly share code, notes, and snippets.

@jcmvbkbc
Created December 1, 2016 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcmvbkbc/deca796d5025298f3e90e31bca943225 to your computer and use it in GitHub Desktop.
Save jcmvbkbc/deca796d5025298f3e90e31bca943225 to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#ifndef FREQ
#define FREQ 44100
#endif
#ifndef CHANNELS
#define CHANNELS 2
#endif
#ifndef SAMPLE_WIDTH
#define SAMPLE_WIDTH 16
#endif
int main()
{
unsigned data_size = 8 * 3 * FREQ * CHANNELS * (SAMPLE_WIDTH / 8);
struct riff {
char id[4];
uint32_t sz;
uint32_t fmt;
char sub1[4];
uint32_t sz1;
uint16_t au_fmt;
uint16_t channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
char sub2[4];
uint32_t sz2;
} hdr = {
.id = "RIFF",
.sz = data_size + 0x24,
.fmt = 0x45564157,
.sub1 = "fmt ",
.sz1 = 0x10,
.au_fmt = 1,
.channels = CHANNELS,
.sample_rate = FREQ,
.byte_rate = FREQ * CHANNELS * SAMPLE_WIDTH / 8,
.block_align = CHANNELS * SAMPLE_WIDTH / 8,
.bits_per_sample = SAMPLE_WIDTH,
.sub2 = "data",
.sz2 = data_size,
};
unsigned freq;
const unsigned amp = 1 << (SAMPLE_WIDTH - 2);
write(STDOUT_FILENO, &hdr, sizeof(hdr));
for (freq = 440; freq < FREQ / 2; freq *= 2) {
unsigned i;
for (i = 0; i < 3 * FREQ; ++i) {
int32_t v[2] = {
amp * sin((2 * 3.141592653589793 * freq * i) / FREQ),
0x55555555,
};
write(STDOUT_FILENO, v, SAMPLE_WIDTH / 8);
write(STDOUT_FILENO, v + 1, SAMPLE_WIDTH / 8);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment