Browse Source

Initialize sample rate and block size of RtAudioDevice in constructor instead of openStream(). Fix algorithm to find closest allowed sample rate of device.

tags/v2.0.6
Andrew Belt 2 years ago
parent
commit
1a973d685e
1 changed files with 15 additions and 18 deletions
  1. +15
    -18
      src/rtaudio.cpp

+ 15
- 18
src/rtaudio.cpp View File

@@ -40,13 +40,21 @@ struct RtAudioDevice : audio::Device {
RtAudio::StreamParameters inputParameters; RtAudio::StreamParameters inputParameters;
RtAudio::StreamParameters outputParameters; RtAudio::StreamParameters outputParameters;
RtAudio::StreamOptions options; RtAudio::StreamOptions options;
int blockSize = 0;
float sampleRate = 0;
int blockSize;
float sampleRate;


RtAudioDevice(RtAudio::Api api, int deviceId) { RtAudioDevice(RtAudio::Api api, int deviceId) {
this->api = api; this->api = api;
this->deviceId = deviceId; this->deviceId = deviceId;


sampleRate = 44100;

// DirectSound latency is too high for 256 block size.
if (api == RtAudio::WINDOWS_DS)
blockSize = 1024;
else
blockSize = 256;

// Create RtAudio object // Create RtAudio object
INFO("Creating RtAudio %s context", RTAUDIO_API_NAMES.at(api).c_str()); INFO("Creating RtAudio %s context", RTAUDIO_API_NAMES.at(api).c_str());
try { try {
@@ -102,25 +110,14 @@ struct RtAudioDevice : audio::Device {
options.numberOfBuffers = 2; options.numberOfBuffers = 2;
options.streamName = "VCV Rack"; options.streamName = "VCV Rack";


// Most people prefer 44100 default sample rate although many devices report 48000 from `deviceInfo.preferredSampleRate`.
float closestSampleRate = 44100;
if (sampleRate > 0) {
// Find the closest sample rate to the requested one.
for (float sr : deviceInfo.sampleRates) {
if (std::fabs(sr - sampleRate) < std::fabs(closestSampleRate - sampleRate)) {
closestSampleRate = sr;
}
float closestSampleRate = INFINITY;
// Find the closest sample rate to the requested one.
for (float sr : deviceInfo.sampleRates) {
if (std::fabs(sr - sampleRate) < std::fabs(closestSampleRate - sampleRate)) {
closestSampleRate = sr;
} }
} }


if (blockSize <= 0) {
// DirectSound should use a higher default block size
if (api == RtAudio::WINDOWS_DS)
blockSize = 1024;
else
blockSize = 256;
}

INFO("Opening RtAudio %s device %d: %s (%d in, %d out, %g sample rate, %d block size)", RTAUDIO_API_NAMES.at(api).c_str(), deviceId, deviceInfo.name.c_str(), inputParameters.nChannels, outputParameters.nChannels, closestSampleRate, blockSize); INFO("Opening RtAudio %s device %d: %s (%d in, %d out, %g sample rate, %d block size)", RTAUDIO_API_NAMES.at(api).c_str(), deviceId, deviceInfo.name.c_str(), inputParameters.nChannels, outputParameters.nChannels, closestSampleRate, blockSize);
try { try {
rtAudio->openStream( rtAudio->openStream(


Loading…
Cancel
Save