Author: titmuss
Date: Thu Nov 13 09:23:57 2008
New Revision: 3359

URL: http://svn.slimdevices.com?rev=3359&root=Jive&view=rev
Log:
Bug: 9984
Description:
Reduce the amount of memory used by the squeezeplay sound effects.


Modified:
    7.3/trunk/squeezeplay/src/squeezeplay/src/audio/decode/decode_sample.c

Modified: 7.3/trunk/squeezeplay/src/squeezeplay/src/audio/decode/decode_sample.c
URL: 
http://svn.slimdevices.com/7.3/trunk/squeezeplay/src/squeezeplay/src/audio/decode/decode_sample.c?rev=3359&root=Jive&r1=3358&r2=3359&view=diff
==============================================================================
--- 7.3/trunk/squeezeplay/src/squeezeplay/src/audio/decode/decode_sample.c 
(original)
+++ 7.3/trunk/squeezeplay/src/squeezeplay/src/audio/decode/decode_sample.c Thu 
Nov 13 09:23:57 2008
@@ -14,7 +14,8 @@
 struct jive_sample {
        unsigned int refcount;
        Uint8 *data;
-       size_t len;
+       size_t frames;
+       int channels;
        size_t pos;
        int mixer;
        bool enabled;
@@ -45,26 +46,37 @@
 void decode_sample_mix(Uint8 *buffer, size_t buflen) {
        const Sint64 max_sample = 0x7fffffffLL;
        const Sint64 min_sample = -0x80000000LL;
+       size_t buf_frames;
        int i;
+
+       buf_frames = BYTES_TO_SAMPLES(buflen);
 
        /* fixme: this crudely mixes the samples onto the buffer */
        for (i=0; i<MAX_SAMPLES; i++) {
-               Sint32 *s, *d;
-               size_t j, len;
+               Sint16 *s;
+               Sint32 *d;
+               size_t j, frames;
 
                if (!sample[i]) {
                        continue;
                }
 
-               len = sample[i]->len - sample[i]->pos;
-               if (len > buflen) {
-                       len = buflen;
-               }
-
-               s = (Sint32 *)(void *)(sample[i]->data + sample[i]->pos);
+               frames = sample[i]->frames - sample[i]->pos;
+               if (frames > buf_frames) {
+                       frames = buf_frames;
+               }
+
                d = (Sint32 *)(void *)buffer;
-               for (j=0; j<len>>2; j++) {
-                       Sint64 tmp = *(s++);
+               s = (Sint16 *)(void *)sample[i]->data;
+               s += (sample[i]->pos * sample[i]->channels);
+
+               for (j=0; j<frames*2; j++) {
+                       Sint64 tmp = *s << 16;
+
+                       if (sample[i]->channels == 2 || (j & 1)) {
+                               s++;
+                       }
+
                        tmp = fixed_mul(effect_gain, tmp);
                        tmp += *d;
 
@@ -77,9 +89,9 @@
                        *(d++) = tmp;
                }
 
-               sample[i]->pos += len;
-
-               if (sample[i]->pos == sample[i]->len) {
+               sample[i]->pos += frames;
+
+               if (sample[i]->pos == sample[i]->frames) {
                        sample[i]->pos = 0;
 
                        sample_free(sample[i]);
@@ -91,6 +103,7 @@
 
 static int decode_sample_obj_gc(lua_State *L) {
        struct jive_sample *sample = *(struct jive_sample **)lua_touserdata(L, 
1);
+
        if (sample) {
                sample_free(sample);
        }
@@ -162,7 +175,6 @@
        SDL_AudioCVT cvt;
        Uint8 *data;
        Uint32 len;
-       int i;
 
        // FIXME rewrite to not use SDL
        if (SDL_LoadWAV(filename, &wave, &data, &len) == NULL) {
@@ -170,14 +182,14 @@
                return NULL;
        }
 
-       /* Convert to signed 16 bit stereo */
+       /* Convert to signed 16 bit stereo/mono */
        if (SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
-                             AUDIO_S16SYS, 2, 44100) < 0) {
+                             AUDIO_S16SYS, wave.channels, 44100) < 0) {
                fprintf(stderr, "Couldn't build audio converter: %s\n", 
SDL_GetError());
                SDL_FreeWAV(data);
                return NULL;
        }
-       cvt.buf = malloc(len * cvt.len_mult * 2);
+       cvt.buf = malloc(len * cvt.len_mult);
        memcpy(cvt.buf, data, len);
        cvt.len = len;
        
@@ -189,16 +201,11 @@
        }
        SDL_FreeWAV(data);
 
-       /* Convert to signed 32 bit stereo, SDL does not support this format */
-       cvt.len_cvt *= 2;
-       for (i=(cvt.len_cvt/4)-1; i>=0; i--) {
-               ((Uint32 *)(void *)cvt.buf)[i] = ((Uint16 *)(void *)cvt.buf)[i] 
<< 16;
-       }
-
        snd = malloc(sizeof(struct jive_sample));
        snd->refcount = 1;
        snd->data = cvt.buf;
-       snd->len = cvt.len_cvt;
+       snd->frames = cvt.len_cvt / 2 /* 16 bit */ / wave.channels;
+       snd->channels = wave.channels;
        snd->pos = 0;
        snd->mixer = mixer;
        snd->enabled = true;

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to