~ubuntu-branches/debian/jessie/scummvm/jessie

« back to all changes in this revision

Viewing changes to engines/mads/audio.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2014-08-10 00:50:36 UTC
  • mfrom: (1.2.22)
  • Revision ID: package-import@ubuntu.com-20140810005036-wls6i0dsxqfxu70g
Tags: 1.7.0+dfsg-1
* New upstream release [July 2014].
  - remove old/obsolete patches.
  + added new "drop1test.patch" to disable problematic test.
  + build with "--disable-eventrecorder" to avoid FTBFS in tests.
  + added "libjpeg-dev" and "libfaad-dev" to Build-Depends.
* Install all arch-independent files (themes, game data, etc.).
* Build-time re-compression of "classic" theme.
* Added "debian/gbp.conf".
* Standards-Version to 3.9.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ScummVM - Graphic Adventure Engine
 
2
 *
 
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.
 
6
 *
 
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.
 
11
 
 
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.
 
16
 
 
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.
 
20
 *
 
21
 */
 
22
 
 
23
#include "mads/audio.h"
 
24
#include "mads/compression.h"
 
25
 
 
26
#include "common/stream.h"
 
27
#include "audio/audiostream.h"
 
28
#include "audio/mixer.h"
 
29
#include "audio/decoders/raw.h"
 
30
 
 
31
namespace MADS {
 
32
 
 
33
AudioPlayer::AudioPlayer(Audio::Mixer *mixer, uint32 gameID) : _mixer(mixer), _gameID(gameID) {
 
34
        setVolume(Audio::Mixer::kMaxChannelVolume);
 
35
        setDefaultSoundGroup();
 
36
}
 
37
 
 
38
AudioPlayer::~AudioPlayer() {
 
39
        _dsrEntries.clear();
 
40
}
 
41
 
 
42
bool AudioPlayer::isPlaying() const {
 
43
        return _mixer->isSoundHandleActive(_handle);
 
44
}
 
45
 
 
46
void AudioPlayer::setVolume(int volume) {
 
47
        _mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume);
 
48
        _mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, volume);
 
49
}
 
50
 
 
51
void AudioPlayer::setDefaultSoundGroup() {
 
52
        switch (_gameID) {
 
53
        case GType_RexNebular:
 
54
                setSoundGroup("rex009.dsr");
 
55
                break;
 
56
        case GType_Dragonsphere:
 
57
                setSoundGroup("drag009.dsr");
 
58
                break;
 
59
        case GType_Phantom:
 
60
                setSoundGroup("phan009.dsr");
 
61
                break;
 
62
        default:
 
63
                error("setDefaultSoundGroup: Unknown game");
 
64
        }
 
65
}
 
66
 
 
67
void AudioPlayer::setSoundGroup(const Common::String &filename) {
 
68
        _dsrEntries.clear();
 
69
 
 
70
        _filename = filename;
 
71
        _dsrFile.open(filename);
 
72
 
 
73
        // Read header
 
74
        uint16 entryCount = _dsrFile.readUint16LE();
 
75
 
 
76
        for (uint16 i = 0; i < entryCount; i++) {
 
77
                DSREntry newEntry;
 
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);
 
84
        }
 
85
 
 
86
        _dsrFile.close();
 
87
}
 
88
 
 
89
void AudioPlayer::playSound(int soundIndex, bool loop) {
 
90
        if (_dsrEntries.empty()) {
 
91
                warning("DSR file not loaded, not playing sound");
 
92
                return;
 
93
        }
 
94
 
 
95
        if (soundIndex < 0 || soundIndex > (int)_dsrEntries.size() - 1) {
 
96
                warning("Invalid sound index: %i (max %i), not playing sound", soundIndex, _dsrEntries.size() - 1);
 
97
                return;
 
98
        }
 
99
 
 
100
        // Get sound data
 
101
        FabDecompressor fab;
 
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);
 
111
        _dsrFile.close();
 
112
 
 
113
        fab.decompress(compData, compSize, buffer, uncompSize);
 
114
 
 
115
        // Play sound
 
116
        Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
 
117
                                Audio::makeRawStream(buffer, uncompSize, frequency, Audio::FLAG_UNSIGNED),
 
118
                                loop ? 0 : 1);
 
119
        _mixer->playStream(Audio::Mixer::kSFXSoundType, &_handle, stream, -1,  Audio::Mixer::kMaxChannelVolume);
 
120
 
 
121
        /*
 
122
        // Dump the sound file
 
123
        FILE *destFile = fopen("sound.raw", "wb");
 
124
        fwrite(_dsrFile.dsrEntries[soundIndex]->data, _dsrFile.dsrEntries[soundIndex].uncompSize, 1, destFile);
 
125
        fclose(destFile);
 
126
        */
 
127
}
 
128
 
 
129
} // End of namespace M4