File: Audio.cpp

package info (click to toggle)
criticalmass 1%3A1.0.0-1.5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,740 kB
  • sloc: ansic: 47,628; cpp: 25,167; sh: 12,025; xml: 3,532; perl: 3,271; makefile: 662; python: 66; awk: 40; lisp: 33
file content (298 lines) | stat: -rw-r--r-- 6,513 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Description:
//   Audio subsysytem.
//
// Copyright (C) 2001 Frank Becker
//
// 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
//
#include <defines.h>
#include <Audio.hpp>
#include <Trace.hpp>
#include <Config.hpp>
#include <ResourceManager.hpp>
#include <zrwops.hpp>
#include <SampleManager.hpp>
#include <Timer.hpp>
#include <GetDataPath.hpp>

#include <sys/types.h>
#include <sys/stat.h>

#include "SDL.h"
#include "SDL_mixer.h"

Audio::Audio():
  _sampleManager(0),
  _soundTrack(0),
  _playDefaultSoundtrack( true),
  _playMusic( true),
  _isPlaying( false),
  _unloadMusic(false),
  _musicVolume(0.8),
  _effectsVolume(0.8)
{
    XTRACE();

    bool dummy;
    //if config variables don't exist (upgrade, etc) update 
    if( ! ConfigS::instance()->getBoolean( "playMusic", dummy))
    {
	Value *v = new Value( _playMusic);
	ConfigS::instance()->updateKeyword( "playMusic", v);
    }
    if( ! ConfigS::instance()->getBoolean( 
	"playDefaultSoundtrack", dummy))
    {
	Value *v = new Value( _playDefaultSoundtrack);
	ConfigS::instance()->updateKeyword( "playDefaultSoundtrack", v);
    }
}

Audio::~Audio()
{
    XTRACE();
    LOG_INFO << "Audio shutdown..." << endl;

    turnMusicOff();

    Mix_CloseAudio();
    delete _sampleManager;

    SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

static void musicFinished( void)
{
    AudioS::instance()->musicFinished();
}

void Audio::musicFinished( void)
{
    _unloadMusic = true;
}

void Audio::loadMusic( const string &mod)
{
    XTRACE();
    if( mod == "") return;

    LOG_INFO << "Loading soundtrack...[" << mod << "]" << endl;

#if 0
//SDL_mixer doesn't have RWops support for MUS yet :(
    if( !ResourceManagerS::instance()->selectResource( mod))
    {
	LOG_ERROR << "Music file [" << mod << "] not found." << endl;
	return;
    }

    ziStream &infile = ResourceManagerS::instance()->getInputStream();
    SDL_RWops *src = RWops_from_ziStream( infile);

    _soundTrack = Mix_LoadMUS_RW(src);
    SDL_RWclose( src);
#else
    _soundTrack = Mix_LoadMUS( mod.c_str());
#endif

    if( !_soundTrack)
    { 
	LOG_ERROR << "Failed to load soundtrack: [" << mod << "]" << endl;
	LOG_ERROR << SDL_GetError() << endl;
    }
    else
    {
	//loop soundtrack
	Mix_FadeInMusic( _soundTrack, -1, 500);
	Mix_HookMusicFinished(::musicFinished);
	_isPlaying = true;
    }
}

bool Audio::init( void)
{
    XTRACE();
    LOG_INFO << "Initializing Audio..." << endl;

    bool audio;
    if( !ConfigS::instance()->getBoolean( "audio", audio))
    {
        audio = false;
    }
    if( audio)
    {
	if( SDL_InitSubSystem( SDL_INIT_AUDIO) < 0 )
	{
	    LOG_ERROR << "Init Audio: failed # " << SDL_GetError() << endl;
	    return false;
	}

        if( Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) < 0 ) 
        {
	    LOG_ERROR << "Init Audio (Mixer): failed # " 
                      << SDL_GetError() << endl;
	    return false;
        }

	_sampleManager = new SampleManager();

	//make sure we have default volumes
	float dummy;
	if( ! ConfigS::instance()->getFloat( "musicVolume", dummy))
	{
	    Value *v = new Value( 0.8f);
	    ConfigS::instance()->updateKeyword( "musicVolume", v);
	}
	if( !ConfigS::instance()->getFloat( "effectsVolume", dummy))
	{
	    Value *v = new Value( 0.8f);
	    ConfigS::instance()->updateKeyword( "effectsVolume", v);
	}

	ConfigS::instance()->getBoolean( "playMusic", _playMusic);
	ConfigS::instance()->getBoolean( 
	    "playDefaultSoundtrack", _playDefaultSoundtrack);
	startMusic();
	updateVolume();

        LOG_INFO << "Audio ON." << endl;
    }
    else
    {
        LOG_INFO << "Audio OFF." << endl;
    }

    return true;
}

