Problem making a step sequencer



I was searching for the right classes to use to make a step sequencer/drum machine, but i wont have a clue where to search (there are soooo many classes in the API) and my knowledge about the technical side of music is almost zero (learned the most while trying to figure this out).

I was searching for a way we can program a sequence so it will play music-samples on specific times (my music-sample is a .wav). I tried to build from the synth demo and tried to manually make MIDI-messages (instead of the keyboard making them). But with this method this will only make a noise (i think its turning it on and off all the time).

What i changed in the getNextAudioBlock function:

    //old (in comment): keyboardState.processNextMidiBuffer (incomingMidi, 0, bufferToFill.numSamples, true);
MidiMessage message = MidiMessage::noteOn(1, 74, (float) 1.0);
incomingMidi.addEvent(message, 0);
    // and now get the synth to process the midi events and generate its output.
synth.renderNextBlock (*bufferToFill.buffer, incomingMidi, 0, bufferToFill.numSamples);

Am i searching in the right way? If yes: What am i doing wrong, if no: how can I do this the best way?

Thanks in advance



I don't have an answer for the best way to do it, but it does indeed look like you're adding that midi message everytime a buffer is processed.  If your buffer size is 512 and your sample rate is 44100, then that message is going to be activated about 86 times per second.

In the very least, you're going to want to keep track of whether you are "playing" or not, and within that keep track off how much time has passed.  You can track how much time has passed by counting the samples that have been processed with each buffer.  You might only want to do this when you are in a "playing" state.  Keep your time in seconds by applying the current sample rate to the sample count.  Once you have that in, then you can check to see what time it is and decide if you have a wav that you want to trigger.  It won't always be at the start of the buffer, but you can offset it in your routine appropriately.

You might want to take a look at the AudioPlayHead which is used with AudioProcessors to get an idea of what kind of time tracking units are conventional.  Note that although you are using an AudioSource (vs AudioProcessor), it's just as easy to create your own structure, or use the AudioPlayHead in your AudioSource as you see fit.

EDIT: Specifically it's the AudioPlayHead::CurrentPositionInfo struct.


Thanks alot!

I thought it was something with the buffer, and now you told me it's quite obvious :D

Only thing i have to figure out now is how i find the sample rate (will check settingsdemo tomorrow)