Making sticks sync for a drum-machine
April 13, 2008
To The Cow God (and anyone else interested)
Making a microcontroller do beats is an awful thing.
Code needs to be in 2 parts;
- The "Hit"
- The "Sequence / note-part"
In 2) There is all the logic that makes up a song, consisting of lines of code divided into segments of 16
In 2) bits are set, such as BD = 1 (Play BassDrum), and also head, speaker-click etc. Once for every 16'th note
Every other "line" in 2) should also have an extra pause, how long it is makes the beat more or less shuffle.
It is awful because:
If you make a sound with the speaker, all halts, and music gets non-rhytmic / out of beat.
If you (in 1) have a lot of instruments hitting at the same time, typical coding will make this beat slow, and music gets non-rhytmic / out of beat.
And.. Not all instruments have the same speed.
Ways to work around;
"Nodes"-segments in 2) should always run 1)
1) is build up this way;
A: All routines for every instrument should always be run.
Say; "No basdrum is to be played?" - Well, run the routine anyway, but just set the pin to 0. "No speaker-click?" Run the routine anyway, just make "no sound-code"
B) Make a mark in the code, called "BANG". At this part, ALL instruments should hit!
have a delay-hard-coded (or measured as we talked about) for each instument.
When starting 1), start a counter that has micro-pauses.
- When slowest instrument (long stick)'s delay-factor is up, fire that stick
- When middle instrument (long stick)'s delay-factor is up, fire that sampler / whatever
- At "bang", also fire the instant speaker-click
- hold short break to ensure all sticks are really out
- release all instruments / take back sticks
- return to next "note" in "2"
I hope this helps a good way, it was actually quite fun and easy to code.