Created
January 3, 2024 21:36
-
-
Save Alwinfy/28e541dea26b8030dbee14995310efae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; wavetables | |
(def andW (make-wt 8000 | |
(fn [x] (if (> x 0.5) 1 -1)))) | |
(def xorW (make-wt 8000 | |
(fn [x] (if (< (abs x) 0.5) 1 -1)))) | |
(def bufferW (make-wt 8000 | |
(fn [x] (if (> x 0.01) 1 -1)))) | |
(def notW (hardsat -10)) | |
; waveshapers | |
(defn and~ [l r] (shape~ (+~ l r) andW)) | |
(defn xor~ [l r] (shape~ (+~ l r) xorW)) | |
(defn buffer~ [l] (shape~ l bufferW)) | |
(defn not~ [l] (shape~ l notW)) | |
; gate a signal | |
(defn gate~ [sig value] | |
(gain~ sig (unipolar (filter~ value 100 :lowpass)))) | |
; latch with hysteresis | |
(defn d-latch [in clk] | |
; the gain~ here has params swapped on purpose | |
; since it registers as 'dead' otherwise | |
(def fb (delay~ (gain~ in (unipolar clk)) 0)) | |
(def out (buffer~ fb)) | |
(connect! (gain~ out 0.6) fb) | |
(connect! (sig~ 0) fb) | |
out) | |
; memory | |
(defn d-flop [in clk] | |
(let [clk- (gain~ clk -1)] | |
(-> in (d-latch clk) (d-latch clk-)))) | |
(defn ripple-counter1 [clk] | |
(def bit-in (delay~ nil 0)) | |
(def bit (d-flop (not~ bit-in) clk)) | |
(connect! bit bit-in) | |
bit) | |
(defn ripple-counter [clk n] | |
(if (zero? n) | |
nil | |
(let [ctr (ripple-counter1 clk)] | |
(cons ctr | |
(ripple-counter ctr (dec n)))))) | |
; my beloved | |
(defn four-bit-counter [clk enable] | |
(def zero-in (delay~ nil 0)) | |
(def zero-bit (d-flop (xor~ zero-in enable) clk)) | |
(connect! zero-bit zero-in) | |
(def one (and~ zero-bit enable)) | |
(def one-in (delay~ nil 0)) | |
(def one-bit (d-flop (xor~ one one-in) clk)) | |
(connect! one-bit one-in) | |
(def three (and~ one one-bit)) | |
(def two-in (delay~ nil 0)) | |
(def two-bit (d-flop (xor~ three two-in) clk)) | |
(connect! two-bit two-in) | |
(def seven (and~ three two-bit)) | |
(def three-in (delay~ nil 0)) | |
(def three-bit (d-flop (xor~ seven three-in) clk)) | |
(connect! three-bit three-in) | |
[[three-bit two-bit one-bit zero-bit] (and~ three-bit seven)]) | |
(defn saw~ [freq] | |
(filter~ | |
(osc~ freq :type :sawtooth) | |
(* 4 freq) :lowpass :q 0.2)) | |
(out~ :4bc | |
(let [clock (osc~ 8 :type :square) | |
[[b3 b2 b1 b0] e] (four-bit-counter clock (sig~ 1)) | |
[[b7 b6 b5 b4] _] (four-bit-counter clock e)] | |
(apply mix~ | |
(map | |
(fn [note bit] | |
(gate~ (saw~ (mtof (+ note 48))) bit)) | |
[ 2 7 12 16 19 23 26 30] | |
[b0 b1 b2 b3 b4 b5 b6 b7])))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment