Skip to content

Commit 638d109

Browse files
committed
Adjust CoreAudio driver latency
Signed-off-by: Claudio Matsuoka <[email protected]>
1 parent 8058998 commit 638d109

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

Changelog

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Stable versions
22
---------------
33

44
4.0.10 ():
5-
- Update deprecated calls in CoreAudio driver
5+
- Adjust CoreAudio driver latency
66

77
4.0.9 (20140926):
88
- Add option '-p' to set the default pan amplitude

src/sound_coreaudio.c

+38-43
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,17 @@ static int init(struct options *options)
132132
AudioComponent comp;
133133
AudioComponentDescription cd;
134134
AURenderCallbackStruct rc;
135-
OSStatus err;
135+
OSStatus status;
136136
UInt32 size, max_frames;
137-
//char **parm = options->driver_parm;
137+
int latency = 250;
138+
char **parm = options->driver_parm;
138139

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;
141146

142147
ad.mSampleRate = options->rate;
143148
ad.mFormatID = kAudioFormatLinearPCM;
@@ -168,48 +173,30 @@ static int init(struct options *options)
168173
cd.componentFlags = 0;
169174
cd.componentFlagsMask = 0;
170175

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;
175178

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;
180181

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;
185184

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;
200188

201189
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;
207193

208194
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;
211197
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;
213200

214201
rc.inputProc = render_proc;
215202
rc.inputProcRefCon = 0;
@@ -218,13 +205,19 @@ static int init(struct options *options)
218205
buf_write_pos = 0;
219206
paused = 1;
220207

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;
226212

227213
return 0;
214+
215+
err2:
216+
free(buffer);
217+
err1:
218+
fprintf(stderr, "initialization error: %d\n", (int)status);
219+
err:
220+
return -1;
228221
}
229222

230223

@@ -268,10 +261,12 @@ static void flush(void)
268261

269262
static void onpause(void)
270263
{
264+
AudioOutputUnitStop(au);
271265
}
272266

273267
static void onresume(void)
274268
{
269+
AudioOutputUnitStart(au);
275270
}
276271

277272
struct sound_driver sound_coreaudio = {

src/xmp.1

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH "XMP" "1" "Version 4\&.0\&.9" "Sep 2014" "Extended Module Player"
1+
.TH "XMP" "1" "Version 4\&.0\&.10" "Nov 2014" "Extended Module Player"
22
.PP
33
.SH "NAME"
44
xmp - Extended Module Player
@@ -179,6 +179,10 @@ The default is 128\&.
179179
.IP "\fB\-D\fP \fIbuffer=size\fP"
180180
Set the size in bytes of the audio buffer\&. Default value is 32 Kb\&.
181181
.PP
182+
CoreAudio driver options:
183+
.IP "\fB\-D\fP \fIbuffer=value\fP"
184+
Set buffer size in ms\&. Default value is 250.
185+
.PP
182186
HP-UX and Solaris driver options:
183187
.IP "\fB\-D\fP \fIgain=value\fP"
184188
Set the audio gain\&. Valid values range from 0 to 255\&.

0 commit comments

Comments
 (0)