12 Etudes, Op. 4: Score Generation

Interaction 12-1. Using the pwd and cd functions.

cm> (pwd)
cm> (cd "/tmp/")
cm> (pwd)

Interaction 12-2. Generating a MIDI event to a MIDI file.

cm> (define one (new midi :time 0
                    :keynum 60 :duration 2))

cm> (events one "myscore.mid")
cm> (midi-file-print "myscore.mid")
File: myscore.midi 
Format: 0
Tracks: 1
Division: 480

Track 0, length 20
       0 #<Tempo Change 1000 ms>
       0 #<Note-On 0 60 64>
     960 #<Note-Off 0 60 127>

Interaction 12-3. Listening to a MIDI file on OSX.

cm> (osx-play-midi-file "myscore.mid")

Interaction 12-4. Activating file versioning and setting an output hook for automatic playback.

cm> (set-midi-file-versions! true)

cm> (set-midi-output-hook! #'osx-play-midi-file)


Example 12-1. Definition of a list of events.

(define up
  (loop for key from 60 to 72 
        for beg from 0 by .1
        collect (new midi :time beg
                     :keynum key
                     :duration 1)))

Interaction 12-5. Generating a score from a list of events.

cm> (events up "myscore.mid")

Example 12-2. Create a list of midi events between two key numbers.

(define (strums key1 key2 rate dur amp)
  (let ((step (if (< key2 key1) -1 1))
        (diff (abs (- key1 key2))))
    (loop repeat (+ diff 1)
          for key from key1 by step
          for beg from 0 by rate
          collect (new midi :time beg
                       :duration dur
                       :amplitude amp
                       :keynum key))))

Interaction 12-6. Generating strum data.

cm> (pprint (strums 60 61 .1 1 .2))
(#i(midi time 0 keynum 60 duration 1 amplitude 0.2 channel 0)
 #i(midi time 0.1 keynum 61 duration 1 amplitude 0.2 channel 0))

Interaction 12-7. Creating two strum sequences.

cm> (new seq :name 'ups
         :subobjects (strums 48 60 .1 1 .4))
#<seq "ups">
cm> (new seq :name 'downs
         :subobjects (strums 84 72 .1 1 .2))
#<seq "downs">
cm> (events (list #&ups #&downs) "myscore.mid")

Interaction 12-8. The effect of start time offsets.

cm> (events (list #&ups #&downs) "myscore.mid" 5)
cm> (events (list #&ups #&downs) "myscore.mid" '(0 .5))
cm> (events (list #&ups #&downs #&ups #&downs )
            '(0 .5 1 1.5))

Chapter Source Code

The source code to all of the examples and interactions in this chapter can be found in the file scores.cm located in the same directory as the HTML file for this chapter. The source file can be edited in a text editor or evaluated inside the Common Music application.