From 45c620a996880da4b4aa29a79305f0dd66a93fb3 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 14 Jan 2015 11:01:02 +0000 Subject: [PATCH] Fix for ALSA CPU use when using an input device without an output device. --- .../native/juce_linux_ALSA.cpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_linux_ALSA.cpp b/modules/juce_audio_devices/native/juce_linux_ALSA.cpp index fbb4010088..edbe361e08 100644 --- a/modules/juce_audio_devices/native/juce_linux_ALSA.cpp +++ b/modules/juce_audio_devices/native/juce_linux_ALSA.cpp @@ -149,7 +149,7 @@ class ALSADevice { public: ALSADevice (const String& devID, bool forInput) - : handle (0), + : handle (nullptr), bitDepth (16), numChannelsRunning (0), latency (0), @@ -183,16 +183,16 @@ public: void closeNow() { - if (handle != 0) + if (handle != nullptr) { snd_pcm_close (handle); - handle = 0; + handle = nullptr; } } bool setParameters (unsigned int sampleRate, int numChannels, int bufferSize) { - if (handle == 0) + if (handle == nullptr) return false; JUCE_ALSA_LOG ("ALSADevice::setParameters(" << deviceID << ", " @@ -644,8 +644,21 @@ public: { while (! threadShouldExit()) { - if (inputDevice != nullptr && inputDevice->handle) + if (inputDevice != nullptr && inputDevice->handle != nullptr) { + if (outputDevice == nullptr || outputDevice->handle == nullptr) + { + JUCE_ALSA_FAILED (snd_pcm_wait (inputDevice->handle, 2000)); + + if (threadShouldExit()) + break; + + snd_pcm_sframes_t avail = snd_pcm_avail_update (inputDevice->handle); + + if (avail < 0) + JUCE_ALSA_FAILED (snd_pcm_recover (inputDevice->handle, avail, 0)); + } + audioIoInProgress = true; if (! inputDevice->readFromInputDevice (inputChannelBuffer, bufferSize)) @@ -679,7 +692,7 @@ public: } } - if (outputDevice != nullptr && outputDevice->handle) + if (outputDevice != nullptr && outputDevice->handle != nullptr) { JUCE_ALSA_FAILED (snd_pcm_wait (outputDevice->handle, 2000)); @@ -702,6 +715,7 @@ public: audioIoInProgress = false; } } + audioIoInProgress = false; }