logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git
commit: d76bf60d58c72a65c018fe6335d6b8e2cae8dd59
parent 186129564113e2d3996b5f94ead92ecbf8a7e8a9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  9 Jan 2022 12:31:44 +0100

c/kagome_kagome: Sinusoidal version

Diffstat:

MC/kagome_kagome.c60++++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 54 insertions(+), 6 deletions(-)

diff --git a/C/kagome_kagome.c b/C/kagome_kagome.c @@ -1,19 +1,61 @@ -#define _POSIX_C_SOURCE 200809L +//#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 #include <stdio.h> #include <inttypes.h> // uint32_t +#include <math.h> // sinf(), M_PI, pow() +#include <stdlib.h> // abort() #include "au.h" #define TICK 3600 -#define NOTE (TICK*1) -#define GAP (TICK*0) +#define NOTE (TICK*0.99) +#define GAP (TICK*0.01) + +// taken from beep(1) manpage +#define MID_C_C 261.63 +#define MID_C_CS 277.18 +#define MID_C_DB 277.18 +#define MID_C_D 293.66 +#define MID_C_DS 311.13 +#define MID_C_EB 311.13 +#define MID_C_E 329.63 +#define MID_C_F 349.23 +#define MID_C_FS 369.99 +#define MID_C_FB 369.99 +#define MID_C_G 392.00 +#define MID_C_GS 415.30 +#define MID_C_AB 415.30 +#define MID_C_A 440.00 +#define MID_C_AS 466.16 +#define MID_C_BB 466.16 +#define MID_C_B 493.88 + +// https://pages.mtu.edu/~suits/NoteFreqCalcs.html +#define TWELTH_ROOT_2 1.059463094359 +double +note_to_hertz(double ref_tune, int steps) { + return ref_tune*pow(TWELTH_ROOT_2,steps); +} + +void +sin_note(int dur, struct au_header *header, int hertz, float volume, float delay) { + char buf[4]; + + for(int t = 0;t<dur;t++) { + putchar((uint8_t)(sinf((delay + t) * 2*M_PI*hertz/header->samplerate) * volume)); + } +} +#if 0 void note(int dur, int pitch) { for(int t = 0;t<dur;t++) { putchar( t*pitch ); } } +#else +#define note(dur,pitch) sin_note(dur, header, note_to_hertz(440, pitch-8), 75, 0) +#endif void silence(int dur) { @@ -21,7 +63,7 @@ silence(int dur) { } void -kagome() { +kagome(struct au_header *header) { note(NOTE*2, 8); silence(GAP*2); note(NOTE*1, 8); silence(GAP); @@ -162,8 +204,14 @@ main(void) // fd 1 is stdout write_au_header(1, &header); - kagome(); - kagome(); + kagome(&header); + kagome(&header); + + // dur pt vol del + //sin_note(header.samplerate, &header, 440, 150, 0); + //sin_note(header.samplerate/2, &header, note_to_hertz(440, 0), 150, 0); + //sin_note(header.samplerate/4, &header, note_to_hertz(440, 1), 150, 0); + //sin_note(header.samplerate/4, &header, note_to_hertz(440, 1), 150, 0); return 0; }