diff options
-rw-r--r-- | src/audio.c | 24 | ||||
-rw-r--r-- | src/audio.h | 2 | ||||
-rw-r--r-- | src/physics.c | 2 | ||||
-rw-r--r-- | src/types.h | 3 |
4 files changed, 14 insertions, 17 deletions
diff --git a/src/audio.c b/src/audio.c index e282f94..5ac4b13 100644 --- a/src/audio.c +++ b/src/audio.c @@ -32,15 +32,16 @@ static void audio_mix(void *data, Uint8 *stream, int len) { for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) { if ( !a->sounds[i].inuse ) continue; - amount = (a->sounds[i].dlen - a->sounds[i].dpos); + if ( amount > len ) { /* The sound remaining in this channel more than fills the buffer */ amount = len; } for ( j=0; j<amount; j++ ) { - stream[j] += a->sounds[i].data[a->sounds[i].dpos + j]; + float samp = a->sounds[i].data[a->sounds[i].dpos + j]; + stream[j] += samp * a->sounds[i].volume; } a->sounds[i].dpos += amount; @@ -57,9 +58,9 @@ static void audio_mix(void *data, Uint8 *stream, int len) { } -void audio_play(AudioContext *a, char *file, float volume) { +void audio_play(AudioContext *a, char *file, float volume, int repeat) { - int idx, i; + int idx; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; @@ -89,21 +90,14 @@ void audio_play(AudioContext *a, char *file, float volume) { SDL_ConvertAudio(&cvt); SDL_FreeWAV(data); - /* Perform volume adjustment here */ - int16_t *aud = (int16_t *)cvt.buf; - for ( i=0; i<dlen/2; i++ ) { - float samp = aud[i]; - samp *= volume; - aud[i] = samp; - } - /* Put the sound data in the slot */ SDL_LockAudio(); - a->sounds[idx].data = cvt.buf; + a->sounds[idx].data = (Uint16 *)cvt.buf; a->sounds[idx].dlen = cvt.len_cvt; a->sounds[idx].dpos = 0; a->sounds[idx].inuse = 1; - a->sounds[idx].repeat = 1; + a->sounds[idx].repeat = repeat; + a->sounds[idx].volume = volume; SDL_UnlockAudio(); } @@ -139,7 +133,7 @@ AudioContext *audio_setup() { } SDL_PauseAudio(0); - audio_play(a, "moan", 0.4); + audio_play(a, "moan", 0.4, 1); return a; diff --git a/src/audio.h b/src/audio.h index a8e5a88..703f6c0 100644 --- a/src/audio.h +++ b/src/audio.h @@ -20,7 +20,7 @@ extern AudioContext *audio_setup(void); extern void audio_shutdown(AudioContext *ctx); -extern void audio_play(AudioContext *a, char *file, float volume); +extern void audio_play(AudioContext *a, char *file, float volume, int repeat); #endif /* AUDIO_H */ diff --git a/src/physics.c b/src/physics.c index 799359d..60347b0 100644 --- a/src/physics.c +++ b/src/physics.c @@ -20,6 +20,7 @@ #include "model.h" #include "game.h" #include "utils.h" +#include "audio.h" /* Acceleration due to gravity in metres per millisecond per millisecond */ #define GRAVITY (9.81e-6) @@ -270,6 +271,7 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { for ( j=0; j<room->num_objects; j++ ) { if ( physics_check_collide(obj, room->objects[j], dt) ) { /* Should be dt - ttc */ collided = 1; + audio_play(game->audio, "moan", 0.4, 0); } } } diff --git a/src/types.h b/src/types.h index 5b4b0a6..8dcdc44 100644 --- a/src/types.h +++ b/src/types.h @@ -147,11 +147,12 @@ typedef struct { typedef struct { struct sound { - Uint8 *data; + Sint16 *data; int dpos; int dlen; int inuse; int repeat; + float volume; } sounds[AUDIO_MAX_SOUNDS]; } AudioContext; |