Ticket #8266: bs2_stream_comp_music.diff

File bs2_stream_comp_music.diff, 8.9 KB (added by eriktorbjorn, 21 years ago)

Patch against a July 31 CVS snapshot

  • scummvm/bs2/driver/d_sound.cpp

    diff -ur ScummVM-cvs20030731/scummvm/bs2/driver/d_sound.cpp ScummVM-cvs20030731+hack/scummvm/bs2/driver/d_sound.cpp
    old new  
    405405
    406406
    407407
    408 Sword2Sound::Sword2Sound(void) {
     408Sword2Sound::Sword2Sound(SoundMixer *mixer) {
    409409
    410410        soundOn = 0;
    411411        speechStatus = 0;
     
    421421        volMusic[1] = 16;
    422422        musicMuted = 0;
    423423
     424        _mixer = mixer;
    424425}
    425426
    426427/*  not used seemingly - khalek
     
    590591
    591592{
    592593        warning("stub InitaliseSound( %d, %d, %d )", freq, channels, bitDepth);
     594
     595        memset(fxId,            0, sizeof(fxId));
     596        memset(fxCached,        0, sizeof(fxCached));
     597        memset(fxiPaused,       0, sizeof(fxiPaused));
     598        memset(fxLooped,        0, sizeof(fxLooped));
     599
     600        memset(musStreaming,    0, sizeof(musStreaming));
     601        memset(musicPaused,     0, sizeof(musicPaused));
     602        memset(musCounter,      0, sizeof(musCounter));
     603        memset(musFading,       0, sizeof(musFading));
     604
     605        memset(musLooping,      0, sizeof(musLooping));
     606
     607        memset(streamCursor,    0, sizeof(streamCursor));
     608        memset(musFilePos,      0, sizeof(musFilePos));
     609        memset(musEnd,          0, sizeof(musEnd));
     610        memset(musLastSample,   0, sizeof(musLastSample));
     611        memset(musId,           0, sizeof(musId));
     612
    593613/*
    594614        int32                   i;
    595615        HRESULT                 hrz;
     
    17181738//      IDirectSoundBuffer_Release(lpDsbMus[i]);
    17191739        musFading[i] = -16;
    17201740//      musStreaming[i] = 0;
    1721         fclose(fpMus[i]);
     1741        fpMus[i].close();
    17221742
    17231743}
    17241744
     
    21222142
    21232143
    21242144
    2125 int32 Sword2Sound::StreamCompMusic(const char *filename, uint32 musicId, int32 looping)
    2126 {
     2145int32 Sword2Sound::StreamCompMusic(const char *filename, const char *directory, uint32 musicId, int32 looping) {
     2146        // FIXME: Find a good buffer size. The original code mentions three
     2147        // seconds, but I believe that's after ADPCM-decoding.
     2148        uint32  buffer_size = 32768;
     2149        uint32  i, j;
     2150        int32   v0, v1;
     2151        uint16  *data16;
     2152        uint8   *data8;
     2153
    21272154        warning("stub StreamCompMusic( %s, %d, %d )", filename, musicId, looping);
     2155
     2156        // Do not allow compressed and uncompressed music to be streamed at
     2157        // the same time.
     2158        if (compressedMusic == 2)
     2159                return RDERR_FXFUCKED;
     2160
     2161        compressedMusic = 1;
     2162
     2163        if (musStreaming[0] + musStreaming[1] == 2) {
     2164                // Both streams in use, try to find a fading stream
     2165                if (musFading[0])
     2166                        i = 0;
     2167                else
     2168                        i = 1;
     2169                       
     2170                musFading[i] = 0;
     2171                _mixer->stop(musicChannels[i]);
     2172                musStreaming[i] = 0;
     2173        }
     2174
     2175        if (musStreaming[0] + musStreaming[1] == 1) {
     2176                // Set i to the free channel
     2177                i = musStreaming[0];
     2178        } else {
     2179                // No music streaming at present
     2180                i = 0;
     2181        }
     2182
     2183        musLooping[i] = looping;                // Save looping info
     2184        strcpy(musFilename[i], filename);       // And tune id's
     2185        musId[i] = musicId;
     2186
     2187        // Don't start streaming if the volume is off.
     2188        if (IsMusicMute())
     2189                return RD_OK;
     2190
     2191        // Always use fpMus[0] (all music in one cluster)
     2192        // musFilePos[i] for different pieces of music.
     2193        if (!fpMus[0].isOpen()) {
     2194                if (!fpMus[0].open(filename, directory))
     2195                        return RDERR_INVALIDFILENAME;
     2196        }
     2197
     2198        if (musStreaming[0] + musStreaming[1] == 1) {
     2199                // Start other music stream fading out
     2200                if (!musFading[i - 1])
     2201                        musFading[i - 1] = -16;
     2202
     2203                // Restart the streaming cursor for this sample
     2204                streamCursor[i] = 0;
     2205        }
     2206
     2207        // Seek to music index
     2208        fpMus[0].seek((musicId + 1) * 8);
     2209
     2210        musFilePos[i] = fpMus[0].readUint32LE();
     2211        musEnd[i] = fpMus[0].readUint32LE();
     2212
     2213        // Check that music is valid (has length & offset)
     2214        if (!musEnd[i] || !musFilePos[i]) {
     2215                fpMus[0].close();
     2216                return RDERR_INVALIDID;
     2217        }
     2218
     2219        // Calculate the file position of the end of the music
     2220        musEnd[i] += musFilePos[i];
     2221
     2222        // Reset streaming cursor and store looping flag
     2223        streamCursor[i] = 0;
     2224
     2225        // Allocate a temporary buffer for compressed data
     2226        data8 = (uint8 *) malloc(buffer_size / 2);
     2227        if (!data8) {
     2228                fpMus[0].close();
     2229                return RDERR_OUTOFMEMORY;
     2230        }
     2231
     2232        // Allocate a sound buffer large enough for 3 seconds
     2233        data16 = (uint16 *) malloc(buffer_size);
     2234        if (!data16) {
     2235                fpMus[0].close();
     2236                free(data8);
     2237                return RDERR_OUTOFMEMORY;
     2238        }
     2239
     2240        lpDsbMus[i] = data16;
     2241
     2242        // Seek to start of the compressed music
     2243        fpMus[0].seek(musFilePos[i]);
     2244
     2245        // Read the compressed data in to the buffer
     2246        if (fpMus[0].read(data8, buffer_size / 2) != buffer_size / 2) {
     2247                fpMus[0].close();
     2248                free(data8);
     2249                free(data16);
     2250                return RDERR_INVALIDID;
     2251        }
     2252
     2253        // Store the current position in the file for future streaming
     2254        musFilePos[i] = fpMus[0].pos();
     2255
     2256        // Decompress the sound into the buffer
     2257
     2258        // First sample value
     2259        data16[0] = READ_LE_UINT16(data8);
     2260        j = 1;
     2261
     2262        while (j < (buffer_size / 2) - 1) {
     2263                if (GetCompressedSign(data8[j + 1]))
     2264                        data16[j] = data16[j - 1] - (GetCompressedAmplitude(data8[j + 1]) << GetCompressedShift(data8[j + 1]));
     2265                else
     2266                        data16[j] = data16[j - 1] + (GetCompressedAmplitude(data8[j + 1]) << GetCompressedShift(data8[j + 1]));
     2267                j++;
     2268        }
     2269
     2270        // Store the value of the last sample ready for next batch of
     2271        // decompression
     2272        musLastSample[i] = data16[j - 1];
     2273
     2274        // Free the decompression buffer
     2275        free(data8);
     2276
     2277        // Modify the volume according to the master volume and music
     2278        // mute state
     2279        if (musicMuted)
     2280                v0 = v1 = 0;
     2281        else {
     2282                v0 = volMusic[0];
     2283                v1 = volMusic[1];
     2284        }
     2285
     2286#if 0
     2287        if (v0 > v1) {
     2288                IDirectSoundBuffer_SetVolume(lpDsbMus[i], musicVolTable[v0]);
     2289                IDirectSoundBuffer_SetPan(lpDsbMus[i], musicVolTable[v1*16/v0]);
     2290        } else if (v1 > v0) {
     2291                IDirectSoundBuffer_SetVolume(lpDsbMus[i], musicVolTable[v1]);
     2292                IDirectSoundBuffer_SetPan(lpDsbMus[i], -musicVolTable[v0*16/v1]);
     2293        } else {
     2294                IDirectSoundBuffer_SetVolume(lpDsbMus[i], musicVolTable[v1]);
     2295                IDirectSoundBuffer_SetPan(lpDsbMus[i], 0);
     2296        }
     2297#else
     2298        warning("FIXME: Implement volume and panning");
     2299#endif
     2300
     2301        // Start the sound effect playing
     2302#if 0
     2303        musicChannels[i] = _mixer->playADPCM(&musicHandle[i], lpDsbMus[i], buffer_size, 22050, SoundMixer::FLAG_16BITS | SoundMixer::FLAG_AUTOFREE);
     2304#else
     2305        warning("FIXME: Implement ADPCM-decoding");
     2306        free(lpDsbMus[i]);
     2307#endif
     2308
     2309        // Record the last variables for streaming and looping
     2310        musStreaming[i] = 1;
     2311        musCounter[i] = 250;
     2312
    21282313/*
    21292314        HRESULT         hr;
    21302315        LPVOID          lpv1, lpv2;
     
    28613046                                musLooping[i] = 0;
    28623047                }
    28633048
    2864                 if (fpMus[0])
    2865                 {
    2866                         fclose(fpMus[0]);
    2867                         fpMus[0] = 0;
    2868                 }
     3049                if (fpMus[0].isOpen())
     3050                        fpMus[0].close();
    28693051                break;
    28703052        case 2:
    28713053                for (i = 0; i<MAXMUS; i++)
  • scummvm/bs2/driver/d_sound.h

    diff -ur ScummVM-cvs20030731/scummvm/bs2/driver/d_sound.h ScummVM-cvs20030731+hack/scummvm/bs2/driver/d_sound.h
    old new  
    4242
    4343class Sword2Sound {
    4444        public:
    45                 Sword2Sound(void);
     45                Sword2Sound(SoundMixer *mixer);
    4646                void FxServer(void);
    4747                int32 InitialiseSound(uint16 freq, uint16 channels, uint16 bitDepth);
    4848                int32 PlaySpeech(uint8 *data, uint8 vol, int8 pan);
     
    6464                int32 PauseMusic(void);
    6565                int32 UnpauseMusic(void);
    6666                int32 StreamMusic(uint8 *filename, int32 looping);
    67                 int32 StreamCompMusic(const char *filename,uint32 musicId, int32 looping);
     67                int32 StreamCompMusic(const char *filename, const char *directory, uint32 musicId, int32 looping);
    6868                int32 MusicTimeRemaining();
    6969                int32 ReverseStereo(void);
    7070                uint8 GetFxVolume(void);
     
    117117
    118118                //DSBUFFERDESC dsbdMus[MAXMUS];
    119119                //LPDIRECTSOUNDBUFFER lpDsbMus[MAXMUS];
    120                 FILE *fpMus[MAXMUS];
     120                PlayingSoundHandle musicHandle[MAXMUS];
     121                int musicChannels[MAXMUS];
     122                uint16 *lpDsbMus[MAXMUS];
     123                File fpMus[MAXMUS];
     124                //FILE *fpMus[MAXMUS];
    121125                //PCMWAVEFORMAT wfMus[MAXMUS];
    122126                int32 streamCursor[MAXMUS];
    123127                char musFilename[MAXMUS][256];
  • scummvm/bs2/sound.cpp

    diff -ur ScummVM-cvs20030731/scummvm/bs2/sound.cpp ScummVM-cvs20030731+hack/scummvm/bs2/sound.cpp
    old new  
    415415        else
    416416                sprintf(filename,"%sCLUSTERS\\MUSIC.CLU", res_man.GetCdPath());
    417417
    418         rv = g_sword2->_sound->StreamCompMusic(filename, params[0], loopFlag);
     418        rv = g_sword2->_sound->StreamCompMusic(filename, g_sword2->getGameDataPath(), params[0], loopFlag);
    419419
    420420        #ifdef _SWORD2_DEBUG
    421421                if (rv)
  • scummvm/bs2/sword2.cpp

    diff -ur ScummVM-cvs20030731/scummvm/bs2/sword2.cpp ScummVM-cvs20030731+hack/scummvm/bs2/sword2.cpp
    old new  
    106106        g_sword2 = this;
    107107        _features = detector->_game.features;
    108108        _gameId = detector->_game.id;
     109
     110        // Setup mixer
     111        if (!_mixer->bindToSystem(syst))
     112                warning("Sound initialization failed");
     113
     114        _mixer->setVolume(kDefaultSFXVolume * kDefaultMasterVolume / 255);
     115
     116        _sound = new Sword2Sound(_mixer);
    109117}
    110118
    111119
     
    167175        Init_event_system();
    168176        Zdebug("RETURNED.");
    169177       
    170         _sound = new Sword2Sound;
    171        
    172178        Zdebug("CALLING: Init_fx_queue");
    173179        Init_fx_queue();                        // initialise the sound fx queue
    174180        Zdebug("RETURNED.");