ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

imuse_track.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/textconsole.h"
00024 
00025 #include "engines/grim/debug.h"
00026 
00027 #include "engines/grim/imuse/imuse.h"
00028 
00029 namespace Grim {
00030 
00031 int Imuse::allocSlot(int priority) {
00032     int l, lowest_priority = 127;
00033     int trackId = -1;
00034 
00035     // allocSlot called by startSound so no locking is necessary
00036     for (l = 0; l < MAX_IMUSE_TRACKS; l++) {
00037         if (!_track[l]->used) {
00038             trackId = l;
00039             break;
00040         }
00041     }
00042 
00043     if (trackId == -1) {
00044         warning("Imuse::startSound(): All slots are full");
00045         for (l = 0; l < MAX_IMUSE_TRACKS; l++) {
00046             Track *track = _track[l];
00047             if (track->used && !track->toBeRemoved &&
00048                     (lowest_priority > track->priority)) {
00049                 lowest_priority = track->priority;
00050                 trackId = l;
00051             }
00052         }
00053         if (lowest_priority <= priority) {
00054             assert(trackId != -1);
00055             Track *track = _track[trackId];
00056 
00057             // Stop the track immediately
00058             g_system->getMixer()->stopHandle(track->handle);
00059             if (track->soundDesc) {
00060                 _sound->closeSound(track->soundDesc);
00061             }
00062 
00063             // Mark it as unused
00064             memset(track, 0, sizeof(Track));
00065         } else {
00066             return -1;
00067         }
00068     }
00069 
00070     return trackId;
00071 }
00072 
00073 bool Imuse::startSound(const char *soundName, int volGroupId, int hookId, int volume, int pan, int priority, Track *otherTrack) {
00074     Common::StackLock lock(_mutex);
00075     Track *track = nullptr;
00076     int i;
00077 
00078     // If the track is fading out bring it back to the normal running tracks
00079     for (i = MAX_IMUSE_TRACKS; i < MAX_IMUSE_TRACKS + MAX_IMUSE_FADETRACKS; i++) {
00080         if (!scumm_stricmp(_track[i]->soundName, soundName) && !_track[i]->toBeRemoved) {
00081 
00082             Track *fadeTrack = _track[i];
00083             track = _track[i - MAX_IMUSE_TRACKS];
00084 
00085             if (track->used) {
00086                 flushTrack(track);
00087                 g_system->getMixer()->stopHandle(track->handle);
00088             }
00089 
00090             // Clone the settings of the given track
00091             memcpy(track, fadeTrack, sizeof(Track));
00092             track->trackId = i - MAX_IMUSE_TRACKS;
00093             // Reset the track
00094             memset(fadeTrack, 0, sizeof(Track));
00095             // Mark as used for now so the track won't be reused again this frame
00096             track->used = true;
00097 
00098             return true;
00099         }
00100     }
00101 
00102     // If the track is already playing then there is absolutely no
00103     // reason to start it again, the existing track should be modified
00104     // instead of starting a new copy of the track
00105     for (i = 0; i < MAX_IMUSE_TRACKS; i++) {
00106         // Filenames are case insensitive, see findTrack
00107         if (!scumm_stricmp(_track[i]->soundName, soundName)) {
00108             Debug::debug(Debug::Sound, "Imuse::startSound(): Track '%s' already playing.", soundName);
00109             return true;
00110         }
00111     }
00112 
00113     // Priority Level 127 appears to mean "load but don't play", so
00114     // within our paradigm this is a much lower priority than everything
00115     // else we're doing
00116     if (priority == 127)
00117         priority = -1;
00118 
00119     int l = allocSlot(priority);
00120     if (l == -1) {
00121         warning("Imuse::startSound() Can't start sound - no free slots");
00122         return false;
00123     }
00124 
00125     track = _track[l];
00126     // Reset the track
00127     memset(track, 0, sizeof(Track));
00128 
00129     track->pan = pan * 1000;
00130     track->vol = volume * 1000;
00131     track->volGroupId = volGroupId;
00132     track->curHookId = hookId;
00133     track->priority = priority;
00134     track->curRegion = -1;
00135     track->trackId = l;
00136 
00137     int bits = 0, freq = 0, channels = 0;
00138 
00139     strcpy(track->soundName, soundName);
00140     track->soundDesc = _sound->openSound(soundName, volGroupId);
00141 
00142     if (!track->soundDesc)
00143         return false;
00144 
00145     bits = _sound->getBits(track->soundDesc);
00146     channels = _sound->getChannels(track->soundDesc);
00147     freq = _sound->getFreq(track->soundDesc);
00148 
00149     assert(bits == 8 || bits == 12 || bits == 16);
00150     assert(channels == 1 || channels == 2);
00151     assert(0 < freq && freq <= 65535);
00152 
00153     track->feedSize = freq * channels * 2;
00154     track->mixerFlags = kFlag16Bits;
00155     if (channels == 2)
00156         track->mixerFlags |= kFlagStereo | kFlagReverseStereo;
00157 
00158     if (otherTrack && otherTrack->used && !otherTrack->toBeRemoved) {
00159         track->curRegion = otherTrack->curRegion;
00160         track->dataOffset = otherTrack->dataOffset;
00161         track->regionOffset = otherTrack->regionOffset;
00162     }
00163 
00164     track->stream = Audio::makeQueuingAudioStream(freq, track->mixerFlags & kFlagStereo);
00165     g_system->getMixer()->playStream(track->getType(), &track->handle, track->stream, -1,
00166                                             track->getVol(), track->getPan(), DisposeAfterUse::YES,
00167                                             false, (track->mixerFlags & kFlagReverseStereo) != 0);
00168     track->used = true;
00169 
00170     return true;
00171 }
00172 
00173 Track *Imuse::findTrack(const char *soundName) {
00174     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00175         Track *track = _track[l];
00176 
00177         // Since the audio (at least for Eva's keystrokes) can be referenced
00178         // two ways: keyboard.IMU and keyboard.imu, make a case insensitive
00179         // search for the track to make sure we can find it
00180         if (track->used && !track->toBeRemoved
00181                 && strlen(track->soundName) != 0 && scumm_stricmp(track->soundName, soundName) == 0) {
00182             return track;
00183         }
00184     }
00185     return nullptr;
00186 }
00187 
00188 void Imuse::setPriority(const char *soundName, int priority) {
00189     Common::StackLock lock(_mutex);
00190     Track *changeTrack = nullptr;
00191     assert ((priority >= 0) && (priority <= 127));
00192 
00193     changeTrack = findTrack(soundName);
00194     // Check to make sure we found the track
00195     if (changeTrack == nullptr) {
00196         warning("Unable to find track '%s' to change priority", soundName);
00197         return;
00198     }
00199     changeTrack->priority = priority;
00200 }
00201 
00202 void Imuse::setVolume(const char *soundName, int volume) {
00203     Common::StackLock lock(_mutex);
00204     Track *changeTrack;
00205 
00206     changeTrack = findTrack(soundName);
00207     if (changeTrack == nullptr) {
00208         warning("Unable to find track '%s' to change volume", soundName);
00209         return;
00210     }
00211     changeTrack->vol = volume * 1000;
00212 }
00213 
00214 void Imuse::setPan(const char *soundName, int pan) {
00215     Common::StackLock lock(_mutex);
00216     Track *changeTrack;
00217 
00218     changeTrack = findTrack(soundName);
00219     if (changeTrack == nullptr) {
00220         warning("Unable to find track '%s' to change pan", soundName);
00221         return;
00222     }
00223     changeTrack->pan = pan * 1000;
00224 }
00225 
00226 int Imuse::getVolume(const char *soundName) {
00227     Common::StackLock lock(_mutex);
00228     Track *getTrack;
00229 
00230     getTrack = findTrack(soundName);
00231     if (getTrack == nullptr) {
00232         warning("Unable to find track '%s' to get volume", soundName);
00233         return 0;
00234     }
00235     return getTrack->vol / 1000;
00236 }
00237 
00238 void Imuse::setHookId(const char *soundName, int hookId) {
00239     Common::StackLock lock(_mutex);
00240     Track *changeTrack;
00241 
00242     changeTrack = findTrack(soundName);
00243     if (changeTrack == nullptr) {
00244         warning("Unable to find track '%s' to change hook id", soundName);
00245         return;
00246     }
00247     changeTrack->curHookId = hookId;
00248 }
00249 
00250 int Imuse::getCountPlayedTracks(const char *soundName) {
00251     Common::StackLock lock(_mutex);
00252     int count = 0;
00253 
00254     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00255         Track *track = _track[l];
00256         if (track->used && !track->toBeRemoved && (scumm_stricmp(track->soundName, soundName) == 0)) {
00257             count++;
00258         }
00259     }
00260 
00261     return count;
00262 }
00263 
00264 void Imuse::selectVolumeGroup(const char *soundName, int volGroupId) {
00265     Common::StackLock lock(_mutex);
00266     Track *changeTrack;
00267     assert((volGroupId >= 1) && (volGroupId <= 4));
00268 
00269     if (volGroupId == 4)
00270         volGroupId = 3;
00271 
00272     changeTrack = findTrack(soundName);
00273     if (changeTrack == nullptr) {
00274         warning("Unable to find track '%s' to change volume group id", soundName);
00275         return;
00276     }
00277     changeTrack->volGroupId = volGroupId;
00278 }
00279 
00280 void Imuse::setFadeVolume(const char *soundName, int destVolume, int duration) {
00281     Common::StackLock lock(_mutex);
00282     Track *changeTrack;
00283 
00284     changeTrack = findTrack(soundName);
00285     if (changeTrack == nullptr) {
00286         warning("Unable to find track '%s' to change fade volume", soundName);
00287         return;
00288     }
00289     changeTrack->volFadeDelay = duration;
00290     changeTrack->volFadeDest = destVolume * 1000;
00291     changeTrack->volFadeStep = (changeTrack->volFadeDest - changeTrack->vol) * 60 * (1000 / _callbackFps) / (1000 * duration);
00292     changeTrack->volFadeUsed = true;
00293 }
00294 
00295 void Imuse::setFadePan(const char *soundName, int destPan, int duration) {
00296     Common::StackLock lock(_mutex);
00297     Track *changeTrack;
00298 
00299     changeTrack = findTrack(soundName);
00300     if (changeTrack == nullptr) {
00301         warning("Unable to find track '%s' to change fade pan", soundName);
00302         return;
00303     }
00304     changeTrack->panFadeDelay = duration;
00305     changeTrack->panFadeDest = destPan * 1000;
00306     changeTrack->panFadeStep = (changeTrack->panFadeDest - changeTrack->pan) * 60 * (1000 / _callbackFps) / (1000 * duration);
00307     changeTrack->panFadeUsed = true;
00308 }
00309 
00310 char *Imuse::getCurMusicSoundName() {
00311     Common::StackLock lock(_mutex);
00312     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00313         Track *track = _track[l];
00314         if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
00315             return track->soundName;
00316         }
00317     }
00318     return nullptr;
00319 }
00320 
00321 int Imuse::getCurMusicPan() {
00322     Common::StackLock lock(_mutex);
00323     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00324         Track *track = _track[l];
00325         if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
00326             return track->pan / 1000;
00327         }
00328     }
00329     return 0;
00330 }
00331 
00332 int Imuse::getCurMusicVol() {
00333     Common::StackLock lock(_mutex);
00334     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00335         Track *track = _track[l];
00336         if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
00337             return track->vol / 1000;
00338         }
00339     }
00340     return 0;
00341 }
00342 
00343 void Imuse::fadeOutMusic(int duration) {
00344     Common::StackLock lock(_mutex);
00345     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00346         Track *track = _track[l];
00347         if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
00348             moveToFadeOutTrack(track, duration);
00349             return;
00350         }
00351     }
00352 }
00353 
00354 void Imuse::fadeOutMusicAndStartNew(int fadeDelay, const char *filename, int hookId, int vol, int pan) {
00355     Common::StackLock lock(_mutex);
00356 
00357     for (int l = 0; l < MAX_IMUSE_TRACKS; l++) {
00358         Track *track = _track[l];
00359         if (track->used && !track->toBeRemoved && (track->volGroupId == IMUSE_VOLGRP_MUSIC)) {
00360             startMusicWithOtherPos(filename, 0, vol, pan, track);
00361             moveToFadeOutTrack(track, fadeDelay);
00362             break;
00363         }
00364     }
00365 }
00366 
00367 Track *Imuse::cloneToFadeOutTrack(Track *track, int fadeDelay) {
00368     assert(track);
00369     Track *fadeTrack;
00370 
00371     if (track->toBeRemoved) {
00372         error("cloneToFadeOutTrack: Tried to clone a track to be removed, please bug report");
00373         return nullptr;
00374     }
00375 
00376     assert(track->trackId < MAX_IMUSE_TRACKS);
00377     fadeTrack = _track[track->trackId + MAX_IMUSE_TRACKS];
00378 
00379     if (fadeTrack->used) {
00380         flushTrack(fadeTrack);
00381         g_system->getMixer()->stopHandle(fadeTrack->handle);
00382     }
00383 
00384     // Clone the settings of the given track
00385     memcpy(fadeTrack, track, sizeof(Track));
00386     fadeTrack->trackId = track->trackId + MAX_IMUSE_TRACKS;
00387 
00388     // Clone the sound.
00389     // leaving bug number for now #1635361
00390     ImuseSndMgr::SoundDesc *soundDesc = _sound->cloneSound(track->soundDesc);
00391     assert(soundDesc);
00392     track->soundDesc = soundDesc;
00393 
00394     // Set the volume fading parameters to indicate a fade out
00395     fadeTrack->volFadeDelay = fadeDelay;
00396     fadeTrack->volFadeDest = 0;
00397     fadeTrack->volFadeStep = (fadeTrack->volFadeDest - fadeTrack->vol) * 60 * (1000 / _callbackFps) / (1000 * fadeDelay);
00398     fadeTrack->volFadeUsed = true;
00399 
00400     // Create an appendable output buffer
00401     fadeTrack->stream = Audio::makeQueuingAudioStream(_sound->getFreq(fadeTrack->soundDesc), track->mixerFlags & kFlagStereo);
00402     g_system->getMixer()->playStream(track->getType(), &fadeTrack->handle, fadeTrack->stream, -1, fadeTrack->getVol(),
00403                                             fadeTrack->getPan(), DisposeAfterUse::YES, false,
00404                                             (track->mixerFlags & kFlagReverseStereo) != 0);
00405     fadeTrack->used = true;
00406 
00407     return fadeTrack;
00408 }
00409 
00410 Track *Imuse::moveToFadeOutTrack(Track *track, int fadeDelay) {
00411     assert(track);
00412     Track *fadeTrack;
00413 
00414     if (track->toBeRemoved) {
00415         error("moveToFadeOutTrack: Tried to move a track to be removed, please bug report");
00416         return nullptr;
00417     }
00418 
00419     // Clamp fade time to remaining time in the current region
00420     if (track->curRegion != -1) {
00421         int remainingLen = _sound->getRegionLength(track->soundDesc, track->curRegion) - track->regionOffset;
00422         int remainingTime = (remainingLen * 60) / track->feedSize;
00423         if (fadeDelay > remainingTime) {
00424             fadeDelay = remainingTime;
00425         }
00426     }
00427 
00428     if (fadeDelay <= 0) {
00429         flushTrack(track);
00430         return nullptr;
00431     }
00432 
00433     assert(track->trackId < MAX_IMUSE_TRACKS);
00434     fadeTrack = _track[track->trackId + MAX_IMUSE_TRACKS];
00435 
00436     if (fadeTrack->used) {
00437         flushTrack(fadeTrack);
00438         g_system->getMixer()->stopHandle(fadeTrack->handle);
00439     }
00440 
00441     // Clone the settings of the given track
00442     memcpy(fadeTrack, track, sizeof(Track));
00443     fadeTrack->trackId = track->trackId + MAX_IMUSE_TRACKS;
00444 
00445     // Reset the track
00446     memset(track, 0, sizeof(Track));
00447 
00448     // Mark as used for now so the track won't be reused again this frame
00449     track->used = true;
00450 
00451     // Set the volume fading parameters to indicate a fade out
00452     fadeTrack->volFadeDelay = fadeDelay;
00453     fadeTrack->volFadeDest = 0;
00454     fadeTrack->volFadeStep = (fadeTrack->volFadeDest - fadeTrack->vol) * 60 * (1000 / _callbackFps) / (1000 * fadeDelay);
00455     fadeTrack->volFadeUsed = true;
00456 
00457     fadeTrack->used = true;
00458 
00459     return fadeTrack;
00460 }
00461 
00462 } // end of namespace Grim


Generated on Sat Aug 24 2019 05:00:54 for ResidualVM by doxygen 1.7.1
curved edge   curved edge