-
Notifications
You must be signed in to change notification settings - Fork 40
/
AudioFileX.h
327 lines (322 loc) · 11.9 KB
/
AudioFileX.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef AudioFileX_H
#define AudioFileX_H
#include "CoreAudio/AudioFile.h"
#include "CoreAudio/AudioFormat.h"
#include "strutil.h"
#include "cautil.h"
namespace afutil {
inline uint32_t getTypesForExtension(const wchar_t *fname)
{
const wchar_t *pos = std::wcsrchr(fname, L'.');
if (!pos)
return 0;
CFStringPtr cfsp = cautil::W2CF(strutil::wslower(pos + 1));
CFStringRef cfsr = cfsp.get();
UInt32 type = 0;
UInt32 size = sizeof(type);
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_TypesForExtension,
sizeof(CFStringRef),
&cfsr, &size, &type));
return type;
}
inline std::vector<UInt32> getReadableTypes()
{
UInt32 size;
CHECKCA(AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_ReadableTypes,
0, 0, &size));
std::vector<UInt32> vec(size / sizeof(UInt32));
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ReadableTypes,
0, 0, &size, vec.data()));
return vec;
}
inline std::vector<UInt32> getWritableTypes()
{
UInt32 size;
CHECKCA(AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_WritableTypes,
0, 0, &size));
std::vector<UInt32> vec(size / sizeof(UInt32));
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_WritableTypes,
0, 0, &size, vec.data()));
return vec;
}
inline std::wstring getFileTypeName(uint32_t type)
{
CFStringRef name;
UInt32 size = sizeof(name);
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_FileTypeName,
sizeof(UInt32), &type, &size, &name));
CFStringPtr _(name, CFRelease);
return cautil::CF2W(name);
}
inline std::vector<std::wstring> getExtensionsForType(uint32_t type)
{
CFArrayRef aref;
UInt32 size = sizeof(aref);
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_ExtensionsForType,
sizeof(UInt32), &type, &size, &aref));
std::shared_ptr<const __CFArray> _(aref, CFRelease);
CFIndex count = CFArrayGetCount(aref);
std::vector<std::wstring> result;
for (CFIndex i = 0; i < count; ++i) {
CFStringRef value =
static_cast<CFStringRef>(CFArrayGetValueAtIndex(aref, i));
result.push_back(cautil::CF2W(value));
}
return result;
}
inline std::vector<UInt32> getAvailableFormatIDs(uint32_t type)
{
UInt32 size;
CHECKCA(AudioFileGetGlobalInfoSize(
kAudioFileGlobalInfo_AvailableFormatIDs,
sizeof(type), &type, &size));
std::vector<UInt32> vec(size / sizeof(UInt32));
CHECKCA(AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AvailableFormatIDs,
sizeof(type), &type, &size, vec.data()));
return vec;
}
inline
std::wstring getASBDFormatName(const AudioStreamBasicDescription &asbd)
{
CFStringRef s;
UInt32 size = sizeof(s);
CHECKCA(AudioFormatGetProperty(kAudioFormatProperty_FormatName,
sizeof(asbd), &asbd,
&size, &s));
CFStringPtr _(s, CFRelease);
std::wstring ws = cautil::CF2W(s);
// XXX
// Workaround for CoreAudio bug. MPEG Layer 1 and 2 is reported as
// Layer 3
if (asbd.mFormatID == '.mp1' || asbd.mFormatID == '.mp2') {
const wchar_t *p;
if ((p = std::wcsstr(ws.c_str(), L"Layer 3")) != 0)
ws[p - ws.c_str() + 6] = asbd.mFormatID & 0xff;
}
return ws;
}
inline CFDictionaryPtr id3TagToDictinary(const void *data, size_t size)
{
UInt32 k = kAudioFormatProperty_ID3TagToDictionary;
CFDictionaryRef dref;
UInt32 osize = sizeof dref;
CHECKCA(AudioFormatGetProperty(k, size, data, &osize, &dref));
CFDictionaryPtr dp(dref, CFRelease);
return dp;
}
inline
std::shared_ptr<AudioChannelLayout> getChannelLayoutForTag(uint32_t tag)
{
UInt32 k = kAudioFormatProperty_ChannelLayoutForTag;
UInt32 size;
CHECKCA(AudioFormatGetPropertyInfo(k, sizeof(tag), &tag, &size));
std::shared_ptr<AudioChannelLayout>
acl(static_cast<AudioChannelLayout*>(std::malloc(size)),
std::free);
CHECKCA(AudioFormatGetProperty(k, sizeof(tag), &tag, &size, acl.get()));
return acl;
}
}
class AudioFileX {
std::shared_ptr<OpaqueAudioFileID> m_file;
public:
AudioFileX() {}
AudioFileX(AudioFileID file, bool takeOwn)
{
attach(file, takeOwn);
}
void attach(AudioFileID file, bool takeOwn)
{
if (takeOwn)
m_file.reset(file, AudioFileClose);
else
m_file.reset(file, [](AudioFileID){});
}
operator AudioFileID() { return m_file.get(); }
void close() { m_file.reset(); }
// property accessors
uint32_t getFileFormat()
{
UInt32 value;
UInt32 size = sizeof value;
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyFileFormat, &size, &value));
return value;
}
AudioStreamBasicDescription getDataFormat()
{
AudioStreamBasicDescription result;
UInt32 size = sizeof(AudioStreamBasicDescription);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyDataFormat, &size, &result));
return result;
}
uint64_t getAudioDataPacketCount()
{
UInt64 value;
UInt32 size = sizeof value;
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyAudioDataPacketCount,
&size, &value));
return value;
}
AudioFilePacketTableInfo getPacketTableInfo()
{
AudioFilePacketTableInfo result;
UInt32 size = sizeof(AudioFilePacketTableInfo);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyPacketTableInfo,
&size, &result));
return result;
}
void setPacketTableInfo(const AudioFilePacketTableInfo &info)
{
CHECKCA(AudioFileSetProperty(m_file.get(),
kAudioFilePropertyPacketTableInfo,
sizeof(AudioFilePacketTableInfo),
&info));
}
std::shared_ptr<AudioChannelLayout> getChannelLayout()
{
UInt32 size;
UInt32 writable;
CHECKCA(AudioFileGetPropertyInfo(m_file.get(),
kAudioFilePropertyChannelLayout, &size, &writable));
std::shared_ptr<AudioChannelLayout> acl(
static_cast<AudioChannelLayout*>(std::malloc(size)),
std::free);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyChannelLayout, &size, acl.get()));
return acl;
}
void setChannelLayout(const AudioChannelLayout &layout)
{
UInt32 size = cautil::sizeofAudioChannelLayout(layout);
CHECKCA(AudioFileSetProperty(m_file.get(),
kAudioFilePropertyChannelLayout,
size,
&layout));
}
std::vector<uint8_t> getMagicCookieData()
{
UInt32 size;
UInt32 writable;
std::vector<uint8_t> vec;
CHECKCA(AudioFileGetPropertyInfo(m_file.get(),
kAudioFilePropertyMagicCookieData,
&size, &writable));
if (size > 0) {
vec.resize(size);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyMagicCookieData,
&size, vec.data()));
}
return vec;
}
void setMagicCookieData(const uint8_t *cookie, size_t size)
{
UInt32 len = size;
CHECKCA(AudioFileSetProperty(m_file.get(),
kAudioFilePropertyMagicCookieData,
len,
cookie));
}
void setReserveDuration(double duration)
{
CHECKCA(AudioFileSetProperty(m_file.get(),
kAudioFilePropertyReserveDuration,
sizeof(duration),
&duration));
}
uint32_t getPacketSizeUpperBound()
{
UInt32 len;
UInt32 size = sizeof(len);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyPacketSizeUpperBound,
&size, &len));
return len;
}
uint32_t getMaximumPacketSize()
{
UInt32 len;
UInt32 size = sizeof(len);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyMaximumPacketSize,
&size, &len));
return len;
}
uint64_t getAudioDataByteCount()
{
UInt64 len;
UInt32 size = sizeof(len);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyAudioDataByteCount,
&size, &len));
return len;
}
CFDictionaryPtr getInfoDictionary()
{
CFDictionaryRef dictref;
UInt32 size = sizeof(dictref);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyInfoDictionary,
&size, &dictref));
CFDictionaryPtr newdic(dictref, CFRelease);
return newdic;
}
void setInfoDictionary(CFDictionaryRef dict)
{
CHECKCA(AudioFileSetProperty(m_file.get(),
kAudioFilePropertyInfoDictionary,
sizeof(dict), &dict));
}
std::vector<uint8_t> getUserData(uint32_t fcc, uint32_t index)
{
UInt32 size;
CHECKCA(AudioFileGetUserDataSize(m_file.get(), fcc, index, &size));
std::vector<uint8_t> vec(size);
CHECKCA(AudioFileGetUserData(m_file.get(), fcc, index, &size,
vec.data()));
return vec;
}
void setUserData(uint32_t fcc, uint32_t index, const void *data,
size_t size)
{
CHECKCA(AudioFileSetUserData(m_file.get(), fcc, index, size, data));
}
std::vector<AudioFormatListItem> getFormatList()
{
UInt32 size;
UInt32 writable;
CHECKCA(AudioFileGetPropertyInfo(m_file.get(),
kAudioFilePropertyFormatList,
&size, &writable));
size_t count = size / sizeof(AudioFormatListItem);
std::vector<AudioFormatListItem> vec(count);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyFormatList,
&size, vec.data()));
return vec;
}
uint32_t getBitrate()
{
UInt32 bitrate;
UInt32 size = sizeof(bitrate);
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyBitRate,
&size, &bitrate));
return bitrate;
}
int64_t getPacketToByte(int64_t packet)
{
AudioBytePacketTranslation trans = { 0 };
trans.mPacket = packet;
UInt32 size = sizeof trans;
CHECKCA(AudioFileGetProperty(m_file.get(),
kAudioFilePropertyPacketToByte,
&size, &trans));
return trans.mByte;
}
};
#endif