Skip to content
Browse files

sin working

  • Loading branch information...
1 parent b11debf commit 99d35cc8e42c896ebce5dde2330ad3136e0b9c39 Jon Vlachoyiannis committed Feb 28, 2011
Showing with 125 additions and 66 deletions.
  1. +125 −66 musicexample.clj
View
191 musicexample.clj
@@ -10,40 +10,21 @@
;; play should execute the timings
-(def kick (.loadSample @*minim* "/home/darksun4/Sources/clj-processing/examples/data/KickDrums1/kickdrum6.wav"))
-
-(def snare (.loadSample @*minim* "/home/darksun4/Sources/clj-processing/examples/data/SnareDrums1/snaredrum2.wav"))
-
-(def hihat (.loadSample @*minim* "/home/darksun4/Sources/clj-processing/examples/data/HiHats1/hihat2.wav"))
-
-(defmacro times [num expression]
- ;; small tempo drums demo
- `(doseq [i# (range ~num)]
- ~expression))
-
-;; Drum Demos
-(times 2
- (p (pattern [kick (+ hihat snare)],2)))
-
-(times 4
- (p (pattern [kick (+ hihat snare) [kick kick] (+ hihat snare)],3)))
-
-(times 4
- (p (pattern [kick (+ hihat snare) [kick kick] (+ hihat snare)],3)))
-
-
(ns examples.music
(:import [ddf.minim Minim AudioOutput AudioSample]
[ddf.minim.signals SquareWave]
+ [ddf.minim.signals SineWave]
[ddf.minim.effects LowPassFS])
(:import (processing.core PApplet PImage PGraphics PFont))
(:use [rosado.processing]
[rosado.processing.applet]))
+
(def *minim* (atom nil))
(def *outp* (atom nil))
-(def *sine* (atom nil))
(def *samples-path* "/home/darksun4/Sources/clj-processing/examples/data/")
+(def *pattern* (atom []))
+
(defn play-note [note]
(.playNote @*outp* (float 0)
@@ -66,6 +47,10 @@
`(def ~n-name (Melement. 50 (float 1.0) (float 1.0) play-note (str '~n-name))))
+(defn play-element [element]
+ ((:play-fn element) element))
+
+
(defn- play-chord
"Executes the play-funcs of of all elements"
[chord-elements]
@@ -74,48 +59,32 @@
(defn join [& notes]
- (Melement. 0 0 0 true play-chord
+ (Melement. 0 0 0 play-chord
(first (conj [] notes))))
-(defn play-element [element]
- ((:play-fn element) element))
-
-
-
+;; Maybe defmulti?
(defmacro + [& more]
;; make chords like (+ kick hihat)
`(join ~@more))
-
;; Notes
(defnote A)
-(defnote B)
-(defnote C)
-(defnote D)
-(defnote E)
-(defnote F)
-(defnote G)
-(defnote E3)
-(defnote G3)
-(defnote B3)
-(defnote E4)
-(defnote _)
+;;(defnote B)
+;;(defnote C)
+;;(defnote D)
+;;(defnote E)
+;;(defnote F)
+;;(defnote G)
+;;(defnote E3)
+;;(defnote G3)
+;;(defnote B3)
+;;(defnote E4)
+;;(defnote _)
(def _ (Melement. 50 1.0 nil (fn[x] nil) "_"))
-;; Drums
-(defsample kick "KickDrums1/kickdrum6.wav")
-(defsample snare "SnareDrums1/snaredrum2.wav")
-(defsample hihat "HiHats1/hihat2.wav")
-
-(def a (.loadSample @*minim* "/home/darksun4/Sources/clj-processing/examples/data/BD.mp3"))
-(.trigger a)
-(.close a)
;; Nothing else matters
-(p (pattern [E3 G3 B3 E4 B3 G3], 4))
-
-;; Drum demo
-(p (pattern [kick kick hihat [kick kick] [kick kick] [kick kick] hihat], 4))
+;;(p (pattern [E3 G3 B3 E4 B3 G3], 4))
;; Working example
;; (p (pattern [kick (+ hihat snare) kick],2))
@@ -124,11 +93,6 @@
pattern))
-(defn calc-duration [elements duration count]
- (map #(pattern % (/ duration count))
- elements))
-
-
;; Maybe a reduce could clean it up more?
(defn pattern
([m-element] (pattern m-element 1))
@@ -139,6 +103,10 @@
(assoc m-element :duration duration))))
+(defn calc-duration [elements duration count]
+ (map #(pattern % (/ duration count))
+ elements))
+
(defn p
[elements]
(doseq [element elements]
@@ -155,11 +123,7 @@
"Runs once."
(swap! *minim* (fn [minim] (Minim. *applet*)))
(swap! *outp* (fn [out]
- (.getLineOut @*minim*)))
- (swap! *sine* (fn [sine]
- (SquareWave. 440 0.5 (.sampleRate @*outp*)))))
-
-
+ (.getLineOut @*minim*))))
(defn draw []
(background-float 124))
@@ -170,21 +134,115 @@
(run example)
(stop example)
+;; Drums
+(defsample kick "KickDrums1/kickdrum6.wav")
+(defsample kick2 "KickDrums4/kickdrum154.wav")
+(defsample snare "SnareDrums1/snaredrum2.wav")
+(defsample snare2 "DistortedSnares2/distortedsnare52.wav")
+(defsample hihat "HiHats1/hihat2.wav")
+
+(defmacro times [num expression]
+ ;; small tempo drums demo
+ `(doseq [i# (range ~num)]
+ ~expression))
+
+(defn keep-looping []
+ (times 500
+ (p (pattern @*pattern*))))
+
+(keep-looping)
+
+;; tron
+;;http://www.newgrounds.com/audio/listen/379726
+(p (pattern [kick kick kick kick snare kick kick kick], 3))
+(times 8
+ (p (pattern [kick2 kick2 kick2 kick2 snare2 kick2 kick2 kick2], 2)))
+
+(defn play! [new-pattern]
+ (swap! *pattern* (fn[x] new-pattern)))
+
+;; Drum Demos
+(times 8
+ (p (pattern [kick (+ hihat snare)],2)))
+(times 4
+ (p (pattern [kick (+ hihat snare) [kick kick] (+ hihat snare)],3)))
+(times 4
+ (p (pattern [kick (+ hihat snare) [kick kick kick kick] kick kick kick kick (+ hihat snare)],3)))
+
+
+(defn play-signal [signal]
+ (.setFreq (:data signal) (:pitch signal))
+ (.addSignal @*outp*
+ (:data signal))
+ (Thread/sleep (* (:duration signal) 500))
+ (.removeSignal @*outp*
+ (:data signal)))
+
+(defmacro defsignal [signal-name Signal]
+ `(def ~signal-name
+ (Melement. 50 (float (get-note-freq ~signal-name)) (float 1) play-signal (new ~Signal 440 1 (.sampleRate (.getLineOut @*minim*))))))
+
+(defsignal mysin SquareWave)
+(defsignal mysine SineWave)
+
+(defn setFreq [signal freq]
+ (assoc signal :pitch freq))
+
+;;(p (pattern [kick A5 Bb5]))
+;;(sig sin [A B C D])
+
+(defmacro get-note-freq
+ "Converts Ab4 to notes: A b 4 and return frequency"
+ [snote]
+ `(let [note# (map char (str '~snote))]
+ (calc-freq
+ (if (> (count note#) 2)
+ (zipmap
+ '(:note :half :scale )
+ note#)
+ (zipmap
+ '(:note :scale)
+ note#)))))
+
+(defn calc-freq [sig-note]
+ (*
+ (clojure.core/+
+ (cond (= \C (:note sig-note)) 16.35
+ (= \D (:note sig-note)) 18.35
+ (= \E (:note sig-note)) 20.60
+ (= \F (:note sig-note)) 21.83
+ (= \G (:note sig-note)) 24.50
+ (= \A (:note sig-note)) 27.50
+ (= \B (:note sig-note)) 30.87)
+ (cond (not (contains? sig-note :half)) 0
+ (= \# (:half sig-note)) 1.0594
+ (= \b (:half sig-note)) -1.0594))
+ (Math/pow 2 (Integer/parseInt (str (:scale sig-note))))))
+
+
+
+(defn square []
+ (let [out (.getLineOut @*minim*)
+ sine (.sampleRate out)]
+ (.addSignal out sine)
+ (.setFreq @*sine* 40)
+
+ )
+
+
float pan = map(mouseX, 0, width, -1, 1);
sine.setPan(pan);
(.addSignal @*outp* @*sine*)
(.removeSignal @*outp* @*sine*)
-(.setFreq @*sine* 40)
+(.setFreq @*sine* 410)
(doseq [i (take 400 (repeatedly (fn [] (rand 400))))]
(.setFreq @*sine* i)
(Thread/sleep 100))
-(def sqr
- (SquareWave. 440 0.5 44100))
(.removeSignal @*outp* @*sine*)
@@ -205,3 +263,4 @@
(print "\r" (.toString bar))
(flush)))
+

0 comments on commit 99d35cc

Please sign in to comment.
Something went wrong with that request. Please try again.