o Make use of the new LinearMemoryStream feature which allows auto-disposing the sound data

o This allows us to get rid of the ChannelRaw class
o Removed the sound index return value from several methods
o Removed all methods dealing with sound indices (i.e. stopChannel and pauseChannel)

svn-id: r11801
This commit is contained in:
Max Horn 2003-12-21 00:44:31 +00:00
parent 676bf4d683
commit ec0ebf5380
7 changed files with 49 additions and 99 deletions

View File

@ -30,7 +30,7 @@
class DigitalTrackInfo {
public:
virtual bool error() = 0;
virtual int play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) = 0;
virtual void play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) = 0;
virtual ~DigitalTrackInfo() { }
};

View File

@ -55,13 +55,13 @@ protected:
public:
int _id;
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, bool isMusic, byte volume, int8 pan)
: _mixer(mixer), _handle(handle), _isMusic(isMusic), _volume(volume), _pan(pan), _paused(false), _converter(0), _input(0), _id(-1) {
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, bool isMusic, byte volume, int8 pan, int id = -1)
: _mixer(mixer), _handle(handle), _isMusic(isMusic), _volume(volume), _pan(pan), _paused(false), _converter(0), _input(0), _id(id) {
assert(mixer);
}
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume, int8 pan, bool reverseStereo = false)
: _mixer(mixer), _handle(handle), _isMusic(isMusic), _volume(volume), _pan(pan), _paused(false), _converter(0), _input(input), _id(-1) {
Channel(SoundMixer *mixer, PlayingSoundHandle *handle, AudioInputStream *input, bool isMusic, byte volume, int8 pan, bool reverseStereo = false, int id = -1)
: _mixer(mixer), _handle(handle), _isMusic(isMusic), _volume(volume), _pan(pan), _paused(false), _converter(0), _input(input), _id(id) {
assert(mixer);
assert(input);
@ -92,13 +92,6 @@ public:
}
};
class ChannelRaw : public Channel {
byte *_ptr;
public:
ChannelRaw(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, byte volume, int8 pan, int id, uint32 loopStart, uint32 loopEnd);
~ChannelRaw();
};
class ChannelStream : public Channel {
public:
ChannelStream(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume, int8 pan);
@ -156,9 +149,9 @@ void SoundMixer::setupPremix(PremixProc *proc, void *param) {
_premixProc = proc;
}
int SoundMixer::newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume, int8 pan) {
void SoundMixer::newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume, int8 pan) {
Common::StackLock lock(_mutex);
return insertChannel(handle, new ChannelStream(this, handle, sound, size, rate, flags, buffer_size, volume, pan));
insertChannel(handle, new ChannelStream(this, handle, sound, size, rate, flags, buffer_size, volume, pan));
}
void SoundMixer::appendStream(PlayingSoundHandle handle, void *sound, uint32 size) {
@ -214,7 +207,7 @@ void SoundMixer::endStream(PlayingSoundHandle handle) {
}
}
int SoundMixer::insertChannel(PlayingSoundHandle *handle, Channel *chan) {
void SoundMixer::insertChannel(PlayingSoundHandle *handle, Channel *chan) {
int index = -1;
for (int i = 0; i != NUM_CHANNELS; i++) {
if (_channels[i] == NULL) {
@ -225,56 +218,70 @@ int SoundMixer::insertChannel(PlayingSoundHandle *handle, Channel *chan) {
if(index == -1) {
warning("SoundMixer::out of mixer slots");
delete chan;
return -1;
return;
}
_channels[index] = chan;
if (handle)
*handle = index + 1;
return index;
}
int SoundMixer::playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, int id, byte volume, int8 pan, uint32 loopStart, uint32 loopEnd) {
void SoundMixer::playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, int id, byte volume, int8 pan, uint32 loopStart, uint32 loopEnd) {
Common::StackLock lock(_mutex);
// Prevent duplicate sounds
if (id != -1) {
for (int i = 0; i != NUM_CHANNELS; i++)
if (_channels[i] != NULL && _channels[i]->_id == id)
return -1;
return;
}
Channel *chan = new ChannelRaw(this, handle, sound, size, rate, flags, volume, pan, id, loopStart, loopEnd);
return insertChannel(handle, chan);
// Create the input stream
AudioInputStream *input;
const bool autoFreeMemory = (flags & SoundMixer::FLAG_AUTOFREE) != 0;
if (flags & SoundMixer::FLAG_LOOP) {
if (loopEnd == 0) {
input = makeLinearInputStream(rate, flags, (byte *)sound, size, 0, size, autoFreeMemory);
} else {
assert(loopStart < loopEnd && loopEnd <= size);
input = makeLinearInputStream(rate, flags, (byte *)sound, size, loopStart, loopEnd - loopStart, autoFreeMemory);
}
} else {
input = makeLinearInputStream(rate, flags, (byte *)sound, size, 0, 0, autoFreeMemory);
}
// Create the channel
Channel *chan = new Channel(this, handle, input, false, volume, pan, (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0, id);
insertChannel(handle, chan);
}
#ifdef USE_MAD
int 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) {
// Create the input stream
AudioInputStream *input = makeMP3Stream(file, mad_timer_zero, size);
return playInputStream(handle, input, false, volume, pan);
playInputStream(handle, input, false, volume, pan);
}
int 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) {
// Create the input stream
AudioInputStream *input = makeMP3Stream(file, duration, 0);
return playInputStream(handle, input, true, volume, pan);
playInputStream(handle, input, true, volume, pan);
}
#endif
#ifdef USE_VORBIS
int 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) {
// Create the input stream
AudioInputStream *input = makeVorbisStream(ov_file, duration);
return playInputStream(handle, input, is_cd_track, volume, pan);
playInputStream(handle, input, is_cd_track, volume, pan);
}
#endif
int 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) {
Common::StackLock lock(_mutex);
// Create the channel
Channel *chan = new Channel(this, handle, input, isMusic, volume, pan);
return insertChannel(handle, chan);
insertChannel(handle, chan);
}
void SoundMixer::mix(int16 *buf, uint len) {
@ -311,17 +318,6 @@ void SoundMixer::stopAll() {
_channels[i]->destroy();
}
void SoundMixer::stopChannel(int index) {
if ((index < 0) || (index >= NUM_CHANNELS)) {
warning("soundMixer::stop has invalid index %d", index);
return;
}
Common::StackLock lock(_mutex);
if (_channels[index])
_channels[index]->destroy();
}
void SoundMixer::stopID(int id) {
Common::StackLock lock(_mutex);
for (int i = 0; i != NUM_CHANNELS; i++) {
@ -388,17 +384,6 @@ void SoundMixer::pauseAll(bool paused) {
_paused = paused;
}
void SoundMixer::pauseChannel(int index, bool paused) {
if ((index < 0) || (index >= NUM_CHANNELS)) {
warning("soundMixer::pauseChannel has invalid index %d", index);
return;
}
Common::StackLock lock(_mutex);
if (_channels[index])
_channels[index]->pause(paused);
}
void SoundMixer::pauseID(int id, bool paused) {
Common::StackLock lock(_mutex);
for (int i = 0; i != NUM_CHANNELS; i++) {
@ -510,35 +495,6 @@ void Channel::mix(int16 *data, uint len) {
}
}
/* RAW mixer */
ChannelRaw::ChannelRaw(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, byte volume, int8 pan, int id, uint32 loopStart, uint32 loopEnd)
: Channel(mixer, handle, false, volume, pan) {
_id = id;
_ptr = (byte *)sound;
// Create the input stream
if (flags & SoundMixer::FLAG_LOOP) {
if (loopEnd == 0) {
_input = makeLinearInputStream(rate, flags, _ptr, size, 0, size);
} else {
assert(loopStart < loopEnd && loopEnd <= size);
_input = makeLinearInputStream(rate, flags, _ptr, size, loopStart, loopEnd - loopStart);
}
} else {
_input = makeLinearInputStream(rate, flags, _ptr, size, 0, 0);
}
if (!(flags & SoundMixer::FLAG_AUTOFREE))
_ptr = 0;
// Get a rate converter instance
_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo(), (flags & SoundMixer::FLAG_REVERSE_STEREO) != 0);
}
ChannelRaw::~ChannelRaw() {
free(_ptr);
}
ChannelStream::ChannelStream(SoundMixer *mixer, PlayingSoundHandle *handle,
void *sound, uint32 size, uint rate,
byte flags, uint32 buffer_size, byte volume, int8 pan)

View File

@ -97,21 +97,21 @@ public:
void setupPremix(PremixProc *proc, void *param);
// start playing a raw sound
int playRaw(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags,
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
int playMP3(PlayingSoundHandle *handle, File *file, uint32 size, byte volume = 255, int8 pan = 0);
int 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);
void playMP3CDTrack(PlayingSoundHandle *handle, File *file, mad_timer_t duration, byte volume = 255, int8 pan = 0);
#endif
#ifdef USE_VORBIS
int playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume = 255, int8 pan = 0);
void playVorbis(PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track, byte volume = 255, int8 pan = 0);
#endif
int 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);
/** Start a new stream. */
int newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume = 255, int8 pan = 0);
void newStream(PlayingSoundHandle *handle, void *sound, uint32 size, uint rate, byte flags, uint32 buffer_size, byte volume = 255, int8 pan = 0);
/** Append to an existing stream. */
void appendStream(PlayingSoundHandle handle, void *sound, uint32 size);
@ -122,9 +122,6 @@ public:
/** stop all currently playing sounds */
void stopAll();
/** stop playing the given channel */
void stopChannel(int channel);
/** stop playing the sound with given ID */
void stopID(int id);
@ -134,9 +131,6 @@ public:
/** pause/unpause all channels */
void pauseAll(bool paused);
/** pause/unpause the given channel */
void pauseChannel(int index, bool paused);
/** pause/unpause the sound with the given ID */
void pauseID(int id, bool paused);
@ -168,7 +162,7 @@ public:
uint getOutputRate() const { return _outputRate; }
private:
int insertChannel(PlayingSoundHandle *handle, Channel *chan);
void insertChannel(PlayingSoundHandle *handle, Channel *chan);
/** main mixer method */
void mix(int16 * buf, uint len);

