From b42e71f0d9004c704c64b8c72bdcb0b71fe2dad2 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Sat, 9 Dec 2023 19:38:15 -0500 Subject: [PATCH] Add audio --- C/audio/README.md | 14 ++++++++++ C/audio/waves.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 C/audio/README.md create mode 100644 C/audio/waves.c diff --git a/C/audio/README.md b/C/audio/README.md new file mode 100644 index 0000000..0b48c77 --- /dev/null +++ b/C/audio/README.md @@ -0,0 +1,14 @@ +# Audio + +Compile the program with + + +```bash +gcc -lm waves.c +``` + +Execute and hear the output with + +```bash +./a.out | aplay +``` diff --git a/C/audio/waves.c b/C/audio/waves.c new file mode 100644 index 0000000..863cd8f --- /dev/null +++ b/C/audio/waves.c @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 12-30-20 +// Testing command line audio + +#include +#include +#include +#include + +int main(int argc, char** argv){ + if(argc != 1){ + printf("Usage: %s\n", argv[0]); + return -1; + } + + //sawtooth wave + for(int t=0, osc=0; t<8000; t++, osc += 14){ + putchar(osc); + } + + //square wave + for(int t=0, osc=0; t<8000; t++, osc += 14){ + putchar(osc & 0x80); + } + + //sine wave + for(int t=0, osc=0; t<8000; t++, osc += 14){ + putchar(127 * sin(osc/255.0*2*3.1415) + 128); + } + + + /* kinda bass guitar? + * + * The idea here is an array full of random data, with + * an array size equal to the period of the wave + * (for 440 its ~18) then loop the array. + * Then we average out the current value and next one + * Till we get a faded noise to silence + * + * Pretty cool ngl + */ + + unsigned char a[18]; + + //init the array with random data + FILE *fp; + fp = fopen("/dev/random", "r"); + if(fp == NULL){ + printf("Couldnt open random stream!\n"); + return -1; + } + if(fgets(a,sizeof(a),fp)==NULL){ + printf("Failed to read the random stream!\n"); + return -1; + } + fclose(fp); + + //play the bass guitar + for(int t=0;t<8000;t++){ + int i = t % sizeof(a); + int j = (t+1) % sizeof(a); + putchar(a[i] = (a[i] + a[j])/2); + } + + return 0; +} -- 2.45.2