/* ============================================================================== This file is part of the JUCE examples. Copyright (c) 2020 - 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" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ #pragma once using namespace dsp; //============================================================================== struct DSPDemoParameterBase : public ChangeBroadcaster { DSPDemoParameterBase (const String& labelName) : name (labelName) {} virtual ~DSPDemoParameterBase() {} virtual Component* getComponent() = 0; virtual int getPreferredHeight() = 0; virtual int getPreferredWidth() = 0; String name; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DSPDemoParameterBase) }; //============================================================================== struct SliderParameter : 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 : 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 : 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() { return currentURL; } void setTransportSource (AudioTransportSource* newSource) { transportSource = newSource; struct ResetCallback : 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; 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); 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 : 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