Skip to content

Instantly share code, notes, and snippets.

@gigasquid
Created February 6, 2013 14:02
Show Gist options
  • Save gigasquid/4722675 to your computer and use it in GitHub Desktop.
Save gigasquid/4722675 to your computer and use it in GitHub Desktop.
Mary had a little lamb in overtone
(ns overtone-tutorial.mary)
(use 'overtone.core)
(boot-external-server)
(definst saw-wave [freq 440 attack 0.01 sustain 0.4 release 0.1 vol 0.4]
(* (env-gen (lin-env attack sustain release) 1 1 0 1 FREE)
(saw freq)
vol))
;; Define a function for convenience
(defn note->hz [music-note]
(midi->hz (note music-note)))
;; Let's make it even easier
(defn saw2 [music-note]
(saw-wave (midi->hz (note music-note))))
(defonce metro (metronome 120))
(metro)
(def mary-notes [:E3 :D3 :C3 :D3 :E3 :E3 :E3 nil :D3 :D3 :D3
nil :E3 :G3 :G3 nil :E3 :D3 :C3 :D3 :E3 :E3
:E3 :E3 :D3 :D3 :E3 :D3 :C3])
(defn song-player [m beat-num notes]
(map-indexed (fn [idx note]
(when note
(at (m (+ idx beat-num)) (saw2 note))))
notes))
(song-player metro (metro) mary-notes)
@samaaron
Copy link

samaaron commented Feb 6, 2013

Cool!

Is there any reason you used an external server rather than the internal one?

Also, your song-player relies on the REPL forcing the evaluation of the lazy sequence returned by map-indexed and uses the metro as a glorified lookup table - scheduling all events to happen ahead of time. This is a similar approach to that used by Chris Ford in his compositions. An alternative is to use what's known as temporal recursion which involves schedule a fn to call itself slightly before the event should happen. There's some examples of this in action in the examples directory found within Overtone's source.

Keep it up :-)

@gigasquid
Copy link
Author

We used the external server because I thought it was the only option if you installed the SuperCollider in the Mac Applications directory.

Cool to know about temporal recursion! I will have to check it out. Overtone is very fun. The intersection of technology and art is a very interesting place to explore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment