logo

bytemedia

Home to byte-level sounds, images, videos, … git clone https://hacktivis.me/git/bytemedia.git
commit: dcb0df488d5f9eb90a881f6842f470b2b4821be6
parent 6586c3e92510a75d16009bd06fb3a1bba28f3810
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 22 Mar 2022 22:54:01 +0100

kagome_kagome: Extract reusable functions to headers

Diffstat:

MC/Makefile11++++++-----
AC/effects.h14++++++++++++++
AC/instruments.h23+++++++++++++++++++++++
MC/kagome_kagome.c47+++++++----------------------------------------
AC/misc.h14++++++++++++++
5 files changed, 64 insertions(+), 45 deletions(-)

diff --git a/C/Makefile b/C/Makefile @@ -1,6 +1,8 @@ WAYLAND_PROTOCOLS=`pkg-config --variable=pkgdatadir wayland-protocols` WAYLAND_SCANNER=`pkg-config --variable=wayland_scanner wayland-scanner` +.SUFFIXES: .au + all: au8b_8kHz au8b_44.1kHz SDL_YUV_visualisation wayland kagome_kagome.au SDL_YUV_visualisation: SDL_YUV_visualisation.c @@ -15,11 +17,10 @@ xdg-shell-protocol.c: xdg-shell-client-protocol.h wayland: wayland.c xdg-shell-protocol.c $(CC) $(CFLAGS) -o $@ $< xdg-shell-protocol.c -lwayland-client -lrt -kagome_kagome: kagome_kagome.c au.h - $(CC) $(CFLAGS) -o $@ $< -lm - -kagome_kagome.au: kagome_kagome - ./kagome_kagome > kagome_kagome.au +.c.au: + $(CC) $(CFLAGS) -o $* $< -lm + ./$* > $@ + rm -f $* .PHONY: clean clean: diff --git a/C/effects.h b/C/effects.h @@ -0,0 +1,14 @@ +float +crossfade(int dur, int t, float vol) +{ + float att = dur/9; + float decay = dur/3; + + if(t < att) { + return vol*(t/att); + } else if((dur-t) < decay) { + return vol*((dur-t)/decay); + } else { + return vol; + } +} diff --git a/C/instruments.h b/C/instruments.h @@ -0,0 +1,23 @@ +#ifndef _XOPEN_SOURCE +#warning Should #define _XOPEN_SOURCE 700 +#define _XOPEN_SOURCE 700 +#endif + +#include <math.h> // sinf(), M_PI + +float +pure_sin(struct au_header *header, int hertz, int t) +{ + float w1 = 2*M_PI*hertz/header->samplerate; + + return sinf(w1*t); +} + +float +clarinet(struct au_header *header, int hertz, int t) +{ + float w1 = 2*M_PI*hertz/header->samplerate; + 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); + + return tune/10; +} diff --git a/C/kagome_kagome.c b/C/kagome_kagome.c @@ -6,43 +6,15 @@ #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); -} - -float -crossfade(int dur, int t, float vol) -{ - float att = dur/9; - float decay = dur/3; - - if(t < att) { - return vol*(t/att); - } else if((dur-t) < decay) { - return vol*((dur-t)/decay); - } else { - return vol; - } -} - -float -clarinet(struct au_header *header, int hertz, int t) -{ - float w1 = 2*M_PI*hertz/header->samplerate; - - return 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); -} +#include "effects.h" +#include "instruments.h" +#include "misc.h" void -sin_note(int dur, struct au_header *header, int hertz, float volume, float delay) { +sin_note(int dur, struct au_header *header, int hertz, float volume) { 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 tune = clarinet(header, hertz, t); float vol = crossfade(dur, t, volume); @@ -58,17 +30,12 @@ note(int dur, int pitch) { } } #else -#define REF_TUNE 440*1 -#define VOLUME 10 -#define note(dur,pitch) sin_note(dur, header, note_to_hertz(REF_TUNE, pitch-8), VOLUME, 0) +#define REF_TUNE 430*1 +#define VOLUME 60 +#define note(dur,pitch) sin_note(dur, header, note_to_hertz(REF_TUNE, pitch-8), VOLUME) #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 / 2; int note = tick * 1.0; diff --git a/C/misc.h b/C/misc.h @@ -0,0 +1,14 @@ +#include <math.h> // pow +#include <stdio.h> // putchar + +// 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 +silence(int dur) { + for(int t = 0;t<dur;t++)putchar( 0x00 ); +}