Skip to content

Commit

Permalink
Adding some small examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosejn committed Feb 10, 2010
1 parent 00bf9bc commit 8c116bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@
midi-clj is being developed for Project Overtone, and it is meant to simplify
the usage of midi devices and the midi system from within Clojure.

(use 'midi)

; Select midi input and output devices
; These functions bring up a GUI chooser window to select a midi port
(def keyboard (midi-in))
(def phat-synth (midi-out))

; Once you know the correct device names for your devices you can save the
; step of opening up the GUI chooser by putting a unique part of the name
; as an argument to midi-in or midi-out. The first device with a name that
; matches with lookup is returned.
(def ax (midi-in "axiom"))

; Connect ins and outs easily
(midi-route keyboard phat-synth)

; Trigger a note (note 40, velocity 100)
(midi-note-on phat-synth 40 100)
(Thread/sleep 500)
(midi-note-off phat-synth 0)

; Or the short-hand version to start and stop a note
(midi-note phat-synth 40 100 500)

; And the same thing with a sequence of notes
(midi-play phat-synth [40 47 40] [80 50 110] [250 500 250])

In Ubuntu Linux I use the snd-virmidi kernel module to provide software midi
ports. USB midi devices should be pretty much plug and play.


### Project Info:

#### Source Repository
Expand Down
54 changes: 33 additions & 21 deletions src/midi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
(java.util.concurrent FutureTask))
(:use clojure.set))

(def *midi-player-pool* (ScheduledThreadPoolExecutor. NUM-PLAYER-THREADS))

(defn- now []
(System/currentTimeMillis))

(defn- schedule
"Schedules fun to be executed after ms-delay milliseconds."
[fun ms-delay]
(.schedule *midi-player-pool* fun (long ms-delay) TimeUnit/MILLISECONDS))

;(defn byte-array [len]
; (make-array (. Byte TYPE) len))

Expand Down Expand Up @@ -118,7 +128,8 @@
(assoc source-info :transmitter (.getTransmitter dev))))

(defn midi-in
"Connect the sequencer to a midi input device."
"Connect the sequencer to a midi input device. If no argument is given then
a selection list pops up to let you browse and select the midi device."
([] (with-transmitter
(.get (midi-port-chooser "Midi Input Selector" (midi-sources)))))
([in]
Expand All @@ -132,7 +143,8 @@
nil)))))

(defn midi-out
"Connect the sequencer to a midi output device."
"Connect the sequencer to a midi output device. If no argument is given then
a selection list pops up to let you browse and select the midi device."
([] (with-receiver
(.get (midi-port-chooser "Midi Output Selector" (midi-sinks)))))

Expand Down Expand Up @@ -181,7 +193,7 @@

(defn midi-note-off
"Send a midi off msg to the sink."
[sink note-num vel]
[sink note-num]
(let [off-msg (ShortMessage.)]
(.setMessage off-msg ShortMessage/NOTE_OFF 0 note-num 0)
(.send (:receiver sink) off-msg -1)))
Expand All @@ -202,21 +214,21 @@
(.setMessage sys-msg bytes (count bytes))
(.send (:receiver sink) sys-msg -1)))

;(defn midi-note
; "Send a midi on/off msg pair to the sink."
; [sink note-num vel dur]
; (midi-note-on sink note-num vel)
; (schedule #(midi-note-off sink note-num 0) dur))
;
;(defn midi-play [out notes velocities durations]
; (loop [notes notes
; velocities velocities
; durations durations
; cur-time 0]
; (if notes
; (let [n (first notes)
; v (first velocities)
; d (first durations)]
; (schedule #(midi-note out n v d) cur-time)
; (recur (next notes) (next velocities) (next durations) (+ cur-time d))))))
;
(defn midi-note
"Send a midi on/off msg pair to the sink."
[sink note-num vel dur]
(midi-note-on sink note-num vel)
(schedule #(midi-note-off sink note-num 0) dur))

(defn midi-play [out notes velocities durations]
(loop [notes notes
velocities velocities
durations durations
cur-time 0]
(if notes
(let [n (first notes)
v (first velocities)
d (first durations)]
(schedule #(midi-note out n v d) cur-time)
(recur (next notes) (next velocities) (next durations) (+ cur-time d))))))

0 comments on commit 8c116bf

Please sign in to comment.