Making A Drum Machine Using Scheme and Impromptu

Following on from the MIDI-based piano idea that I outlined earlier, today we will be going through writing a drum machione in Impromptu as was done in the KKB210 Computational Arts 1 week 3 tutorial.

You may have noticed that it is a bit of a pain to have to evaluate each note each time you want to play it. There is an easy way around this that we have used in the drum machine tutorial that can be applied to pretty much anything that needs to repeat in Impromptu.


First up, we disconnect everything.

; Make sure that everything is disconnected
(au:clear-graph)

Then we define the library and connect it.

(define dls (au:make-node "aumu" "dls " "appl"))
(au:connect-node dls 0 *au:output-node* 0)
(au:update-graph)

You should recognise most of this so far from the piano tutorial. We can play a drum the same way. Have a look at the General MIDI Level 1 Sound Set for more information on what numbers equate to what drums.

; Playing a drum beat the same way as we played
; notes in the piano tutorial.
(play-note (now) dls 36 80 20000 9)

Now to simplify the playing of each note or beat, we want to create a function. In this case we will be calling it drum-machine.

; Define the drum-machine function.
(define drum-machine

; Tell Impromptu to move forward in steps.
(lambda (time step)
(print step)
(case step

; For steps 0 and 8 do this:
((0 8 )
(play-note (now) dls 42 80 20000 9)
(play-note (now) dls 36 80 20000 9))

; For steps 4 and 12 do this:
((4 12)
(play-note (now) dls 42 80 20000 9)
(play-note (now) dls 38 80 20000 9))

; For step 15 do this:
((15)
(play-note (now) dls 46 80 20000 9))

; For any other steps do this:
(else (play-note (now) dls 44 80 20000 9)))

That’s fairly straight forward right? It uses exactly the same play-note commands and parameters that we have used before, however it puts them within a step function that tells impromptu to play through the notes in steps and to play certain notes at certain steps.

This doesn’t repeat though. In order to make it repeat, we tell Impromptu to run a callback to the drum-machine function. This sends it back to drum-machine. and so it goes through the steps again and will continue to do so until something gets changed that doesn’t work, such as a close bracket immediately after (define drum-machine.

(callback (+ time 10000) drum-machine (+ time 9000) (modulo (+ step 1) 16))))

Now, of course just having this setup doesn’t mean it will run when you evaluate it. You have to create a function call. This is specified below along with when to run it (now).

(drum-machine (now) 0)

Once you have evaluated all of the components of the program you should be able to evaluate the function call and it will run the drum-machine function, a constantly looping drum machine that you can change on the fly.

Thats right, if you want to change one of the drum sounds, when it’s playing, or anything like that, all you have to do is edit it and then re-evaluate that single line and it will be played from then on as part of the constant loop. You can also add more steps, remove steps, change what is played for certain steps, re-evaluate it and it becomes part of the loop. Easy as that.

Tags: , , , , , ,

2 Responses to “Making A Drum Machine Using Scheme and Impromptu”

  1. […] you want to hear it. This is a pain and it will be addressed in my next post where we will be making a drum machine in Impromptu. Share and Enjoy: These icons link to social bookmarking sites where readers can share and […]

  2. jean says:

    Hi, I managed to get until creating the drum-machine, but when i get to the last line to play it
    (drum-machine (now) 0)
    impromptu says ‘illegal function’…
    thx for your help
    jean

Leave a Reply