Skip to content

Commit

Permalink
miniaudio: Add config.coreaudio.allowNominalSampleRateChange
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Oct 21, 2024
1 parent 5312af4 commit 7f754c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion source/src/node/io/input/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void read_callback(ma_device *pDevice,
AudioIn::AudioIn(unsigned int num_channels)
: AudioIn_Abstract()
{
shared_in = this;
this->name = "audioin-miniaudio";
this->num_channels = num_channels;
this->init();
Expand All @@ -59,7 +60,7 @@ void AudioIn::init()
ma_device_config config = ma_device_config_init(ma_device_type_capture);
config.capture.format = ma_format_f32;
config.capture.channels = this->num_channels;
config.periodSizeInFrames = 0;
config.periodSizeInFrames = this->get_graph()->get_output_buffer_size();
config.sampleRate = this->get_graph()->get_sample_rate();
config.dataCallback = read_callback;

Expand All @@ -71,6 +72,12 @@ void AudioIn::init()

this->set_channels(0, device.capture.internalChannels);

/*--------------------------------------------------------------------------------
* Note that the underlying sample rate used by the recording hardware
* (`device.capture.internalSampleRate`) may not be the same as that used
* by `AudioIn`: SignalFlow requires that the input and output streams are both
* on the same sample rate, so miniaudio's resampling is used to unify them.
*-------------------------------------------------------------------------------*/
std::string s = device.capture.internalChannels == 1 ? "" : "s";
std::cerr << "[miniaudio] Input device: " << std::string(device.capture.name) << " (" << device.capture.internalSampleRate << "Hz, "
<< "buffer size " << device.capture.internalPeriodSizeInFrames << " samples, " << device.capture.internalChannels << " channel" << s << ")"
Expand Down
10 changes: 9 additions & 1 deletion source/src/node/io/output/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ void AudioOut::init()
// Buffer blocks into a fixed number of frames
config.noFixedSizedCallback = 1;

// On Core Audio, let the application select a preferred sample rate.
config.coreaudio.allowNominalSampleRateChange = 1;

rv = ma_device_init(NULL, &config, &device);
if (rv != MA_SUCCESS)
{
Expand All @@ -168,9 +171,14 @@ void AudioOut::init()
this->sample_rate = device.playback.internalSampleRate;
}

/*--------------------------------------------------------------------------------
* Update AudioOut's buffer size to reflect the actual underlying buffer size.
*-------------------------------------------------------------------------------*/
this->buffer_size = device.playback.internalPeriodSizeInFrames;

std::string s = device.playback.internalChannels == 1 ? "" : "s";
std::cerr << "[miniaudio] Output device: " << std::string(device.playback.name) << " (" << device.playback.internalSampleRate << "Hz, "
<< "buffer size " << device.playback.internalPeriodSizeInFrames << " samples, " << device.playback.internalChannels << " channel" << s << ")"
<< "buffer size " << this->buffer_size << " samples, " << device.playback.internalChannels << " channel" << s << ")"
<< std::endl;
}

Expand Down

0 comments on commit 7f754c4

Please sign in to comment.