Ticket #8751: imuse.patch

File imuse.patch, 8.7 KB (added by fingolfin, 16 years ago)
  • engines/scumm/imuse_digi/dimuse.cpp

     
    158158                if (!ser->isSaving()) {
    159159                        if (!track->used)
    160160                                continue;
    161                         track->readyToRemove = false;
    162161                        if ((track->toBeRemoved) || (track->souStreamUsed) || (track->curRegion == -1)) {
    163162                                track->streamSou= NULL;
    164163                                track->stream = NULL;
     
    231230
    232231        for (int l = 0; l < MAX_DIGITAL_TRACKS + MAX_DIGITAL_FADETRACKS; l++) {
    233232                Track *track = _track[l];
    234                 if (track->used && !track->readyToRemove) {
     233                if (track->used) {
     234                        // Remove tracks if necessary
    235235                        if (track->toBeRemoved) {
    236                                 track->readyToRemove = true;
     236                                flushTrack(track);
    237237                                continue;
    238238                        }
    239239
     
    249249                                                        track->volFadeUsed = false;
    250250                                                }
    251251                                                if (track->vol == 0) {
    252                                                         track->toBeRemoved = true;
     252                                                        // Fade out complete -> remove this track
     253                                                        flushTrack(track);
     254                                                        continue;
    253255                                                }
    254256                                        }
    255257                                } else if (track->volFadeStep > 0) {
     
    357359                                } while (feedSize != 0);
    358360                        } else if (track->streamSou) {
    359361                                if (_mixer->isReady()) {
     362                                        // FIXME: Can't we replace track->mixerStreamRunning by
     363                                        // _mixer->isSoundHandleActive(track->mixChanHandle) ?
    360364                                        if (!track->mixerStreamRunning) {
    361365                                                track->mixerStreamRunning = true;
    362366                                                _mixer->playInputStream(type, &track->mixChanHandle, track->streamSou, -1, vol, pan, false);
     
    375379        debug(5, "switchToNextRegion(track:%d)", track->trackId);
    376380
    377381        if (track->trackId >= MAX_DIGITAL_TRACKS) {
    378                 track->toBeRemoved = true;
     382                flushTrack(track);
    379383                debug(5, "exit (fadetrack can't go next region) switchToNextRegion(trackId:%d)", track->trackId);
    380384                return;
    381385        }
     
    383387        int num_regions = _sound->getNumRegions(track->soundDesc);
    384388
    385389        if (++track->curRegion == num_regions) {
    386                 track->toBeRemoved = true;
     390                flushTrack(track);
    387391                debug(5, "exit (end of regions) switchToNextRegion(track:%d)", track->trackId);
    388392                return;
    389393        }
  • engines/scumm/imuse_digi/dimuse_script.cpp

     
    163163        }
    164164}
    165165
     166void IMuseDigital::flushTrack(Track *track) {
     167        track->toBeRemoved = true;
     168        if (track->stream) {
     169                // Finalize the appendable stream
     170                track->stream->finish();
     171                // There might still be some data left in the buffers of the
     172                // appendable stream. We play it nice and wait till all of it
     173                // played.
     174                if (track->stream->endOfStream()) {
     175                        _mixer->stopHandle(track->mixChanHandle);
     176                        delete track->stream;
     177                        track->stream = NULL;
     178                        _sound->closeSound(track->soundDesc);
     179                        track->soundDesc = NULL;
     180                        track->used = false;
     181                }
     182        } else if (track->streamSou) {
     183                _mixer->stopHandle(track->mixChanHandle);
     184                delete track->streamSou;
     185                track->streamSou = NULL;
     186                track->used = false;
     187        }
     188}
     189
    166190void IMuseDigital::flushTracks() {
    167191        Common::StackLock lock(_mutex, "IMuseDigital::flushTracks()");
    168192        debug(5, "flushTracks()");
    169193        for (int l = 0; l < MAX_DIGITAL_TRACKS + MAX_DIGITAL_FADETRACKS; l++) {
    170194                Track *track = _track[l];
    171                 if (track->used && track->readyToRemove) {
    172                         if (track->stream) {
    173                                 // Finalize the appendable stream
    174                                 track->stream->finish();
    175                                 // There might still be some data left in the buffers of the
    176                                 // appendable stream. We play it nice and wait till all of it
    177                                 // played.
    178                                 if (track->stream->endOfStream()) {
    179                                         _mixer->stopHandle(track->mixChanHandle);
    180                                         delete track->stream;
    181                                         track->stream = NULL;
    182                                         _sound->closeSound(track->soundDesc);
    183                                         track->soundDesc = NULL;
    184                                         track->used = false;
    185                                 }
    186                         } else if (track->streamSou) {
    187                                 _mixer->stopHandle(track->mixChanHandle);
    188                                 delete track->streamSou;
    189                                 track->streamSou = NULL;
    190                                 track->used = false;
    191                         }
     195                if (track->used && track->toBeRemoved) {
     196                        flushTrack(track);
    192197                }
    193198        }
    194199}
     
    288293                Track *track = _track[l];
    289294                if (track->soundId == sound) {
    290295                        if ((track->streamSou && _mixer->isSoundHandleActive(track->mixChanHandle)) ||
    291                                 (track->stream && track->used && !track->readyToRemove)) {
     296                                (track->stream && !track->stream->endOfStream())) {
    292297                                        return 1;
    293298                        }
    294299                }
  • engines/scumm/imuse_digi/dimuse_track.cpp

     
    5959                }
    6060                if (lowest_priority <= priority) {
    6161                        assert(trackId != -1);
    62                         _track[trackId]->toBeRemoved = true;
     62                        Track *track = _track[trackId];
     63                        while (1) {
     64                                if (!track->used) {
     65                                        break;
     66                                }
     67                                // The designated track is not yet available. So, we call flushTrack()
     68                                // to get it processed (and thus made ready for us). Since the actual
     69                                // processing is done by another thread, we also call parseEvents to
     70                                // give it some time (and to avoid busy waiting/looping).
     71                                flushTrack(track);
     72                                _mutex.unlock();
     73                #ifndef __PLAYSTATION2__
     74                                _vm->parseEvents();
     75                #endif
     76                                _mutex.lock();
     77                        }
    6378                        debug(5, "IMuseDigital::allocSlot(): Removed sound %d from track %d", _track[trackId]->soundId, trackId);
    6479                } else {
    6580                        debug(5, "IMuseDigital::allocSlot(): Priority sound too low");
     
    7186}
    7287
    7388void IMuseDigital::startSound(int soundId, const char *soundName, int soundType, int volGroupId, Audio::AudioStream *input, int hookId, int volume, int priority) {
     89        Common::StackLock lock(_mutex, "IMuseDigital::startSound()");
    7490        debug(5, "IMuseDigital::startSound(%d)", soundId);
    7591
    7692        int l = allocSlot(priority);
     
    8096        }
    8197
    8298        Track *track = _track[l];
    83         while (1) {
    84                 _mutex.lock();
    85                 if (!track->used) {
    86                         break;
    87                 }
    88                 // The designated track is not yet available. So, we call flushTracks()
    89                 // to get it processed (and thus made ready for us). Since the actual
    90                 // processing is done by another thread, we also call parseEvents to
    91                 // give it some time (and to avoid busy waiting/looping).
    92                 flushTracks();
    93                 _mutex.unlock();
    94 #ifndef __PLAYSTATION2__
    95                 _vm->parseEvents();
    96 #endif
    97         }
    9899
    99100        track->pan = 64;
    100101        track->vol = volume * 1000;
     
    113114        track->dataMod12Bit = 0;
    114115        track->mixerFlags = 0;
    115116        track->toBeRemoved = false;
    116         track->readyToRemove = false;
    117117        track->soundType = soundType;
    118118
    119119        int bits = 0, freq = 0, channels = 0;
     
    191191        }
    192192
    193193        track->used = true;
    194         _mutex.unlock();
    195194}
    196195
    197196void IMuseDigital::setPriority(int soundId, int priority) {
     
    309308                Track *track = _track[l];
    310309                if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
    311310                        cloneToFadeOutTrack(track, fadeDelay);
    312                         track->toBeRemoved = true;
     311                        flushTrack(track);
    313312                }
    314313        }
    315314}
     
    319318        Track *fadeTrack = 0;
    320319
    321320        debug(0, "IMuseDigital::cloneToFadeOutTrack(%d, %d)", track->trackId, fadeDelay);
     321       
     322        if (track->toBeRemoved) {
     323                error("IMuseDigital::cloneToFadeOutTrack: Tried to clone a track to be removed");
     324                return NULL;
     325        }
    322326
    323327        if (_track[track->trackId + MAX_DIGITAL_TRACKS]->used) {
    324                 warning("IMuseDigital::cloneToFadeOutTrack: Not free fade track");
     328                warning("IMuseDigital::cloneToFadeOutTrack: No free fade track");
    325329                return NULL;
    326330        }
    327331
     
    360364        }
    361365        fadeTrack->stream = Audio::makeAppendableAudioStream(_sound->getFreq(fadeTrack->soundDesc), makeMixerFlags(fadeTrack->mixerFlags));
    362366        _mixer->playInputStream(type, &fadeTrack->mixChanHandle, fadeTrack->stream, -1, fadeTrack->vol / 1000, fadeTrack->pan, false);
    363 
    364367        fadeTrack->mixerStreamRunning = true;
    365368        fadeTrack->used = true;
    366369
  • engines/scumm/imuse_digi/dimuse.h

     
    8383                char soundName[15]; // sound name but also filename of sound in bundle data
    8484                bool used;                      // flag mean that track is used
    8585                bool toBeRemoved;   // flag mean that track need to be free
    86                 bool readyToRemove; // flag mean that track is ready to stop
    8786                bool mixerStreamRunning;        // flag mean sound mixer's stream is running
    8887                bool souStreamUsed;     // flag mean that track use stream from sou file
    8988                bool sndDataExtComp;// flag mean that sound data is compressed by scummvm tools
     
    152151        void setDigMusicSequence(int seqId);
    153152        void playDigMusic(const char *songName, const imuseDigTable *table, int atribPos, bool sequence);
    154153
     154        void flushTrack(Track *track);
     155
    155156public:
    156157        IMuseDigital(ScummEngine_v7 *scumm, Audio::Mixer *mixer, int fps);
    157158        virtual ~IMuseDigital();