void Audio::playSample( const string &sampleName)
{
    if( !_sampleManager) return;

    Mix_Chunk *sample = _sampleManager->getSample( sampleName);
    if( sample)
    {
	//any channel, no loop
	Mix_PlayChannel(-1, sample, 0);
    }
}

void Audio::turnMusicOff( void)
{
    if( _isPlaying)
    {
	Mix_FadeOutMusic(500);
	_isPlaying = false;
    }
}

void Audio::startMusic( void)
{
    if( _playMusic)
    {
	string soundtrack = "";

	if( _playDefaultSoundtrack)
	{
	    //look in developer file tree first
	    soundtrack = string("../data/music/lg-criti.xm");

	    struct stat statInfo;
	    if( stat( soundtrack.c_str(), &statInfo) == -1)
	    {
		//try global
		soundtrack = getDataPath()+string("lg-criti.xm");
	    }
	}
	else
	{
	    ConfigS::instance()->getString( "soundtrack", soundtrack);
	}

        turnMusicOff();
	loadMusic( soundtrack);
    }
    else
    {
        turnMusicOff();
    }
}

void Audio::updateSettings( void)
{
    if( Mix_FadingMusic() != MIX_NO_FADING) return;

    bool oldPlayMusic = _playMusic;
    bool oldPlayDefaultSoundtrack = _playDefaultSoundtrack;

    ConfigS::instance()->getBoolean( "playMusic", _playMusic);
    ConfigS::instance()->getBoolean( 
	"playDefaultSoundtrack", _playDefaultSoundtrack);

    if( (oldPlayMusic == _playMusic) &&
	(oldPlayDefaultSoundtrack == _playDefaultSoundtrack))
    {
	//no changes...
	return;
    }

    startMusic();
}

void Audio::updateVolume( void)
{
    int newVolume;

    float musicVolume = 0.8;
    ConfigS::instance()->getFloat( "musicVolume", musicVolume);
    newVolume = (int)(MIX_MAX_VOLUME * musicVolume);
    if( _musicVolume != musicVolume)
    {
	_musicVolume = musicVolume;
	Mix_VolumeMusic( newVolume);
    }

    float effectsVolume = 0.8;
    ConfigS::instance()->getFloat( "effectsVolume", effectsVolume);
    newVolume = (int)(MIX_MAX_VOLUME * effectsVolume);
    if( _effectsVolume != effectsVolume)
    {
	_effectsVolume = effectsVolume;
	Mix_Volume( -1, newVolume);
	playSample( "sounds/beep.wav");
    }
}

bool Audio::update( void)
{
    if( !_sampleManager) return true;

    if( _unloadMusic)
    {
	if( _soundTrack)
	{
	    Mix_FreeMusic( _soundTrack);
	    _soundTrack = 0;
	}
	_unloadMusic = false;
    }

    updateVolume();

    static float nextTime = Timer::getTime()+0.5f;
    float thisTime = Timer::getTime();
    if( thisTime > nextTime)
    {
	updateSettings();
	nextTime = thisTime+0.5f;
    }

    return true;
}