Sharing a MidiBuffer between AudioSources


#1

Hi everybody!

I am relatively new to Juce and I want to build a small drum machine (Trying to tie all those audio examples together).

Kick, snare, hihat, etc. should be independent "tracks", in order to be able to set the volume of each track independently with a fader. 

I am using the MixerAudioSource to connect multiple Tracks (AudioSources) together, which each have their own sampler (Synthesiser, SamplerVoices) inside.

Then, I generate Midi noteOn events to trigger the samplers inside the tracks.

It works but only for the first track. The problem seems to be, that when getNextAudioBlock() of the first track is called, the midiBuffers are cleared internally.

    // the synth always adds its output to the audio buffer, so we have to clear it
    // first..
    bufferToFill.clearActiveBufferRegion();

    // fill a midi buffer with incoming messages from the midi input.
    MidiBuffer incomingMidi;

    midiCollector.removeNextBlockOfMessages (incomingMidi, bufferToFill.numSamples);

    // pass these messages to the keyboard state so that it can update the component
    // to show on-screen which keys are being pressed on the physical midi keyboard.
    // This call will also add midi messages to the buffer which were generated by
    // the mouse-clicking on the on-screen keyboard.
    keyboardState.processNextMidiBuffer (incomingMidi, 0, bufferToFill.numSamples, true);

    // and now get the synth to process the midi events and generate its output.
    renderNextBlock (*bufferToFill.buffer, incomingMidi, 0, bufferToFill.numSamples);

Is there a way to do this nicely? Or is this kind of structure just too unusual? Any hints would be helpful.

Thanks!


#2

Anyone? I'm a bit stuck here..


#3

You can just make a by-value copy a MidiBuffer, you know?


#4
    for (int i=0; i<sources.size(); i++) {
        MidiBuffer sharedMidi = incomingMidi;
        sources[i]->incomingMidi = sharedMidi;
    }

I am copying and distributing the MidiBuffer like this, and it works, thanks!


#5

Ok.. but that code is unnecessarily copying it twice. No need to make an intermediate copy.