-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioQueuePlayer.m
More file actions
176 lines (154 loc) · 5.5 KB
/
AudioQueuePlayer.m
File metadata and controls
176 lines (154 loc) · 5.5 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
//
// Copyright 2011 Mike Coleman
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Note: You are NOT required to make the license available from within your
// iOS application. Including it in your project is sufficient.
//
// Attribution is not required, but appreciated :)
//
#import "AudioQueuePlayer.h"
#define NUM_AUDIO_BUFFERS 3
@interface AudioQueuePlayer() {
AudioStreamBasicDescription audioDesc;
AudioQueueRef audioQueue;
AudioQueueBufferRef audioBuffers[NUM_AUDIO_BUFFERS];
BOOL m_running;
}
- (void)setupAudioFormat;
- (void)cleanupAudioQueue;
- (void)makeAudioQueue;
- (void)makeBuffers;
- (void)handleQueuePropertyChange:(AudioQueueRef)inAQ propertyID:(AudioQueuePropertyID)inID;
@end
@implementation AudioQueuePlayer
@synthesize addSampleCallback;
@synthesize delegate;
- (BOOL)running {
return m_running;
}
static void MyAudioQueuePropertyListenerProc(void *inUserData, AudioQueueRef inAQ, AudioQueuePropertyID inID) {
AudioQueuePlayer *player = (AudioQueuePlayer *)inUserData;
[player handleQueuePropertyChange:inAQ propertyID:inID];
}
static void HandleOutputBuffer(void *inData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
OSStatus err = noErr;
AudioQueuePlayer *player = (AudioQueuePlayer *)inData;
if (player == nil) {
NSLog(@"nil player!");
return;
}
NSLog(@"%s: running = %d, addSampleCallback %@", __func__, player->m_running, player.addSampleCallback ? @"present" : @"missing");
if (player->m_running && player.addSampleCallback) {
inBuffer->mAudioDataByteSize = 0;
(player.addSampleCallback)(player, inBuffer);
if (inBuffer->mAudioDataByteSize) {
NSLog(@"enqueue");
err = AudioQueueEnqueueBuffer(player->audioQueue, inBuffer, 0, 0);
}
}
else {
NSLog(@"%s: stopping audio queue", __func__);
err = AudioQueueStop(player->audioQueue, NO);
}
}
- (id)init {
self = [super init];
if (self) {
m_running = NO;
}
return self;
}
- (void)dealloc {
delegate = nil;
[super dealloc];
}
- (void)setupAudioFormat {
audioDesc.mSampleRate = 44100.00;
audioDesc.mFormatID = kAudioFormatLinearPCM;
audioDesc.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
audioDesc.mChannelsPerFrame = 2;
audioDesc.mFramesPerPacket = 1;
audioDesc.mBitsPerChannel = 16;
audioDesc.mBytesPerPacket = 4;
audioDesc.mBytesPerFrame = 4;
//ok canonicial implies interleaved, 2 channels, 16 bits per channel == 32 bits == 4 bytesPerFrame/Packet
//TODO why isnt this ABSD populated from the file selected?
//TODO using AudioFileGetProperty, kAudioFilePropertyDataFormat?
}
- (void)cleanupAudioQueue {
if (audioQueue != NULL) {
AudioQueueDispose(audioQueue, true);
}
audioQueue = NULL;
}
- (void)makeAudioQueue {
OSStatus err;
[self cleanupAudioQueue];
err = AudioQueueNewOutput(&audioDesc, HandleOutputBuffer, self,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &audioQueue);
NSAssert(err == noErr, @"expected noErr", err);
err = AudioQueueAddPropertyListener(audioQueue, kAudioQueueProperty_IsRunning, MyAudioQueuePropertyListenerProc, self);
NSAssert(err == noErr, @"expected noErr", err);
}
- (void)makeBuffers {
OSStatus err;
for (int i = 0; i < NUM_AUDIO_BUFFERS; ++i) {
err = AudioQueueAllocateBufferWithPacketDescriptions(audioQueue, 32768, 0, &audioBuffers[i]);
NSAssert(err == noErr, @"expected noErr", err);
HandleOutputBuffer(self, audioQueue, audioBuffers[i]);
}
}
- (void)start {
OSStatus err = noErr;
m_running = YES;
[self setupAudioFormat];
[self makeAudioQueue];
[self makeBuffers];
err = AudioQueueStart(audioQueue, NULL);
NSAssert(err == noErr, @"expected noErr", err);
NSLog(@"leaving %s", __func__);
}
- (void)stop {
OSStatus err = noErr;
m_running = NO;
if (audioQueue) {
err = AudioQueueStop(audioQueue, NO);
NSAssert(err == noErr, @"expected noErr", err);
}
}
- (void)handleQueuePropertyChange:(AudioQueueRef)inAQ propertyID:(AudioQueuePropertyID)inID; {
if (delegate) {
switch (inID) {
case kAudioQueueProperty_IsRunning: {
UInt32 isRunning;
UInt32 ioDataSize = sizeof(isRunning);
AudioQueueGetProperty(inAQ, inID, &isRunning, &ioDataSize);
if (isRunning) {
if ([delegate respondsToSelector:@selector(audioQueuePlayerDidStartPlayback:)]) {
[delegate audioQueuePlayerDidStartPlayback:self];
}
}
else {
if ([delegate respondsToSelector:@selector(audioQueuePlayerDidStopPlayback:)]) {
[delegate audioQueuePlayerDidStopPlayback:self];
}
}
}
break;
default:
break;
}
}
}
@end