From 06c7fb5b0199dcaa36e21cde9ef1b2c726c187d6 Mon Sep 17 00:00:00 2001 From: hogliux Date: Tue, 26 Sep 2017 11:01:27 +0100 Subject: [PATCH] Add xrun counter to device manager --- .../audio_io/juce_AudioDeviceManager.cpp | 13 ++++++++++++- .../audio_io/juce_AudioDeviceManager.h | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp index 6f0be5db75..957f482087 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.cpp @@ -731,6 +731,9 @@ void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelDat const double msTaken = Time::getMillisecondCounterHiRes() - callbackStartTime; const double filterAmount = 0.2; cpuUsageMs += filterAmount * (msTaken - cpuUsageMs); + + if (msTaken > msPerBlock) + xruns++; } else { @@ -756,13 +759,14 @@ void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelDat void AudioDeviceManager::audioDeviceAboutToStartInt (AudioIODevice* const device) { cpuUsageMs = 0; + xruns = 0; const double sampleRate = device->getCurrentSampleRate(); const int blockSize = device->getCurrentBufferSizeSamples(); if (sampleRate > 0.0 && blockSize > 0) { - const double msPerBlock = 1000.0 * blockSize / sampleRate; + msPerBlock = 1000.0 * blockSize / sampleRate; timeToCpuScale = (msPerBlock > 0.0) ? (1.0 / msPerBlock) : 0.0; } @@ -779,6 +783,7 @@ void AudioDeviceManager::audioDeviceStoppedInt() { cpuUsageMs = 0; timeToCpuScale = 0; + xruns = 0; sendChangeMessage(); const ScopedLock sl (audioCallbackLock); @@ -998,6 +1003,12 @@ void AudioDeviceManager::playTestSound() } } +int AudioDeviceManager::getXRunCount() const noexcept +{ + auto deviceXRuns = (currentAudioDevice != nullptr ? currentAudioDevice->getXRunCount() : -1); + return (deviceXRuns >= 0 ? deviceXRuns : xruns); +} + double AudioDeviceManager::getCurrentInputLevel() const noexcept { return inputLevelMeter.getCurrentLevel(); } double AudioDeviceManager::getCurrentOutputLevel() const noexcept { return outputLevelMeter.getCurrentLevel(); } diff --git a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h index 5d7ae84038..6e5d682545 100644 --- a/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h +++ b/modules/juce_audio_devices/audio_io/juce_AudioDeviceManager.h @@ -436,6 +436,15 @@ public: */ CriticalSection& getMidiCallbackLock() noexcept { return midiCallbackLock; } + //============================================================================== + /** Returns the number of under- or over runs reported. + + This method will use the underlying device's native getXRunCount if it supports + it. Otherwise it will estimate the number of under-/overruns by measuring the + time it spent in the audio callback. + */ + int getXRunCount() const noexcept; + private: //============================================================================== OwnedArray availableDeviceTypes; @@ -468,7 +477,8 @@ private: ScopedPointer testSound; int testSoundPosition; - double cpuUsageMs, timeToCpuScale; + double cpuUsageMs, timeToCpuScale, msPerBlock; + int xruns; struct LevelMeter {