-
Notifications
You must be signed in to change notification settings - Fork 913
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
152 lines (132 loc) · 3.61 KB
/
AudioManager.cpp
File metadata and controls
152 lines (132 loc) · 3.61 KB
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
#include "AudioManager.h"
#include <SDL.h>
#include "Log.h"
std::vector<std::shared_ptr<Sound> > AudioManager::sSoundVector;
SDL_AudioSpec AudioManager::sAudioFormat;
std::shared_ptr<AudioManager> AudioManager::sInstance;
void AudioManager::mixAudio(void *unused, Uint8 *stream, int len)
{
bool stillPlaying = false;
//initialize the buffer to "silence"
SDL_memset(stream, 0, len);
//iterate through all our samples
std::vector<std::shared_ptr<Sound> >::const_iterator soundIt = sSoundVector.cbegin();
while (soundIt != sSoundVector.cend())
{
std::shared_ptr<Sound> sound = *soundIt;
if(sound->isPlaying())
{
//calculate rest length of current sample
Uint32 restLength = (sound->getLength() - sound->getPosition());
if (restLength > (Uint32)len) {
//if stream length is smaller than smaple lenght, clip it
restLength = len;
}
//mix sample into stream
SDL_MixAudio(stream, &(sound->getData()[sound->getPosition()]), restLength, SDL_MIX_MAXVOLUME);
if (sound->getPosition() + restLength < sound->getLength())
{
//sample hasn't ended yet
stillPlaying = true;
}
//set new sound position. if this is at or beyond the end of the sample, it will stop automatically
sound->setPosition(sound->getPosition() + restLength);
}
//advance to next sound
++soundIt;
}
//we have processed all samples. check if some will still be playing
if (!stillPlaying) {
//no. pause audio till a Sound::play() wakes us up
SDL_PauseAudio(1);
}
}
AudioManager::AudioManager()
{
init();
}
AudioManager::~AudioManager()
{
deinit();
}
std::shared_ptr<AudioManager> & AudioManager::getInstance()
{
//check if an AudioManager instance is already created, if not create one
if (sInstance == nullptr) {
sInstance = std::shared_ptr<AudioManager>(new AudioManager);
}
return sInstance;
}
void AudioManager::init()
{
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0)
{
LOG(LogError) << "Error initializing SDL audio!\n" << SDL_GetError();
return;
}
//stop playing all Sounds
for(unsigned int i = 0; i < sSoundVector.size(); i++)
{
if(sSoundVector.at(i)->isPlaying())
{
sSoundVector[i]->stop();
}
}
//Set up format and callback. Play 16-bit stereo audio at 44.1Khz
sAudioFormat.freq = 44100;
sAudioFormat.format = AUDIO_S16;
sAudioFormat.channels = 2;
sAudioFormat.samples = 1024;
sAudioFormat.callback = mixAudio;
sAudioFormat.userdata = NULL;
//Open the audio device and pause
if (SDL_OpenAudio(&sAudioFormat, NULL) < 0) {
LOG(LogError) << "AudioManager Error - Unable to open SDL audio: " << SDL_GetError() << std::endl;
}
}
void AudioManager::deinit()
{
//stop all playback
stop();
//completely tear down SDL audio. else SDL hogs audio resources and emulators might fail to start...
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
void AudioManager::registerSound(std::shared_ptr<Sound> & sound)
{
getInstance();
sSoundVector.push_back(sound);
}
void AudioManager::unregisterSound(std::shared_ptr<Sound> & sound)
{
getInstance();
for(unsigned int i = 0; i < sSoundVector.size(); i++)
{
if(sSoundVector.at(i) == sound)
{
sSoundVector[i]->stop();
sSoundVector.erase(sSoundVector.begin() + i);
return;
}
}
LOG(LogError) << "AudioManager Error - tried to unregister a sound that wasn't registered!";
}
void AudioManager::play()
{
getInstance();
//unpause audio, the mixer will figure out if samples need to be played...
SDL_PauseAudio(0);
}
void AudioManager::stop()
{
//stop playing all Sounds
for(unsigned int i = 0; i < sSoundVector.size(); i++)
{
if(sSoundVector.at(i)->isPlaying())
{
sSoundVector[i]->stop();
}
}
//pause audio
SDL_PauseAudio(1);
}