-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppController.m
More file actions
270 lines (175 loc) · 6.68 KB
/
AppController.m
File metadata and controls
270 lines (175 loc) · 6.68 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
// AppController.h -- application controller for Recordotron
// Copyright (C) 2003 Borkware
// We use the BSD License. Check out http://borkware.com/license
#import "AppController.h"
#import <MTCoreAudio/MTCoreAudio.h>
// about 25 seconds of recording time
#define SOUND_BUFFER_SIZE (8 * 1024 * 1024)
static unsigned char g_soundBuffer[SOUND_BUFFER_SIZE];
// for recording and playback, this is where we are in the buffer
static int g_lastIndex;
// how much data is in the buffer
static int g_bufferSize;
@implementation AppController
// this is the MTCoreAudioDevice IO target for recording. It's
// callback saying that there is new data to be read. Since we're
// just doing real simple buffering of data, and then regurgitating it
// when we play, we don't mess with too many of the arguments.
- (OSStatus) readCycleForDevice: (MTCoreAudioDevice *) theDevice
timeStamp: (const AudioTimeStamp *) now
inputData: (const AudioBufferList *) inputData
inputTime: (const AudioTimeStamp *) inputTime
outputData: (AudioBufferList *) outputData
outputTime: (const AudioTimeStamp *) outputTime
clientData: (void *) clientData
{
// peer into the data
const AudioBuffer *buffer;
buffer = &inputData->mBuffers[0];
// will this sample put us over the line? If so, dump the data
// and tell the UI to stop the recording and disable the Stop
// button. We don't stop the actual reading from here
// because it seems to leave some stale locks in the MTCoreAudio
// guts.
if (g_lastIndex + buffer->mDataByteSize > SOUND_BUFFER_SIZE) {
[self performSelectorOnMainThread: @selector(stopRecording:)
withObject: self
waitUntilDone: NO];
} else {
// append the data to the end of our buffer
memcpy (g_soundBuffer + g_lastIndex,
buffer->mData, buffer->mDataByteSize);
g_lastIndex += buffer->mDataByteSize;
}
return (noErr);
} // readCycleForDevice
// the MTCoreAudioDevice IO target for playback. We feed data from
// our buffer into the sound system
- (OSStatus) writeCycleForDevice: (MTCoreAudioDevice *) theDevice
timeStamp: (const AudioTimeStamp *) now
inputData: (const AudioBufferList *) inputData
inputTime: (const AudioTimeStamp *) inputTime
outputData: (AudioBufferList *) outputData
outputTime: (const AudioTimeStamp *) outputTime
clientData: (void *) clientData
{
// are we done?
if (g_lastIndex >= g_bufferSize) {
// yep. tell the UI part to shut down the playback.
[self performSelectorOnMainThread: @selector(stopPlaying:)
withObject: self
waitUntilDone: NO];
} else {
// otherwise stick some data into the buffer
AudioBuffer *buffer;
buffer = &outputData->mBuffers[0];
memcpy (buffer->mData,
g_soundBuffer + g_lastIndex,
buffer->mDataByteSize);
g_lastIndex += buffer->mDataByteSize;
}
return (noErr);
} // writeCycleForDevice
// update the UI based on the current sytem volume and input volume
- (void) setStuffBasedOnVolume
{
MTCoreAudioVolumeInfo volumeInfo;
volumeInfo = [inputDevice volumeInfoForChannel: 1
forDirection: kMTCoreAudioDeviceRecordDirection];
[inputVolumeSlider setFloatValue: volumeInfo.theVolume];
volumeInfo = [outputDevice volumeInfoForChannel: 1
forDirection: kMTCoreAudioDevicePlaybackDirection];
[outputVolumeSlider setFloatValue: volumeInfo.theVolume];
} // setStuffBasedOnVolume
// we've been intantiated. acquire our audio devices and set up the UI
- (void) awakeFromNib
{
inputDevice = [MTCoreAudioDevice defaultInputDevice];
[inputDevice retain];
outputDevice = [MTCoreAudioDevice defaultOutputDevice];
[outputDevice retain];
// set up the recording callback
[inputDevice setIOTarget: self
withSelector: @selector(readCycleForDevice:timeStamp:inputData:inputTime:outputData:outputTime:clientData:)
withClientData: NULL];
// set up the palyback callback
[outputDevice setIOTarget: self
withSelector: @selector(writeCycleForDevice:timeStamp:inputData:inputTime:outputData:outputTime:clientData:)
withClientData: NULL];
// update the UI
[self setStuffBasedOnVolume];
} // awakeFromNib
// clean up our mess
- (void) dealloc
{
[inputDevice release];
[outputDevice release];
[super dealloc];
} // dealloc
// button handler. kick off the recording
- (IBAction) startRecording: (id) sender
{
// reset our buffer position to the start
g_lastIndex = 0;
g_bufferSize = 0;
// update the UI so the user can stop the recording
[recordStopButton setEnabled: YES];
// start recording
[inputDevice deviceStart];
} // startRecording
// button handler. cease recording.
- (IBAction) stopRecording: (id) sender
{
// stop recording
[inputDevice deviceStop];
// snarf how much data we've gotten
g_bufferSize = g_lastIndex;
// update the UI to turn off the 'stop' button
[recordStopButton setEnabled: NO];
} // stopRecording
// button handler, start playback of our data
- (IBAction) startPlaying: (id) sender
{
// start playing from the beginning
g_lastIndex = 0;
// update the UI so the user can stop the playback
[playStopButton setEnabled: YES];
// start playback
[outputDevice deviceStart];
} // play
// button handler. cease playback
- (IBAction) stopPlaying: (id) sender
{
// stop playback
[outputDevice deviceStop];
// update the UI to turn off the 'stop' button
[playStopButton setEnabled: NO];
} // stopPlaying
// slider action handler. set the recording volume (the gain on the
// microphone)
- (IBAction) changeInputVolume: (id) sender
{
// setting volume on channel zero (the master channel) doesn't
// seem to affect the actual recording volume. So set each
// side of the input volume independently
[inputDevice setVolume: [inputVolumeSlider floatValue]
forChannel: 1
forDirection: kMTCoreAudioDeviceRecordDirection];
[inputDevice setVolume: [inputVolumeSlider floatValue]
forChannel: 2
forDirection: kMTCoreAudioDeviceRecordDirection];
} // changeInputVolume
// slider action handler. set the playback volume.
- (IBAction) changeOutputVolume: (id) sender
{
// setting volume on channel zero (the master channel) doesn't
// seem to affect the actual playback volume. So set each
// side of the input volume independently
[outputDevice setVolume: [outputVolumeSlider floatValue]
forChannel: 1
forDirection: kMTCoreAudioDevicePlaybackDirection];
[outputDevice setVolume: [outputVolumeSlider floatValue]
forChannel: 2
forDirection: kMTCoreAudioDevicePlaybackDirection];
} // changeOutputVolume
@end // AppController