diff options
author | quou <quou@disroot.org> | 2023-05-07 17:09:58 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2023-05-07 17:09:58 +1000 |
commit | 955f6ae56cdd3b20eabb44e7c9b931bdb17c2b3e (patch) | |
tree | 62f5dc92a675a5fdc48a0474d11d229cb5f2780d /sound.c | |
parent | 1867e71ac2870f904e0856fd3b093abed6e8a58b (diff) |
Basic music and sound.
Diffstat (limited to 'sound.c')
-rw-r--r-- | sound.c | 105 |
1 files changed, 105 insertions, 0 deletions
@@ -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++; + } +} |