Skip to content

Instantly share code, notes, and snippets.

@ethancrawford
Last active November 2, 2017 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethancrawford/9cdf974b0081f22647e497244b4903a8 to your computer and use it in GitHub Desktop.
Save ethancrawford/9cdf974b0081f22647e497244b4903a8 to your computer and use it in GitHub Desktop.
;; This is a WIP attempt to convert SuperCollider's PingPong class
;; https://github.com/supercollider/supercollider/blob/cca12ff02a774a9ea212e8883551d3565bb24a6f/SCClassLibrary/Common/Audio/DelayWr.sc#L1
;; into Overtone/clojure code so that it can be used as part of
;; a larger Overtone synthdef.
;; Underneath the ping-pong cgen below is a variation of the beep
;; synthdef used in Sonic Pi, which I am attempting to add ping pong
;; delay to.
;; I've been attempting to compile and test this according to
;; https://github.com/samaaron/sonic-pi/blob/master/SYNTH_DESIGN.md
;; So far, several errors have been temporarily worked around
;; by hardcoding various numbers in to replace several functions,
;; but there are still errors being generated with this design.
;; (Eventually, a way to bring any necessary functions back in
;; Will need to be found).
(ns sonic-pi.synths.basic
(:use [overtone.live])
(:require [sonic-pi.synths.core :as core]))
(defn rotation [xs n]
"Rotate a list N places to the left."
(let [c (count xs)]
(take c (drop (mod n c) (cycle xs)))))
(defn wrap-val [x min-x max-x]
(let [x-range (- (inc max-x) min-x)]
(-> x (- min-x) (mod x-range) (+ min-x))))
(defcgen ping-pong
"Create a synth-local buffer."
[bufnum {:default 0 :doc "The id of the buffer to use."}
inputs {:doc "The input signals to the effect."}
delay-time {:default 0.2 :doc "The amount of time to delay between outputs"}
feedback {:default 0.5 :doc "The amount of feedback for the effect"}
r {:default 1 :doc "Which output to start the effect on"}
]
(:ar (let [delay-samps (Math/round (max 0 (- (* delay-time 44100) 1024)))
frames (buf-frames:kr bufnum)
phase (phasor:ar 0 1 0 frames)
feedback-channels (* (local-in:ar (count inputs)) feedback)
delayed-signals (buf-rd:ar (count inputs) bufnum (wrap-val ((- phase delay-samps) 0 frames)) 0)]
(local-out:ar delayed-signals)
(buf-wr:ar (rotation (+ inputs feedback-channels) r)))))
(without-namespace-in-synthdef
(defsynth sonic-pi-beep [note 52
note_slide 0
note_slide_shape 1
note_slide_curve 0
amp 1
amp_slide 0
amp_slide_shape 1
amp_slide_curve 0
pan 0
pan_slide 0
pan_slide_shape 1
pan_slide_curve 0
attack 0
decay 0
sustain 0
release 1
attack_level 1
decay_level -1
sustain_level 1
env_curve 1
out_bus 0]
(let [decay_level (select:kr (= -1 decay_level) [decay_level sustain_level])
note (varlag note note_slide note_slide_curve note_slide_shape)
amp (varlag amp amp_slide amp_slide_curve amp_slide_shape)
amp-fudge 1
pan (varlag pan pan_slide pan_slide_curve pan_slide_shape)
freq (midicps note)
snd (sin-osc freq)
env (env-gen:kr (core/shaped-adsr attack decay sustain release attack_level decay_level sustain_level env_curve) :action FREE)
buf (local-buf (* 44100 2) 2)
echo_snd (ping-pong buf [snd snd])
]
(out out_bus (pan2 (* amp-fudge env echo_snd) pan amp)))))
(core/save-synthdef sonic-pi-beep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment