Permalink
Browse files

STARTREK: Music/SFX enable and disable functions

  • Loading branch information...
Drenn1 authored and sev- committed May 9, 2018
1 parent e578aa1 commit aac4b2e99d4cfa64dfb25bae85012235702ab6fe
@@ -147,8 +147,6 @@ class Graphics {
void showOptionsMenu(int x, int y);
void showSaveMenu();
void showLoadMenu();
void setMusicEnabled(bool enabled);
void setSfxEnabled(bool enabled);
void showQuitGamePrompt(int x, int y);
void showTextConfigurationMenu(bool fromOptionMenu);

@@ -17,6 +17,7 @@
* 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 "startrek/sound.h"
@@ -59,7 +60,7 @@ Sound::Sound(StarTrekEngine *vm) : _vm(vm) {
loadedSoundData = nullptr;

for (int i=1; i<8; i++) {
_sfxSlotList.push_back(&_midiSlots[i]);
_midiSlotList.push_back(&_midiSlots[i]);
}

if (!SearchMan.hasFile("voc/speech.mrk")) {
@@ -78,37 +79,50 @@ Sound::~Sound() {
}


/**
* Plays a midi track as a sound effect (one of midi slots 1-7)
*/
void Sound::playMidiTrack(int track) {
if (!_vm->_musicEnabled || !_vm->_musicWorking)
return;

assert(loadedSoundData != NULL);

debugC(6, kDebugSound, "Playing MIDI track %d", track);
assert(loadedSoundData != nullptr);

// Check if a midi slot for this track exists already
for (int i=1; i<8; i++) {
if (_midiSlots[i].track == track) {
debugC(6, kDebugSound, "Playing MIDI track %d (slot %d)", track, i);
_midiSlots[i].midiParser->loadMusic(loadedSoundData, sizeof(loadedSoundData));
_midiSlots[i].midiParser->setTrack(track);

// Shift this to the back (most recently used)
_sfxSlotList.remove(&_midiSlots[i]);
_sfxSlotList.push_back(&_midiSlots[i]);
_midiSlotList.remove(&_midiSlots[i]);
_midiSlotList.push_back(&_midiSlots[i]);
return;
}
}

// Take the least recently used slot and use that for the sound effect
MidiPlaybackSlot *slot = _sfxSlotList.front();
_sfxSlotList.pop_front();
_sfxSlotList.push_back(slot);
slot->track = track;
slot->midiParser->loadMusic(loadedSoundData, sizeof(loadedSoundData));
slot->midiParser->setTrack(track);
MidiPlaybackSlot *slot = _midiSlotList.front();
_midiSlotList.pop_front();
_midiSlotList.push_back(slot);
playMidiTrackInSlot(slot->slot, track);
}

void Sound::loadMusicFile(const char *baseSoundName) {
void Sound::playMidiTrackInSlot(int slot, int track) {
assert(loadedSoundData != nullptr);
debugC(6, kDebugSound, "Playing MIDI track %d (slot %d)", track, slot);

clearMidiSlot(slot);

if (track != -1) {
_midiSlots[slot].track = track;
_midiSlots[slot].midiParser->loadMusic(loadedSoundData, sizeof(loadedSoundData));
_midiSlots[slot].midiParser->setTrack(track);
}
}

void Sound::loadMusicFile(const Common::String &baseSoundName) {
clearAllMidiSlots();
/*
if (_vm->getPlatform() == Common::kPlatformAmiga)
@@ -122,21 +136,35 @@ void Sound::loadMusicFile(const char *baseSoundName) {
loadPCMusicFile(baseSoundName);
}

void Sound::playSoundEffect(const char *baseSoundName) {
void Sound::playMidiMusicTracks(int startTrack, int loopTrack) {
if (!_vm->_musicWorking || !_vm->_musicEnabled)
return;

if (loopTrack == -3)
_loopingMidiTrack = startTrack;
else if (loopTrack != -2)
_loopingMidiTrack = loopTrack;

if (startTrack != -2 && _vm->_musicEnabled)
playMidiTrackInSlot(0, startTrack);
}

void Sound::playVoc(const Common::String &baseSoundName) {
/*
if (_vm->getPlatform() == Common::kPlatformAmiga)
playAmigaSoundEffect(baseSoundName);
else if (_vm->getPlatform() == Common::kPlatformMacintosh)
playMacSoundEffect(baseSoundName);
else
*/
if (scumm_stricmp(baseSoundName+4, "loop") == 0)
_loopingAudioName = Common::String(baseSoundName);
if (baseSoundName.size() == 8 && baseSoundName.hasSuffixIgnoreCase("loop"))
_loopingAudioName = baseSoundName;

if (!_vm->_sfxEnabled || !_vm->_sfxWorking)
return;

/*
// This is probably just driver initialization stuff...
if (word_5113a == 0)
sub_2aaa3();
*/
@@ -156,7 +184,7 @@ void Sound::playSoundEffect(const char *baseSoundName) {
return;
}

debugC(3, kDebugSound, "No sound slot to play '%s'", baseSoundName);
debugC(3, kDebugSound, "No sound slot to play '%s'", baseSoundName.c_str());
}

void Sound::playSpeech(const Common::String &basename) {
@@ -196,6 +224,17 @@ void Sound::playSpeech(const Common::String &basename) {
_playingSpeech = true;
}

/**
* Called when disabling sfx.
*/
void Sound::stopAllVocSounds() {
stopPlayingSpeech();

for (int i = 0; i < MAX_SFX_PLAYING; i++) {
_vm->_system->getMixer()->stopHandle(_sfxHandles[i]);
}
}

void Sound::stopPlayingSpeech() {
if (_playingSpeech) {
debugC(5, kDebugSound, "Canceled speech playback");
@@ -205,8 +244,40 @@ void Sound::stopPlayingSpeech() {
}


void Sound::setMusicEnabled(bool enable) {
if (!_vm->_musicWorking || _vm->_musicEnabled == enable)
return;

_vm->_musicEnabled = enable;

if (enable)
playMidiMusicTracks(_loopingMidiTrack, _loopingMidiTrack);
else
clearMidiSlot(0);
}

void Sound::setSfxEnabled(bool enable) {
if (!_vm->_sfxWorking || _vm->_sfxEnabled == enable)
return;

_vm->_sfxEnabled = enable;

if (!enable) {
for (int i = 1; i < 8; i++)
clearMidiSlot(i);
}

if (!enable) {
stopAllVocSounds();
}
else if (!_loopingAudioName.empty()) {
playVoc(_loopingAudioName);
}
}


// XMIDI or SM sound
void Sound::loadPCMusicFile(const char *baseSoundName) {
void Sound::loadPCMusicFile(const Common::String &baseSoundName) {
Common::String soundName = baseSoundName;

soundName += '.';
@@ -18,9 +18,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL: https://scummvm-startrek.googlecode.com/svn/trunk/sound.h $
* $Id: sound.h 15 2010-06-27 06:13:42Z clone2727 $
*
*/

#ifndef STARTREK_SOUND_H
@@ -41,7 +38,7 @@ namespace StarTrek {
class StarTrekEngine;


const int MAX_LOADED_SFX_FILES = 8;
// Max # of VOC files that can play at once
const int MAX_SFX_PLAYING = 4;

struct MidiPlaybackSlot {
@@ -57,24 +54,29 @@ class Sound {
~Sound();

void playMidiTrack(int track);

void loadMusicFile(const char *baseSoundName);
void playSoundEffect(const char *baseSoundName);
void playMidiTrackInSlot(int slot, int track);
void loadMusicFile(const Common::String &baseSoundName);
void playMidiMusicTracks(int startTrack, int loopTrack);
void playVoc(const Common::String &baseSoundName);
void playSpeech(const Common::String &basename);
void stopAllVocSounds();
void stopPlayingSpeech();
void setMusicEnabled(bool enable);
void setSfxEnabled(bool enable);

private:
StarTrekEngine *_vm;
Audio::SoundHandle *_soundHandle;

void loadPCMusicFile(const char *baseSoundName);
void loadPCMusicFile(const Common::String &baseSoundName);
void clearMidiSlot(int slot);
void clearAllMidiSlots();

// MIDI-Related Variables
MidiDriver *_midiDriver;
MidiPlaybackSlot _midiSlots[8]; // 0 is for music; 1-7 are for sfx
Common::List<MidiPlaybackSlot*> _sfxSlotList; // Sorts midi slots by most recently used
Common::List<MidiPlaybackSlot*> _midiSlotList; // Sorts midi slots by most recently used
int _loopingMidiTrack;

byte *loadedSoundData;
uint32 _midiDevice;
@@ -81,7 +81,7 @@ Common::Error StarTrekEngine::run() {
assert(_macResFork->hasDataFork() && _macResFork->hasResFork());
}

initGraphics(320, 200);
initGraphics(SCREEN_WIDTH, SCREEN_HEIGHT);

initializeEventsAndMouse();

@@ -199,49 +199,49 @@ void StarTrekEngine::pollSystemEvents() {
void StarTrekEngine::playSoundEffectIndex(int index) {
switch(index) {
case 0x04:
_sound->playSoundEffect("tricorde");
_sound->playVoc("tricorde");
break;
case 0x05:
_sound->playSoundEffect("STDOOR1");
_sound->playVoc("STDOOR1");
break;
case 0x06:
_sound->playSoundEffect("PHASSHOT");
_sound->playVoc("PHASSHOT");
break;
case 0x07:
_sound->playMidiTrack(index);
break;
case 0x08:
_sound->playSoundEffect("TRANSDEM");
_sound->playVoc("TRANSDEM");
break;
case 0x09:
_sound->playSoundEffect("TRANSMAT");
_sound->playVoc("TRANSMAT");
break;
case 0x0a:
_sound->playSoundEffect("TRANSENE");
_sound->playVoc("TRANSENE");
break;
case 0x10: // Menu selection sound
_sound->playMidiTrack(index);
break;
case 0x22:
_sound->playSoundEffect("HAILING");
_sound->playVoc("HAILING");
break;
case 0x24:
_sound->playSoundEffect("PHASSHOT");
_sound->playVoc("PHASSHOT");
break;
case 0x25:
_sound->playSoundEffect("PHOTSHOT");
_sound->playVoc("PHOTSHOT");
break;
case 0x26:
_sound->playSoundEffect("HITSHIEL");
_sound->playVoc("HITSHIEL");
break;
case 0x27:
_sound->playMidiTrack(index);
break;
case 0x28:
_sound->playSoundEffect("REDALERT");
_sound->playVoc("REDALERT");
break;
case 0x29:
_sound->playSoundEffect("WARP");
_sound->playVoc("WARP");
break;
default:
break;
@@ -164,11 +164,11 @@ class StarTrekEngine : public ::Engine {
bool _inQuitGameMenu;


private:
Graphics *_gfx;
Sound *_sound;
Common::MacResManager *_macResFork;

private:
Common::MacResManager *_macResFork;
Room *_room;
};

Oops, something went wrong.

0 comments on commit aac4b2e

Please sign in to comment.