@@ -132,12 +132,17 @@ static int init(struct options *options)
132
132
AudioComponent comp ;
133
133
AudioComponentDescription cd ;
134
134
AURenderCallbackStruct rc ;
135
- OSStatus err ;
135
+ OSStatus status ;
136
136
UInt32 size , max_frames ;
137
- //char **parm = options->driver_parm;
137
+ int latency = 250 ;
138
+ char * * parm = options -> driver_parm ;
138
139
139
- //parm_init(parm);
140
- //parm_end();
140
+ parm_init (parm );
141
+ chkparm1 ("buffer" , latency = strtoul (token , NULL , 0 ));
142
+ parm_end ();
143
+
144
+ if (latency < 20 )
145
+ latency = 20 ;
141
146
142
147
ad .mSampleRate = options -> rate ;
143
148
ad .mFormatID = kAudioFormatLinearPCM ;
@@ -168,48 +173,30 @@ static int init(struct options *options)
168
173
cd .componentFlags = 0 ;
169
174
cd .componentFlagsMask = 0 ;
170
175
171
- if ((comp = AudioComponentFindNext (NULL , & cd )) == NULL ) {
172
- fprintf (stderr , "error: FindNextComponent\n" );
173
- return -1 ;
174
- }
176
+ if ((comp = AudioComponentFindNext (NULL , & cd )) == NULL )
177
+ goto err ;
175
178
176
- if ((err = AudioComponentInstanceNew (comp , & au ))) {
177
- fprintf (stderr , "error: OpenAComponent (%d)\n" , (int )err );
178
- return -1 ;
179
- }
179
+ if ((status = AudioComponentInstanceNew (comp , & au )))
180
+ goto err1 ;
180
181
181
- if ((err = AudioUnitInitialize (au ))) {
182
- fprintf (stderr , "error: AudioUnitInitialize (%d)\n" , (int )err );
183
- return -1 ;
184
- }
182
+ if ((status = AudioUnitInitialize (au )))
183
+ goto err1 ;
185
184
186
- if ((err = AudioUnitSetProperty (au , kAudioUnitProperty_StreamFormat ,
187
- kAudioUnitScope_Input , 0 , & ad , sizeof (ad )))) {
188
- fprintf (stderr , "error: AudioUnitSetProperty: StreamFormat (%d)\n" , (int )err );
189
- fprintf (stderr , "mSampleRate = %lf\n" , ad .mSampleRate );
190
- fprintf (stderr , "mFormatID = 0x%x\n" , (unsigned )ad .mFormatID );
191
- fprintf (stderr , "mFormatFlags = 0x%x\n" , (unsigned )ad .mFormatFlags );
192
- fprintf (stderr , "mChannelsPerFrame = %d\n" , (int )ad .mChannelsPerFrame );
193
- fprintf (stderr , "mBitsPerChannel = %d\n" , (int )ad .mBitsPerChannel );
194
- fprintf (stderr , "mBytesPerFrame = %d\n" , (int )ad .mBytesPerFrame );
195
- fprintf (stderr , "mBytesPerPacket = %d\n" , (int )ad .mBytesPerPacket );
196
- fprintf (stderr , "mFramesPerPacket = %d\n" , (int )ad .mFramesPerPacket );
197
-
198
- return -1 ;
199
- }
185
+ if ((status = AudioUnitSetProperty (au , kAudioUnitProperty_StreamFormat ,
186
+ kAudioUnitScope_Input , 0 , & ad , sizeof (ad ))))
187
+ goto err1 ;
200
188
201
189
size = sizeof (UInt32 );
202
- if ((err = AudioUnitGetProperty (au , kAudioDevicePropertyBufferSize ,
203
- kAudioUnitScope_Input , 0 , & max_frames , & size ))) {
204
- fprintf (stderr , "error: AudioUnitGetProperty: BufferSize (%d)\n" , (int )err );
205
- return -1 ;
206
- }
190
+ if ((status = AudioUnitGetProperty (au , kAudioDevicePropertyBufferSize ,
191
+ kAudioUnitScope_Input , 0 , & max_frames , & size )))
192
+ goto err1 ;
207
193
208
194
chunk_size = max_frames ;
209
- num_chunks = (options -> rate * ad .mBytesPerFrame + chunk_size - 1 ) /
210
- chunk_size ;
195
+ num_chunks = (options -> rate * ad .mBytesPerFrame * latency / 1000
196
+ + chunk_size - 1 ) / chunk_size ;
211
197
buffer_len = (num_chunks + 1 ) * chunk_size ;
212
- buffer = calloc (num_chunks + 1 , chunk_size );
198
+ if ((buffer = calloc (num_chunks + 1 , chunk_size )) == NULL )
199
+ goto err ;
213
200
214
201
rc .inputProc = render_proc ;
215
202
rc .inputProcRefCon = 0 ;
@@ -218,13 +205,19 @@ static int init(struct options *options)
218
205
buf_write_pos = 0 ;
219
206
paused = 1 ;
220
207
221
- if ((err = AudioUnitSetProperty (au , kAudioUnitProperty_SetRenderCallback ,
222
- kAudioUnitScope_Input , 0 , & rc , sizeof (rc )))) {
223
- fprintf (stderr , "error: AudioUnitSetProperty: SetRenderCallback (%d)\n" , (int )err );
224
- return -1 ;
225
- }
208
+ if ((status = AudioUnitSetProperty (au ,
209
+ kAudioUnitProperty_SetRenderCallback ,
210
+ kAudioUnitScope_Input , 0 , & rc , sizeof (rc ))))
211
+ goto err2 ;
226
212
227
213
return 0 ;
214
+
215
+ err2 :
216
+ free (buffer );
217
+ err1 :
218
+ fprintf (stderr , "initialization error: %d\n" , (int )status );
219
+ err :
220
+ return -1 ;
228
221
}
229
222
230
223
@@ -268,10 +261,12 @@ static void flush(void)
268
261
269
262
static void onpause (void )
270
263
{
264
+ AudioOutputUnitStop (au );
271
265
}
272
266
273
267
static void onresume (void )
274
268
{
269
+ AudioOutputUnitStart (au );
275
270
}
276
271
277
272
struct sound_driver sound_coreaudio = {
0 commit comments