History

add wav file loading to test JUCE project

Matthew Wang committed 5 years ago

Commit 7ea48641 parent be773ed5 files · +65 −14Browse files

Unified · Split

5 files
  1. TestPlugin/Source/LEAFLink.cpp +4 −1
  2. TestPlugin/Source/LEAFLink.h +2 −0
  3. TestPlugin/Source/MyTest.cpp +23 −6
  4. TestPlugin/Source/UIComponent.cpp +31 −5
  5. TestPlugin/Source/UIComponent.h +5 −2
TestPlugin/Source/LEAFLink.cpp+4−1
@@ -15,7 +15,7 @@ · 14 unmodified lines
1515
1616 std::vector<juce::String> cButtonNames = std::vector<juce::String>
1717 {
18−
18+ "load"
1919 };
2020
2121 std::vector<juce::String> cSliderNames = std::vector<juce::String>
@@ -40,6 +40,8 @@ std::vector<float> cSliderValues(cSliderNames.size()); · 18 unmodified lines
4040 std::vector<bool> cButtonStates(cButtonNames.size());
4141 std::vector<int> cComboBoxStates(cComboBoxNames.size());
4242
43+Array<AudioBuffer<float>> loadedAudio;
44+
4345 void setSliderModelValue(String name, float val)
4446 {
4547 for (int i = 0; i < cSliderNames.size(); i++)
@@ -142,3 +144,4 @@ float getRandomFloat(void) · 96 unmodified lines
142144 {
143145 return ((float)rand()/RAND_MAX);
144146 }
147+
TestPlugin/Source/LEAFLink.h+2−0
@@ -39,6 +39,8 @@ extern std::vector<bool> cButtonStates; · 38 unmodified lines
3939
4040 extern std::vector<int> cComboBoxStates;
4141
42+extern Array<AudioBuffer<float>> loadedAudio;
43+
4244 void printSliderValues(void);
4345 bool getButtonState(String name);
4446 void setButtonState(String name, bool on);
TestPlugin/Source/MyTest.cpp+23−6
@@ -38,6 +38,9 @@ tWaveset ws; · 37 unmodified lines
3838 tBuffer samp;
3939 tMBSampler sampler;
4040
41+const int numWavetables = 1;
42+tWavetable wavetables[numWavetables];
43+
4144 float gain;
4245 float dtime;
4346 bool buttonState;
@@ -54,6 +57,8 @@ LEAF leaf; · 10 unmodified lines
5457 #define MSIZE 2048000
5558 char memory[MSIZE];
5659
60+int lastLoadedAudioSize = 0;
61+
5762 void LEAFTest_init (float sampleRate, int blockSize)
5863 {
5964 LEAF_init(&leaf, sampleRate, blockSize, memory, MSIZE, &getRandomFloat);
@@ -79,6 +84,9 @@ void LEAFTest_init (float sampleRate, int blockSize) · 19 unmodified lines
7984
8085 tWaveset_init(&ws, set, 4, 2048, 10000.f, &leaf);
8186 tWaveset_setIndexGain(&ws, 0, -1.0f);
87+
88+ lastLoadedAudioSize = 0;
89+ loadedAudio.clear();
8290 }
8391
8492 inline double getSawFall(double angle) {
@@ -92,12 +100,12 @@ inline double getSawFall(double angle) { · 7 unmodified lines
92100
93101 float LEAFTest_tick (float input)
94102 {
95−// return tRetune_tick(&retune, input)[0];
96−// return tSimpleRetune_tick(&sretune, input);
97−// tMBPulse_sync(&bpulse, tPhasor_tick(&phasor) * 2.f - 1.f);
98−// return tMBPulse_tick(&bpulse);
99−// return tWavetable_tick(&wt);
100− return tWaveset_tick(&ws);
103+ float out = 0.0f;
104+ for (int i = 0; i < fmin(loadedAudio.size(), numWavetables); ++i)
105+ {
106+ out += tWavetable_tick(&wavetables[i]);
107+ }
108+ return out;
101109 }
102110
103111 int firstFrame = 1;
@@ -121,6 +129,15 @@ void LEAFTest_block (void) · 17 unmodified lines
121129 val = getSliderValue("slider3");
122130 // tRetune_setPitchFactor(&retune, val * 3.0f + 0.5f, 2);
123131
132+
133+ if (lastLoadedAudioSize < loadedAudio.size())
134+ {
135+ int i = (loadedAudio.size() - 1) % numWavetables;
136+ if (loadedAudio.size() - 1 >= numWavetables) tWavetable_free(&wavetables[i]);
137+ tWavetable_init(&wavetables[i], loadedAudio[loadedAudio.size() - 1].getReadPointer(0), loadedAudio[loadedAudio.size() - 1].getNumSamples(), 20000, &leaf);
138+
139+ lastLoadedAudioSize = loadedAudio.size();
140+ }
124141 }
125142
126143 void LEAFTest_controllerInput (int cnum, float cval)
TestPlugin/Source/UIComponent.cpp+31−5
@@ -13,8 +13,9 @@ · 12 unmodified lines
1313 #include "LEAFLink.h"
1414
1515 UIComponent::UIComponent()
16−
1716 {
17+ formatManager.registerBasicFormats();
18+
1819 for (int i = 0; i < cSliderNames.size(); i++)
1920 {
2021
@@ -123,8 +124,9 @@ void UIComponent::resized() · 102 unmodified lines
123124
124125 for (int i = 0; i < cButtonNames.size(); i++)
125126 {
126− buttons[i] ->setBounds(cLeftOffset + ((cButtonWidth + cXSpacing) * i),
127− 500,
127+ buttons[i] ->setBounds(cLeftOffset + ((cButtonWidth + cXSpacing) * i) -
128+ (cButtonNames[i].length() * 2.0f),
129+ 290,
128130 cButtonWidth,
129131 cButtonHeight);
130132 }
@@ -144,12 +146,36 @@ void UIComponent::sliderValueChanged(Slider* s) · 13 unmodified lines
144146 setSliderValue(s->getName(), s->getValue());
145147 }
146148
147−void UIComponent::buttonStateChanged (Button* b)
149+void UIComponent::buttonStateChanged(Button* b)
148150 {
149151 setButtonState(b->getName(), b->getState());
150152 }
151153
152−void UIComponent::comboBoxChanged (ComboBox* cb)
154+void UIComponent::comboBoxChanged(ComboBox* cb)
153155 {
154156 setComboBoxState(cb->getName(), cb->getSelectedId() - 1);
155157 }
158+
159+
160+void UIComponent::buttonClicked(Button *b)
161+{
162+ if (b->getName() == "load")
163+ {
164+ juce::FileChooser chooser ("Select a Wave file to play...", {}, "*.wav");
165+
166+ if (chooser.browseForFileToOpen())
167+ {
168+ auto file = chooser.getResult();
169+ auto* reader = formatManager.createReaderFor (file);
170+
171+ if (reader != nullptr)
172+ {
173+ AudioBuffer<float> buffer = AudioBuffer<float>(reader->numChannels, int(reader->lengthInSamples));
174+
175+ reader->read(&buffer, 0, buffer.getNumSamples(), 0, true, true);
176+
177+ loadedAudio.add(buffer);
178+ }
179+ }
180+ }
181+}
TestPlugin/Source/UIComponent.h+5−2
@@ -35,7 +35,7 @@ public: · 34 unmodified lines
3535
3636 void sliderValueChanged(Slider* s) override;
3737
38− void buttonClicked(Button*b) override {};
38+ void buttonClicked(Button*b) override;
3939 void buttonStateChanged(Button* b) override;
4040
4141 void textEditorTextChanged (TextEditor&) override;
@@ -59,7 +59,7 @@ private: · 17 unmodified lines
5959 static const int cLabelWidth = cSliderWidth + cXSpacing;
6060
6161 static const int cButtonHeight = 30;
62− static const int cButtonWidth = 60;
62+ static const int cButtonWidth = 75;
6363
6464 static const int cBoxHeight = 20;
6565 static const int cBoxWidth = 100;
@@ -70,6 +70,9 @@ private: · 4 unmodified lines
7070 OwnedArray<TextEditor> textFields;
7171 OwnedArray<ComboBox> comboBoxes;
7272
73+ juce::AudioFormatManager formatManager;
74+ std::unique_ptr<juce::AudioFormatReaderSource> readerSource;
75+
7376 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UIComponent)
7477 };
7578

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t