shithub: leaf

Download patch

ref: 7ea48646e9c3b03b99da97ed99f6080b993b9d36
parent: be773ed33f71a0faa6d518c50b44eed4524c3522
author: Matthew Wang <mjw7@princeton.edu>
date: Thu Jan 21 13:10:21 EST 2021

add wav file loading to test JUCE project

--- a/TestPlugin/Source/LEAFLink.cpp
+++ b/TestPlugin/Source/LEAFLink.cpp
@@ -15,7 +15,7 @@
 
 std::vector<juce::String> cButtonNames =  std::vector<juce::String>
 {
-    
+    "load"
 };
 
 std::vector<juce::String> cSliderNames =  std::vector<juce::String>
@@ -40,6 +40,8 @@
 std::vector<bool> cButtonStates(cButtonNames.size());
 std::vector<int> cComboBoxStates(cComboBoxNames.size());
 
+Array<AudioBuffer<float>> loadedAudio;
+
 void setSliderModelValue(String name, float val)
 {
     for (int i = 0; i < cSliderNames.size(); i++)
@@ -142,3 +144,4 @@
 {
     return ((float)rand()/RAND_MAX);
 }
+
--- a/TestPlugin/Source/LEAFLink.h
+++ b/TestPlugin/Source/LEAFLink.h
@@ -39,6 +39,8 @@
 
 extern std::vector<int> cComboBoxStates;
 
+extern Array<AudioBuffer<float>> loadedAudio;
+
 void printSliderValues(void);
 bool getButtonState(String name);
 void setButtonState(String name, bool on);
--- a/TestPlugin/Source/MyTest.cpp
+++ b/TestPlugin/Source/MyTest.cpp
@@ -38,6 +38,9 @@
 tBuffer samp;
 tMBSampler sampler;
 
+const int numWavetables = 1;
+tWavetable wavetables[numWavetables];
+
 float gain;
 float dtime;
 bool buttonState;
@@ -54,6 +57,8 @@
 #define MSIZE 2048000
 char memory[MSIZE];
 
+int lastLoadedAudioSize = 0;
+
 void    LEAFTest_init            (float sampleRate, int blockSize)
 {
     LEAF_init(&leaf, sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
@@ -79,6 +84,9 @@
     
     tWaveset_init(&ws, set, 4, 2048, 10000.f, &leaf);
     tWaveset_setIndexGain(&ws, 0, -1.0f);
+    
+    lastLoadedAudioSize = 0;
+    loadedAudio.clear();
 }
 
 inline double getSawFall(double angle) {
@@ -92,12 +100,12 @@
 
 float   LEAFTest_tick            (float input)
 {
-//    return tRetune_tick(&retune, input)[0];
-//    return tSimpleRetune_tick(&sretune, input);
-//    tMBPulse_sync(&bpulse, tPhasor_tick(&phasor) * 2.f - 1.f);
-//    return tMBPulse_tick(&bpulse);
-//    return tWavetable_tick(&wt);
-    return tWaveset_tick(&ws);
+    float out = 0.0f;
+    for (int i = 0; i < fmin(loadedAudio.size(), numWavetables); ++i)
+    {
+        out += tWavetable_tick(&wavetables[i]);
+    }
+    return out;
 }
 
 int firstFrame = 1;
@@ -121,6 +129,15 @@
     val = getSliderValue("slider3");
 //    tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 2);
         
+    
+    if (lastLoadedAudioSize < loadedAudio.size())
+    {
+        int i = (loadedAudio.size() - 1) % numWavetables;
+        if (loadedAudio.size() - 1 >= numWavetables) tWavetable_free(&wavetables[i]);
+        tWavetable_init(&wavetables[i], loadedAudio[loadedAudio.size() - 1].getReadPointer(0), loadedAudio[loadedAudio.size() - 1].getNumSamples(), 20000, &leaf);
+        
+        lastLoadedAudioSize = loadedAudio.size();
+    }
 }
 
 void    LEAFTest_controllerInput (int cnum, float cval)
--- a/TestPlugin/Source/UIComponent.cpp
+++ b/TestPlugin/Source/UIComponent.cpp
@@ -13,8 +13,9 @@
 #include "LEAFLink.h"
 
 UIComponent::UIComponent()
-
 {
+    formatManager.registerBasicFormats();
+    
     for (int i = 0; i < cSliderNames.size(); i++)
     {
         
@@ -123,8 +124,9 @@
     
     for (int i = 0; i < cButtonNames.size(); i++)
     {
-        buttons[i]      ->setBounds(cLeftOffset + ((cButtonWidth + cXSpacing) * i),
-                                    500,
+        buttons[i]      ->setBounds(cLeftOffset + ((cButtonWidth + cXSpacing) * i) -
+                                    (cButtonNames[i].length() * 2.0f),
+                                    290,
                                     cButtonWidth,
                                     cButtonHeight);
     }
@@ -144,12 +146,36 @@
     setSliderValue(s->getName(), s->getValue());
 }
 
-void UIComponent::buttonStateChanged (Button* b)
+void UIComponent::buttonStateChanged(Button* b)
 {
     setButtonState(b->getName(), b->getState());
 }
 
-void UIComponent::comboBoxChanged (ComboBox* cb)
+void UIComponent::comboBoxChanged(ComboBox* cb)
 {
     setComboBoxState(cb->getName(), cb->getSelectedId() - 1);
+}
+
+
+void UIComponent::buttonClicked(Button *b)
+{
+    if (b->getName() == "load")
+    {
+        juce::FileChooser chooser ("Select a Wave file to play...", {}, "*.wav");
+        
+        if (chooser.browseForFileToOpen())
+        {
+            auto file = chooser.getResult();
+            auto* reader = formatManager.createReaderFor (file);
+            
+            if (reader != nullptr)
+            {
+                AudioBuffer<float> buffer = AudioBuffer<float>(reader->numChannels, int(reader->lengthInSamples));
+                
+                reader->read(&buffer, 0, buffer.getNumSamples(), 0, true, true);
+                
+                loadedAudio.add(buffer);
+            }
+        }
+    }
 }
--- a/TestPlugin/Source/UIComponent.h
+++ b/TestPlugin/Source/UIComponent.h
@@ -35,7 +35,7 @@
     
     void sliderValueChanged(Slider* s) override;
     
-    void buttonClicked(Button*b) override {};
+    void buttonClicked(Button*b) override;
     void buttonStateChanged(Button* b) override;
     
     void textEditorTextChanged (TextEditor&) override;
@@ -59,7 +59,7 @@
     static const int cLabelWidth  = cSliderWidth + cXSpacing;
     
     static const int cButtonHeight = 30;
-    static const int cButtonWidth  = 60;
+    static const int cButtonWidth  = 75;
     
     static const int cBoxHeight = 20;
     static const int cBoxWidth  =  100;
@@ -69,6 +69,9 @@
     OwnedArray<TextButton>  buttons;
     OwnedArray<TextEditor>  textFields;
     OwnedArray<ComboBox>    comboBoxes;
+    
+    juce::AudioFormatManager formatManager;
+    std::unique_ptr<juce::AudioFormatReaderSource> readerSource;
     
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIComponent)
 };