From da842ee14ed6a91894449ab277f93289a8744dad Mon Sep 17 00:00:00 2001 From: yinsimei Date: Wed, 7 Jun 2017 09:13:42 +0200 Subject: SLUDGE: remove unused sound files and rename cpp --- engines/sludge/module.mk | 2 +- engines/sludge/sound.cpp | 833 +++++++++++++++++++++++++++++++++++++++ engines/sludge/sound_bass.cpp | 409 ------------------- engines/sludge/sound_nosound.cpp | 137 ------- engines/sludge/sound_openal.cpp | 831 -------------------------------------- 5 files changed, 834 insertions(+), 1378 deletions(-) create mode 100644 engines/sludge/sound.cpp delete mode 100644 engines/sludge/sound_bass.cpp delete mode 100644 engines/sludge/sound_nosound.cpp delete mode 100644 engines/sludge/sound_openal.cpp (limited to 'engines/sludge') diff --git a/engines/sludge/module.mk b/engines/sludge/module.mk index b0913617a1..f1e4cadc42 100644 --- a/engines/sludge/module.mk +++ b/engines/sludge/module.mk @@ -30,7 +30,7 @@ MODULE_OBJS := \ savedata.o \ sludge.o \ sludger.o \ - sound_openal.o \ + sound.o \ sprbanks.o \ sprites.o \ statusba.o \ diff --git a/engines/sludge/sound.cpp b/engines/sludge/sound.cpp new file mode 100644 index 0000000000..360ad6d516 --- /dev/null +++ b/engines/sludge/sound.cpp @@ -0,0 +1,833 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#if 0 +#include "AL/alure.h" +#endif + +#include "common/file.h" + +#include "sludge/allfiles.h" +#include "sludge/debug.h" +#include "sludge/newfatal.h" +#include "sludge/sound.h" +#include "sludge/moreio.h" +#include "sludge/fileset.h" + +#define MAX_SAMPLES 8 +#define MAX_MODS 3 +#define NUM_BUFS 3 + +namespace Sludge { + +bool soundOK = false; +bool cacheLoopySound = false; +bool SilenceIKillYou = false; + +struct soundThing { +#if 0 + alureStream *stream; + ALuint playingOnSource; +#endif + bool playing; + int fileLoaded, vol; //Used for sounds only. + bool looping; //Used for sounds only. +}; + +soundThing soundCache[MAX_SAMPLES]; +soundThing modCache[MAX_MODS]; +int intpointers[MAX_SAMPLES]; + +int defVol = 128; +int defSoundVol = 255; +const float modLoudness = 0.95f; + +/* + * Set up, tear down: + */ + +bool initSoundStuff(HWND hwnd) { +#if 0 + if (!alureInitDevice(NULL, NULL)) { + debugOut("Failed to open OpenAL device: %s\n", alureGetErrorString()); + return 1; + } + + int a; + for (a = 0; a < MAX_SAMPLES; a ++) { + soundCache[a].stream = NULL; + soundCache[a].playing = false; + soundCache[a].fileLoaded = -1; + soundCache[a].looping = false; + intpointers[a] = a; + } + + for (a = 0; a < MAX_MODS; a ++) { + modCache[a].stream = NULL; + modCache[a].playing = false; + } + + if (! alureUpdateInterval(0.01)) { + debugOut("Failed to set Alure update interval: %s\n", alureGetErrorString()); + return 1; + } +#endif + return soundOK = true; +} + +void killSoundStuff() { + if (!soundOK) + return; +#if 0 + SilenceIKillYou = true; + for (int i = 0; i < MAX_SAMPLES; i ++) { + if (soundCache[i].playing) { + + if (! alureStopSource(soundCache[i].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", + alureGetErrorString()); + } + + } + + if (soundCache[i].stream != NULL) { + + if (! alureDestroyStream(soundCache[i].stream, 0, NULL)) { + debugOut("Failed to destroy stream: %s\n", + alureGetErrorString()); + } + + } + } + + for (int i = 0; i < MAX_MODS; i ++) { + if (modCache[i].playing) { + + if (! alureStopSource(modCache[i].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", + alureGetErrorString()); + } + + } + + if (modCache[i].stream != NULL) { + + if (! alureDestroyStream(modCache[i].stream, 0, NULL)) { + debugOut("Failed to destroy stream: %s\n", + alureGetErrorString()); + } + + } + } + + SilenceIKillYou = false; + + alureShutdownDevice(); +#endif +} + +/* + * Some setters: + */ + +void setMusicVolume(int a, int v) { + if (!soundOK) + return; + + if (modCache[a].playing) { +#if 0 + alSourcef(modCache[a].playingOnSource, AL_GAIN, (float) modLoudness * v / 256); +#endif + } +} + +void setDefaultMusicVolume(int v) { + defVol = v; +} + +void setSoundVolume(int a, int v) { + if (!soundOK) + return; + int ch = findInSoundCache(a); + if (ch != -1) { + if (soundCache[ch].playing) { + soundCache[ch].vol = v; +#if 0 + alSourcef(soundCache[ch].playingOnSource, AL_GAIN, (float) v / 256); +#endif + } + } +} + +void setDefaultSoundVolume(int v) { + defSoundVol = v; +} + +void setSoundLoop(int a, int s, int e) { +//#pragma unused (a,s,e) +} + +/* + * End of stream callbacks: + */ + +#if 0 +static void sound_eos_callback(void *cacheIndex, ALuint source) { + int *a = (int *)cacheIndex; + + alDeleteSources(1, &source); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } + + soundCache[*a].playingOnSource = 0; + soundCache[*a].playing = false; + soundCache[*a].looping = false; + +} +#endif + +#if 0 +static void mod_eos_callback(void *cacheIndex, ALuint source) { + int *a = (int *)cacheIndex; + + alDeleteSources(1, &source); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } + + modCache[*a].playingOnSource = 0; + + if (! alureDestroyStream(modCache[*a].stream, 0, NULL)) { + debugOut("Failed to destroy stream: %s\n", + alureGetErrorString()); + } + + modCache[*a].stream = NULL; + modCache[*a].playing = false; +} +#endif + +/* + * Stopping things: + */ + +int findInSoundCache(int a) { + int i; + for (i = 0; i < MAX_SAMPLES; i++) { + if (soundCache[i].fileLoaded == a) { + return i; + } + } + return -1; +} + +void stopMOD(int i) { + if (!soundOK) + return; +#if 0 + alGetError(); + if (modCache[i].playing) { + if (! alureStopSource(modCache[i].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", alureGetErrorString()); + } + } +#endif +} + +void huntKillSound(int filenum) { + if (!soundOK) + return; +#if 0 + // Clear OpenAL errors to make sure they don't block anything: + alGetError(); + + int gotSlot = findInSoundCache(filenum); + if (gotSlot == -1) return; + + SilenceIKillYou = true; + + if (soundCache[gotSlot].playing) { + if (! alureStopSource(soundCache[gotSlot].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", alureGetErrorString()); + } + } +#endif + SilenceIKillYou = false; +} + +void freeSound(int a) { + if (!soundOK) + return; +#if 0 + // Clear OpenAL errors to make sure they don't block anything: + alGetError(); + + SilenceIKillYou = true; + + if (soundCache[a].playing) { + if (! alureStopSource(soundCache[a].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", + alureGetErrorString()); + } + } + if (! alureDestroyStream(soundCache[a].stream, 0, NULL)) { + debugOut("Failed to destroy stream: %s\n", + alureGetErrorString()); + } + + soundCache[a].stream = NULL; + soundCache[a].fileLoaded = -1; +#endif + SilenceIKillYou = false; +} + +void huntKillFreeSound(int filenum) { + if (!soundOK) + return; + int gotSlot = findInSoundCache(filenum); + if (gotSlot == -1) + return; + freeSound(gotSlot); +} + +/* + * Loading and playing: + */ + +void playStream(int a, bool isMOD, bool loopy) { +#if 0 + if (! soundOK) return; + ALboolean ok; + ALuint src; + soundThing *st; + void (*eos_callback)(void *userdata, ALuint source); + + if (isMOD) { + st = &modCache[a]; + eos_callback = mod_eos_callback; + } else { + st = &soundCache[a]; + eos_callback = sound_eos_callback; + } + + alGenSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to create OpenAL source!\n"); + return; + } + + if (isMOD) { + alSourcef(src, AL_GAIN, (float) modLoudness * defVol / 256); + } else { + alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); + } + + if (loopy) { + ok = alurePlaySourceStream(src, (*st).stream, + NUM_BUFS, -1, eos_callback, &intpointers[a]); + } else { + ok = alurePlaySourceStream(src, (*st).stream, + NUM_BUFS, 0, eos_callback, &intpointers[a]); + } + + if (!ok) { + + debugOut("Failed to play stream: %s\n", alureGetErrorString()); + alDeleteSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } + + (*st).playingOnSource = 0; + } else { + (*st).playingOnSource = src; + (*st).playing = true; + } +#endif +} + +char *loadEntireFileToMemory(Common::SeekableReadStream *inputFile, + uint32 size) { + char *allData = new char[size]; + if (!allData) + return NULL; + + size_t bytes_read = inputFile->read(allData, size); + if (bytes_read != size && inputFile->err()) { + debugOut("Reading error in loadEntireFileToMemory.\n"); + } + + finishAccess(); + + return allData; +} + +bool playMOD(int f, int a, int fromTrack) { + if (!soundOK) + return true; + stopMOD(a); + + setResourceForFatal(f); + uint32 length = openFileFromNum(f); + if (length == 0) { + finishAccess(); + setResourceForFatal(-1); + return false; + } +#if 0 + unsigned char *memImage; + memImage = (unsigned char *) loadEntireFileToMemory(bigDataFile, length); + if (! memImage) return fatal(ERROR_MUSIC_MEMORY_LOW); + + modCache[a].stream = alureCreateStreamFromMemory(memImage, length, 19200, 0, NULL); + + delete memImage; + + if (modCache[a].stream != NULL) { + setMusicVolume(a, defVol); + + if (! alureSetStreamOrder(modCache[a].stream, fromTrack)) { + debugOut("Failed to set stream order: %s\n", + alureGetErrorString()); + } + + playStream(a, true, true); + + } else { + + debugOut("Failed to create stream from MOD: %s\n", + alureGetErrorString()); + + warning(ERROR_MUSIC_ODDNESS); + soundCache[a].stream = NULL; + soundCache[a].playing = false; + soundCache[a].playingOnSource = 0; + } + setResourceForFatal(-1); +#endif + return true; +} + +bool stillPlayingSound(int ch) { + if (soundOK) + if (ch != -1) + if (soundCache[ch].fileLoaded != -1) + if (soundCache[ch].playing) + return true; + + return false; +} + +bool forceRemoveSound() { + for (int a = 0; a < MAX_SAMPLES; a++) { + if (soundCache[a].fileLoaded != -1 && !stillPlayingSound(a)) { +// soundWarning ("Deleting silent sound", a); + freeSound(a); + return 1; + } + } + + for (int a = 0; a < MAX_SAMPLES; a++) { + if (soundCache[a].fileLoaded != -1) { +// soundWarning ("Deleting playing sound", a); + freeSound(a); + return 1; + } + } +// soundWarning ("Cache is empty!", 0); + return 0; +} + +int emptySoundSlot = 0; + +int findEmptySoundSlot() { + int t; + for (t = 0; t < MAX_SAMPLES; t++) { + emptySoundSlot++; + emptySoundSlot %= MAX_SAMPLES; +#if 0 + if (soundCache[emptySoundSlot].stream == NULL) + return emptySoundSlot; +#endif + } + + for (t = 0; t < MAX_SAMPLES; t++) { + emptySoundSlot++; + emptySoundSlot %= MAX_SAMPLES; + if (!soundCache[emptySoundSlot].playing) + return emptySoundSlot; + } + + // Argh! They're all playing! Let's trash the oldest that's not looping... + + for (t = 0; t < MAX_SAMPLES; t++) { + emptySoundSlot++; + emptySoundSlot %= MAX_SAMPLES; + if (!soundCache[emptySoundSlot].looping) + return emptySoundSlot; + } + + // Holy crap, they're all looping! What's this twat playing at? + + emptySoundSlot++; + emptySoundSlot %= MAX_SAMPLES; + return emptySoundSlot; +} + +int cacheSound(int f) { + + if (!soundOK) + return -1; + + unsigned int chunkLength; + int retval; + bool loopy; +#if 0 + loopy = cacheLoopySound; + cacheLoopySound = false; + + setResourceForFatal(f); + + if (! soundOK) return 0; + + int a = findInSoundCache(f); + if (a != -1) { + + if (soundCache[a].playing) { + if (! alureStopSource(soundCache[a].playingOnSource, AL_TRUE)) { + debugOut("Failed to stop source: %s\n", + alureGetErrorString()); + } + } + if (! alureRewindStream(soundCache[a].stream)) { + debugOut("Failed to rewind stream: %s\n", + alureGetErrorString()); + } + + return a; + } + if (f == -2) return -1; + a = findEmptySoundSlot(); + freeSound(a); + + uint32 length = openFileFromNum(f); + if (! length) return -1; + + unsigned char *memImage; + + bool tryAgain = true; + + while (tryAgain) { + memImage = (unsigned char *)loadEntireFileToMemory(bigDataFile, length); + tryAgain = memImage == NULL; + if (tryAgain) { + if (! forceRemoveSound()) { + fatal(ERROR_SOUND_MEMORY_LOW); + return -1; + } + } + } + + chunkLength = 19200; + + // Small looping sounds need small chunklengths. + if (loopy) { + if (length < NUM_BUFS * chunkLength) { + chunkLength = length / NUM_BUFS; + } + } else if (length < chunkLength) { + chunkLength = length; + } + + soundCache[a].stream = alureCreateStreamFromMemory(memImage, length, chunkLength, 0, NULL); + + delete[] memImage; + + if (soundCache[a].stream != NULL) { + soundCache[a].fileLoaded = f; + setResourceForFatal(-1); + retval = a; + } else { + + debugOut("Failed to create stream from sound: %s\n", + alureGetErrorString()); + + warning(ERROR_SOUND_ODDNESS); + soundCache[a].stream = NULL; + soundCache[a].playing = false; + soundCache[a].playingOnSource = 0; + soundCache[a].fileLoaded = -1; + soundCache[a].looping = false; + retval = -1; + } +#endif + return retval; +} + +bool startSound(int f, bool loopy) { + if (soundOK) { +#if 0 + cacheLoopySound = loopy; + int a = cacheSound(f); + if (a == -1) { + debugOut("Failed to cache sound!\n"); + return false; + } + soundCache[a].looping = loopy; + soundCache[a].vol = defSoundVol; + + playStream(a, false, loopy); +#endif + } + return true; +} + +void saveSounds(Common::WriteStream *stream) { + if (soundOK) { + for (int i = 0; i < MAX_SAMPLES; i++) { + if (soundCache[i].looping) { + stream->writeByte(1); + stream->writeUint16BE(soundCache[i].fileLoaded); + stream->writeUint16BE(soundCache[i].vol); + } + } + } + stream->writeByte(0); + stream->writeUint16BE(defSoundVol); + stream->writeUint16BE(defVol); +} + +void loadSounds(Common::SeekableReadStream *stream) { + for (int i = 0; i < MAX_SAMPLES; i++) + freeSound(i); + + while (stream->readByte()) { + int fileLoaded = stream->readUint16BE(); + defSoundVol = stream->readUint16BE(); + startSound(fileLoaded, 1); + } + + defSoundVol = stream->readUint16BE(); + defVol = stream->readUint16BE(); +} + +bool getSoundCacheStack(stackHandler *sH) { + variable newFileHandle; + newFileHandle.varType = SVT_NULL; + + for (int a = 0; a < MAX_SAMPLES; a++) { + if (soundCache[a].fileLoaded != -1) { + setVariable(newFileHandle, SVT_FILE, soundCache[a].fileLoaded); + if (!addVarToStackQuick(newFileHandle, sH->first)) + return false; + if (sH->last == NULL) + sH->last = sH->first; + } + } + return true; +} + +soundList *deleteSoundFromList(soundList *s) { + // Don't delete a playing sound. + if (s->cacheIndex) + return NULL; + + soundList *o = NULL; + if (!s->next) { + o = s->prev; + if (o) + o->next = NULL; + delete s; + return o; + } + if (s != s->next) { + o = s->next; + o->prev = s->prev; + if (o->prev) + o->prev->next = o; + } + delete s; + return o; +} + +#if 0 +static void list_eos_callback(void *list, ALuint source) { + soundList *s = (soundList *) list; + + int a = s->cacheIndex; +#if 0 + alDeleteSources(1, &source); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } +#endif + soundCache[a].playingOnSource = 0; + soundCache[a].playing = false; + soundCache[a].looping = false; + s-> cacheIndex = false; + if (SilenceIKillYou) { + while (s = deleteSoundFromList(s)); + } else { + if (s->next) { + if (s->next == s) { + int v = defSoundVol; + defSoundVol = soundCache[a].vol; + startSound(s->sound, true); + defSoundVol = v; + while (s = deleteSoundFromList(s)); + return; + } + s->next->vol = soundCache[a].vol; + playSoundList(s->next); + } else { + while (s = deleteSoundFromList(s)); + } + } +} +#endif + +void playSoundList(soundList *s) { +#if 0 + if (soundOK) { + cacheLoopySound = true; + int a = cacheSound(s->sound); + if (a == -1) { + debugOut("Failed to cache sound!\n"); + return; + } + soundCache[a].looping = false; + if (s->vol < 0) + soundCache[a].vol = defSoundVol; + else + soundCache[a].vol = s->vol; + s-> cacheIndex = a; + + ALboolean ok; + ALuint src; + soundThing *st; + + st = &soundCache[a]; + + alGenSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to create OpenAL source!\n"); + return; + } + + alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); + + ok = alurePlaySourceStream(src, (*st).stream, + NUM_BUFS, 0, list_eos_callback, s); + + if (!ok) { + + debugOut("Failed to play stream: %s\n", alureGetErrorString()); + alDeleteSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } + + (*st).playingOnSource = 0; + } else { + (*st).playingOnSource = src; + (*st).playing = true; + } + } +#endif +} + +void playMovieStream(int a) { +#if 0 + if (! soundOK) return; + ALboolean ok; + ALuint src; + + alGenSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to create OpenAL source!\n"); + return; + } + + alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); + + ok = alurePlaySourceStream(src, soundCache[a].stream, + 10, 0, sound_eos_callback, &intpointers[a]); + if (!ok) { + debugOut("Failed to play stream: %s\n", alureGetErrorString()); + alDeleteSources(1, &src); + if (alGetError() != AL_NO_ERROR) { + debugOut("Failed to delete OpenAL source!\n"); + } + + soundCache[a].playingOnSource = 0; + } else { + soundCache[a].playingOnSource = src; + soundCache[a].playing = true; + } +#endif +} + +#if 0 +int initMovieSound(int f, ALenum format, int audioChannels, ALuint samplerate, + ALuint(*callback)(void *userdata, ALubyte *data, ALuint bytes)) { + if (! soundOK) return 0; + + int retval; + int a = findEmptySoundSlot(); + freeSound(a); + + soundCache[a].looping = false; +#if 0 + // audioChannel * sampleRate gives us a buffer of half a second. Not much, but it should be enough. + soundCache[a].stream = alureCreateStreamFromCallback( + callback, + &intpointers[a], format, samplerate, + audioChannels * samplerate, 0, NULL); +#endif + if (soundCache[a].stream != NULL) { + soundCache[a].fileLoaded = f; + soundCache[a].vol = defSoundVol; + retval = a; + } else { +#if 0 + debugOut("Failed to create stream from sound: %s\n", + alureGetErrorString()); +#endif + warning(ERROR_SOUND_ODDNESS); + soundCache[a].stream = NULL; + soundCache[a].playing = false; + soundCache[a].playingOnSource = 0; + soundCache[a].fileLoaded = -1; + retval = -1; + } + //fprintf (stderr, "Stream %d created. Sample rate: %d Channels: %d\n", retval, samplerate, audioChannels); + + return retval; +} +#endif + +unsigned int getSoundSource(int index) { + return 0; /*soundCache[index].playingOnSource;*/ //TODO:false value +} + +} // End of namespace Sludge diff --git a/engines/sludge/sound_bass.cpp b/engines/sludge/sound_bass.cpp deleted file mode 100644 index d85981ed46..0000000000 --- a/engines/sludge/sound_bass.cpp +++ /dev/null @@ -1,409 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#include "sludge/allfiles.h" -#include "sludge/newfatal.h" -#include "sludge/sound.h" -#include "sludge/moreio.h" -#include "sludge/fileset.h" - -#define MAX_SAMPLES 8 -#define MAX_MODS 3 -#define EFFECT_CHANNELS 8 -#define TOTAL_CHANNELS 32 - -namespace Sludge { - -bool soundOK = false; - -struct soundThing { - HSAMPLE sample; - int fileLoaded, vol; - int mostRecentChannel; - bool looping; -}; - -DWORD mod[MAX_MODS]; - -soundThing soundCache[MAX_SAMPLES]; - -int defVol = 128; -int defSoundVol = 255; - -char *loadEntireFileToMemory(Common::SeekableReadStream *inputFile, uint32 size) { - char *allData = new char[size]; - if (!allData) - return NULL; - inputFile->read(allData, size); - finishAccess(); - - return allData; -} - -void stopMOD(int i) { - if (mod[i]) { - BASS_ChannelStop(mod[i]); - BASS_MusicFree(mod[i]); - mod[i] = NULL; - } -} - -int findInSoundCache(int a) { - int i; - for (i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].fileLoaded == a) - return i; - } - return -1; -} - -void huntKillSound(int filenum) { - int gotSlot = findInSoundCache(filenum); - if (gotSlot == -1) - return; - soundCache[gotSlot].looping = false; - BASS_SampleStop(soundCache[gotSlot].sample); -} - -void freeSound(int a) { - BASS_SampleFree(soundCache[a].sample); - soundCache[a].sample = NULL; - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; -} - -void huntKillFreeSound(int filenum) { - int gotSlot = findInSoundCache(filenum); - if (gotSlot != -1) - freeSound(gotSlot); -} - -bool initSoundStuff(HWND hwnd) { - if (HIWORD(BASS_GetVersion()) != BASSVERSION) { - warning(WARNING_BASS_WRONG_VERSION); - return false; - } - - if (!BASS_Init(-1, 44100, 0, hwnd, NULL)) { - warning(WARNING_BASS_FAIL); - return false; - } - - int a; - for (a = 0; a < MAX_SAMPLES; a ++) { - soundCache[a].sample = NULL; - soundCache[a].fileLoaded = -1; - } - - BASS_SetConfig(BASS_CONFIG_GVOL_MUSIC, 10000); - BASS_SetConfig(BASS_CONFIG_GVOL_SAMPLE, 10000); - BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 10000); - return soundOK = true; -} - -void killSoundStuff() { - if (soundOK) { - int a; - for (a = 0; a < MAX_MODS; a++) - stopMOD(a); - for (a = 0; a < MAX_SAMPLES; a++) - freeSound(a); - BASS_Free(); - } -} - -bool playMOD(int f, int a, int fromTrack) { - if (soundOK) { - - stopMOD(a); - - setResourceForFatal(f); - uint32 length = openFileFromNum(f); - if (length == 0) - return NULL; - - char *memImage; - memImage = loadEntireFileToMemory(bigDataFile, length); - if (!memImage) - return fatal(ERROR_MUSIC_MEMORY_LOW); - - mod[a] = BASS_MusicLoad(true, memImage, 0, length, BASS_MUSIC_LOOP | BASS_MUSIC_RAMP/*|BASS_MUSIC_PRESCAN needed too if we're going to set the position in bytes*/, 0); - delete memImage; - - if (!mod[a]) { - - } else { - setMusicVolume(a, defVol); - - if (!BASS_ChannelPlay(mod[a], true)) - debugOut("playMOD: Error %d!\n", BASS_ErrorGetCode()); - - BASS_ChannelSetPosition(mod[a], MAKELONG(fromTrack, 0), BASS_POS_MUSIC_ORDER); - BASS_ChannelFlags(mod[a], BASS_SAMPLE_LOOP | BASS_MUSIC_RAMP, BASS_SAMPLE_LOOP | BASS_MUSIC_RAMP); - } - setResourceForFatal(-1); - } - return true; -} - -void setMusicVolume(int a, int v) { - int ret; - if (soundOK && mod[a]) { - ret = BASS_ChannelSetAttribute(mod[a], BASS_ATTRIB_VOL, (float)v / 256); - if (!ret) { - debugOut("setMusicVolume: Error %d\n", BASS_ErrorGetCode()); - } - } -} - -void setDefaultMusicVolume(int v) { - defVol = v; -} - -void setSoundVolume(int a, int v) { - if (soundOK) { - int ch = findInSoundCache(a); - if (ch != -1) { - if (BASS_ChannelIsActive(soundCache[ch].mostRecentChannel)) { - BASS_ChannelSetAttribute(soundCache[ch].mostRecentChannel, BASS_ATTRIB_VOL, (float)v / 256); - } - } - } -} - -bool stillPlayingSound(int ch) { - if (soundOK) - if (ch != -1) - if (soundCache[ch].fileLoaded != -1) - if (BASS_ChannelIsActive(soundCache[ch].mostRecentChannel) != BASS_ACTIVE_STOPPED) - return true; - return false; -} - -void setSoundLoop(int a, int s, int e) { -// if (soundOK) { -// int ch = findInSoundCache (a); -// if (ch != -1) { -// int en = FSOUND_Sample_GetLength (soundCache[ch].sample); -// if (e < 1 || e >= en) e = en - 1; -// if (s < 0 || s >= e) s = 0; -// -// FSOUND_Sample_SetLoopPoints (soundCache[ch].sample, s, e); -// } -// } -} - -void setDefaultSoundVolume(int v) { - defSoundVol = v; -} - -int emptySoundSlot = 0; - -int findEmptySoundSlot() { - int t; - for (t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!soundCache[emptySoundSlot].sample) - return emptySoundSlot; - } - - // Argh!They're all playing!Let's trash the oldest that's not looping... - - for (t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!soundCache[emptySoundSlot].looping) - return emptySoundSlot; - } - - // Holy crap, they're all looping!What's this twat playing at? - - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - return emptySoundSlot; -} - -int guessSoundFree = 0; - -/* - void soundWarning (char * t, int i) { - FILE * u = fopen ("soundlog.txt", "at"); - fprintf (u, "%s: %i\n", t, i); - fclose (u); - } - */ - -bool forceRemoveSound() { - for (int a = 0; a < 8; a++) { - if (soundCache[a].fileLoaded != -1 && !stillPlayingSound(a)) { -// soundWarning ("Deleting silent sound", a); - freeSound(a); - return 1; - } - } - - for (int a = 0; a < 8; a++) { - if (soundCache[a].fileLoaded != -1) { -// soundWarning ("Deleting playing sound", a); - freeSound(a); - return 1; - } - } -// soundWarning ("Cache is empty!", 0); - return 0; -} - -int cacheSound(int f) { - setResourceForFatal(f); - - if (!soundOK) - return 0; - - int a = findInSoundCache(f); - if (a != -1) - return a; - if (f == -2) - return -1; - a = findEmptySoundSlot(); - freeSound(a); - - uint32 length = openFileFromNum(f); - if (!length) - return -1; - - char *memImage; - - bool tryAgain = true; - - while (tryAgain) { - memImage = loadEntireFileToMemory(bigDataFile, length); - tryAgain = memImage == NULL; - if (tryAgain) { - if (!forceRemoveSound()) { - fatal(ERROR_SOUND_MEMORY_LOW); - return -1; - } - } - } - - for (;;) { -// soundWarning (" Trying to load sound into slot", a); - soundCache[a].sample = BASS_SampleLoad(true, memImage, 0, length, 65535, 0); - - if (soundCache[a].sample) { - soundCache[a].fileLoaded = f; - delete memImage; - setResourceForFatal(-1); - return a; - } - - warning(ERROR_SOUND_ODDNESS); - soundCache[a].sample = NULL; - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; - return -1; - } -} - -bool startSound(int f, bool loopy) { - if (soundOK) { - int a = cacheSound(f); - if (a == -1) - return false; - - soundCache[a].looping = loopy; - soundCache[a].vol = defSoundVol; - - soundCache[a].mostRecentChannel = BASS_SampleGetChannel(soundCache[a].sample, false); - if (soundCache[a].mostRecentChannel) { - BASS_ChannelPlay(soundCache[a].mostRecentChannel, true); - BASS_ChannelSetAttribute(soundCache[a].mostRecentChannel, BASS_ATTRIB_VOL, defSoundVol); - if (loopy) { - BASS_ChannelFlags(soundCache[a].mostRecentChannel, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP); // set LOOP flag - } - } - - } - return true; -} - -/* - void debugSounds () { - FILE * fp = fopen ("newdebug.txt", "at"); - if (fp) { - for (int aa = 0; aa < 32; aa ++) { - if (aa == EFFECT_CHANNELS) fprintf (fp, "|"); - fprintf (fp, FSOUND_IsPlaying (aa) ? "#" : "."); - } - fprintf (fp, "\n"); - fclose (fp); - } - } - // */ - -void saveSounds(Common::WriteStream *stream) { - if (soundOK) { - for (int i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].looping) { - stream->writeByte(1); - stream->writeUint16BE(soundCache[i].fileLoaded); - stream->writeUint16BE(soundCache[i].vol); - } - } - } - stream->writeByte(0); - stream->writeUint16BE(defSoundVol); - stream->writeUint16BE(defVol); -} - -void loadSounds(Common::SeekableReadStream *stream) { - for (int i = 0; i < MAX_SAMPLES; i++) - freeSound(i); - - while (stream->readByte()) { - int fileLoaded = stream->readUint16BE(); - defSoundVol = stream->readUint16BE(); - startSound(fileLoaded, 1); - } - - defSoundVol = stream->readUint16BE(); - defVol = stream->readUint16BE(); -} - -bool getSoundCacheStack(stackHandler *sH) { - variable newFileHandle; - newFileHandle.varType = SVT_NULL; - - for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1) { - setVariable(newFileHandle, SVT_FILE, soundCache[a].fileLoaded); - if (!addVarToStackQuick(newFileHandle, sH->first)) - return false; - if (sH->last == NULL) - sH->last = sH->first; - } - } - return true; -} - -} // End of namespace Sludge diff --git a/engines/sludge/sound_nosound.cpp b/engines/sludge/sound_nosound.cpp deleted file mode 100644 index 46b6963166..0000000000 --- a/engines/sludge/sound_nosound.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#include "sludge/allfiles.h" -#include "sludge/newfatal.h" -#include "sludge/sound.h" -#include "sludge/moreio.h" -#include "sludge/fileset.h" - -namespace Sludge { - -bool soundOK = false; - -int defVol = 128; -int defSoundVol = 255; - -#if 0 -char *loadEntireFileToMemory(FILE *inputFile, uint32 size) { - char *allData = new char[size]; - if (! allData) return NULL; - fread(allData, size, 1, inputFile); - finishAccess(); - - return allData; -} -#endif - -int findInSoundCache(int a) { -//#pragma unused(a) - return -1; -} - -void stopMOD(int i) { -//#pragma unused(i) -} - -void huntKillSound(int filenum) { -//#pragma unused(filenum) -} - -void huntKillFreeSound(int filenum) { -//#pragma unused(filenum) -} - -bool initSoundStuff(HWND hwnd) { -// #pragma unused(hwnd) - return false; -} - -void killSoundStuff() { -} - -bool playMOD(int f, int a, int fromTrack) { -//#pragma unused (f,a,fromTrack) - return true; -} - -void setMusicVolume(int a, int v) { -//#pragma unused (a,v) -} - -void setDefaultMusicVolume(int v) { - defVol = v; -} - -void setSoundVolume(int a, int v) { -//#pragma unused (a,v) -} - -bool stillPlayingSound(int ch) { -//#pragma unused (ch) - return false; -} - -void setSoundLoop(int a, int s, int e) { -//#pragma unused (a,s,e) -} - -void setDefaultSoundVolume(int v) { - defSoundVol = v; -} - -bool forceRemoveSound() { - return 0; -} - -int cacheSound(int f) { -//#pragma unused (f) - return 0; -} - -bool startSound(int f, bool loopy) { -//#pragma unused (f,loopy) - return true; -} - -void saveSounds(Common::WriteStream *stream) { - stream->writeByte(0); - stream->writeUint16BE(defSoundVol); - stream->writeUint16BE(defVol); -} - -void loadSounds(Common::SeekableReadStream *stream) { - while (stream->readByte()) { - stream->readUint16BE(); - stream->readUint16BE(); - } - - defSoundVol = stream->readUint16BE(); - defVol = stream->readUint16BE(); -} - -bool getSoundCacheStack(stackHandler *sH) { -//#pragma unused (sH) - return true; -} - -} // End of namespace Sludge diff --git a/engines/sludge/sound_openal.cpp b/engines/sludge/sound_openal.cpp deleted file mode 100644 index c72815134d..0000000000 --- a/engines/sludge/sound_openal.cpp +++ /dev/null @@ -1,831 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#if 0 -#include "AL/alure.h" -#endif - -#include "common/file.h" - -#include "sludge/allfiles.h" -#include "sludge/debug.h" -#include "sludge/newfatal.h" -#include "sludge/sound.h" -#include "sludge/moreio.h" -#include "sludge/fileset.h" - -#define MAX_SAMPLES 8 -#define MAX_MODS 3 -#define NUM_BUFS 3 - -namespace Sludge { - -bool soundOK = false; -bool cacheLoopySound = false; -bool SilenceIKillYou = false; - -struct soundThing { -#if 0 - alureStream *stream; - ALuint playingOnSource; -#endif - bool playing; - int fileLoaded, vol; //Used for sounds only. - bool looping; //Used for sounds only. -}; - -soundThing soundCache[MAX_SAMPLES]; -soundThing modCache[MAX_MODS]; -int intpointers[MAX_SAMPLES]; - -int defVol = 128; -int defSoundVol = 255; -const float modLoudness = 0.95f; - -/* - * Set up, tear down: - */ - -bool initSoundStuff(HWND hwnd) { -#if 0 - if (!alureInitDevice(NULL, NULL)) { - debugOut("Failed to open OpenAL device: %s\n", alureGetErrorString()); - return 1; - } - - int a; - for (a = 0; a < MAX_SAMPLES; a ++) { - soundCache[a].stream = NULL; - soundCache[a].playing = false; - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; - intpointers[a] = a; - } - - for (a = 0; a < MAX_MODS; a ++) { - modCache[a].stream = NULL; - modCache[a].playing = false; - } - - if (! alureUpdateInterval(0.01)) { - debugOut("Failed to set Alure update interval: %s\n", alureGetErrorString()); - return 1; - } -#endif - return soundOK = true; -} - -void killSoundStuff() { - if (!soundOK) - return; -#if 0 - SilenceIKillYou = true; - for (int i = 0; i < MAX_SAMPLES; i ++) { - if (soundCache[i].playing) { - - if (! alureStopSource(soundCache[i].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", - alureGetErrorString()); - } - - } - - if (soundCache[i].stream != NULL) { - - if (! alureDestroyStream(soundCache[i].stream, 0, NULL)) { - debugOut("Failed to destroy stream: %s\n", - alureGetErrorString()); - } - - } - } - - for (int i = 0; i < MAX_MODS; i ++) { - if (modCache[i].playing) { - - if (! alureStopSource(modCache[i].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", - alureGetErrorString()); - } - - } - - if (modCache[i].stream != NULL) { - - if (! alureDestroyStream(modCache[i].stream, 0, NULL)) { - debugOut("Failed to destroy stream: %s\n", - alureGetErrorString()); - } - - } - } - - SilenceIKillYou = false; - - alureShutdownDevice(); -#endif -} - -/* - * Some setters: - */ - -void setMusicVolume(int a, int v) { - if (!soundOK) - return; - - if (modCache[a].playing) { -#if 0 - alSourcef(modCache[a].playingOnSource, AL_GAIN, (float) modLoudness * v / 256); -#endif - } -} - -void setDefaultMusicVolume(int v) { - defVol = v; -} - -void setSoundVolume(int a, int v) { - if (!soundOK) - return; - int ch = findInSoundCache(a); - if (ch != -1) { - if (soundCache[ch].playing) { - soundCache[ch].vol = v; -#if 0 - alSourcef(soundCache[ch].playingOnSource, AL_GAIN, (float) v / 256); -#endif - } - } -} - -void setDefaultSoundVolume(int v) { - defSoundVol = v; -} - -void setSoundLoop(int a, int s, int e) { -//#pragma unused (a,s,e) -} - -/* - * End of stream callbacks: - */ - -#if 0 -static void sound_eos_callback(void *cacheIndex, ALuint source) { - int *a = (int *)cacheIndex; - - alDeleteSources(1, &source); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } - - soundCache[*a].playingOnSource = 0; - soundCache[*a].playing = false; - soundCache[*a].looping = false; - -} -#endif - -#if 0 -static void mod_eos_callback(void *cacheIndex, ALuint source) { - int *a = (int *)cacheIndex; - - alDeleteSources(1, &source); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } - - modCache[*a].playingOnSource = 0; - - if (! alureDestroyStream(modCache[*a].stream, 0, NULL)) { - debugOut("Failed to destroy stream: %s\n", - alureGetErrorString()); - } - - modCache[*a].stream = NULL; - modCache[*a].playing = false; -} -#endif - -/* - * Stopping things: - */ - -int findInSoundCache(int a) { - int i; - for (i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].fileLoaded == a) { - return i; - } - } - return -1; -} - -void stopMOD(int i) { - if (!soundOK) - return; -#if 0 - alGetError(); - if (modCache[i].playing) { - if (! alureStopSource(modCache[i].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", alureGetErrorString()); - } - } -#endif -} - -void huntKillSound(int filenum) { - if (!soundOK) - return; -#if 0 - // Clear OpenAL errors to make sure they don't block anything: - alGetError(); - - int gotSlot = findInSoundCache(filenum); - if (gotSlot == -1) return; - - SilenceIKillYou = true; - - if (soundCache[gotSlot].playing) { - if (! alureStopSource(soundCache[gotSlot].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", alureGetErrorString()); - } - } -#endif - SilenceIKillYou = false; -} - -void freeSound(int a) { - if (!soundOK) - return; -#if 0 - // Clear OpenAL errors to make sure they don't block anything: - alGetError(); - - SilenceIKillYou = true; - - if (soundCache[a].playing) { - if (! alureStopSource(soundCache[a].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", - alureGetErrorString()); - } - } - if (! alureDestroyStream(soundCache[a].stream, 0, NULL)) { - debugOut("Failed to destroy stream: %s\n", - alureGetErrorString()); - } - - soundCache[a].stream = NULL; - soundCache[a].fileLoaded = -1; -#endif - SilenceIKillYou = false; -} - -void huntKillFreeSound(int filenum) { - if (!soundOK) - return; - int gotSlot = findInSoundCache(filenum); - if (gotSlot == -1) - return; - freeSound(gotSlot); -} - -/* - * Loading and playing: - */ - -void playStream(int a, bool isMOD, bool loopy) { -#if 0 - if (! soundOK) return; - ALboolean ok; - ALuint src; - soundThing *st; - void (*eos_callback)(void *userdata, ALuint source); - - if (isMOD) { - st = &modCache[a]; - eos_callback = mod_eos_callback; - } else { - st = &soundCache[a]; - eos_callback = sound_eos_callback; - } - - alGenSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to create OpenAL source!\n"); - return; - } - - if (isMOD) { - alSourcef(src, AL_GAIN, (float) modLoudness * defVol / 256); - } else { - alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); - } - - if (loopy) { - ok = alurePlaySourceStream(src, (*st).stream, - NUM_BUFS, -1, eos_callback, &intpointers[a]); - } else { - ok = alurePlaySourceStream(src, (*st).stream, - NUM_BUFS, 0, eos_callback, &intpointers[a]); - } - - if (!ok) { - - debugOut("Failed to play stream: %s\n", alureGetErrorString()); - alDeleteSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } - - (*st).playingOnSource = 0; - } else { - (*st).playingOnSource = src; - (*st).playing = true; - } -#endif -} - -char *loadEntireFileToMemory(Common::SeekableReadStream *inputFile, - uint32 size) { - char *allData = new char[size]; - if (!allData) - return NULL; - - size_t bytes_read = inputFile->read(allData, size); - if (bytes_read != size && inputFile->err()) { - debugOut("Reading error in loadEntireFileToMemory.\n"); - } - - finishAccess(); - - return allData; -} - -bool playMOD(int f, int a, int fromTrack) { - if (!soundOK) - return true; - stopMOD(a); - - setResourceForFatal(f); - uint32 length = openFileFromNum(f); - if (length == 0) { - finishAccess(); - setResourceForFatal(-1); - return false; - } -#if 0 - unsigned char *memImage; - memImage = (unsigned char *) loadEntireFileToMemory(bigDataFile, length); - if (! memImage) return fatal(ERROR_MUSIC_MEMORY_LOW); - - modCache[a].stream = alureCreateStreamFromMemory(memImage, length, 19200, 0, NULL); - - delete memImage; - - if (modCache[a].stream != NULL) { - setMusicVolume(a, defVol); - - if (! alureSetStreamOrder(modCache[a].stream, fromTrack)) { - debugOut("Failed to set stream order: %s\n", - alureGetErrorString()); - } - - playStream(a, true, true); - - } else { - - debugOut("Failed to create stream from MOD: %s\n", - alureGetErrorString()); - - warning(ERROR_MUSIC_ODDNESS); - soundCache[a].stream = NULL; - soundCache[a].playing = false; - soundCache[a].playingOnSource = 0; - } - setResourceForFatal(-1); -#endif - return true; -} - -bool stillPlayingSound(int ch) { - if (soundOK) - if (ch != -1) - if (soundCache[ch].fileLoaded != -1) - if (soundCache[ch].playing) - return true; - - return false; -} - -bool forceRemoveSound() { - for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1 && !stillPlayingSound(a)) { -// soundWarning ("Deleting silent sound", a); - freeSound(a); - return 1; - } - } - - for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1) { -// soundWarning ("Deleting playing sound", a); - freeSound(a); - return 1; - } - } -// soundWarning ("Cache is empty!", 0); - return 0; -} - -int emptySoundSlot = 0; - -int findEmptySoundSlot() { - int t; - for (t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; -#if 0 - if (soundCache[emptySoundSlot].stream == NULL) - return emptySoundSlot; -#endif - } - - for (t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!soundCache[emptySoundSlot].playing) - return emptySoundSlot; - } - - // Argh! They're all playing! Let's trash the oldest that's not looping... - - for (t = 0; t < MAX_SAMPLES; t++) { - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - if (!soundCache[emptySoundSlot].looping) - return emptySoundSlot; - } - - // Holy crap, they're all looping! What's this twat playing at? - - emptySoundSlot++; - emptySoundSlot %= MAX_SAMPLES; - return emptySoundSlot; -} - -int cacheSound(int f) { - - if (!soundOK) - return -1; - - unsigned int chunkLength; - int retval; - bool loopy; -#if 0 - loopy = cacheLoopySound; - cacheLoopySound = false; - - setResourceForFatal(f); - - if (! soundOK) return 0; - - int a = findInSoundCache(f); - if (a != -1) { - - if (soundCache[a].playing) { - if (! alureStopSource(soundCache[a].playingOnSource, AL_TRUE)) { - debugOut("Failed to stop source: %s\n", - alureGetErrorString()); - } - } - if (! alureRewindStream(soundCache[a].stream)) { - debugOut("Failed to rewind stream: %s\n", - alureGetErrorString()); - } - - return a; - } - if (f == -2) return -1; - a = findEmptySoundSlot(); - freeSound(a); - - uint32 length = openFileFromNum(f); - if (! length) return -1; - - unsigned char *memImage; - - bool tryAgain = true; - - while (tryAgain) { - memImage = (unsigned char *)loadEntireFileToMemory(bigDataFile, length); - tryAgain = memImage == NULL; - if (tryAgain) { - if (! forceRemoveSound()) { - fatal(ERROR_SOUND_MEMORY_LOW); - return -1; - } - } - } - - chunkLength = 19200; - - // Small looping sounds need small chunklengths. - if (loopy) { - if (length < NUM_BUFS * chunkLength) { - chunkLength = length / NUM_BUFS; - } - } else if (length < chunkLength) { - chunkLength = length; - } - - soundCache[a].stream = alureCreateStreamFromMemory(memImage, length, chunkLength, 0, NULL); - - delete[] memImage; - - if (soundCache[a].stream != NULL) { - soundCache[a].fileLoaded = f; - setResourceForFatal(-1); - retval = a; - } else { - - debugOut("Failed to create stream from sound: %s\n", - alureGetErrorString()); - - warning(ERROR_SOUND_ODDNESS); - soundCache[a].stream = NULL; - soundCache[a].playing = false; - soundCache[a].playingOnSource = 0; - soundCache[a].fileLoaded = -1; - soundCache[a].looping = false; - retval = -1; - } -#endif - return retval; -} - -bool startSound(int f, bool loopy) { - if (soundOK) { - cacheLoopySound = loopy; - int a = cacheSound(f); - if (a == -1) { - debugOut("Failed to cache sound!\n"); - return false; - } - soundCache[a].looping = loopy; - soundCache[a].vol = defSoundVol; - - playStream(a, false, loopy); - } - return true; -} - -void saveSounds(Common::WriteStream *stream) { - if (soundOK) { - for (int i = 0; i < MAX_SAMPLES; i++) { - if (soundCache[i].looping) { - stream->writeByte(1); - stream->writeUint16BE(soundCache[i].fileLoaded); - stream->writeUint16BE(soundCache[i].vol); - } - } - } - stream->writeByte(0); - stream->writeUint16BE(defSoundVol); - stream->writeUint16BE(defVol); -} - -void loadSounds(Common::SeekableReadStream *stream) { - for (int i = 0; i < MAX_SAMPLES; i++) - freeSound(i); - - while (stream->readByte()) { - int fileLoaded = stream->readUint16BE(); - defSoundVol = stream->readUint16BE(); - startSound(fileLoaded, 1); - } - - defSoundVol = stream->readUint16BE(); - defVol = stream->readUint16BE(); -} - -bool getSoundCacheStack(stackHandler *sH) { - variable newFileHandle; - newFileHandle.varType = SVT_NULL; - - for (int a = 0; a < MAX_SAMPLES; a++) { - if (soundCache[a].fileLoaded != -1) { - setVariable(newFileHandle, SVT_FILE, soundCache[a].fileLoaded); - if (!addVarToStackQuick(newFileHandle, sH->first)) - return false; - if (sH->last == NULL) - sH->last = sH->first; - } - } - return true; -} - -soundList *deleteSoundFromList(soundList *s) { - // Don't delete a playing sound. - if (s->cacheIndex) - return NULL; - - soundList *o = NULL; - if (!s->next) { - o = s->prev; - if (o) - o->next = NULL; - delete s; - return o; - } - if (s != s->next) { - o = s->next; - o->prev = s->prev; - if (o->prev) - o->prev->next = o; - } - delete s; - return o; -} - -#if 0 -static void list_eos_callback(void *list, ALuint source) { - soundList *s = (soundList *) list; - - int a = s->cacheIndex; -#if 0 - alDeleteSources(1, &source); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } -#endif - soundCache[a].playingOnSource = 0; - soundCache[a].playing = false; - soundCache[a].looping = false; - s-> cacheIndex = false; - if (SilenceIKillYou) { - while (s = deleteSoundFromList(s)); - } else { - if (s->next) { - if (s->next == s) { - int v = defSoundVol; - defSoundVol = soundCache[a].vol; - startSound(s->sound, true); - defSoundVol = v; - while (s = deleteSoundFromList(s)); - return; - } - s->next->vol = soundCache[a].vol; - playSoundList(s->next); - } else { - while (s = deleteSoundFromList(s)); - } - } -} -#endif - -void playSoundList(soundList *s) { -#if 0 - if (soundOK) { - cacheLoopySound = true; - int a = cacheSound(s->sound); - if (a == -1) { - debugOut("Failed to cache sound!\n"); - return; - } - soundCache[a].looping = false; - if (s->vol < 0) - soundCache[a].vol = defSoundVol; - else - soundCache[a].vol = s->vol; - s-> cacheIndex = a; - - ALboolean ok; - ALuint src; - soundThing *st; - - st = &soundCache[a]; - - alGenSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to create OpenAL source!\n"); - return; - } - - alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); - - ok = alurePlaySourceStream(src, (*st).stream, - NUM_BUFS, 0, list_eos_callback, s); - - if (!ok) { - - debugOut("Failed to play stream: %s\n", alureGetErrorString()); - alDeleteSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } - - (*st).playingOnSource = 0; - } else { - (*st).playingOnSource = src; - (*st).playing = true; - } - } -#endif -} - -void playMovieStream(int a) { -#if 0 - if (! soundOK) return; - ALboolean ok; - ALuint src; - - alGenSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to create OpenAL source!\n"); - return; - } - - alSourcef(src, AL_GAIN, (float) soundCache[a].vol / 256); - - ok = alurePlaySourceStream(src, soundCache[a].stream, - 10, 0, sound_eos_callback, &intpointers[a]); - if (!ok) { - debugOut("Failed to play stream: %s\n", alureGetErrorString()); - alDeleteSources(1, &src); - if (alGetError() != AL_NO_ERROR) { - debugOut("Failed to delete OpenAL source!\n"); - } - - soundCache[a].playingOnSource = 0; - } else { - soundCache[a].playingOnSource = src; - soundCache[a].playing = true; - } -#endif -} - -#if 0 -int initMovieSound(int f, ALenum format, int audioChannels, ALuint samplerate, - ALuint(*callback)(void *userdata, ALubyte *data, ALuint bytes)) { - if (! soundOK) return 0; - - int retval; - int a = findEmptySoundSlot(); - freeSound(a); - - soundCache[a].looping = false; -#if 0 - // audioChannel * sampleRate gives us a buffer of half a second. Not much, but it should be enough. - soundCache[a].stream = alureCreateStreamFromCallback( - callback, - &intpointers[a], format, samplerate, - audioChannels * samplerate, 0, NULL); -#endif - if (soundCache[a].stream != NULL) { - soundCache[a].fileLoaded = f; - soundCache[a].vol = defSoundVol; - retval = a; - } else { -#if 0 - debugOut("Failed to create stream from sound: %s\n", - alureGetErrorString()); -#endif - warning(ERROR_SOUND_ODDNESS); - soundCache[a].stream = NULL; - soundCache[a].playing = false; - soundCache[a].playingOnSource = 0; - soundCache[a].fileLoaded = -1; - retval = -1; - } - //fprintf (stderr, "Stream %d created. Sample rate: %d Channels: %d\n", retval, samplerate, audioChannels); - - return retval; -} -#endif - -unsigned int getSoundSource(int index) { - return 0; /*soundCache[index].playingOnSource;*/ //TODO:false value -} - -} // End of namespace Sludge -- cgit v1.2.3