Enable OSX output plugin to set hardware sample rate and bit depth at the same time

This PR will fix #271.

special thanks to @coroner21 who contributed a nice way to score hardware supported format in #292

Also, The DSD related code are all guarded with ENABLE_DSD  flag.
This commit is contained in:
Yue Wang 2018-07-13 12:48:43 -07:00 committed by GitHub
parent d4ce9c0df2
commit 40a1ebee29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 166 additions and 134 deletions

View File

@ -30,6 +30,8 @@
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "thread/Cond.hxx" #include "thread/Cond.hxx"
#include "system/ByteOrder.hxx" #include "system/ByteOrder.hxx"
#include "util/StringBuffer.hxx"
#include "util/StringFormat.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <CoreAudio/CoreAudio.h> #include <CoreAudio/CoreAudio.h>
@ -41,6 +43,22 @@
static constexpr unsigned MPD_OSX_BUFFER_TIME_MS = 100; static constexpr unsigned MPD_OSX_BUFFER_TIME_MS = 100;
static StringBuffer<64>
StreamDescriptionToString(const AudioStreamBasicDescription desc) {
// Only convert the lpcm formats (nothing else supported / used by MPD)
assert(desc.mFormatID == kAudioFormatLinearPCM);
return StringFormat<64>("%u channel %s %sinterleaved %u-bit %s %s (%uHz)",
desc.mChannelsPerFrame,
(desc.mFormatFlags & kAudioFormatFlagIsNonMixable) ? "" : "mixable",
(desc.mFormatFlags & kAudioFormatFlagIsNonInterleaved) ? "non-" : "",
desc.mBitsPerChannel,
(desc.mFormatFlags & kAudioFormatFlagIsFloat) ? "Float" : "SInt",
(desc.mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
(UInt32)desc.mSampleRate);
}
struct OSXOutput final : AudioOutput { struct OSXOutput final : AudioOutput {
/* configuration settings */ /* configuration settings */
OSType component_subtype; OSType component_subtype;
@ -48,7 +66,6 @@ struct OSXOutput final : AudioOutput {
const char *device_name; const char *device_name;
const char *channel_map; const char *channel_map;
bool hog_device; bool hog_device;
bool sync_sample_rate;
bool pause; bool pause;
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
/** /**
@ -57,13 +74,12 @@ struct OSXOutput final : AudioOutput {
* @see http://dsd-guide.com/dop-open-standard * @see http://dsd-guide.com/dop-open-standard
*/ */
bool dop_setting; bool dop_setting;
Manual<PcmExport> pcm_export;
#endif #endif
AudioDeviceID dev_id; AudioDeviceID dev_id;
AudioComponentInstance au; AudioComponentInstance au;
AudioStreamBasicDescription asbd; AudioStreamBasicDescription asbd;
Float64 initial_sample_rate;
Manual<PcmExport> pcm_export;
boost::lockfree::spsc_queue<uint8_t> *ring_buffer; boost::lockfree::spsc_queue<uint8_t> *ring_buffer;
@ -130,7 +146,6 @@ OSXOutput::OSXOutput(const ConfigBlock &block)
channel_map = block.GetBlockValue("channel_map"); channel_map = block.GetBlockValue("channel_map");
hog_device = block.GetBlockValue("hog_device", false); hog_device = block.GetBlockValue("hog_device", false);
sync_sample_rate = block.GetBlockValue("sync_sample_rate", false);
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
dop_setting = block.GetBlockValue("dop", false); dop_setting = block.GetBlockValue("dop", false);
#endif #endif
@ -311,102 +326,143 @@ osx_output_set_channel_map(OSXOutput *oo)
} }
} }
static float
osx_output_score_sample_rate(Float64 destination_rate, unsigned int source_rate) {
float score = 0;
double int_portion;
double frac_portion = modf(source_rate / destination_rate, &int_portion);
// prefer sample rates that are multiples of the source sample rate
score += (1 - frac_portion) * 1000;
// prefer exact matches over other multiples
score += (int_portion == 1.0) ? 500 : 0;
// prefer higher multiples if source rate higher than dest rate
if(source_rate >= destination_rate)
score += (int_portion > 1 && int_portion < 100) ? (100 - int_portion) / 100 * 100 : 0;
else
score += (int_portion > 1 && int_portion < 100) ? (100 + int_portion) / 100 * 100 : 0;
return score;
}
static float
osx_output_score_format(const AudioStreamBasicDescription &format_desc, const AudioFormat &format) {
float score = 0;
// Score only linear PCM formats (everything else MPD cannot use)
if (format_desc.mFormatID == kAudioFormatLinearPCM) {
score += osx_output_score_sample_rate(format_desc.mSampleRate, format.sample_rate);
// Just choose the stream / format with the highest number of output channels
score += format_desc.mChannelsPerFrame * 5;
if (format.format == SampleFormat::FLOAT) {
// for float, prefer the highest bitdepth we have
if (format_desc.mBitsPerChannel >= 16)
score += (format_desc.mBitsPerChannel / 8);
} else {
if (format_desc.mBitsPerChannel == ((format.format == SampleFormat::S24_P32) ? 24 : format.GetSampleSize() * 8))
score += 5;
else if (format_desc.mBitsPerChannel > format.GetSampleSize() * 8)
score += 1;
}
}
return score;
}
static Float64 static Float64
osx_output_sync_device_sample_rate(AudioDeviceID dev_id, Float64 requested_rate) osx_output_set_device_format(AudioDeviceID dev_id, const AudioFormat &audio_format)
{ {
FormatDebug(osx_output_domain, "Syncing sample rate.");
AudioObjectPropertyAddress aopa = { AudioObjectPropertyAddress aopa = {
kAudioDevicePropertyAvailableNominalSampleRates, kAudioDevicePropertyStreams,
kAudioObjectPropertyScopeOutput, kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMaster kAudioObjectPropertyElementMaster
}; };
UInt32 property_size; UInt32 property_size;
OSStatus err = AudioObjectGetPropertyDataSize(dev_id, OSStatus err = AudioObjectGetPropertyDataSize(dev_id, &aopa, 0, NULL, &property_size);
&aopa, if (err != noErr) {
0, throw FormatRuntimeError("Cannot get number of streams: %d\n", err);
NULL, }
&property_size);
int count = property_size/sizeof(AudioValueRange);
AudioValueRange ranges[count];
property_size = sizeof(ranges);
err = AudioObjectGetPropertyData(dev_id,
&aopa,
0,
NULL,
&property_size,
&ranges);
// Get the maximum and minimum sample rates as fallback. int n_streams = property_size / sizeof(AudioStreamID);
Float64 sample_rate_min = ranges[0].mMinimum; AudioStreamID streams[n_streams];
Float64 sample_rate_max = ranges[0].mMaximum; err = AudioObjectGetPropertyData(dev_id, &aopa, 0, NULL, &property_size, streams);
for (int i = 0; i < count; i++) { if (err != noErr) {
if (ranges[i].mMaximum > sample_rate_max) throw FormatRuntimeError("Cannot get streams: %d\n", err);
sample_rate_max = ranges[i].mMaximum; }
if( ranges[i].mMinimum < sample_rate_min)
sample_rate_min = ranges[i].mMinimum; bool format_found = false;
} int output_stream;
AudioStreamBasicDescription output_format;
// Now try to see if the device support our format sample rate. for (int i = 0; i < n_streams; i++) {
// For some media samples, the frame rate may exceed device UInt32 direction;
// capability. In this case, we downsample or upsample AudioStreamID stream = streams[i];
// with an integer factor ranging from 1 to 4. aopa.mSelector = kAudioStreamPropertyDirection;
Float64 sample_rate = sample_rate_max; property_size = sizeof(direction);
Float64 rate; err = AudioObjectGetPropertyData(stream,
if(requested_rate >= sample_rate_min) { &aopa,
for (int f = 4; f > 0; f--) { 0,
rate = requested_rate / f; NULL,
for (int i = 0; i < count; i++) { &property_size,
if (ranges[i].mMinimum <= rate &direction);
&& rate <= ranges[i].mMaximum) { if (err != noErr) {
sample_rate = rate; throw FormatRuntimeError("Cannot get streams direction: %d\n", err);
break;
}
}
} }
} if (direction != 0) {
else { continue;
sample_rate = sample_rate_min; }
for (int f = 4; f > 1; f--) {
rate = requested_rate * f; aopa.mSelector = kAudioStreamPropertyAvailablePhysicalFormats;
for (int i = 0; i < count; i++) { err = AudioObjectGetPropertyDataSize(stream, &aopa, 0, NULL, &property_size);
if (ranges[i].mMinimum <= rate if (err != noErr)
&& rate <= ranges[i].mMaximum) { throw FormatRuntimeError("Unable to get format size s for stream %d. Error = %s", streams[i], err);
sample_rate = rate;
break; int format_count = property_size / sizeof(AudioStreamRangedDescription);
} AudioStreamRangedDescription format_list[format_count];
err = AudioObjectGetPropertyData(stream, &aopa, 0, NULL, &property_size, format_list);
if (err != noErr)
throw FormatRuntimeError("Unable to get available formats for stream %d. Error = %s", streams[i], err);
float output_score = 0;
for (int j = 0; j < format_count; j++) {
AudioStreamBasicDescription format_desc = format_list[j].mFormat;
std::string format_string;
// for devices with kAudioStreamAnyRate
// we use the requested samplerate here
if (format_desc.mSampleRate == kAudioStreamAnyRate)
format_desc.mSampleRate = audio_format.sample_rate;
float score = osx_output_score_format(format_desc, audio_format);
// print all (linear pcm) formats and their rating
if(score > 0.0)
FormatDebug(osx_output_domain, "Format: %s rated %f", StreamDescriptionToString(format_desc).c_str(), score);
if (score > output_score) {
output_score = score;
output_format = format_desc;
output_stream = stream; // set the idx of the stream in the device
format_found = true;
} }
} }
} }
aopa.mSelector = kAudioDevicePropertyNominalSampleRate, if (format_found) {
property_size = sizeof(sample_rate); aopa.mSelector = kAudioStreamPropertyPhysicalFormat;
err = AudioObjectSetPropertyData(dev_id, err = AudioObjectSetPropertyData(output_stream,
&aopa, &aopa,
0, 0,
NULL, NULL,
property_size, sizeof(output_format),
&sample_rate); &output_format);
if (err != noErr) { if (err != noErr) {
FormatWarning(osx_output_domain, throw FormatRuntimeError("Failed to change the stream format: %d\n", err);
"Failed to synchronize the sample rate: %d", }
err); }
// Something went wrong with synchronization, get current device sample_rate and return that
err = AudioObjectGetPropertyData(dev_id, return output_format.mSampleRate;
&aopa,
0,
NULL,
&property_size,
&sample_rate);
if(err != noErr)
throw std::runtime_error("Cannot get sample rate of macOS output device");
} else {
FormatDebug(osx_output_domain,
"Sample rate synced to %f Hz.",
sample_rate);
}
return sample_rate;
} }
static OSStatus static OSStatus
@ -652,34 +708,20 @@ OSXOutput::Enable()
throw FormatRuntimeError("Unable to open OS X component: %s", throw FormatRuntimeError("Unable to open OS X component: %s",
errormsg); errormsg);
} }
#ifdef ENABLE_DSD
pcm_export.Construct(); pcm_export.Construct();
#endif
try { try {
osx_output_set_device(this); osx_output_set_device(this);
} catch (...) { } catch (...) {
AudioComponentInstanceDispose(au); AudioComponentInstanceDispose(au);
#ifdef ENABLE_DSD
pcm_export.Destruct(); pcm_export.Destruct();
#endif
throw; throw;
} }
AudioObjectPropertyAddress aopa = {
kAudioDevicePropertyNominalSampleRate,
kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
UInt32 property_size = sizeof(initial_sample_rate);
status = AudioObjectGetPropertyData(dev_id,
&aopa,
0,
NULL,
&property_size,
&initial_sample_rate);
if(status != noErr) {
AudioComponentInstanceDispose(au);
pcm_export.Destruct();
throw std::runtime_error("Cannot get sample rate of macOS output device");
}
if (hog_device) if (hog_device)
osx_output_hog_device(dev_id, true); osx_output_hog_device(dev_id, true);
} }
@ -688,7 +730,9 @@ void
OSXOutput::Disable() noexcept OSXOutput::Disable() noexcept
{ {
AudioComponentInstanceDispose(au); AudioComponentInstanceDispose(au);
#ifdef ENABLE_DSD
pcm_export.Destruct(); pcm_export.Destruct();
#endif
if (hog_device) if (hog_device)
osx_output_hog_device(dev_id, false); osx_output_hog_device(dev_id, false);
@ -697,29 +741,8 @@ OSXOutput::Disable() noexcept
void void
OSXOutput::Close() noexcept OSXOutput::Close() noexcept
{ {
AudioObjectPropertyAddress aopa = {
kAudioDevicePropertyNominalSampleRate,
kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
OSStatus err;
AudioOutputUnitStop(au); AudioOutputUnitStop(au);
AudioUnitUninitialize(au); AudioUnitUninitialize(au);
// Reset sample rate to initial state
if(sync_sample_rate
#ifdef ENABLE_DSD
|| dop_setting
#endif
) {
err = AudioObjectSetPropertyData(dev_id,
&aopa,
0,
NULL,
sizeof(initial_sample_rate),
&initial_sample_rate);
if(err != noErr)
FormatWarning(osx_output_domain, "Unable to reset sample rate of macOS output device");
}
delete ring_buffer; delete ring_buffer;
} }
@ -727,10 +750,9 @@ void
OSXOutput::Open(AudioFormat &audio_format) OSXOutput::Open(AudioFormat &audio_format)
{ {
char errormsg[1024]; char errormsg[1024];
Float64 sample_rate = initial_sample_rate; #ifdef ENABLE_DSD
PcmExport::Params params; PcmExport::Params params;
params.alsa_channel_order = true; params.alsa_channel_order = true;
#ifdef ENABLE_DSD
bool dop = dop_setting; bool dop = dop_setting;
params.dop = false; params.dop = false;
#endif #endif
@ -748,6 +770,10 @@ OSXOutput::Open(AudioFormat &audio_format)
asbd.mBitsPerChannel = 16; asbd.mBitsPerChannel = 16;
break; break;
case SampleFormat::S24_P32:
asbd.mBitsPerChannel = 24;
break;
case SampleFormat::S32: case SampleFormat::S32:
asbd.mBitsPerChannel = 32; asbd.mBitsPerChannel = 32;
break; break;
@ -766,25 +792,23 @@ OSXOutput::Open(AudioFormat &audio_format)
asbd.mBitsPerChannel = 32; asbd.mBitsPerChannel = 32;
break; break;
} }
#ifdef ENABLE_DSD
asbd.mSampleRate = params.CalcOutputSampleRate(audio_format.sample_rate); asbd.mSampleRate = params.CalcOutputSampleRate(audio_format.sample_rate);
#endif
if (IsBigEndian()) if (IsBigEndian())
asbd.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; asbd.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
asbd.mBytesPerPacket = audio_format.GetFrameSize();
#ifdef ENABLE_DSD
if (audio_format.format == SampleFormat::DSD) if (audio_format.format == SampleFormat::DSD)
asbd.mBytesPerPacket = 4 * audio_format.channels; asbd.mBytesPerPacket = 4 * audio_format.channels;
else #endif
asbd.mBytesPerPacket = audio_format.GetFrameSize();
asbd.mFramesPerPacket = 1; asbd.mFramesPerPacket = 1;
asbd.mBytesPerFrame = asbd.mBytesPerPacket; asbd.mBytesPerFrame = asbd.mBytesPerPacket;
asbd.mChannelsPerFrame = audio_format.channels; asbd.mChannelsPerFrame = audio_format.channels;
if (sync_sample_rate Float64 sample_rate = osx_output_set_device_format(dev_id, audio_format);
#ifdef ENABLE_DSD
|| params.dop // sample rate needs to be synchronized for DoP
#endif
)
sample_rate = osx_output_sync_device_sample_rate(dev_id, asbd.mSampleRate);
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
if(params.dop && (sample_rate != asbd.mSampleRate)) { // fall back to PCM in case sample_rate cannot be synchronized if(params.dop && (sample_rate != asbd.mSampleRate)) { // fall back to PCM in case sample_rate cannot be synchronized
@ -835,8 +859,13 @@ OSXOutput::Open(AudioFormat &audio_format)
} }
pcm_export->Open(audio_format.format, audio_format.channels, params); pcm_export->Open(audio_format.format, audio_format.channels, params);
#ifdef ENABLE_DSD
size_t ring_buffer_size = std::max<size_t>(buffer_frame_size, size_t ring_buffer_size = std::max<size_t>(buffer_frame_size,
MPD_OSX_BUFFER_TIME_MS * pcm_export->GetFrameSize(audio_format) * asbd.mSampleRate / 1000); MPD_OSX_BUFFER_TIME_MS * pcm_export->GetFrameSize(audio_format) * asbd.mSampleRate / 1000);
#else
size_t ring_buffer_size = std::max<size_t>(buffer_frame_size,
- MPD_OSX_BUFFER_TIME_MS * audio_format.GetFrameSize() * audio_format.sample_rate / 1000);
#endif
ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(ring_buffer_size); ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(ring_buffer_size);
status = AudioOutputUnitStart(au); status = AudioOutputUnitStart(au);
@ -861,6 +890,7 @@ OSXOutput::Play(const void *chunk, size_t size)
throw std::runtime_error("Unable to restart audio output after pause"); throw std::runtime_error("Unable to restart audio output after pause");
} }
} }
#ifdef ENABLE_DSD
const auto e = pcm_export->Export({chunk, size}); const auto e = pcm_export->Export({chunk, size});
if (e.size == 0) if (e.size == 0)
/* the DoP (DSD over PCM) filter converts two frames /* the DoP (DSD over PCM) filter converts two frames
@ -874,6 +904,8 @@ OSXOutput::Play(const void *chunk, size_t size)
size_t bytes_written = ring_buffer->push((const uint8_t *)e.data, size_t bytes_written = ring_buffer->push((const uint8_t *)e.data,
e.size); e.size);
return pcm_export->CalcSourceSize(bytes_written); return pcm_export->CalcSourceSize(bytes_written);
#endif
return ring_buffer->push((const uint8_t *)chunk, size);
} }
std::chrono::steady_clock::duration std::chrono::steady_clock::duration