-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioSystem.cpp
More file actions
65 lines (52 loc) · 1.5 KB
/
AudioSystem.cpp
File metadata and controls
65 lines (52 loc) · 1.5 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
#include "AudioSystem.h"
#include "SDL.h""
#include "Error.h"
AudioSystem &AudioSystem::GetInstance()
{
static AudioSystem instance;
return instance;
}
AudioSystem::AudioSystem() : callback(nullptr)
{
SDL_AudioSpec audioDevice;
audioDevice.freq = 44100; // sample rate
audioDevice.format = AUDIO_S16SYS; // bit depth
audioDevice.samples = 512; // number of sample frames (one sample frame contains as many samples as channels)
audioDevice.channels = 1; // number of channels (one:mono, two: stereo)
audioDevice.size; // calculated: sample byte count * sample frames * channels
audioDevice.silence; // calculated
audioDevice.callback = AudioCallback;
SDL_OpenAudio(&audioDevice, 0);
}
AudioSystem::~AudioSystem()
{
SDL_CloseAudio();
}
void AudioSystem::StartPlayback()
{
SDL_PauseAudio(0);
}
void AudioSystem::StopPlayback()
{
SDL_PauseAudio(1);
}
void AudioSystem::EnqueueSample(int16_t sample)
{
ringBuffer.InsertLast(sample);
}
// called by audio thread
void AudioSystem::AudioCallback(void *userData, uint8_t *audioData, int length)
{
int numSamples = length / 2;
RingBuffer &buffer = GetInstance().ringBuffer;
// fill audio driver buffer
int i;
for (i = 0; !buffer.Empty() && i < numSamples; i++)
{
reinterpret_cast<int16_t*>(audioData)[i] = buffer.First();
buffer.RemoveFirst();
}
while (i < numSamples)
reinterpret_cast<int16_t*>(audioData)[i++] = 0x0000;
GetInstance().callback();
}