/* ============================================================================== This file is part of the JUCE framework examples. Copyright (c) Raw Material Software Limited The code included in this file is provided under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ============================================================================== */ #pragma once using namespace dsp; //============================================================================== struct DSPDemoParameterBase : public ChangeBroadcaster { DSPDemoParameterBase (const String& labelName) : name (labelName) {} virtual ~DSPDemoParameterBase() = default; virtual Component* getComponent() = 0; virtual int getPreferredHeight() = 0; virtual int getPreferredWidth() = 0; String name; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DSPDemoParameterBase) }; //============================================================================== struct SliderParameter final : public DSPDemoParameterBase { SliderParameter (Range range, double skew, double initialValue, const String& labelName, const String& suffix = {}) : DSPDemoParameterBase (labelName) { slider.setRange (range.getStart(), range.getEnd(), 0.01); slider.setSkewFactor (skew); slider.setValue (initialValue); if (suffix.isNotEmpty()) slider.setTextValueSuffix (suffix); slider.onValueChange = [this] { sendChangeMessage(); }; } Component* getComponent() override { return &slider; } int getPreferredHeight() override { return 40; } int getPreferredWidth() override { return 500; } double getCurrentValue() const { return slider.getValue(); } private: Slider slider; }; //============================================================================== struct ChoiceParameter final : public DSPDemoParameterBase { ChoiceParameter (const StringArray& options, int initialId, const String& labelName) : DSPDemoParameterBase (labelName) { parameterBox.addItemList (options, 1); parameterBox.onChange = [this] { sendChangeMessage(); }; parameterBox.setSelectedId (initialId); } Component* getComponent() override { return ¶meterBox; } int getPreferredHeight() override { return 25; } int getPreferredWidth() override { return 250; } int getCurrentSelectedID() const { return parameterBox.getSelectedId(); } private: ComboBox parameterBox; }; //============================================================================== class AudioThumbnailComponent final : public Component, public FileDragAndDropTarget, public ChangeBroadcaster, private ChangeListener, private Timer { public: AudioThumbnailComponent (AudioDeviceManager& adm, AudioFormatManager& afm) : audioDeviceManager (adm), thumbnailCache (5), thumbnail (128, afm, thumbnailCache) { thumbnail.addChangeListener (this); } ~AudioThumbnailComponent() override { thumbnail.removeChangeListener (this); } void paint (Graphics& g) override { g.fillAll (Colour (0xff495358)); g.setColour (Colours::white); if (thumbnail.getTotalLength() > 0.0) { thumbnail.drawChannels (g, getLocalBounds().reduced (2), 0.0, thumbnail.getTotalLength(), 1.0f); g.setColour (Colours::black); g.fillRect (static_cast (currentPosition * getWidth()), 0.0f, 1.0f, static_cast (getHeight())); } else { g.drawFittedText ("No audio file loaded.\nDrop a file here or click the \"Load File...\" button.", getLocalBounds(), Justification::centred, 2); } } bool isInterestedInFileDrag (const StringArray&) override { return true; } void filesDropped (const StringArray& files, int, int) override { loadURL (URL (File (files[0])), true); } void setCurrentURL (const URL& u) { if (currentURL == u) return; loadURL (u); } URL getCurrentURL() const { return currentURL; } void setTransportSource (AudioTransportSource* newSource) { transportSource = newSource; struct ResetCallback final : public CallbackMessage { ResetCallback (AudioThumbnailComponent& o) : owner (o) {} void messageCallback() override { owner.reset(); } AudioThumbnailComponent& owner; }; (new ResetCallback (*this))->post(); } private: AudioDeviceManager& audioDeviceManager; AudioThumbnailCache thumbnailCache; AudioThumbnail thumbnail; AudioTransportSource* transportSource = nullptr; URL currentURL; double currentPosition = 0.0; //============================================================================== void changeListenerCallback (ChangeBroadcaster*) override { repaint(); } void reset() { currentPosition = 0.0; repaint(); if (transportSource == nullptr) stopTimer(); else startTimerHz (25); } void loadURL (const URL& u, bool notify = false) { if (currentURL == u) return; currentURL = u; thumbnail.setSource (makeInputSource (u).release()); if (notify) sendChangeMessage(); } void timerCallback() override { if (transportSource != nullptr) { currentPosition = transportSource->getCurrentPosition() / thumbnail.getTotalLength(); repaint(); } } void mouseDrag (const MouseEvent& e) override { if (transportSource != nullptr) { const ScopedLock sl (audioDeviceManager.getAudioCallbackLock()); transportSource->setPosition ((jmax (static_cast (e.x), 0.0) / getWidth()) * thumbnail.getTotalLength()); } } }; //============================================================================== class DemoParametersComponent final : public Component { public: DemoParametersComponent (const std::vector& demoParams) { parameters = demoParams; for (auto demoParameter : parameters) { addAndMakeVisible (demoParameter->getComponent()); auto* paramLabel = new Label ({}, demoParameter->name); paramLabel->attachToComponent (demoParameter->getComponent(), true); paramLabel->setJustificationType (Justification::centredLeft); addAndMakeVisible (paramLabel); labels.add (paramLabel); } } void resized() override { auto bounds = getLocalBounds(); bounds.removeFromLeft (100); for (auto* p : parameters) { auto* comp = p->getComponent(); comp->setSize (jmin (bounds.getWidth(), p->getPreferredWidth()), p->getPreferredHeight()); auto compBounds = bounds.removeFromTop (p->getPreferredHeight()); comp->setCentrePosition (compBounds.getCentre()); } } int getHeightNeeded() { auto height = 0; for (auto* p : parameters) height += p->getPreferredHeight(); return height + 10; } private: std::vector parameters; OwnedArray