diff --git a/src/JackServerSettings.hpp b/src/JackServerSettings.hpp index f2b28c8..fcee6e0 100644 --- a/src/JackServerSettings.hpp +++ b/src/JackServerSettings.hpp @@ -59,6 +59,7 @@ namespace pipedal } uint64_t GetSampleRate() const { return sampleRate_; } + void SetSampleRate(uint64_t sampleRate) { sampleRate_ = sampleRate; } uint32_t GetBufferSize() const { return bufferSize_; } uint32_t GetNumberOfBuffers() const { return numberOfBuffers_; } const std::string &GetAlsaInputDevice() const { return alsaInputDevice_; } diff --git a/src/PiPedalModel.cpp b/src/PiPedalModel.cpp index b4f51fa..fdd1acd 100644 --- a/src/PiPedalModel.cpp +++ b/src/PiPedalModel.cpp @@ -1376,6 +1376,31 @@ void PiPedalModel::RestartAudio(bool useDummyAudioDriver) { jackServerSettings.UseDummyAudioDevice(); } + else + { + auto devices = GetAlsaDevices(); + AlsaDeviceInfo inDev, outDev; + bool inFound = false, outFound = false; + for (auto &d : devices) + { + if (d.id_ == jackServerSettings.GetAlsaInputDevice()) + { + inDev = d; + inFound = true; + } + if (d.id_ == jackServerSettings.GetAlsaOutputDevice()) + { + outDev = d; + outFound = true; + } + } + if (inFound && outFound) + { + uint32_t best = SelectBestSampleRate(inDev, outDev, (uint32_t)jackServerSettings.GetSampleRate()); + jackServerSettings.SetSampleRate(best); + this->jackServerSettings.SetSampleRate(best); + } + } auto jackConfiguration = this->jackConfiguration; jackConfiguration.AlsaInitialize(jackServerSettings); @@ -2747,6 +2772,31 @@ static bool HasAlsaDevice(const std::vector devices, const std:: return false; } +static uint32_t SelectBestSampleRate(const AlsaDeviceInfo &inDev, const AlsaDeviceInfo &outDev, uint32_t desiredRate) +{ + std::vector intersection; + for (auto sr : inDev.sampleRates_) + { + if (std::find(outDev.sampleRates_.begin(), outDev.sampleRates_.end(), sr) != outDev.sampleRates_.end()) + { + intersection.push_back(sr); + } + } + if (intersection.empty()) return desiredRate; + if (desiredRate != 0 && std::find(intersection.begin(), intersection.end(), desiredRate) != intersection.end()) + { + return desiredRate; + } + uint32_t best = 0; + for (auto sr : intersection) + { + if (sr == 48000) return 48000; + if (sr <= 48000 && sr > best) best = sr; + } + if (best == 0) best = intersection.back(); + return best; +} + void PiPedalModel::StartHotspotMonitoring() { this->avahiService = std::make_unique();