-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio-device.cpp
61 lines (48 loc) · 1.37 KB
/
audio-device.cpp
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
#include "audio-device.h"
#include <stdexcept>
#include <string>
AudioDevice::AudioDevice(const cc_u8f channels, cc_u32f &sample_rate, cc_u32f &total_buffer_frames, DebugLog &debug_log)
: channels(channels)
, debug_log(debug_log)
{
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = 48000;
want.format = AUDIO_S16SYS;
want.channels = channels;
// We want a 10ms buffer (this value must be a power of two).
want.samples = 1;
while (want.samples < want.freq / (1000 / 10))
want.samples <<= 1;
want.callback = nullptr;
device = SDL_OpenAudioDevice(nullptr, 0, &want, &have, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE);
if (device == 0)
{
debug_log.Log("SDL_OpenAudioDevice failed with the following message - '%s'", SDL_GetError());
}
else
{
total_buffer_frames = static_cast<cc_u32f>(have.samples);
sample_rate = static_cast<cc_u32f>(have.freq);
// Unpause audio device, so that playback can begin.
SDL_PauseAudioDevice(device, 0);
}
}
AudioDevice::~AudioDevice()
{
if (device == 0)
return;
SDL_CloseAudioDevice(device);
}
void AudioDevice::QueueFrames(const cc_s16l *buffer, cc_u32f total_frames)
{
if (device == 0)
return;
SDL_QueueAudio(device, buffer, total_frames * SIZE_OF_FRAME);
}
cc_u32f AudioDevice::GetTotalQueuedFrames()
{
if (device == 0)
return 0;
return SDL_GetQueuedAudioSize(device) / SIZE_OF_FRAME;
}