Skip to content

Instantly share code, notes, and snippets.

@DuskyElf
Created September 8, 2023 10:41
Show Gist options
  • Select an option

  • Save DuskyElf/10987c1ff51bee7ae383465f4903b859 to your computer and use it in GitHub Desktop.

Select an option

Save DuskyElf/10987c1ff51bee7ae383465f4903b859 to your computer and use it in GitHub Desktop.
A Simple Sine wave frequency generator
/*
* Freq Generator
* A Simple Sine wave frequency generator
*
* Developed by - DuskyElf
* MIT Licence @duskyelf 2023
*
* compile command -
* `cc -o sine_freq sine_freq.c -lm`
*
* requirements -
* ffmpeg (ffplay)
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define FREQ 440
#define VOLUME 0.01f
#define DURATION 5
#define SAMPLE_RATE 44100
const char *output_file = "output.bin";
#define Arr_Len(arr) (sizeof(arr) / sizeof(arr[0]))
float wave[SAMPLE_RATE * DURATION];
int main(void) {
printf("Generating %s...\n", output_file);
FILE *output = fopen(output_file, "wb");
assert(output_file != NULL && "Could not open file");
for (int i = 0; i < Arr_Len(wave); ++i) {
wave[i] = sinf((float)i / (float)SAMPLE_RATE * 2 * M_PI * FREQ) * VOLUME;
}
fwrite(wave, sizeof(float), Arr_Len(wave), output);
char buff[1024] = {0};
sprintf(buff, "ffplay -f f32le -ar %d %s", SAMPLE_RATE, output_file);
system(buff);
fclose(output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment