From 955f6ae56cdd3b20eabb44e7c9b931bdb17c2b3e Mon Sep 17 00:00:00 2001 From: quou Date: Sun, 7 May 2023 17:09:58 +1000 Subject: Basic music and sound. --- sound.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 sound.c (limited to 'sound.c') diff --git a/sound.c b/sound.c new file mode 100644 index 0000000..611bca9 --- /dev/null +++ b/sound.c @@ -0,0 +1,105 @@ +#include "config.h" +#include "sound.h" + +#if DEBUG +#include "error.h" +#include "platform.h" +#endif + +typedef struct { + int pitch; + int length; +} Beep; + +static struct { + unsigned t; + Song song; + Beep beeps[max_beeps]; + int beep_count; +} sys; + +typedef unsigned char (*Song_Func)(unsigned t); + +static unsigned char menu_song(unsigned t) { + return 0; +} + +static unsigned char main_song(unsigned t) { + /* From tejeez 2011-10-05 + * I didn't have time to make my own music PepeHands */ + return (~t>>2)*((127&t*(7&t>>10))<(245&t*(2+(5&t>>14)))); +} + +static unsigned char dead_song(unsigned t) { + return 0; +} + +static unsigned char win_song(unsigned t) { + return 0; +} + +void init_sound() { + sys.t = 0; + sys.song = song_menu; +} + +void set_song(Song song) { + sys.song = song; + sys.t = 0; +} + +Song_Func get_current_song_f() { + switch (sys.song) { + case song_menu: + return menu_song; + case song_main: + return main_song; + case song_win: + return win_song; + case song_dead: + return dead_song; + } + return 0; +} + +void play_beep(int pitch, int length) { + Beep* beep; + +#if DEBUG + if (sys.beep_count >= max_beeps) { + platform_err("Too many beeps.\n"); + platform_abort(error_sound_error); + } +#endif + + beep = &sys.beeps[sys.beep_count++]; + beep->pitch = pitch; + beep->length = length; +} + +void sound_mix(unsigned char* stream, int len) { + int i, j; + Song_Func f; + Beep* beep; + + f = get_current_song_f(); + + for (i = 0; i < len; i++) { +/* stream[i] = sys.t % 50;*/ + stream[i] = 0; + stream[i] = f(sys.t) / 2; + for (j = sys.beep_count - 1; j >= 0; j--) { + beep = &sys.beeps[j]; + stream[i] |= (sys.t % beep->pitch) * 5; + beep->length--; + + if (beep->length <= 0) { + if (sys.beep_count > 1) { + *beep = sys.beeps[sys.beep_count - 1]; + } + sys.beep_count--; + } + } + sys.t++; + } +} -- cgit v1.2.3-54-g00ecf