coreaudio: port to the new audio backend api

Signed-off-by: Kővágó, Zoltán <DirtY.iCE.hu@gmail.com>
Message-id: 586a1e66de5cbc6c5234f9ae556d24befb6afada.1568927990.git.DirtY.iCE.hu@gmail.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Kővágó, Zoltán 2019-09-19 23:24:11 +02:00 committed by Gerd Hoffmann
parent 286a5d201e
commit 2ceb8240fa

View File

@ -43,9 +43,6 @@ typedef struct coreaudioVoiceOut {
UInt32 audioDevicePropertyBufferFrameSize; UInt32 audioDevicePropertyBufferFrameSize;
AudioStreamBasicDescription outputStreamBasicDescription; AudioStreamBasicDescription outputStreamBasicDescription;
AudioDeviceIOProcID ioprocid; AudioDeviceIOProcID ioprocid;
size_t live;
size_t decr;
size_t rpos;
} coreaudioVoiceOut; } coreaudioVoiceOut;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
@ -397,31 +394,29 @@ static int coreaudio_unlock (coreaudioVoiceOut *core, const char *fn_name)
return 0; return 0;
} }
static size_t coreaudio_run_out(HWVoiceOut *hw, size_t live) #define COREAUDIO_WRAPPER_FUNC(name, ret_type, args_decl, args) \
{ static ret_type glue(coreaudio_, name)args_decl \
size_t decr; { \
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; \
ret_type ret; \
if (coreaudio_lock (core, "coreaudio_run_out")) { \
return 0; if (coreaudio_lock(core, "coreaudio_" #name)) { \
return 0; \
} \
\
ret = glue(audio_generic_, name)args; \
\
coreaudio_unlock(core, "coreaudio_" #name); \
return ret; \
} }
COREAUDIO_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size),
if (core->decr > live) { (hw, size))
ldebug ("core->decr %d live %d core->live %d\n", COREAUDIO_WRAPPER_FUNC(put_buffer_out_nowrite, size_t,
core->decr, (HWVoiceOut *hw, void *buf, size_t size),
live, (hw, buf, size))
core->live); COREAUDIO_WRAPPER_FUNC(write, size_t, (HWVoiceOut *hw, void *buf, size_t size),
} (hw, buf, size))
#undef COREAUDIO_WRAPPER_FUNC
decr = MIN (core->decr, live);
core->decr -= decr;
core->live = live - decr;
hw->rpos = core->rpos;
coreaudio_unlock (core, "coreaudio_run_out");
return decr;
}
/* callback to feed audiooutput buffer */ /* callback to feed audiooutput buffer */
static OSStatus audioDeviceIOProc( static OSStatus audioDeviceIOProc(
@ -433,19 +428,11 @@ static OSStatus audioDeviceIOProc(
const AudioTimeStamp* inOutputTime, const AudioTimeStamp* inOutputTime,
void* hwptr) void* hwptr)
{ {
UInt32 frame, frameCount; UInt32 frameCount, pending_frames;
float *out = outOutputData->mBuffers[0].mData; void *out = outOutputData->mBuffers[0].mData;
HWVoiceOut *hw = hwptr; HWVoiceOut *hw = hwptr;
coreaudioVoiceOut *core = (coreaudioVoiceOut *) hwptr; coreaudioVoiceOut *core = (coreaudioVoiceOut *) hwptr;
int rpos, live; size_t len;
struct st_sample *src;
#ifndef FLOAT_MIXENG
#ifdef RECIPROCAL
const float scale = 1.f / UINT_MAX;
#else
const float scale = UINT_MAX;
#endif
#endif
if (coreaudio_lock (core, "audioDeviceIOProc")) { if (coreaudio_lock (core, "audioDeviceIOProc")) {
inInputTime = 0; inInputTime = 0;
@ -453,42 +440,51 @@ static OSStatus audioDeviceIOProc(
} }
frameCount = core->audioDevicePropertyBufferFrameSize; frameCount = core->audioDevicePropertyBufferFrameSize;
live = core->live; pending_frames = hw->pending_emul >> hw->info.shift;
/* if there are not enough samples, set signal and return */ /* if there are not enough samples, set signal and return */
if (live < frameCount) { if (pending_frames < frameCount) {
inInputTime = 0; inInputTime = 0;
coreaudio_unlock (core, "audioDeviceIOProc(empty)"); coreaudio_unlock (core, "audioDeviceIOProc(empty)");
return 0; return 0;
} }
rpos = core->rpos; len = frameCount << hw->info.shift;
src = hw->mix_buf + rpos; while (len) {
size_t write_len;
ssize_t start = ((ssize_t) hw->pos_emul) - hw->pending_emul;
if (start < 0) {
start += hw->size_emul;
}
assert(start >= 0 && start < hw->size_emul);
/* fill buffer */ write_len = MIN(MIN(hw->pending_emul, len),
for (frame = 0; frame < frameCount; frame++) { hw->size_emul - start);
#ifdef FLOAT_MIXENG
*out++ = src[frame].l; /* left channel */ memcpy(out, hw->buf_emul + start, write_len);
*out++ = src[frame].r; /* right channel */ hw->pending_emul -= write_len;
#else len -= write_len;
#ifdef RECIPROCAL out += write_len;
*out++ = src[frame].l * scale; /* left channel */
*out++ = src[frame].r * scale; /* right channel */
#else
*out++ = src[frame].l / scale; /* left channel */
*out++ = src[frame].r / scale; /* right channel */
#endif
#endif
} }
rpos = (rpos + frameCount) % hw->samples;
core->decr += frameCount;
core->rpos = rpos;
coreaudio_unlock (core, "audioDeviceIOProc"); coreaudio_unlock (core, "audioDeviceIOProc");
return 0; return 0;
} }
static UInt32 coreaudio_get_flags(struct audio_pcm_info *info,
struct audsettings *as)
{
UInt32 flags = info->sign ? kAudioFormatFlagIsSignedInteger : 0;
if (as->endianness) { /* 0 = little, 1 = big */
flags |= kAudioFormatFlagIsBigEndian;
}
if (flags == 0) { /* must not be 0 */
flags = kAudioFormatFlagsAreAllClear;
}
return flags;
}
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
void *drv_opaque) void *drv_opaque)
{ {
@ -576,6 +572,16 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
/* set Samplerate */ /* set Samplerate */
core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq; core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
core->outputStreamBasicDescription.mFormatID = kAudioFormatLinearPCM;
core->outputStreamBasicDescription.mFormatFlags =
coreaudio_get_flags(&hw->info, as);
core->outputStreamBasicDescription.mBytesPerPacket =
core->outputStreamBasicDescription.mBytesPerFrame =
hw->info.nchannels * hw->info.bits / 8;
core->outputStreamBasicDescription.mFramesPerPacket = 1;
core->outputStreamBasicDescription.mChannelsPerFrame = hw->info.nchannels;
core->outputStreamBasicDescription.mBitsPerChannel = hw->info.bits;
status = coreaudio_set_streamformat(core->outputDeviceID, status = coreaudio_set_streamformat(core->outputDeviceID,
&core->outputStreamBasicDescription); &core->outputStreamBasicDescription);
if (status != kAudioHardwareNoError) { if (status != kAudioHardwareNoError) {
@ -686,7 +692,9 @@ static void coreaudio_audio_fini (void *opaque)
static struct audio_pcm_ops coreaudio_pcm_ops = { static struct audio_pcm_ops coreaudio_pcm_ops = {
.init_out = coreaudio_init_out, .init_out = coreaudio_init_out,
.fini_out = coreaudio_fini_out, .fini_out = coreaudio_fini_out,
.run_out = coreaudio_run_out, .write = coreaudio_write,
.get_buffer_out = coreaudio_get_buffer_out,
.put_buffer_out = coreaudio_put_buffer_out_nowrite,
.ctl_out = coreaudio_ctl_out .ctl_out = coreaudio_ctl_out
}; };