View File

@ -97,7 +97,7 @@ error:
delete file;
}
int MP3TrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) {
void MP3TrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) {
unsigned int offset;
mad_timer_t durationTime;
@ -116,7 +116,7 @@ int MP3TrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startF
}
// Play it
return mixer->playMP3CDTrack(handle, _file, durationTime);
mixer->playMP3CDTrack(handle, _file, durationTime);
}
MP3TrackInfo::~MP3TrackInfo() {

View File

@ -44,7 +44,7 @@ public:
MP3TrackInfo(File *file);
~MP3TrackInfo();
bool error() { return _error_flag; }
int play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration);
void play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration);
};
AudioInputStream *makeMP3Stream(File *file, mad_timer_t duration, uint size = 0);

View File

@ -111,13 +111,13 @@ VorbisTrackInfo::VorbisTrackInfo(File *file) {
#define VORBIS_TREMOR
#endif
int VorbisTrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) {
void VorbisTrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration) {
#ifdef VORBIS_TREMOR
ov_time_seek(&_ov_file, (ogg_int64_t)(startFrame / 75.0 * 1000));
#else
ov_time_seek(&_ov_file, startFrame / 75.0);
#endif
return mixer->playVorbis(handle, &_ov_file,
mixer->playVorbis(handle, &_ov_file,
duration * ov_info(&_ov_file, -1)->rate / 75, true);
}

View File

@ -43,7 +43,7 @@ public:
VorbisTrackInfo(File *file);
~VorbisTrackInfo();
bool error() { return _error_flag; }
int play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration);
void play(SoundMixer *mixer, PlayingSoundHandle *handle, int startFrame, int duration);
};