-
Notifications
You must be signed in to change notification settings - Fork 43
/
AudioConverterX.cpp
242 lines (240 loc) · 8.64 KB
/
AudioConverterX.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
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
#include "AudioConverterX.h"
#include <limits>
#include <cmath>
#include "CoreAudio/AudioCodec.h"
#include "cautil.h"
AudioConverterX::AudioConverterX(const AudioStreamBasicDescription &iasbd,
const AudioStreamBasicDescription &oasbd)
{
AudioConverterRef converter;
CHECKCA(AudioConverterNew(&iasbd, &oasbd, &converter));
attach(converter, true);
}
void AudioConverterX::attach(AudioConverterRef converter, bool takeOwn)
{
if (takeOwn)
m_converter.reset(converter, AudioConverterDispose);
else
m_converter.reset(converter, [](AudioConverterRef){});
}
UInt32 AudioConverterX::getSampleRateConverterComplexity()
{
UInt32 value;
UInt32 size = sizeof value;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterSampleRateConverterComplexity, &size, &value));
return value;
}
void AudioConverterX::setSampleRateConverterComplexity(UInt32 complexity)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterSampleRateConverterComplexity,
sizeof complexity, &complexity));
}
UInt32 AudioConverterX::getSampleRateConverterQuality()
{
UInt32 value;
UInt32 size = sizeof value;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterSampleRateConverterQuality,
&size, &value));
return value;
}
void AudioConverterX::setSampleRateConverterQuality(UInt32 quality)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterSampleRateConverterQuality,
sizeof quality, &quality));
}
UInt32 AudioConverterX::getPrimeMethod()
{
UInt32 value;
UInt32 size = sizeof value;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterPrimeMethod,
&size, &value));
return value;
}
void AudioConverterX::setPrimeMethod(UInt32 value)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterPrimeMethod,
sizeof value, &value));
}
AudioConverterPrimeInfo AudioConverterX::getPrimeInfo()
{
AudioConverterPrimeInfo result;
UInt32 size = sizeof(AudioConverterPrimeInfo);
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterPrimeInfo,
&size, &result));
return result;
}
void AudioConverterX::setPrimeInfo(const AudioConverterPrimeInfo &info)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterPrimeInfo,
sizeof(info), &info));
}
std::vector<uint8_t> AudioConverterX::getCompressionMagicCookie()
{
UInt32 size;
Boolean writable;
CHECKCA(AudioConverterGetPropertyInfo(m_converter.get(),
kAudioConverterCompressionMagicCookie, &size, &writable));
std::vector<uint8_t> vec(size / sizeof(uint8_t));
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterCompressionMagicCookie, &size, vec.data()));
return vec;
}
void AudioConverterX::setDecompressionMagicCookie(const std::vector<uint8_t> &v)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterDecompressionMagicCookie,
v.size(), v.data()));
}
UInt32 AudioConverterX::getEncodeBitRate()
{
UInt32 value;
UInt32 size = sizeof value;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterEncodeBitRate, &size, &value));
return value;
}
void AudioConverterX::setEncodeBitRate(UInt32 value)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterEncodeBitRate,
sizeof value, &value));
}
std::shared_ptr<AudioChannelLayout> AudioConverterX::getInputChannelLayout()
{
UInt32 size;
Boolean writable;
CHECKCA(AudioConverterGetPropertyInfo(m_converter.get(),
kAudioConverterInputChannelLayout, &size, &writable));
std::shared_ptr<AudioChannelLayout>
acl(static_cast<AudioChannelLayout*>(std::malloc(size)),
std::free);
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterInputChannelLayout, &size, acl.get()));
return acl;
}
void AudioConverterX::setInputChannelLayout(const AudioChannelLayout &value)
{
UInt32 size = cautil::sizeofAudioChannelLayout(value);
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterInputChannelLayout, size, &value));
}
std::shared_ptr<AudioChannelLayout> AudioConverterX::getOutputChannelLayout()
{
UInt32 size;
Boolean writable;
CHECKCA(AudioConverterGetPropertyInfo(m_converter.get(),
kAudioConverterOutputChannelLayout, &size, &writable));
std::shared_ptr<AudioChannelLayout> acl(
static_cast<AudioChannelLayout*>(std::malloc(size)),
std::free);
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterOutputChannelLayout, &size, acl.get()));
return acl;
}
void AudioConverterX::setOutputChannelLayout(const AudioChannelLayout &value)
{
UInt32 size = cautil::sizeofAudioChannelLayout(value);
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterOutputChannelLayout, size, &value));
}
std::vector<AudioValueRange> AudioConverterX::getApplicableEncodeBitRates()
{
UInt32 size;
Boolean writable;
CHECKCA(AudioConverterGetPropertyInfo(m_converter.get(),
kAudioConverterApplicableEncodeBitRates, &size, &writable));
std::vector<AudioValueRange> vec(size / sizeof(AudioValueRange));
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterApplicableEncodeBitRates, &size, vec.data()));
return vec;
}
std::vector<AudioValueRange> AudioConverterX::getApplicableEncodeSampleRates()
{
UInt32 size;
Boolean writable;
CHECKCA(AudioConverterGetPropertyInfo(m_converter.get(),
kAudioConverterApplicableEncodeSampleRates,
&size, &writable));
std::vector<AudioValueRange> vec(size / sizeof(AudioValueRange));
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterApplicableEncodeSampleRates, &size, vec.data()));
return vec;
}
AudioStreamBasicDescription AudioConverterX::getInputStreamDescription()
{
AudioStreamBasicDescription result;
UInt32 size = sizeof(result);
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterCurrentInputStreamDescription,
&size, &result));
return result;
}
AudioStreamBasicDescription AudioConverterX::getOutputStreamDescription()
{
AudioStreamBasicDescription result;
UInt32 size = sizeof(result);
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterCurrentOutputStreamDescription,
&size, &result));
return result;
}
UInt32 AudioConverterX::getMaximumOutputPacketSize()
{
UInt32 result;
UInt32 size = sizeof result;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterPropertyMaximumOutputPacketSize,
&size, &result));
return result;
}
UInt32 AudioConverterX::getBitRateControlMode()
{
UInt32 result;
UInt32 size = sizeof result;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioCodecPropertyBitRateControlMode,
&size, &result));
return result;
}
void AudioConverterX::setBitRateControlMode(UInt32 value)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioCodecPropertyBitRateControlMode,
sizeof value, &value));
}
UInt32 AudioConverterX::getSoundQualityForVBR()
{
UInt32 result;
UInt32 size = sizeof result;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioCodecPropertySoundQualityForVBR,
&size, &result));
return result;
}
void AudioConverterX::setSoundQualityForVBR(UInt32 value)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioCodecPropertySoundQualityForVBR,
sizeof value, &value));
}
UInt32 AudioConverterX::getCodecQuality()
{
UInt32 result;
UInt32 size = sizeof result;
CHECKCA(AudioConverterGetProperty(m_converter.get(),
kAudioConverterCodecQuality, &size, &result));
return result;
}
void AudioConverterX::setCodecQuality(UInt32 value)
{
CHECKCA(AudioConverterSetProperty(m_converter.get(),
kAudioConverterCodecQuality, sizeof value, &value));
}