diff --git a/src/audio.c b/src/audio.c index 558b1a4..dffc115 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,16 +1,17 @@ #include "audio.h" +#include void generate_audio(void); struct game_sounds game_sounds; Uint8 big_hum(unsigned int t); -Uint8 func0(int t); +Uint8 func0(unsigned int t); void fill_audio(void *func, Uint8 *stream, int len) { - Uint8 (*wavegen)(int) = func; + Uint8 (*wavegen)(unsigned int) = func; - static int t = 1; + unsigned int t = 1; for(int i = 0; i < len; i++,t++) { stream[i] = wavegen(t); } @@ -38,14 +39,20 @@ void start_audio(void) { fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); exit(1); } + + if (Mix_AllocateChannels(20)) { + fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); + } + generate_audio(); - Mix_Resume(-1); + } void play_game_sound(Mix_Chunk *chunk, int len, int channel) { if (Mix_Playing(channel)) { return; } + Mix_FadeInChannelTimed(channel, chunk, -1, 1, len); } @@ -97,10 +104,10 @@ void callbackfn(void *unused, Uint8 *stream, int len) /* Waveform Generators */ Uint8 big_hum(unsigned int t) { - return ( (sin(t*2*3.14159265/16000*200)+1) * 20) + ( (sin(t*2*3.14159265/16000*110)+1) * 20) + ( (sin(t*2*3.14159265/16000*230)+1) * 20) + ( (sin((t % 80)*2*3.14159265/16000 * 6)+1) * 12); + return ( (sin(t*2*3.14159265/8000*200)+1) * 20) + ( (sin(t*2*3.14159265/8000*110)+1) * 20) + ( (sin(t*2*3.14159265/8000*230)+1) * 20) + ( (sin((t % 80)*2*3.14159265/8000 * 6)+1) * 12); } -unsigned int sine_wave_sound(int t) { +unsigned int sine_wave_sound(unsigned int t) { const double R=8000; // sample rate (samples per second) const double C=261.625565; // frequency of middle-C (hertz) const double F=R/256; // bytebeat frequency of 1*t due to 8-bit truncation (hertz) @@ -108,17 +115,18 @@ unsigned int sine_wave_sound(int t) { return (sin(t*2*M_PI/R*C)+1)*V; } -unsigned int winch_sound(int t) { - return (t % 80) | (t % 36) | (6 * (t % 6)); +Uint8 winch_sound(unsigned t) { + return (((t % 80) | (t % 36) | (6 * (t % 6))) + 20) * 1.6; + return ((t % 80) | (t % 36) | (6 * (t % 6))); } -unsigned int beat_2(int t) { +Uint8 beat_2(unsigned t) { return ((((t % 250) / 2)) | (t % 129)) % 110; } -Uint8 func0(int t) { +Uint8 func0(unsigned t) { int note = 18; - if (t % 1240) { + if (!(t % 1240)) { note = (1 + rand()) % 18 + 1; } @@ -126,26 +134,57 @@ Uint8 func0(int t) { return (t % note); } -unsigned int func1(int t) { +Uint8 func1(unsigned int t) { int note = 10; if (!(t % 8000)) { note = (1 + rand()) % 10 + 5; - printf("%d %d\n", t, note); } return ((t & 110) % 73 | (t % 1711)); } +Uint8 collision_test(unsigned int t) { + // try not to loop + return ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351)) + + ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 11)); + return ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351)) + + ((4000 - t % 4000) * 0.0003 * (t % 128)) + + ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 64)); // has a big low sound + return (t % 4) * (4000 - t % 4000) * 0.01; +} -void generate_audio(void) { - const int sfx_len_ms = 2000; +Uint8 boop_noise(unsigned int t) { + return ((t >> 10) & 42) * t; +} + +Uint8 rope_attach(unsigned int t) { + return (((2000 - (t % 2000)))/40 * (t % 20) | (t % 44) + 20) * 1.6; +} + +void generate_audio(void) { + const int sfx_len_ms = 1000; unsigned int sfx_len = (G_AUDIO_SFREQ/1000) * G_AUDIO_BSIZE * sfx_len_ms; - Uint8 raw_data[sfx_len]; // 10 seconds + Uint8 *raw_data = malloc(sfx_len); fill_audio(winch_sound, raw_data, sfx_len); game_sounds.rope_pull = Mix_QuickLoad_RAW(raw_data, sfx_len); - fill_audio(beat_2, raw_data, sfx_len); - game_sounds.collision = Mix_QuickLoad_RAW(raw_data, sfx_len); + Mix_Volume(BC_ROPE_PULL, MIX_MAX_VOLUME * 0.4); + + Uint8 *raw_data2 = malloc(sfx_len); + fill_audio(collision_test, raw_data2, sfx_len); + game_sounds.collision = Mix_QuickLoad_RAW(raw_data2, sfx_len); + //Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME); + + Uint8 *raw_data3 = malloc(sfx_len); + fill_audio(big_hum, raw_data3, sfx_len); + game_sounds.background = Mix_QuickLoad_RAW(raw_data3, sfx_len); + Mix_Volume(BC_MUSIC, MIX_MAX_VOLUME * 0.2); + + Uint8 *raw_data4 = malloc(sfx_len); + fill_audio(rope_attach, raw_data4, sfx_len); + game_sounds.rope_attach = Mix_QuickLoad_RAW(raw_data4, sfx_len); + //Mix_Volume(BC_ROPE_ATTACH, MIX_MAX_VOLUME); + } diff --git a/src/audio.h b/src/audio.h index fff0f6c..16e55f9 100644 --- a/src/audio.h +++ b/src/audio.h @@ -15,6 +15,7 @@ struct game_sounds { Mix_Chunk *collision; Mix_Chunk *rope_attach; Mix_Chunk *rope_pull; + Mix_Chunk *background; /* Looping samples (need backbuffer to fill while playing ) */ Mix_Chunk *menu; }; @@ -23,4 +24,11 @@ extern struct game_sounds game_sounds; void play_game_sound(Mix_Chunk *chunk, int len, int channel); void start_audio(void); + +enum audio_channel_names { + BC_COLLISION = 1, + BC_ROPE_PULL = 2, + BC_ROPE_ATTACH = 3, + BC_MUSIC = 4, +}; #endif diff --git a/src/game.c b/src/game.c index 0c8ee42..3f5323e 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,8 @@ #include "game.h" #include "draw.h" +#include "audio.h" +#include "types.h" +#include #include #include #include @@ -92,7 +95,6 @@ long get_best_time(void) { } - int load_score_times(char *filename) { char *fn; @@ -915,6 +917,8 @@ void ratcheted_winch_motor_update(Motor* motor) { body->strings[0].max_length = vect_mag(st); winch_motor_update(motor); + play_game_sound(game_sounds.rope_pull, 300, BC_ROPE_PULL); + } void winch_motor_update (Motor* motor) { @@ -950,6 +954,8 @@ void pull_rope(double newtons) { if (!player.physics->motors[M_WINCH].stop) { return; } + + // set force set_motor_newtons(player.physics, M_WINCH, 0, newtons); set_motor_status(player.physics, M_WINCH, true); @@ -959,6 +965,7 @@ void pull_rope(double newtons) { } void stop_pull_rope(void) { + Mix_HaltChannel(BC_ROPE_PULL); player.physics->motors[M_WINCH].stop = true; } @@ -968,6 +975,8 @@ void add_rope(int mouse_x, int mouse_y) { return; } + play_game_sound(game_sounds.rope_attach, 400, -1); + Vect mouse; mouse.x = mouse_x; mouse.y = mouse_y; @@ -1597,6 +1606,8 @@ void startgame(SDL_Renderer * ren) { game_paused = 0; viewport_pos.x = player.physics->position.x - width / 2; viewport_pos.y = player.physics->position.y - height / 2; + + play_game_sound(game_sounds.background , -1, BC_MUSIC); //get_room(); } diff --git a/src/main.c b/src/main.c index 6e9d326..237a5d7 100644 --- a/src/main.c +++ b/src/main.c @@ -11,8 +11,7 @@ #include #include -#include "audio.h" -// only for mkdirat lol +#include "audio.h" // only for mkdirat lol #include #include @@ -113,6 +112,7 @@ int game(void) { physics_thread = SDL_CreateThread(physics_loop, "physics", (void *)ren); start_audio(); + play_game_sound(game_sounds.background , -1, BC_MUSIC); int count = 0; while (!close) { @@ -144,7 +144,7 @@ int game(void) { redraw(ren); if (player.physics->colliding) { count++; - play_game_sound(game_sounds.collision, 200, 1); + play_game_sound(game_sounds.collision, 437, BC_COLLISION); } else { count = 0; }