diff options
author | Max Horn | 2009-03-04 07:38:49 +0000 |
---|---|---|
committer | Max Horn | 2009-03-04 07:38:49 +0000 |
commit | ac2d012ae5ee9b7d7431432f2ce312d9f0b8e391 (patch) | |
tree | 3ec98103da26c0fcc09fc03e09405e9a8b72ca37 /engines/sci/sfx | |
parent | e783859d5c58546b05649c1eb92c6bdbf66b5bb2 (diff) | |
download | scummvm-rg350-ac2d012ae5ee9b7d7431432f2ce312d9f0b8e391.tar.gz scummvm-rg350-ac2d012ae5ee9b7d7431432f2ce312d9f0b8e391.tar.bz2 scummvm-rg350-ac2d012ae5ee9b7d7431432f2ce312d9f0b8e391.zip |
SCI: Merged sfx/mixer.cpp into sfx/player/polled.cpp
svn-id: r39116
Diffstat (limited to 'engines/sci/sfx')
-rw-r--r-- | engines/sci/sfx/core.cpp | 1 | ||||
-rw-r--r-- | engines/sci/sfx/mixer.cpp | 204 | ||||
-rw-r--r-- | engines/sci/sfx/mixer.h | 41 | ||||
-rw-r--r-- | engines/sci/sfx/player/polled.cpp | 163 |
4 files changed, 161 insertions, 248 deletions
diff --git a/engines/sci/sfx/core.cpp b/engines/sci/sfx/core.cpp index bef2b1b8b8..fc1e75eca5 100644 --- a/engines/sci/sfx/core.cpp +++ b/engines/sci/sfx/core.cpp @@ -28,7 +28,6 @@ #include "sci/tools.h" #include "sci/sfx/iterator_internal.h" #include "sci/sfx/player.h" -#include "sci/sfx/mixer.h" #include "sci/sfx/sci_midi.h" #include "common/system.h" diff --git a/engines/sci/sfx/mixer.cpp b/engines/sci/sfx/mixer.cpp deleted file mode 100644 index 8ec4f3a31e..0000000000 --- a/engines/sci/sfx/mixer.cpp +++ /dev/null @@ -1,204 +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. - * - * $URL$ - * $Id$ - * - */ - -#include "common/system.h" - -#include "sci/tools.h" -#include "sci/sfx/mixer.h" - -#include "sound/audiostream.h" -#include "sound/mixer.h" - -namespace Sci { - -class PCMFeedAudioStream : public Audio::AudioStream { -protected: - enum FeedMode { - FEED_MODE_ALIVE, - FEED_MODE_IDLE, - FEED_MODE_DEAD, - FEED_MODE_RESTART - }; - - /* Whether feed is alive or dead. */ - FeedMode _mode; - - /* Blank gap in frames. */ - int _gap; - - /* Feed. */ - sfx_pcm_feed_t *_feed; - - /* Timestamp of next frame requested by stream driver. */ - Audio::Timestamp _time; - -public: - PCMFeedAudioStream(sfx_pcm_feed_t *feed) - : _feed(feed), - _time(g_system->getMillis(), feed->conf.rate) { - - _feed->frame_size = (_feed->conf.stereo ? 2 : 1) * ((_feed->conf.format & SFX_PCM_FORMAT_16) ? 2 : 1); - _mode = FEED_MODE_ALIVE; - _gap = 0; - } - - ~PCMFeedAudioStream() { - _feed->destroy(_feed); - } - - virtual int readBuffer(int16 *buffer, const int numSamples); - - virtual bool isStereo() const { return _feed->conf.stereo; } - virtual int getRate() const { return _feed->conf.rate; } - - virtual bool endOfData() const { return _mode == FEED_MODE_DEAD; } - -protected: - void queryTimestamp(); -}; - -void PCMFeedAudioStream::queryTimestamp() { - if (_feed->get_timestamp) { - Audio::Timestamp stamp; - int val = _feed->get_timestamp(_feed, stamp); - - switch (val) { - case PCM_FEED_TIMESTAMP: - _gap = stamp.frameDiff(_time); - - if (_gap >= 0) - _mode = FEED_MODE_ALIVE; - else { - // FIXME: I don't quite understand what FEED_MODE_RESTART is for. - // The original DC mixer seemed to just stop and restart the stream. - // But why? To catch up with lagging sound? - // - // Walter says the following: - // "The FEED_MODE_RESTART might be there to re-sync after a debugger - // session where time passes for the mixer but not for the engine. - // I may have added this as a workaround for not being able to come - // up with a convenient way to implement mixer->pause() and mixer->resume() - // on DC." - // That makes some sense. - _mode = FEED_MODE_RESTART; - _time = Audio::Timestamp(g_system->getMillis(), _feed->conf.rate); - _gap = stamp.frameDiff(_time); - - if (_gap < 0) - _gap = 0; - } - break; - case PCM_FEED_IDLE: - _mode = FEED_MODE_IDLE; - break; - case PCM_FEED_EMPTY: - _mode = FEED_MODE_DEAD; - _gap = 0; - } - } else { - _mode = FEED_MODE_DEAD; - _gap = 0; - } -} - -static void U8_to_S16(byte *buf, int samples) { - for (int i = samples - 1; i >= 0; i--) { - buf[i * 2 + 1] = buf[i] - 0x80; - buf[i * 2] = 0; - } -} - -int PCMFeedAudioStream::readBuffer(int16 *buffer, const int numSamples) { - // FIXME: If ScummVM's mixer supported timestamps, then it would pass them - // as a parameter to this function. But currently, it doesn't. Therefore, we - // create a fake timestamp based on the current time. For comparison, a real - // timestamp could be adjusted for pauses in sound processing. And it would - // be synced for all audio streams. - Audio::Timestamp timestamp(g_system->getMillis(), _feed->conf.rate); - - int channels, frames_req; - int frames_recv = 0; - - _time = timestamp; - channels = _feed->conf.stereo == SFX_PCM_MONO ? 1 : 2; - frames_req = numSamples / channels; - - while (frames_req != frames_recv) { - int frames_left = frames_req - frames_recv; - byte *buf_pos = ((byte *)buffer) + frames_recv * channels * 2; - - if (_mode == FEED_MODE_IDLE) - queryTimestamp(); - - if (_mode == FEED_MODE_IDLE || _mode == FEED_MODE_DEAD) { - memset(buf_pos, 0, frames_left * channels * 2); - - _time = _time.addFrames(frames_left); - break; - } - - if (_gap) { - int frames = _gap; - - if (frames > frames_left) - frames = frames_left; - - memset(buf_pos, 0, frames * channels * 2); - - _gap -= frames; - frames_recv += frames; - _time = _time.addFrames(frames); - } else { - int frames = _feed->poll(_feed, buf_pos, frames_left); - - if (_feed->conf.format == SFX_PCM_FORMAT_U8) - U8_to_S16(buf_pos, frames * channels); - - frames_recv += frames; - _time = _time.addFrames(frames); - - if (frames < frames_left) - queryTimestamp(); - } - } - - return numSamples; -} - -void mixer_subscribe(sfx_pcm_feed_t *feed) { - if ((feed->conf.format != SFX_PCM_FORMAT_S16_NATIVE) && (feed->conf.format != SFX_PCM_FORMAT_U8)) { - error("[soft-mixer] Unsupported feed format %d", feed->conf.format); - } - - PCMFeedAudioStream *newStream = new PCMFeedAudioStream(feed); - - // FIXME: Is this sound type appropriate? The mixer seems to handle music, too. - g_system->getMixer()->playInputStream(Audio::Mixer::kSFXSoundType, 0, newStream); - - debug(2, "[soft-mixer] Subscribed %s-%x (%d Hz, %d/%x)", - feed->debug_name, feed->debug_nr, feed->conf.rate, feed->conf.stereo, feed->conf.format); -} - -} // End of namespace Sci diff --git a/engines/sci/sfx/mixer.h b/engines/sci/sfx/mixer.h deleted file mode 100644 index 955a29289f..0000000000 --- a/engines/sci/sfx/mixer.h +++ /dev/null @@ -1,41 +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. - * - * $URL$ - * $Id$ - * - */ - -#ifndef SCI_SFX_MIXER_H -#define SCI_SFX_MIXER_H - -#include "sci/sfx/sfx_pcm.h" - -namespace Sci { - -/** - * Subscribes the mixer to a new feed. - * @param feed The feed to subscribe to - */ -void mixer_subscribe(sfx_pcm_feed_t *feed); - -} // End of namespace Sci - -#endif // SCI_SFX_MIXER_H diff --git a/engines/sci/sfx/player/polled.cpp b/engines/sci/sfx/player/polled.cpp index a8f92f8218..fcf8868545 100644 --- a/engines/sci/sfx/player/polled.cpp +++ b/engines/sci/sfx/player/polled.cpp @@ -29,7 +29,9 @@ #include "common/file.h" #include "sci/sfx/player.h" #include "sci/sfx/softseq.h" -#include "sci/sfx/mixer.h" + +#include "sound/audiostream.h" +#include "sound/mixer.h" namespace Sci { @@ -50,6 +52,160 @@ static void pp_tell_synth(int buf_nr, byte *buf) { } +class PCMFeedAudioStream : public Audio::AudioStream { +protected: + enum FeedMode { + FEED_MODE_ALIVE, + FEED_MODE_IDLE, + FEED_MODE_DEAD, + FEED_MODE_RESTART + }; + + /* Whether feed is alive or dead. */ + FeedMode _mode; + + /* Blank gap in frames. */ + int _gap; + + /* Feed. */ + sfx_pcm_feed_t *_feed; + + /* Timestamp of next frame requested by stream driver. */ + Audio::Timestamp _time; + +public: + PCMFeedAudioStream(sfx_pcm_feed_t *feed) + : _feed(feed), + _time(g_system->getMillis(), feed->conf.rate) { + + _feed->frame_size = (_feed->conf.stereo ? 2 : 1) * ((_feed->conf.format & SFX_PCM_FORMAT_16) ? 2 : 1); + _mode = FEED_MODE_ALIVE; + _gap = 0; + } + + ~PCMFeedAudioStream() { + _feed->destroy(_feed); + } + + virtual int readBuffer(int16 *buffer, const int numSamples); + + virtual bool isStereo() const { return _feed->conf.stereo; } + virtual int getRate() const { return _feed->conf.rate; } + + virtual bool endOfData() const { return _mode == FEED_MODE_DEAD; } + +protected: + void queryTimestamp(); +}; + +void PCMFeedAudioStream::queryTimestamp() { + if (_feed->get_timestamp) { + Audio::Timestamp stamp; + int val = _feed->get_timestamp(_feed, stamp); + + switch (val) { + case PCM_FEED_TIMESTAMP: + _gap = stamp.frameDiff(_time); + + if (_gap >= 0) + _mode = FEED_MODE_ALIVE; + else { + // FIXME: I don't quite understand what FEED_MODE_RESTART is for. + // The original DC mixer seemed to just stop and restart the stream. + // But why? To catch up with lagging sound? + // + // Walter says the following: + // "The FEED_MODE_RESTART might be there to re-sync after a debugger + // session where time passes for the mixer but not for the engine. + // I may have added this as a workaround for not being able to come + // up with a convenient way to implement mixer->pause() and mixer->resume() + // on DC." + // That makes some sense. + _mode = FEED_MODE_RESTART; + _time = Audio::Timestamp(g_system->getMillis(), _feed->conf.rate); + _gap = stamp.frameDiff(_time); + + if (_gap < 0) + _gap = 0; + } + break; + case PCM_FEED_IDLE: + _mode = FEED_MODE_IDLE; + break; + case PCM_FEED_EMPTY: + _mode = FEED_MODE_DEAD; + _gap = 0; + } + } else { + _mode = FEED_MODE_DEAD; + _gap = 0; + } +} + +static void U8_to_S16(byte *buf, int samples) { + for (int i = samples - 1; i >= 0; i--) { + buf[i * 2 + 1] = buf[i] - 0x80; + buf[i * 2] = 0; + } +} + +int PCMFeedAudioStream::readBuffer(int16 *buffer, const int numSamples) { + // FIXME: If ScummVM's mixer supported timestamps, then it would pass them + // as a parameter to this function. But currently, it doesn't. Therefore, we + // create a fake timestamp based on the current time. For comparison, a real + // timestamp could be adjusted for pauses in sound processing. And it would + // be synced for all audio streams. + Audio::Timestamp timestamp(g_system->getMillis(), _feed->conf.rate); + + int channels, frames_req; + int frames_recv = 0; + + _time = timestamp; + channels = _feed->conf.stereo == SFX_PCM_MONO ? 1 : 2; + frames_req = numSamples / channels; + + while (frames_req != frames_recv) { + int frames_left = frames_req - frames_recv; + byte *buf_pos = ((byte *)buffer) + frames_recv * channels * 2; + + if (_mode == FEED_MODE_IDLE) + queryTimestamp(); + + if (_mode == FEED_MODE_IDLE || _mode == FEED_MODE_DEAD) { + memset(buf_pos, 0, frames_left * channels * 2); + + _time = _time.addFrames(frames_left); + break; + } + + if (_gap) { + int frames = _gap; + + if (frames > frames_left) + frames = frames_left; + + memset(buf_pos, 0, frames * channels * 2); + + _gap -= frames; + frames_recv += frames; + _time = _time.addFrames(frames); + } else { + int frames = _feed->poll(_feed, buf_pos, frames_left); + + if (_feed->conf.format == SFX_PCM_FORMAT_U8) + U8_to_S16(buf_pos, frames * channels); + + frames_recv += frames; + _time = _time.addFrames(frames); + + if (frames < frames_left) + queryTimestamp(); + } + } + + return numSamples; +} + /*----------------------*/ /* Mixer implementation */ /*----------------------*/ @@ -193,7 +349,10 @@ static int pp_init(ResourceManager *resmgr, int expected_latency) { pcmfeed.conf = seq->pcm_conf; seq->set_volume(seq, volume); - mixer_subscribe(&pcmfeed); + + PCMFeedAudioStream *newStream = new PCMFeedAudioStream(&pcmfeed); + // FIXME: Is this sound type appropriate? + g_system->getMixer()->playInputStream(Audio::Mixer::kSFXSoundType, 0, newStream); sfx_player_polled.polyphony = seq->polyphony; return SFX_OK; |