From 0aafdff1f08889c207cc16109aa1e76714d1c34f Mon Sep 17 00:00:00 2001 From: alistair Date: Sun, 27 Nov 2022 15:36:34 +1000 Subject: [PATCH] fix audio popping --- build-android/src/audio.c | 29 +++++++++++++++++++---------- build-android/src/game.c | 9 +++++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/build-android/src/audio.c b/build-android/src/audio.c index 32949b3..339218b 100644 --- a/build-android/src/audio.c +++ b/build-android/src/audio.c @@ -17,6 +17,9 @@ int mute = false; unsigned int t = 1; for(int i = 0; i < len; i++,t++) { stream[i] = wavegen(t); + if (len - i < 300) { + stream[i] = (float)wavegen(t) * (len - i) / 300.0; + } } } @@ -38,7 +41,7 @@ void start_audio(void) { } */ //SDL_PauseAudio(0); - if (Mix_OpenAudio(G_AUDIO_SFREQ,AUDIO_U8,1,G_AUDIO_BSIZE) < 0) { + if (Mix_OpenAudio(G_AUDIO_SFREQ,AUDIO_S8,1,G_AUDIO_BSIZE) < 0) { fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); exit(1); } @@ -58,9 +61,8 @@ play_game_sound(Mix_Chunk *chunk, int len, int channel) return; } - if(Mix_PlayChannelTimed(channel, chunk, -1, len) < 0) + if(Mix_PlayChannel(channel, chunk, 1) < 0) fprintf(stderr, "Unable to play audio: %s\n", SDL_GetError()); - //Mix_FadeInChannelTimed(channel, chunk, -1, 1, len); } Uint8 @@ -179,8 +181,15 @@ Uint8 func1(unsigned int t) { Uint8 collision_test(unsigned int t) { // try not to loop - return ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351)) + int x = ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351)) + ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 14)); + float y = x; + float lerp = 1; + if (t > 80) + lerp = (100 - t) / 30; + if (lerp > 1) lerp = 1; + if (lerp < 0) lerp = 0; + return y; 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 @@ -201,22 +210,22 @@ void generate_audio(void) { unsigned int sfx_len = (G_AUDIO_SFREQ/1000) * G_AUDIO_BSIZE * sfx_len_ms; sfx_len = G_AUDIO_BSIZE; - Uint8 *raw_data = malloc(sfx_len); + Uint8 *raw_data = calloc(sfx_len, sizeof(Uint8)); fill_audio(winch_sound, raw_data, sfx_len); game_sounds.rope_pull = Mix_QuickLoad_RAW(raw_data, sfx_len); - Mix_Volume(BC_ROPE_PULL, MIX_MAX_VOLUME * 0.3); + Mix_Volume(BC_ROPE_PULL, MIX_MAX_VOLUME * 0.1); - Uint8 *raw_data2 = malloc(sfx_len); + Uint8 *raw_data2 = calloc(sfx_len, sizeof(Uint8)); 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 * 1.2); + Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME); - Uint8 *raw_data3 = malloc(sfx_len); + Uint8 *raw_data3 = calloc(sfx_len, sizeof(Uint8)); 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.1); - Uint8 *raw_data4 = malloc(sfx_len); + Uint8 *raw_data4 = calloc(sfx_len, sizeof(Uint8)); 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 * 0.1); diff --git a/build-android/src/game.c b/build-android/src/game.c index 0948e01..33eb6e6 100644 --- a/build-android/src/game.c +++ b/build-android/src/game.c @@ -1324,9 +1324,10 @@ void advance_thing(Body * thing) { translation.x = translation.x / mag; translation.y = translation.y / mag; - if (mag > 0.10) { - Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME * mag); - play_game_sound(game_sounds.collision, 230, BC_COLLISION); + if (mag > 0.02) { + Mix_Volume(BC_COLLISION, 1.2 * mag * MIX_MAX_VOLUME); + play_game_sound(game_sounds.collision, 100, BC_COLLISION); + Mix_FadeOutChannel(BC_COLLISION, 100* 0.8); } // get velocity in direction of collision @@ -1674,7 +1675,7 @@ void startgame(SDL_Renderer * ren) { debug_ren = ren; - level = 1; + level = rand(); #ifdef SCORE_SYSTEM save_times_lst = (struct sg_times_list){};