-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_audio_device.cpp
More file actions
285 lines (230 loc) · 9.7 KB
/
core_audio_device.cpp
File metadata and controls
285 lines (230 loc) · 9.7 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-License-Identifier: MIT
#include "core_audio_device.h"
#include "conditions.h"
#include <iostream>
// Implementation based on:
// https://developer.apple.com/library/archive/technotes/tn2091/_index.html
namespace plac {
namespace {
OSStatus Raw16(const AudioObjectID /*unused*/, const AudioTimeStamp * /*unused*/, const AudioBufferList * /*unused*/,
const AudioTimeStamp * /*unused*/, AudioBufferList *output, const AudioTimeStamp * /*unused*/,
void *__nullable client_data) {
CoreAudioDevice *const dec = static_cast<CoreAudioDevice *>(client_data);
u_char *out = static_cast<u_char *>(output->mBuffers[0].mData);
auto writer = [&out](AudioFormat /*unused*/, const u_char *const data, const size_t count) {
for (size_t i = 0; i < count; ++i) {
out[1] = data[4 * i + 0];
out[2] = data[4 * i + 1];
out[4] = data[4 * i + 2];
out[5] = data[4 * i + 3];
out += 12;
}
return count;
};
AudioFormat physical_format{};
physical_format.bits = 24;
physical_format.channels = 4;
auto count = AsFrames(physical_format, output->mBuffers[0].mDataByteSize);
while (count > 0) {
const ssize_t n = dec->audio_buffer_.Read(dec->format_, count, writer);
if (n >= 0) {
count -= n;
}
}
dec->flow_.Notify();
return noErr;
}
OSStatus Raw24(const AudioObjectID /*unused*/, const AudioTimeStamp * /*unused*/, const AudioBufferList * /*unused*/,
const AudioTimeStamp * /*unused*/, AudioBufferList *output, const AudioTimeStamp * /*unused*/,
void *__nullable client_data) {
CoreAudioDevice *const dec = static_cast<CoreAudioDevice *>(client_data);
u_char *out = static_cast<u_char *>(output->mBuffers[0].mData);
auto writer = [&out](AudioFormat /*unused*/, const u_char *const data, const size_t count) {
for (size_t i = 0; i < count; ++i) {
out[0] = data[6 * i + 0];
out[1] = data[6 * i + 1];
out[2] = data[6 * i + 2];
out[3] = data[6 * i + 3];
out[4] = data[6 * i + 4];
out[5] = data[6 * i + 5];
out += 12;
}
return count;
};
AudioFormat physical_format{};
physical_format.bits = 24;
physical_format.channels = 4;
auto count = AsFrames(physical_format, output->mBuffers[0].mDataByteSize);
while (count > 0) {
const ssize_t n = dec->audio_buffer_.Read(dec->format_, count, writer);
if (n >= 0) {
count -= n;
}
}
dec->flow_.Notify();
return noErr;
}
std::vector<AudioObjectID> GetAudioDevices() {
OSStatus err = noErr;
AudioObjectPropertyAddress address{};
address.mSelector = kAudioHardwarePropertyDevices;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
UInt32 size;
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &address, 0, nullptr, &size);
ENSURES(err == noErr, "can't get number of audio devices");
std::vector<AudioObjectID> ids;
ids.resize(size / sizeof(AudioObjectID));
static_assert(std::is_integral<decltype(ids)::value_type>::value, "NO");
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &address, 0, nullptr, &size, ids.data());
ENSURES(err == noErr, "can't get audio device ids");
return ids;
}
struct StringRef {
StringRef() : ref{nullptr} {}
StringRef(CFStringRef _ref) : ref{_ref} {}
StringRef(StringRef &) = delete;
StringRef(StringRef &&other) noexcept : ref{other.ref} { other.ref = nullptr; }
StringRef &operator=(StringRef &) = delete;
StringRef &operator=(StringRef &&) = delete;
~StringRef() noexcept {
if (ref != nullptr) {
CFRelease(ref);
}
}
CFStringRef ref;
};
bool operator==(const StringRef &lhs, const StringRef &rhs) {
return CFStringCompare(lhs.ref, rhs.ref, 0) == kCFCompareEqualTo;
}
StringRef GetDeviceName(const AudioObjectID id) {
AudioObjectPropertyAddress address{};
address.mSelector = kAudioObjectPropertyName;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
CFStringRef raw{nullptr};
UInt32 size{sizeof(raw)};
static_assert(std::is_pod<decltype(raw)>::value, "NO");
const OSStatus err = AudioObjectGetPropertyData(id, &address, 0, nullptr, &size, &raw);
StringRef name{raw};
ENSURES(err == noErr, "can't get audio device name");
return name;
}
void SetHogMode(const AudioObjectID id) {
AudioObjectPropertyAddress address{};
address.mSelector = kAudioDevicePropertyHogMode;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
const pid_t p = getpid();
const OSStatus err = AudioObjectSetPropertyData(id, &address, 0, nullptr, sizeof(p), &p);
ENSURES(err == noErr, "setting hog-mode failed");
}
void SetFrameSize(const AudioObjectID id) {
AudioObjectPropertyAddress address{};
address.mSelector = kAudioDevicePropertyBufferFrameSize;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
const UInt32 f = 1024;
const OSStatus err = AudioObjectSetPropertyData(id, &address, 0, nullptr, sizeof(f), &f);
ENSURES(err == noErr, "setting buffer frame size failed");
}
std::string PrintAvailableFormats(const AudioObjectID id) {
OSStatus err = noErr;
AudioObjectPropertyAddress address{};
address.mSelector = kAudioStreamPropertyAvailablePhysicalFormats;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
UInt32 size;
err = AudioObjectGetPropertyDataSize(id, &address, 0, nullptr, &size);
ENSURES(err == noErr, "can't get number of audio devices");
std::vector<AudioStreamRangedDescription> des;
des.resize(size / sizeof(AudioStreamRangedDescription));
err = AudioObjectGetPropertyData(id, &address, 0, nullptr, &size, des.data());
ENSURES(err == noErr, "failed");
for (auto desc : des) {
std::cout << "Format: " << desc.mFormat.mSampleRate << ", " << desc.mFormat.mFormatID << ", "
<< desc.mFormat.mFormatFlags << ", " << desc.mFormat.mBitsPerChannel << ", "
<< desc.mFormat.mChannelsPerFrame << ", " << desc.mFormat.mBytesPerFrame << ", "
<< desc.mFormat.mFramesPerPacket << ", " << desc.mFormat.mBytesPerPacket << std::endl;
}
return {};
}
bool IsEqual(const AudioStreamBasicDescription lhs, const AudioStreamBasicDescription rhs) {
return (lhs.mSampleRate == rhs.mSampleRate) && //
(lhs.mFormatID == rhs.mFormatID) && //
(lhs.mFormatFlags == rhs.mFormatFlags) && //
(lhs.mBitsPerChannel == rhs.mBitsPerChannel) && //
(lhs.mChannelsPerFrame == rhs.mChannelsPerFrame) && //
(lhs.mBytesPerFrame == rhs.mBytesPerFrame) && //
(lhs.mFramesPerPacket == rhs.mFramesPerPacket) && //
(lhs.mBytesPerPacket == rhs.mBytesPerPacket);
}
AudioFormat SetStreamFormat(const AudioObjectID id, const AudioFormat format) {
OSStatus err = noErr;
AudioStreamBasicDescription desc{};
desc.mSampleRate = format.rate;
desc.mFormatID = kAudioFormatLinearPCM;
desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonMixable;
desc.mBitsPerChannel = 24;
desc.mChannelsPerFrame = 4;
desc.mBytesPerFrame = 12;
desc.mFramesPerPacket = 1;
desc.mBytesPerPacket = desc.mFramesPerPacket * desc.mBytesPerFrame;
AudioObjectPropertyAddress address{};
address.mSelector = kAudioStreamPropertyPhysicalFormat;
address.mScope = kAudioObjectPropertyScopeOutput;
address.mElement = kAudioObjectPropertyElementMaster;
err = AudioObjectSetPropertyData(id, &address, 0, nullptr, sizeof(desc), &desc);
ENSURES(err == noErr, "setting physical format failed");
AudioStreamBasicDescription actual{};
UInt32 size{sizeof(actual)};
address.mSelector = kAudioStreamPropertyPhysicalFormat;
err = AudioObjectGetPropertyData(id, &address, 0, nullptr, &size, &actual);
ENSURES(err == noErr, "getting physical format failed");
ENSURES(IsEqual(desc, actual), "wrong phyiscal format");
address.mSelector = kAudioStreamPropertyVirtualFormat;
err = AudioObjectGetPropertyData(id, &address, 0, nullptr, &size, &actual);
ENSURES(err == noErr, "getting virtual format failed");
ENSURES(IsEqual(desc, actual), "wrong virtual format");
return {desc.mBitsPerChannel, desc.mChannelsPerFrame, static_cast<unsigned>(desc.mSampleRate)};
}
} // namespace
CoreAudioDevice::CoreAudioDevice(AudioBuffer<228000> &audio_buffer, FlowControl &flow)
: uln2_{}, proc_id_{}, format_{}, audio_buffer_{audio_buffer}, flow_{flow} {}
CoreAudioDevice::~CoreAudioDevice() {
OSStatus err = noErr;
err = AudioDeviceStop(uln2_, proc_id_);
ENSURES(err == noErr, "stop failed");
err = AudioDeviceDestroyIOProcID(uln2_, proc_id_);
ENSURES(err == noErr, "destroy failed");
}
void CoreAudioDevice::Init(const AudioFormat format) {
const std::vector<AudioObjectID> ids = GetAudioDevices();
const StringRef metric_halo{CFSTR("ULN-2")};
for (const AudioObjectID id : ids) {
const StringRef device_name = GetDeviceName(id);
if (device_name == metric_halo) {
uln2_ = id;
break;
}
}
ENSURES(uln2_ != 0, "can't find ULN2");
SetHogMode(uln2_);
SetFrameSize(uln2_);
SetStreamFormat(uln2_, format);
if (format.bits == 16) {
const OSStatus err = AudioDeviceCreateIOProcID(uln2_, Raw16, this, &proc_id_);
ENSURES(err == noErr, "setting callback failed");
} else if (format.bits == 24) {
const OSStatus err = AudioDeviceCreateIOProcID(uln2_, Raw24, this, &proc_id_);
ENSURES(err == noErr, "setting callback failed");
} else {
ENSURES(false, "unsupported format.bits");
}
format_ = format;
}
void CoreAudioDevice::Playback() {
const OSStatus err = AudioDeviceStart(uln2_, proc_id_);
ENSURES(err == noErr, "start failed");
}
} // namespace plac