Skip to content

Instantly share code, notes, and snippets.

@ctford
ctford / lydia.cljs
Last active September 22, 2016 13:34
A livecoded piece for Klangmeister
(defn synth [note]
(connect->
(add (sawtooth (:pitch note)))
;(low-pass (connect-> (sine 4.5) (gain 660)))
(adsr 2.7 0.3 0.5 0.01)
(gain 0.15)))
(defn shorten [section]
(->> section (take-while #(-> % :time (< 8)))))
@ctford
ctford / demo.cljs
Last active February 27, 2016 20:31
A demonstration of Klangmeister, designed to be loaded through the URL http://ctford.github.io/klangmeister/?gist=4b04fd7f2d361c6604c4
; A synthesiser we can play our piece on.
(defn synth [note]
(connect->
(add (square (* 1.01 (:pitch note))) (sawtooth (:pitch note)))
(low-pass 600)
(adsr 0.001 0.4 0.5 0.1)
(gain 0.15)))
(def melody
; The durations and pitches of the notes. Try changing them.
@ctford
ctford / processing.clj
Created December 24, 2015 23:29
Evaluating CLJS in the context of music fns.
(ns leipzig-live.processing
(:require
[leipzig-live.music :as music]
[leipzig-live.instruments :as instrument]
[leipzig-live.actions :as action]
[leipzig-live.framework :as framework]
[cljs.js :as cljs]))
(defn add-namespace [expr-str]
(str
@ctford
ctford / tail-position-problems.clj
Created December 22, 2015 02:47
A refactoring that breaks the tail call.
(defn factorial [n]
(letfn [(factorial' [n tally]
(if (zero? n)
tally
(factorial' (dec n) (* n tally))))]
(factorial' n 1)))
(defn factorial [n]
(letfn [(factorial' [n tally]
(if (zero? n)
@ctford
ctford / scared.rb
Created September 28, 2015 17:55
You scared the life out of me
# Welcome to Sonic Pi v2.8.0-dev-9d58e
use_bpm 120
use_synth :blade
define :melody do |pitches, sleeps|
durations = sleeps.to_a # Make sure it's indexable.
pitches.each_with_index do |pitch, i|
play(pitch, sustain: durations[i] - 0.4)
sleep durations[i]
end
@ctford
ctford / lens.clj
Created June 21, 2015 22:53
Failure to type "each"
(ns traversy.lens
(:require [clojure.core.typed :as typed]))
(typed/defalias Focus
(typed/TFn [[a :variance :contravariant] [b :variance :covariant]] [a -> (typed/Option (typed/NonEmptySeq b))]))
(typed/defalias Fmap
(typed/TFn [[a :variance :invariant] [b :variance :invariant]] [[b -> b] a -> a]))
(typed/defalias Lens
@ctford
ctford / dag.sh
Last active August 29, 2015 14:21
A sketch of an alternative git porcelain.
# dag is based on git's underlying directed acyclic graph of commits.
#
# There are "labels" (ie branches) that you can "extend" from (ie commit).
dag import "https://github..." # Clone a project from Github.
# cd project
# create some files
dag extend "Change some stuff" # Commit *all* changes (no staging of individual files).
dag label "bug-fix" # Create a branch, which is really just a label.
dag switch "bug-fix" # Checkout the new branch.
@ctford
ctford / deftrack.clj
Created April 12, 2015 22:29
An unhygienic macro for generating the standard template I use for instruments.
(defmacro deftrack
"Defines an instrument with frequency, duration, volume, position and wet pre-defined.
NB: these variables will shadow any instances in a wider scope.
(deftrack simple [vibrato 3]
(* (saw frequency) (sin-osc vibrato)) ; Signal
(adsr)) ; Envelope
(simple :frequency 440 :position -1 :wet 1)
@ctford
ctford / boids.cljs
Created March 31, 2015 20:24
Independent boids
(ns universe.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(def app-state (atom {:text "Hello Boids"
:birds (map vector
(repeatedly 20 #(rand-int 1000))
(repeatedly 20 #(rand-int 1000)))}))
(defn make-bird [[x y]]
@ctford
ctford / parse.clj
Created March 27, 2015 23:29
Parsing with Traversy
(defn integer [s] (java.lang.Integer/parseInt s))
(defn parse [from-str]
(lens (fn [x] (-> x from-str list))
(fn [f x] (-> x from-str f str))))
(fact "The 'parse' lens focuses on a value in a string."
(-> "2015" (view (parse integer))) => [2015]
(-> "2015" (update (parse integer) inc)) => "2016")
(defn catching [thunk x] (try (thunk) (catch Exception e x)))