forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreAudio.swift
303 lines (252 loc) · 9.94 KB
/
CoreAudio.swift
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
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
// UNSUPPORTED: OS=watchos
import StdlibUnittest
import StdlibCollectionUnittest
import CoreAudio
// Used in tests below.
extension AudioBuffer : Equatable {}
public func == (lhs: AudioBuffer, rhs: AudioBuffer) -> Bool {
return lhs.mNumberChannels == rhs.mNumberChannels &&
lhs.mDataByteSize == rhs.mDataByteSize &&
lhs.mData == rhs.mData
}
var CoreAudioTestSuite = TestSuite("CoreAudio")
// The size of the non-flexible part of an AudioBufferList.
#if arch(i386) || arch(arm)
let ablHeaderSize = 4
#elseif arch(x86_64) || arch(arm64)
let ablHeaderSize = 8
#endif
CoreAudioTestSuite.test("UnsafeBufferPointer.init(_: AudioBuffer)") {
do {
let audioBuffer = AudioBuffer(
mNumberChannels: 0, mDataByteSize: 0, mData: nil)
let result: UnsafeBufferPointer<Float> = UnsafeBufferPointer(audioBuffer)
expectEqual(nil, result.baseAddress)
expectEqual(0, result.count)
}
do {
let audioBuffer = AudioBuffer(
mNumberChannels: 2, mDataByteSize: 1024,
mData: UnsafeMutableRawPointer(bitPattern: 0x1234_5678))
let result: UnsafeBufferPointer<Float> = UnsafeBufferPointer(audioBuffer)
expectEqual(audioBuffer.mData, UnsafeRawPointer(result.baseAddress!))
expectEqual(256, result.count)
}
}
CoreAudioTestSuite.test("UnsafeMutableBufferPointer.init(_: AudioBuffer)") {
do {
let audioBuffer = AudioBuffer(
mNumberChannels: 0, mDataByteSize: 0, mData: nil)
let result: UnsafeMutableBufferPointer<Float> =
UnsafeMutableBufferPointer(audioBuffer)
expectEqual(nil, result.baseAddress)
expectEqual(0, result.count)
}
do {
let audioBuffer = AudioBuffer(
mNumberChannels: 2, mDataByteSize: 1024,
mData: UnsafeMutableRawPointer(bitPattern: 0x1234_5678))
let result: UnsafeMutableBufferPointer<Float> =
UnsafeMutableBufferPointer(audioBuffer)
expectEqual(audioBuffer.mData!, UnsafeMutableRawPointer(result.baseAddress!))
expectEqual(256, result.count)
}
}
CoreAudioTestSuite.test(
"AudioBuffer.init(_: UnsafeMutableBufferPointer, numberOfChannels: Int)") {
do {
// NULL pointer.
let buffer = UnsafeMutableBufferPointer<Float>(start: nil, count: 0)
let result = AudioBuffer(buffer, numberOfChannels: 2)
expectEqual(2, result.mNumberChannels)
expectEqual(0, result.mDataByteSize)
expectEqual(nil, result.mData)
}
do {
// Non-NULL pointer.
let buffer = UnsafeMutableBufferPointer<Float>(
start: UnsafeMutablePointer<Float>(bitPattern: 0x1234_5678), count: 0)
let result = AudioBuffer(buffer, numberOfChannels: 2)
expectEqual(2, result.mNumberChannels)
expectEqual(0, result.mDataByteSize)
expectEqual(buffer.baseAddress, result.mData)
}
}
CoreAudioTestSuite.test(
"AudioBuffer.init(_: UnsafeMutableBufferPointer, numberOfChannels: Int)/trap") {
#if arch(i386) || arch(arm)
let overflowingCount = Int.max
#elseif arch(x86_64) || arch(arm64)
let overflowingCount = Int(UInt32.max)
#endif
let buffer = UnsafeMutableBufferPointer<Float>(
start: UnsafeMutablePointer<Float>(bitPattern: 0x1234_5678),
count: overflowingCount)
expectCrashLater()
// An overflow happens when we try to compute the value for mDataByteSize.
_ = AudioBuffer(buffer, numberOfChannels: 2)
}
CoreAudioTestSuite.test("AudioBufferList.sizeInBytes(maximumBuffers: Int)") {
expectEqual(ablHeaderSize + MemoryLayout<AudioBuffer>.stride,
AudioBufferList.sizeInBytes(maximumBuffers: 1))
expectEqual(ablHeaderSize + 16 * MemoryLayout<AudioBuffer>.stride,
AudioBufferList.sizeInBytes(maximumBuffers: 16))
}
CoreAudioTestSuite.test("AudioBufferList.sizeInBytes(maximumBuffers: Int)/trap/count<0") {
expectCrashLater()
_ = AudioBufferList.sizeInBytes(maximumBuffers: -1)
}
CoreAudioTestSuite.test("AudioBufferList.sizeInBytes(maximumBuffers: Int)/trap/count==0") {
expectCrashLater()
_ = AudioBufferList.sizeInBytes(maximumBuffers: -1)
}
CoreAudioTestSuite.test("AudioBufferList.sizeInBytes(maximumBuffers: Int)/trap/overflow") {
expectCrashLater()
_ = AudioBufferList.sizeInBytes(maximumBuffers: Int.max)
}
CoreAudioTestSuite.test("AudioBufferList.allocate(maximumBuffers: Int)") {
do {
let ablPtrWrapper = AudioBufferList.allocate(maximumBuffers: 1)
expectEqual(1, ablPtrWrapper.count)
free(ablPtrWrapper.unsafeMutablePointer)
}
do {
let ablPtrWrapper = AudioBufferList.allocate(maximumBuffers: 16)
expectEqual(16, ablPtrWrapper.count)
free(ablPtrWrapper.unsafeMutablePointer)
}
}
CoreAudioTestSuite.test("AudioBufferList.allocate(maximumBuffers: Int)/trap/count==0") {
expectCrashLater()
_ = AudioBufferList.allocate(maximumBuffers: 0)
}
CoreAudioTestSuite.test("AudioBufferList.allocate(maximumBuffers: Int)/trap/count<0") {
expectCrashLater()
_ = AudioBufferList.allocate(maximumBuffers: -1)
}
CoreAudioTestSuite.test("AudioBufferList.allocate(maximumBuffers: Int)/trap/overflow") {
expectCrashLater()
_ = AudioBufferList.allocate(maximumBuffers: Int.max)
}
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer/AssociatedTypes") {
typealias Subject = UnsafeMutableAudioBufferListPointer
expectRandomAccessCollectionAssociatedTypes(
collectionType: Subject.self,
iteratorType: IndexingIterator<Subject>.self,
subSequenceType: Slice<Subject>.self,
indexType: Int.self,
indicesType: CountableRange<Int>.self)
}
CoreAudioTestSuite.test(
"UnsafeMutableAudioBufferListPointer.init(_: UnsafeMutablePointer<AudioBufferList>)," +
"UnsafeMutableAudioBufferListPointer.unsafePointer," +
"UnsafeMutableAudioBufferListPointer.unsafeMutablePointer") {
do {
let ablPtrWrapper = UnsafeMutableAudioBufferListPointer(nil)
expectNil(ablPtrWrapper)
}
do {
let ablPtrWrapper = UnsafeMutableAudioBufferListPointer(
UnsafeMutablePointer<AudioBufferList>(bitPattern: 0x1234_5678)!)
expectEqual(
UnsafePointer<AudioBufferList>(bitPattern: 0x1234_5678),
ablPtrWrapper.unsafePointer)
expectEqual(
UnsafePointer<AudioBufferList>(bitPattern: 0x1234_5678),
ablPtrWrapper.unsafeMutablePointer)
}
do {
let ablPtrWrapper = UnsafeMutableAudioBufferListPointer(
UnsafeMutablePointer<AudioBufferList>(bitPattern: 0x1234_5678))
expectNotNil(ablPtrWrapper)
expectEqual(
UnsafePointer<AudioBufferList>(bitPattern: 0x1234_5678),
ablPtrWrapper!.unsafePointer)
expectEqual(
UnsafePointer<AudioBufferList>(bitPattern: 0x1234_5678),
ablPtrWrapper!.unsafeMutablePointer)
}
}
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.count") {
let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16)
let rawPtr = UnsafeMutableRawPointer.allocate(
byteCount: sizeInBytes, alignment: MemoryLayout<AudioBufferList>.alignment)
let ablPtr = rawPtr.bindMemory(to: AudioBufferList.self,
capacity: sizeInBytes / MemoryLayout<AudioBufferList>.stride)
// It is important that 'ablPtrWrapper' is a 'let'. We are verifying that
// the 'count' property has a nonmutating setter.
let ablPtrWrapper = UnsafeMutableAudioBufferListPointer(ablPtr)
// Test getter.
rawPtr.storeBytes(of: 0x1234_5678, as: UInt32.self)
expectEqual(0x1234_5678, ablPtrWrapper.count)
// Test setter.
ablPtrWrapper.count = 0x7765_4321
expectEqual(0x7765_4321, rawPtr.load(as: UInt32.self))
rawPtr.deallocate()
}
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)") {
let sizeInBytes = AudioBufferList.sizeInBytes(maximumBuffers: 16)
let rawPtr = UnsafeMutableRawPointer.allocate(
byteCount: sizeInBytes, alignment: 1)
let ablPtr = rawPtr.bindMemory(
to: AudioBufferList.self,
capacity: sizeInBytes / MemoryLayout<AudioBufferList>.stride)
// It is important that 'ablPtrWrapper' is a 'let'. We are verifying that
// the subscript has a nonmutating setter.
let ablPtrWrapper = UnsafeMutableAudioBufferListPointer(ablPtr)
do {
// Test getter.
let audioBuffer = AudioBuffer(
mNumberChannels: 2, mDataByteSize: 1024,
mData: UnsafeMutableRawPointer(bitPattern: 0x1234_5678))
let bufPtr = (rawPtr + ablHeaderSize).assumingMemoryBound(
to: AudioBuffer.self)
bufPtr.pointee = audioBuffer
ablPtrWrapper.count = 1
expectEqual(2, ablPtrWrapper[0].mNumberChannels)
expectEqual(1024, ablPtrWrapper[0].mDataByteSize)
expectEqual(audioBuffer.mData, ablPtrWrapper[0].mData)
}
do {
// Test setter.
let audioBuffer = AudioBuffer(
mNumberChannels: 5, mDataByteSize: 256,
mData: UnsafeMutableRawPointer(bitPattern: 0x8765_4321 as UInt))
ablPtrWrapper.count = 2
ablPtrWrapper[1] = audioBuffer
let audioBufferPtr = (rawPtr + ablHeaderSize).assumingMemoryBound(
to: AudioBuffer.self) + 1
expectEqual(5, audioBufferPtr.pointee.mNumberChannels)
expectEqual(256, audioBufferPtr.pointee.mDataByteSize)
expectEqual(audioBuffer.mData, audioBufferPtr.pointee.mData)
}
ablPtr.deallocate()
}
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer.subscript(_: Int)/trap") {
let ablPtrWrapper = AudioBufferList.allocate(maximumBuffers: 4)
ablPtrWrapper[0].mNumberChannels = 42
ablPtrWrapper[1].mNumberChannels = 42
ablPtrWrapper[2].mNumberChannels = 42
ablPtrWrapper[3].mNumberChannels = 42
expectCrashLater()
ablPtrWrapper[4].mNumberChannels = 42
}
CoreAudioTestSuite.test("UnsafeMutableAudioBufferListPointer/Collection") {
var ablPtrWrapper = AudioBufferList.allocate(maximumBuffers: 16)
expectType(UnsafeMutableAudioBufferListPointer.self, &ablPtrWrapper)
var expected: [AudioBuffer] = []
for i in 0..<16 {
let audioBuffer = AudioBuffer(
mNumberChannels: UInt32(2 + i), mDataByteSize: UInt32(1024 * i),
mData: UnsafeMutableRawPointer(bitPattern: 0x1234_5678 + i * 10))
ablPtrWrapper[i] = audioBuffer
expected.append(audioBuffer)
}
// FIXME: use checkMutableRandomAccessCollection, when we have that function.
checkRandomAccessCollection(expected, ablPtrWrapper)
free(ablPtrWrapper.unsafeMutablePointer)
}
runAllTests()