Audio Sequencing - How To ?

Blitz3D Forums/Blitz3D Programming/Audio Sequencing - How To ?

semar(Posted 2009) [#1]
Hi Blitzers.

I'm developing a drum editor using Blitz3D.

My goal is to input drum events on a music score, and have the ability to play the sequence accordingly with the choosen beat per minute (bpm).

Each note on the music score is associated with an audio instrument (a vaw file).

What kind of technique should I use, in order to play each instrument at the right moment ?

In any word, I'm in the needing of coding a small audio sequencer.

I'm tempted to use a Timer with CreateTimer, but if the user want to change the play speed during the sequencing, he/she may experience stuttering, because the timer should be re-created in real time.

Does someone of you have already tryed something like this ? Should I use some specific DLL instead of coding directly in Blitz ?

Regards,
Sergio.


Warner(Posted 2009) [#2]
Well, I tried writing one in Delphi, and I ended up using high-precision timers (multimedia timers), and I tried rendering the wav before playing it. Maybe there is an API that can create such a timer.
Instead of recreating the timer for each different speed, you could maybe create a timer that ticks at the fastest rate that you want to achieve, and then have the program respond to each nth tick, depending on the speed setting.


GW(Posted 2009) [#3]
Timers won't do it. I fell into that logic when when I created a sequencer a long time ago.
you'll have to use audio buffers for each channel where you write the wav data at the right point. Then just play the buffers.


semar(Posted 2009) [#4]
@GW,
Timers won't do it. I fell into that logic when when I created a sequencer a long time ago. you'll have to use audio buffers for each channel where you write the wav data at the right point. Then just play the buffers.

But what if I have a quite long sequencing ? I should write the data to the buffer at each bar in real time, otherwies an *huge* buffer to play with is needed.

Do you have a small code example of your theory ?

Sergio.

[EDIT]
Wait a moment, I could prepare the next audio buffer while the first is playing though. Hummm... So using 2 audio buffers would work.
Again, I need a code example to test your theory. Anyone ?


Warner(Posted 2009) [#5]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1112
http://www.blitzbasic.com/codearcs/codearcs.php?code=1111


semar(Posted 2009) [#6]
Thanks Warner,
I've also found these interesting code archive entries, but then I've become skeptical, when I've reached this line of code :
Unfortunately only ONE sample at a time can be played from a sound bank


That means, an audio bank can play only a wave file. That means, I can't build a bank to hold several audio files inside.

And more, how can I build such an audio bank, when for example two sounds should be played togheter at the same moment - for example, a high hat and a tom ?

Anyway, if a timer created with CreateTimer is precise enough, it would already work ok for my purpose. But changing the tempo, in this case, can't work at real time.

Humm...


Warner(Posted 2009) [#7]
In that case, create a timer with the smallest interval you need, and then execute the routine less often to slow it down. Sort of like this:
Function OnTimer()
  
   c = c + 1
   if c < 10 then return
   c = 0

   ..timer function

End Function

Secondly, take a look at multimedia timers. They were created for this purpose. How they would behave with B3D, I don't know.
If you want to combine two samples, you can blend the samples together:
wav = (wav1 + wav2) / 2.0


GW(Posted 2009) [#8]
But what if I have a quite long sequencing ? I should write the data to the buffer at each bar in real time, otherwies an *huge* buffer to play with is needed.

you don't write the buffers in realtime you do it at the start of playback or rendering.
as far as long sequences, you break your composition into measures. then you define your song as a sequence of measures. (many will repeat)
you could make your life a lot easier by just using a mod tracker imo.


semar(Posted 2009) [#9]
Yes GW, breaking the composition into measures is exactly my goal; the problem is to get them in sync :)

Using a mod tracker would transfer the program control from my B3D application to another application. This is not what I'm trying to achieve...