# RackAFX single sample to JUCE audio buffer example?

**URL:** <https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262>\
**Category:** General JUCE discussion\
**Created:** [April 16, 2018, 5:29pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262 "2018-04-16T17:29:45Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 16, 2018, 5:29pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/1 "2018-04-16T17:29:45Z")

</div>

Not sure if there are many people on this forum who have purchased Will Pirkle’s audio plugin books or are users of his RackAFX software. I was currently on working through his synth book and trying to recreate the different projects in JUCE as learning exercises. One thing that is confusing me is how to go from the single sample output that RackAFX uses to fill up a JUCE audio buffer. Does anyone happen to have an example for how to go about doing this? Anyone else ported RackAFX projects into JUCE?

---

<div class="post-metadata">

**Author:** ![matthieu-brucher](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@matthieu-brucher](https://forum.juce.com/u/matthieu-brucher)\
**Post date:** [April 16, 2018, 8:11pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/2 "2018-04-16T20:11:29Z")

</div>

Just write a for loop on all the samples in the loop, and call your code on each sample.

Yet another example why the book is awful.

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 16, 2018, 8:54pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/3 "2018-04-16T20:54:40Z")

</div>

Right. Okay I’m working on the for loop right now. However, the other thing that is confusing me is the fact that the processBlock function in the PluginProcessor does not explicitly have StartSample or NumSamples inputs. I have handleNoteOn and handleNoteOff functions in my PluginEditor which listen for changes in the MidiKeyboard component I am using. However, those functions don’t explicitly deal with audio buffers so I’m a bit confused on how to link all these functions together. Should I just call the processBlock from the handleNoteOn function in the PluginEditor every time a new MIDI note is triggered? Guessing there is no relation between the default buffer size and MIDI note duration. Sorry for the noob questions but it would be very informative to get some clarity on this issue since it seems critical.

---

<div class="post-metadata">

**Author:** ![JacobPenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jacobpenn/32/3385_2.png) [@JacobPenn](https://forum.juce.com/u/JacobPenn)\
**Post date:** [April 16, 2018, 9:35pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/4 "2018-04-16T21:35:25Z")

</div>

That book is alright but the code is bad, just use the DSP concepts, ditch the rest.

PluginProcessor receives an AudioBuffer. you can call getNumSamples() & getNumChannels()

```
for (int channel = 0; channel < totalNumInputChannels; ++channel)
{
    float* channelData = buffer.getWritePointer (channel);

    // ..do something to the data...

    for (int sample = 0; sample < buffer.getNumSamples(); sample++) {
        
        channelData[sample] = DSP_OUTPUT;
    }
}
```

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 16, 2018, 9:45pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/5 "2018-04-16T21:45:46Z")

</div>

Jacob thats helpful but if the plugin is a synth, does the PluginProcessor have to receive an AudioBuffer? Can it not just create an AudioBuffer? The reason I ask that is because I’m confused how the handleNoteOn and handleNoteOff in the PluginEditor which communicates with the MidiKeyboardComponent syncs with the AudioBuffers in the PluginProcessor. Those functions don’t have inputs which are AudioBuffers, just MIDI related inputs. Do you just create a new AudioBuffer every time a new MIDI note is triggered?

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [April 16, 2018, 10:05pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/6 "2018-04-16T22:05:39Z")

</div>

> [@shossa3](#):
>
> does the PluginProcessor have to receive an AudioBuffer? Can it not just create an AudioBuffer

The AudioBuffer passed in will contain input data (which you can ignore), but you also write into the same buffer and this is what is output to the host. This is often referred to as process replacing, i.e. you are replacing the input with your output. In your case you may not care about the input which is fine. But even some synths may want input, for example if your synth is also a vocoder if might be useful to have the input.

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 16, 2018, 10:31pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/7 "2018-04-16T22:31:19Z")

</div>

```
void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override
{
    MidiBuffer incomingMidi;
    processor.midiNoteOn(midiChannel, midiNoteNumber, velocity);
    processor.processBlock(AudioBuffer<float>& buffer, incomingMidi);
}

```

^The above function is in my PluginEditor for responding to notes from the MidiKeyboardComponent. Is this the proper way to communicate between the keyboardcomponent, editor, and processor? Right now, the editor is generating an error related to passing the audiobuffer. Should I inherent from “AudioProcessor” in my PluginEditor and then pass a buffer to the PluginProcessor from there every time a note is triggered as I have above?

---

<div class="post-metadata">

**Author:** ![theaudioprogrammer](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/theaudioprogrammer/32/18383_2.png) [@theaudioprogrammer](https://forum.juce.com/u/theaudioprogrammer)\
**Post date:** [April 16, 2018, 10:53pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/8 "2018-04-16T22:53:38Z")

</div>

The easiest way to implement a synth in Juce is to use the synthesiser class. In order to implement it you also need to use the synthesiserVoice and synthesiserSound classes. These will come with a lot of the built in functionality you’ll need for your synth.

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 16, 2018, 10:58pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/9 "2018-04-16T22:58:59Z")

</div>

Joshua, I’ll give that a shot. It might be tricky since I’m importing code from RackAFX projects but perhaps that might be the easiest way to go. Thanks for the tip.

---

<div class="post-metadata">

**Author:** ![theaudioprogrammer](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/theaudioprogrammer/32/18383_2.png) [@theaudioprogrammer](https://forum.juce.com/u/theaudioprogrammer)\
**Post date:** [April 16, 2018, 11:07pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/10 "2018-04-16T23:07:34Z")

</div>

Maybe this will help you get in the right direction

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 17, 2018, 12:51am UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/11 "2018-04-17T00:51:19Z")

</div>

Joshua, I didn’t realize that you were the one behind the AudioProgrammer videos/channel on youtube. It’s an incredibly helpful resource. Thanks for your contributions! Watching the above tutorial right now.

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 17, 2018, 4:28pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/12 "2018-04-17T16:28:06Z")

