From 43875b42fcefae5e0f009acd87407e8d545623c6 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 23 Dec 2003 19:14:57 +0000 Subject: Allow sound ID for MP3/Vorbis sounds, too; cleaned up Vorbis playback code a bit svn-id: r11879 --- sound/mixer.cpp | 36 ++++++++++++++++++++++++++---------- sound/mixer.h | 17 +++++++++++------ sound/vorbis.cpp | 35 +++++++++++++++++------------------ sound/vorbis.h | 3 +-- 4 files changed, 55 insertions(+), 36 deletions(-) (limited to 'sound') diff --git a/sound/mixer.cpp b/sound/mixer.cpp index 1a069bb3b0..7fbd030f6f 100644 --- a/sound/mixer.cpp +++ b/sound/mixer.cpp @@ -256,34 +256,50 @@ void SoundMixer::playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, u } #ifdef USE_MAD -void SoundMixer::playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume, int8 pan) { +void SoundMixer::playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume, int8 pan, int id) { // Create the input stream AudioInputStream *input = makeMP3Stream(file, mad_timer_zero, size); - playInputStream(handle, input, false, volume, pan); + playInputStream(handle, input, false, volume, pan, id); } -void SoundMixer::playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume, int8 pan) { +void SoundMixer::playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume, int8 pan, int id) { // Create the input stream AudioInputStream *input = makeMP3Stream(file, duration, 0); - playInputStream(handle, input, true, volume, pan); + playInputStream(handle, input, true, volume, pan, id); } #endif #ifdef USE_VORBIS -void SoundMixer::playVorbis(PlayingSoundHandle *handle, File *file, uint32 size) { - playSfxSound_Vorbis(this, file, size, handle); +void SoundMixer::playVorbis(PlayingSoundHandle *handle, File *file, uint32 size, byte volume, int8 pan, int id) { + // Create the input stream + AudioInputStream *input = makeVorbisStream(file, size); + playInputStream(handle, input, false, volume, pan, id); } -void SoundMixer::playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume, int8 pan) { +void SoundMixer::playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume, int8 pan, int id) { // Create the input stream AudioInputStream *input = makeVorbisStream(ov_file, duration); - playInputStream(handle, input, is_cd_track, volume, pan); + playInputStream(handle, input, is_cd_track, volume, pan, id); } #endif -void SoundMixer::playInputStream(PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume, int8 pan) { +void SoundMixer::playInputStream(PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume, int8 pan, int id) { Common::StackLock lock(_mutex); + if (input == 0) { + warning("input stream is 0"); + return; + } + + // Prevent duplicate sounds + if (id != -1) { + for (int i = 0; i != NUM_CHANNELS; i++) + if (_channels[i] != 0 && _channels[i]->getId() == id) { + delete input; + return; + } + } + // Create the channel - Channel *chan = new Channel(this, handle, input, isMusic, volume, pan); + Channel *chan = new Channel(this, handle, input, isMusic, volume, pan, false, id); insertChannel(handle, chan); } diff --git a/sound/mixer.h b/sound/mixer.h index 33151b7007..3ed2cd798f 100644 --- a/sound/mixer.h +++ b/sound/mixer.h @@ -99,15 +99,15 @@ public: void playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, int id = -1, byte volume = 255, int8 pan = 0, uint32 loopStart = 0, uint32 loopEnd = 0); #ifdef USE_MAD - void playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume = 255, int8 pan = 0); - void playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume = 255, int8 pan = 0); + void playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume = 255, int8 pan = 0, int id = -1); + void playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume = 255, int8 pan = 0, int id = -1); #endif #ifdef USE_VORBIS - void playVorbis(PlayingSoundHandle *handle, File *file, uint32 size); - void playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume = 255, int8 pan = 0); + void playVorbis(PlayingSoundHandle *handle, File *file, uint32 size, byte volume = 255, int8 pan = 0, int id = -1); + void playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume = 255, int8 pan = 0, int id = -1); #endif - void playInputStream(PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume = 255, int8 pan = 0); + void playInputStream(PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume = 255, int8 pan = 0, int id = -1); /** Start a new stream. */ @@ -116,7 +116,12 @@ public: /** Append to an existing stream. */ void appendStream(PlayingSoundHandle handle, void *sound, uint32 size); - /** Mark a stream as finished - it will play all its remaining data, then stop. */ + /** + * Mark a stream as finished. + * Where stopHandle() would stop the sound immediately, when using this + * method, the stream will first finish playing all its data before it + * finally stops. + */ void endStream(PlayingSoundHandle handle); /** stop all currently playing sounds */ diff --git a/sound/vorbis.cpp b/sound/vorbis.cpp index b5c8e033fc..d5fcb47526 100644 --- a/sound/vorbis.cpp +++ b/sound/vorbis.cpp @@ -128,24 +128,6 @@ VorbisTrackInfo::~VorbisTrackInfo() { } } -void playSfxSound_Vorbis(SoundMixer *mixer, File *file, uint32 size, PlayingSoundHandle *handle) { - OggVorbis_File *ov_file = new OggVorbis_File; - file_info *f = new file_info; - - f->file = file; - f->start = file->pos(); - f->len = size; - f->curr_pos = 0; - - if (ov_open_callbacks((void *) f, ov_file, NULL, 0, g_File_wrap) < 0) { - warning("Invalid file format"); - delete ov_file; - delete f; - } else - mixer->playVorbis(handle, ov_file, 0, false); -} - - #pragma mark - #pragma mark --- Ogg Vorbis stream --- #pragma mark - @@ -264,5 +246,22 @@ AudioInputStream *makeVorbisStream(OggVorbis_File *file, int duration) { return new VorbisInputStream(file, duration); } +AudioInputStream *makeVorbisStream(File *file, uint32 size) { + OggVorbis_File *ov_file = new OggVorbis_File; + file_info *f = new file_info; + + f->file = file; + f->start = file->pos(); + f->len = size; + f->curr_pos = 0; + + if (ov_open_callbacks((void *) f, ov_file, NULL, 0, g_File_wrap) < 0) { + warning("Invalid file format"); + delete ov_file; + delete f; + return 0; + } else + return new VorbisInputStream(ov_file, 0); +} #endif diff --git a/sound/vorbis.h b/sound/vorbis.h index 93e548c8f5..8b4fd0d11f 100644 --- a/sound/vorbis.h +++ b/sound/vorbis.h @@ -47,9 +47,8 @@ public: }; -void playSfxSound_Vorbis(SoundMixer *mixer, File *file, uint32 size, PlayingSoundHandle *handle); - AudioInputStream *makeVorbisStream(OggVorbis_File *file, int duration); +AudioInputStream *makeVorbisStream(File *file, uint32 size); #endif -- cgit v1.2.3