aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-29 15:11:44 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-06-29 15:11:44 +0000
commit8014bafd7352ffc856747de4bc2178e3c3fbcf12 (patch)
treefef82fa7d71e0748950c2f71d9769db0e7e7600b /src/audio.c
parent570900e819a69a5c522640f2ec48e3802ce4fda9 (diff)
Working mixer. Yay!
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@111 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src/audio.c')
-rw-r--r--src/audio.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/audio.c b/src/audio.c
index 5ac4b13..8bb95fa 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -18,21 +18,28 @@
#include "types.h"
-static void audio_mix(void *data, Uint8 *stream, int len) {
+static void audio_mix(void *data, Uint8 *stream8, int len) {
AudioContext *a = data;
- int i;
+ int i, j;
Uint32 amount;
- size_t j;
+ Sint16 *stream = (Sint16 *)stream8;
+
+ len /= 2; /* Samples */
+ /* Zero the buffer */
for ( j=0; j<len; j++ ) {
stream[j] = 0;
}
+ /* For each currently playing sound... */
for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) {
+ /* Playing? */
if ( !a->sounds[i].inuse ) continue;
- amount = (a->sounds[i].dlen - a->sounds[i].dpos);
+
+ /* Calculate how many samples to mix */
+ amount = a->sounds[i].dlen - a->sounds[i].dpos;
if ( amount > len ) {
/* The sound remaining in this channel more than fills the buffer */
@@ -40,7 +47,7 @@ static void audio_mix(void *data, Uint8 *stream, int len) {
}
for ( j=0; j<amount; j++ ) {
- float samp = a->sounds[i].data[a->sounds[i].dpos + j];
+ Sint16 samp = a->sounds[i].data[a->sounds[i].dpos + j];
stream[j] += samp * a->sounds[i].volume;
}
@@ -92,8 +99,8 @@ void audio_play(AudioContext *a, char *file, float volume, int repeat) {
/* Put the sound data in the slot */
SDL_LockAudio();
- a->sounds[idx].data = (Uint16 *)cvt.buf;
- a->sounds[idx].dlen = cvt.len_cvt;
+ a->sounds[idx].data = (Sint16 *)cvt.buf;
+ a->sounds[idx].dlen = cvt.len_cvt / 2;
a->sounds[idx].dpos = 0;
a->sounds[idx].inuse = 1;
a->sounds[idx].repeat = repeat;