</div>

Joshua, quick question. I’m working through your tutorial on building a synthesiser using JUCE. I’m on the second video where you are adding MIDI functionality. However, I’m having an issue. After adding the following code:

```
mySynth.clearVoices();

for (int i =0; i < 16; i++)
{
    mySynth.addVoice(new SynthVoice());
}

mySynth.clearSounds();
mySynth.addSound(new SynthSound());

```

There is an error saying allocating an object of abstract class type. I know this is because of the two virtual functions, appliesToNote and appliesToChannel. However, I wasn’t sure if I was supposed to somehow specify them in the PluginProcessor or even how to specify them. Could you perhaps provide a bit of example code of where and how to instantiate these functions?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 17, 2018, 4:32pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/13 "2018-04-17T16:32:00Z")

</div>

I answered here:

> [@Problems with the method addVoice\[SOLVED\]](https://forum.juce.com/t/problems-with-the-method-addvoice-solved/19418/5):
>
> The methods are members of the sound, so you need to implement them there (it is part of the interface the sound has to satisfy). The [docs](https://docs.juce.com/develop/classSynthesiserSound.html#abc192aba46964c8539206db1e6f6f747) say: virtual bool SynthesiserSound::appliesToNote (int midiNoteNumber) - pure virtual Returns true if this sound should be played when a given midi note is pressed. The Synthesiser will use this information when deciding which sounds to trigger for a given note. Which means, if your sound can play all notes, you can simply return true in every case, …

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 17, 2018, 4:36pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/14 "2018-04-17T16:36:51Z")

</div>

When I try to add SynthesiserSound::appliesToNote (int) and SynthesiserSound::appliesToChannel (int) in my PluginProcessor.h file as follow:

```
SynthesiserSound::appliesToNote(int);
SynthesiserSound::appliesToChannel (int);

```

I get the following error:

"Non-friend class member "appliesToChannel"cannot have a qualified name. "

Therefore, I think I’m missing something about where to implement these functions.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 17, 2018, 4:40pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/15 "2018-04-17T16:40:30Z")

</div>

> [@shossa3](#):
>
> SynthesiserSound::appliesToNote(int);  
> SynthesiserSound::appliesToChannel (int);

It should look like:

```auto
class SynthSound : public SynthesizerSound {
// ...
void appliesToNote(int) override { return true; }
void appliesToChannel (int) override { return true; }
// ...

```

HTH

---

<div class="post-metadata">

**Author:** ![shossa3](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shossa3/32/3860_2.png) [@shossa3](https://forum.juce.com/u/shossa3)\
**Post date:** [April 17, 2018, 4:46pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/16 "2018-04-17T16:46:03Z")

</div>

Daniel, thanks so much. That did the trick! So is the lesson to be learned here that whenever I inherent from a class with pure virtual functions, I just use “override” on those functions in the derived class?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [April 17, 2018, 4:51pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/17 "2018-04-17T16:51:34Z")

</div>

Yes, and tripple check spelling… 😉

`override` makes the compilation fail, if there wasn’t a virtual method in one of the inherited classes to override.

Unfortunately the `override` keyword was added later, so if you never use `override`, it might also compile, depending on some compiler flags, warning levels etc.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [April 17, 2018, 4:51pm UTC](https://forum.juce.com/t/rackafx-single-sample-to-juce-audio-buffer-example/27262/18 "2018-04-17T16:51:34Z")

</div>

The “override” keyword doesn’t do anything else but give you a compile time error if the function actually doesn’t override a virtual (or pure virtual) method in a parent class. So it’s useful to add when overriding methods to avoid mistakes with method name typos or wrong function signatures.
