Loading [MathJax]/extensions/MathZoom.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Soundtrack.cxx
Go to the documentation of this file.
1 #include "Soundtrack.hxx"
2 #include "../util/Exception.hxx"
3 #include "../util/LOG.hxx"
4 #include "../basics/Settings.hxx"
5 
6 Soundtrack::Soundtrack(SoundtrackID id, ChannelID channelID, DecodedAudioData *dAudioData, RepeatCount repeat, bool isMusic,
7  bool isPlaying, bool isPlayable, bool isTriggerable)
8  : ID(id), Channel(channelID), Loop(repeat), isMusic(isMusic), isPlaying(isPlaying), isTriggerable(isTriggerable),
9  isPlayable(isPlayable), source(), buffer(0)
10 {
11 
12  /* initialize buffer */
13  alGenBuffers(1, &buffer);
14  ALenum errorCode = alGetError();
15  if (errorCode != AL_NO_ERROR)
16  throw AudioError(TRACE_INFO "Could not create buffers: Error " + std::to_string(errorCode));
17 
18  /* set buffer data */
19  ALenum format = Settings::instance().audio3DStatus ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
20 
21  /* parameters: buffer, format, data, sample length in bytes, frequency(sample rate) */
22  alBufferData(buffer, format, dAudioData->char_data_vec.data(), dAudioData->nBytes, dAudioData->data_sample_rate);
23 
24  errorCode = alGetError();
25  if (errorCode != AL_NO_ERROR)
26  throw AudioError(TRACE_INFO "Failed to load audio data into buffer: Error " + std::to_string(errorCode));
27 
28  /* initialize sources */
29  alGenSources(2, source);
30  alSourcei(source[0], AL_SOURCE_RELATIVE, AL_FALSE); // sfx channel/source
31  alSourcei(source[1], AL_SOURCE_RELATIVE, AL_FALSE); // music channel/source
32  errorCode = alGetError();
33  if (errorCode != AL_NO_ERROR)
34  throw AudioError(TRACE_INFO "Failed to setup sound source: Error " + std::to_string(errorCode));
35 
36  /* attach buffer to source */
37  alSourcei(source[isMusic], AL_BUFFER, buffer);
38 }
39 
41 {
42  if (alIsSource(source[0]))
43  alDeleteSources(2, source);
44  if (alIsBuffer(buffer))
45  alDeleteBuffers(1, &buffer);
46 }
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
StrongType< string, struct SoundtrackIDTag >
Soundtrack::source
ALuint source[2]
The OpenAL sources of the sound track.
Definition: Soundtrack.hxx:73
DecodedAudioData::data_sample_rate
int data_sample_rate
Definition: Soundtrack.hxx:29
Soundtrack::Soundtrack
Soundtrack(SoundtrackID, ChannelID, DecodedAudioData *, RepeatCount, bool, bool, bool, bool)
Definition: Soundtrack.cxx:6
Soundtrack.hxx
DecodedAudioData::char_data_vec
std::vector< char > char_data_vec
pcm audio data
Definition: Soundtrack.hxx:27
SettingsData::audio3DStatus
bool audio3DStatus
whether to play 3D sound or not
Definition: Settings.hxx:103
DecodedAudioData
Container for raw pcm data that read from .ogg sound file.
Definition: Soundtrack.hxx:25
Soundtrack::~Soundtrack
~Soundtrack()
Definition: Soundtrack.cxx:40
Soundtrack::isMusic
bool isMusic
true if the Soundtrack is Music, false if it's a Sound
Definition: Soundtrack.hxx:51
Soundtrack::buffer
ALuint buffer
The OpenAL buffer of the sound track.
Definition: Soundtrack.hxx:80
DecodedAudioData::nBytes
long nBytes
number of bytes in decoded audio data
Definition: Soundtrack.hxx:28
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
AudioError
An audio-related error occured.
Definition: Exception.hxx:52