From 06a71ccce08369641ae6a1bf1d66944b8201ce56 Mon Sep 17 00:00:00 2001 From: reuk Date: Tue, 8 Nov 2022 16:08:39 +0000 Subject: [PATCH] Android: Fix file loading in DSP demos --- examples/Assets/DSPDemos_Common.h | 58 ++++++++++-------------------- examples/Assets/DemoUtilities.h | 15 ++++++++ examples/Audio/AudioPlaybackDemo.h | 16 --------- 3 files changed, 34 insertions(+), 55 deletions(-) diff --git a/examples/Assets/DSPDemos_Common.h b/examples/Assets/DSPDemos_Common.h index 49ccc27872..9fa1d5c3a0 100644 --- a/examples/Assets/DSPDemos_Common.h +++ b/examples/Assets/DSPDemos_Common.h @@ -25,7 +25,7 @@ using namespace dsp; struct DSPDemoParameterBase : public ChangeBroadcaster { DSPDemoParameterBase (const String& labelName) : name (labelName) {} - virtual ~DSPDemoParameterBase() {} + virtual ~DSPDemoParameterBase() = default; virtual Component* getComponent() = 0; @@ -142,7 +142,7 @@ public: loadURL (u); } - URL getCurrentURL() { return currentURL; } + URL getCurrentURL() const { return currentURL; } void setTransportSource (AudioTransportSource* newSource) { @@ -189,21 +189,7 @@ private: currentURL = u; - InputSource* inputSource = nullptr; - - #if ! JUCE_IOS - if (u.isLocalFile()) - { - inputSource = new FileInputSource (u.getLocalFile()); - } - else - #endif - { - if (inputSource == nullptr) - inputSource = new URLInputSource (u); - } - - thumbnail.setSource (inputSource); + thumbnail.setSource (makeInputSource (u).release()); if (notify) sendChangeMessage(); @@ -407,33 +393,27 @@ public: transportSource.reset(); readerSource.reset(); - AudioFormatReader* newReader = nullptr; + auto source = makeInputSource (fileToPlay); - #if ! JUCE_IOS - if (fileToPlay.isLocalFile()) - { - newReader = formatManager.createReaderFor (fileToPlay.getLocalFile()); - } - else - #endif - { - if (newReader == nullptr) - newReader = formatManager.createReaderFor (fileToPlay.createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress))); - } + if (source == nullptr) + return false; - reader.reset (newReader); + auto stream = rawToUniquePtr (source->createInputStream()); - if (reader.get() != nullptr) - { - readerSource.reset (new AudioFormatReaderSource (reader.get(), false)); - readerSource->setLooping (loopState.getValue()); + if (stream == nullptr) + return false; - init(); + reader = rawToUniquePtr (formatManager.createReaderFor (std::move (stream))); - return true; - } + if (reader == nullptr) + return false; - return false; + readerSource.reset (new AudioFormatReaderSource (reader.get(), false)); + readerSource->setLooping (loopState.getValue()); + + init(); + + return true; } void togglePlay() @@ -613,7 +593,7 @@ private: { if (fc.getURLResults().size() > 0) { - auto u = fc.getURLResult(); + const auto u = fc.getURLResult(); if (! audioFileReader.loadURL (u)) NativeMessageBox::showAsync (MessageBoxOptions() diff --git a/examples/Assets/DemoUtilities.h b/examples/Assets/DemoUtilities.h index bfc1c5d286..ffdfc3631c 100644 --- a/examples/Assets/DemoUtilities.h +++ b/examples/Assets/DemoUtilities.h @@ -242,4 +242,19 @@ struct SlowerBouncingNumber : public BouncingNumber } }; +inline std::unique_ptr makeInputSource (const URL& url) +{ + #if JUCE_ANDROID + if (auto doc = AndroidDocument::fromDocument (url)) + return std::make_unique (doc); + #endif + + #if ! JUCE_IOS + if (url.isLocalFile()) + return std::make_unique (url.getLocalFile()); + #endif + + return std::make_unique (url); +} + #endif // PIP_DEMO_UTILITIES_INCLUDED diff --git a/examples/Audio/AudioPlaybackDemo.h b/examples/Audio/AudioPlaybackDemo.h index 54cbbcc42c..6d317f4466 100644 --- a/examples/Audio/AudioPlaybackDemo.h +++ b/examples/Audio/AudioPlaybackDemo.h @@ -48,22 +48,6 @@ #include "../Assets/DemoUtilities.h" -inline std::unique_ptr makeInputSource (const URL& url) -{ - #if JUCE_ANDROID - if (auto doc = AndroidDocument::fromDocument (url)) - return std::make_unique (doc); - #endif - - #if ! JUCE_IOS - if (url.isLocalFile()) - return std::make_unique (url.getLocalFile()); - #endif - - return std::make_unique (url); -} - -//============================================================================== class DemoThumbnailComp : public Component, public ChangeListener, public FileDragAndDropTarget,