logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git
commit: fb1727239bff5c5eaba68316cd60805d964f7914
parent 25287ed92b8cbfd75ffe83b56fb240019c3fcd2e
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri,  4 Mar 2022 22:10:19 +0100

kagome_kagome_clarinet.c: New

Diffstat:

AC/kagome_kagome_clarinet.c207+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 207 insertions(+), 0 deletions(-)

diff --git a/C/kagome_kagome_clarinet.c b/C/kagome_kagome_clarinet.c @@ -0,0 +1,207 @@ +//#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" + +// 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) % 210); + float w1 = 2*M_PI*hertz/header->samplerate; + + // https://pages.mtu.edu/~suits/clarinet.html + float tune = sin(w1*t) + 0.75*sin(3*w1*t) + 0.5*sin(5*w1*t) + 0.14*sin(7*w1*t) + 0.5*sin(9*w1*t) + 0.12*sin(11*w1*t) + 0.17*sin(13*w1*t); + putchar((uint8_t)(tune * volume)); + } +} + +#if 0 +void +note(int dur, int pitch) { + for(int t = 0;t<dur;t++) { + putchar( t*pitch ); + } +} +#else +#define REF_TUNE 440/4 +#define VOLUME 60 +#define note(dur,pitch) sin_note(dur, header, note_to_hertz(REF_TUNE, pitch-8), VOLUME, 0) +#endif + +void +silence(int dur) { + for(int t = 0;t<dur;t++)putchar( 0x00 ); +} + +void +kagome_kagome(struct au_header *header) { + int tick = header->samplerate / 1.9; + //int note = tick * 0.99; + //int gap = tick * 0.01; + int note = tick; + int gap = 0; + + note(note*2, 8); silence(gap*2); + + note(note*1, 8); silence(gap); + + note(note*1, 9); silence(gap); + + // ---- + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + silence(tick*1); + + // ---- + + note(note*1, 8); silence(gap); + + note(note/2, 8); silence(gap/2); + note(note/2, 8); silence(gap/2); + + note(note*1, 8); silence(gap); + + note(note/2, 7); silence(gap/2); + note(note/2, 7); silence(gap/2); + + // ---- + + note(note*1, 8); silence(gap); + + note(note/2, 8); silence(gap/2); + note(note/2, 7); silence(gap/2); + + note(note*1, 5); silence(gap); + + silence(tick*1); + + // ---- + + note(note, 8); silence(gap); + + note(note, 7); silence(gap); + + note(note, 8); silence(gap); + + note(note, 7); silence(gap); + + // ---- + + note(note*1, 8); silence(gap); + + note(note/2, 8); silence(gap/2); + note(note/2, 7); silence(gap/2); + + note(note*1, 5); silence(gap); + + silence(tick*1); + + // ---- + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + note(note*1, 9); silence(gap); + + // ---- + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + silence(tick*1); + + // ---- + + note(note*1, 8); silence(gap); + + note(note/2, 7); silence(gap/2); + note(note/2, 7); silence(gap/2); + + note(note*1, 8); silence(gap); + + note(note/2, 7); silence(gap/2); + note(note/2, 7); silence(gap/2); + + // ---- + + note(note*1, 8); silence(gap); + + note(note*1, 8); silence(gap); + + note(note*1, 5); silence(gap); + + silence(tick*1); + + // ---- + + note(note/2, 8); silence(gap/2); + note(note/2, 8); silence(gap/2); + + note(note/2, 8); silence(gap/2); + note(note/2, 8); silence(gap/2); + + note(note*1, 8); silence(gap); + + note(note*1, 9); silence(gap); + + // ---- + + note(tick*1, 8); // silence(gap); + + note(note*1, 7); silence(gap); + + note(note*1, 8); silence(gap); + + silence(tick*1); +} + +int +main(void) +{ + // Use AU defaults + struct au_header header = { + .offset = 24, // no-annotation, in octets + .length = 0xFFFFFFFF, // unknown data size + //.encoding = AU_ENCODING_32B_FPCM, + .encoding = 02, + .samplerate = 48000, // Hz + .channels = 1 + }; + + // fd 1 is stdout + write_au_header(1, &header); + + kagome_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; +}