-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioDevice_impl.h
189 lines (148 loc) · 5.91 KB
/
AudioDevice_impl.h
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
#ifndef AUDIO_DEVICE_IMPL_H
#define AUDIO_DEVICE_IMPL_H
#ifndef AUDIO_DEVICE_H
#error "Include via AudioDevice.h"
#endif // AUDIO_DEVICE_H
#include <iostream>
#include <stdexcept>
namespace audio {
namespace detail {
static const uint8_t audioSpecSilence = 0;
static const uint16_t audioSpecPadding = 0;
static const uint32_t audioSpecSize = 0;
static const int allowedAudioChange = 0;
static const int pauseEnable = 1;
static const int pauseDisable = 0;
template<typename T>
struct FormatLookUp
{
};
template<>
struct FormatLookUp<float>
{
static const SDL_AudioFormat format = AUDIO_F32SYS;
};
inline bool isValid(SDL_AudioDeviceID deviceId)
{
return (deviceId >= 2); // see SDL_OpenAudioDevice()
}
inline void printDevices(int isCapture)
{
const int numAudioDevices = SDL_GetNumAudioDevices(isCapture);
std::cout << "Available audio " << (isCapture ? "capture" : "playback") << " devices:\n";
for(int i = 0; i < numAudioDevices; ++i) {
std::cout << SDL_GetAudioDeviceName(i, isCapture) << std::endl;
}
}
} // namespace detail
template<typename T>
DeviceCapture<T>::DeviceCapture(const Metadata& metadata)
: seq_{metadata, {}}
{
const SDL_AudioSpec want = {
metadata.sampleRate, /**< DSP frequency -- samples per second */
detail::FormatLookUp<T>::format, /**< Audio data format */
metadata.channelCount, /**< Number of channels: 1 mono, 2 stereo */
detail::audioSpecSilence, /**< Audio buffer silence value (calculated) */
metadata.sampleCount, /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
detail::audioSpecPadding, /**< Necessary for some compile environments */
detail::audioSpecSize, /**< Audio buffer size in bytes (calculated) */
DeviceCapture<T>::deviceCallback, /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
this /**< Userdata passed to callback (ignored for NULL callbacks). */
};
static const int isCapture = SDL_TRUE;
detail::printDevices(isCapture);
SDL_AudioSpec have;
deviceId_ = SDL_OpenAudioDevice(nullptr, isCapture, &want, &have, detail::allowedAudioChange);
if(!detail::isValid(deviceId_))
throw std::runtime_error(std::string("Failed to open audio: ") + SDL_GetError());
}
template<typename T>
DeviceCapture<T>::~DeviceCapture()
{
SDL_CloseAudioDevice(deviceId_);
}
template<typename T>
Sequence<T> DeviceCapture<T>::record(std::chrono::milliseconds length)
{
using Msec = std::chrono::duration<uint32_t, std::milli>;
return record(std::chrono::duration_cast<Msec>(length).count());
}
template<typename T>
Sequence<T> DeviceCapture<T>::record(uint32_t lengthMsec)
{
std::cout << "recording for " << lengthMsec << "ms ..." << std::endl;
SDL_PauseAudioDevice(deviceId_, detail::pauseDisable);
// block here for the duration of the recording
SDL_Delay(lengthMsec);
SDL_PauseAudioDevice(deviceId_, detail::pauseEnable);
return Sequence<T>{seq_.metadata, std::move(seq_.storage)};
}
template<typename T>
void DeviceCapture<T>::deviceCallback(void* userdata, uint8_t* stream, int len)
{
auto instance = reinterpret_cast<DeviceCapture<T>*>(userdata);
instance->deviceCallback(stream, len);
}
template<typename T>
void DeviceCapture<T>::deviceCallback(uint8_t* stream, int len)
{
seq_.push(stream, len);
}
template<typename T>
DevicePlayback<T>::DevicePlayback(const Metadata& metadata)
{
const SDL_AudioSpec want = {
metadata.sampleRate, /**< DSP frequency -- samples per second */
detail::FormatLookUp<T>::format, /**< Audio data format */
metadata.channelCount, /**< Number of channels: 1 mono, 2 stereo */
detail::audioSpecSilence, /**< Audio buffer silence value (calculated) */
metadata.sampleCount, /**< Audio buffer size in sample FRAMES (total samples divided by channel count) */
detail::audioSpecPadding, /**< Necessary for some compile environments */
detail::audioSpecSize, /**< Audio buffer size in bytes (calculated) */
DevicePlayback<T>::deviceCallback, /**< Callback that feeds the audio device (NULL to use SDL_QueueAudio()). */
this /**< Userdata passed to callback (ignored for NULL callbacks). */
};
static const int isCapture = SDL_FALSE;
detail::printDevices(isCapture);
SDL_AudioSpec have;
deviceId_ = SDL_OpenAudioDevice(nullptr, isCapture, &want, &have, detail::allowedAudioChange);
if(!detail::isValid(deviceId_))
throw std::runtime_error(std::string("Failed to open audio: ") + SDL_GetError());
}
template<typename T>
DevicePlayback<T>::~DevicePlayback()
{
SDL_CloseAudioDevice(deviceId_);
}
template<typename T>
void DevicePlayback<T>::play(Sequence<T> seq)
{
seq_ = std::move(seq);
std::cout << "playback for " << seq_.duration().count() << "ms ..." << std::endl;
SDL_PauseAudioDevice(deviceId_, detail::pauseDisable);
// block here for the duration of the playback
SDL_Delay(seq_.duration().count());
}
template<typename T>
void DevicePlayback<T>::deviceCallback(void* userdata, uint8_t* stream, int len)
{
auto instance = reinterpret_cast<DevicePlayback<T>*>(userdata);
instance->deviceCallback(stream, len);
}
template<typename T>
void DevicePlayback<T>::deviceCallback(uint8_t* stream, int len)
{
auto samples = seq_.pop();
if(samples.empty()) {
SDL_PauseAudioDevice(deviceId_, detail::pauseEnable);
return;
}
const auto first = reinterpret_cast<uint8_t*>(samples.data());
const size_t byteSize = samples.size() * sizeof(T);
const auto writeByteSize = std::min(byteSize, static_cast<size_t>(len));
memcpy(stream, first, writeByteSize);
memset(stream + writeByteSize, 0, static_cast<size_t>(len) - writeByteSize);
}
} // namespace audio
#endif // AUDIO_DEVICE_IMPL_H