1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include "mads/audio.h"
24
#include "mads/compression.h"
26
#include "common/stream.h"
27
#include "audio/audiostream.h"
28
#include "audio/mixer.h"
29
#include "audio/decoders/raw.h"
33
AudioPlayer::AudioPlayer(Audio::Mixer *mixer, uint32 gameID) : _mixer(mixer), _gameID(gameID) {
34
setVolume(Audio::Mixer::kMaxChannelVolume);
35
setDefaultSoundGroup();
38
AudioPlayer::~AudioPlayer() {
42
bool AudioPlayer::isPlaying() const {
43
return _mixer->isSoundHandleActive(_handle);
46
void AudioPlayer::setVolume(int volume) {
47
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume);
48
_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, volume);
51
void AudioPlayer::setDefaultSoundGroup() {
53
case GType_RexNebular:
54
setSoundGroup("rex009.dsr");
56
case GType_Dragonsphere:
57
setSoundGroup("drag009.dsr");
60
setSoundGroup("phan009.dsr");
63
error("setDefaultSoundGroup: Unknown game");
67
void AudioPlayer::setSoundGroup(const Common::String &filename) {
71
_dsrFile.open(filename);
74
uint16 entryCount = _dsrFile.readUint16LE();
76
for (uint16 i = 0; i < entryCount; i++) {
78
newEntry.frequency = _dsrFile.readUint16LE();
79
newEntry.channels = _dsrFile.readUint32LE();
80
newEntry.compSize = _dsrFile.readUint32LE();
81
newEntry.uncompSize = _dsrFile.readUint32LE();
82
newEntry.offset = _dsrFile.readUint32LE();
83
_dsrEntries.push_back(newEntry);
89
void AudioPlayer::playSound(int soundIndex, bool loop) {
90
if (_dsrEntries.empty()) {
91
warning("DSR file not loaded, not playing sound");
95
if (soundIndex < 0 || soundIndex > (int)_dsrEntries.size() - 1) {
96
warning("Invalid sound index: %i (max %i), not playing sound", soundIndex, _dsrEntries.size() - 1);
102
int32 compSize = _dsrEntries[soundIndex].compSize;
103
int32 uncompSize = _dsrEntries[soundIndex].uncompSize;
104
int32 offset = _dsrEntries[soundIndex].offset;
105
int16 frequency = _dsrEntries[soundIndex].frequency;
106
byte *compData = new byte[compSize];
107
byte *buffer = new byte[uncompSize];
108
_dsrFile.open(_filename);
109
_dsrFile.seek(offset, SEEK_SET);
110
_dsrFile.read(compData, compSize);
113
fab.decompress(compData, compSize, buffer, uncompSize);
116
Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
117
Audio::makeRawStream(buffer, uncompSize, frequency, Audio::FLAG_UNSIGNED),
119
_mixer->playStream(Audio::Mixer::kSFXSoundType, &_handle, stream, -1, Audio::Mixer::kMaxChannelVolume);
122
// Dump the sound file
123
FILE *destFile = fopen("sound.raw", "wb");
124
fwrite(_dsrFile.dsrEntries[soundIndex]->data, _dsrFile.dsrEntries[soundIndex].uncompSize, 1, destFile);
129
} // End of namespace M4