;; code_2.sal -- examples using unit generators load "pianosyn" play piano-note(5, c3, 100) ;; use multiply this by an envelope play piano-note(5, c3, 100) * pwl(0.6, 1, 1.2, 1, 1.6) ;; let's encapsulate function pno() return piano-note(5, c3, 100) function env1() return pwl(0.6, 1, 1.2, 1, 1.6) function pe() return pno() * env1() ;; what does it look like? exec s-plot(pe()) ;; what does the envelope look like? exec s-plot(env1()) ;; modulate by a sinusoid, duration of lfo = 1.0s play pe() * lfo(5) ;; streth lfo to length of pe() (could write lfo(5) ~ 1.6) play pe() * lfo(5, 1.6) exec s-plot(lfo(5, 1.6)) exec s-plot(pe() * lfo(5, 1.6)) ;; do something more harsh exec s-plot(osc-pulse(10, 0)) exec s-plot(osc-pulse(10, 0) + 1) play pe() * ((osc-pulse(10, 0) + 1) ~ 1.6) ;; modulate the frequency of the pulses function pulse-freq() return pwev(5, 1.6, 200) exec s-plot(osc-pulse(pulse-freq(), 0)) play (pe() * (osc-pulse(pulse-freq(), 0) + 1)) ~ 3 ;; let's make some classic table-lookup instruments ;; STEP 1. Make a sine tone print *table* exec s-plot(first(*table*)) play osc(c4, 1, *table*) ;; STEP 2. Make a more interesting table variable *table1* = list( sim(0.50 * build-harmonic(2.0, 2048), -0.50 * build-harmonic(3.0, 2048), 0.50 * build-harmonic(5.0, 2048), -0.50 * build-harmonic(7.0, 2048), 0.50 * build-harmonic(8.0, 2048), -0.40 * build-harmonic(12.0, 2048), 0.40 * build-harmonic(13.0, 2048), -0.25 * build-harmonic(15.0, 2048), 0.25 * build-harmonic(16.0, 2048), -0.25 * build-harmonic(17.0, 2048), 0.25 * build-harmonic(18.0, 2048), -0.14 * build-harmonic(19.0, 2048), 0.14 * build-harmonic(20.0, 2048)), hz-to-step(1), #t) exec s-plot(first(*table1*)) play osc(c4, 1, *table1*) ;; STEP 3. Add an envelope play osc(c4, 1, *table1*) * env(0.01, 0.05, 0.3, 1, 0.8, 0.2) ;; STEP 4. Encapsulate function bright(pitch: c4) return osc(pitch, 1, *table1*) * env(0.01, 0.05, 0.3, 1, 0.8, 0.2) play seqrep(i, 10, bright(pitch: c4 + random(12)) ~ real-random(0.1, 1.0)) ;; back to unit generators: modulate our work so far function little-piece() return seqrep(i, 10, bright(pitch: c2 + random(12)) ~ real-random(0.1, 1.0)) play little-piece() play little-piece() * pwlv(0.3, 2, 1, 5, 0.3, 100, 0) play little-piece() * sine(A4, 100) play little-piece() * lfo(3, 100) * pwlv(0.3, 2, 1, 20) ;;************************************************** ;;* EXAMPLES FOR WEEK 2, DAY 2 -- LISTS AND SCORES * ;;************************************************** ;; Make and play a simple score, using the "bright" instrument ;; (be sure bright() is defined for the following) set tune = {{0 1 {bright pitch: c2}} {1 0.3 {bright pitch: d2}} {1.2 0.3 {bright pitch: f2}} {1.4 0.3 {bright pitch: g2}} {1.6 0.3 {bright pitch: a2}} {1.8 2 {bright pitch: g2}}} exec score-play(tune) ~ 0.6 ;; constructing a score from a list function gen-score(data) begin with score loop for start in data for dur = 0.2 * (rrandom() + 0.5) for pitch = random(12) + 36 set score @= list(start, dur, list(quote(bright), keyword(pitch), pitch)) end return reverse(score) end ;; look at the score generated by gen-score() set start-times = {0 1 2 3 4} exec score-print(gen-score(start-times)) ;; play the score exec score-play(gen-score(start-times)) ;; try some variations set start-times2 = {0 1 1.1 1.2 1.3 1.4 1.5 1.6 1.8 1.9 2.1 2.2 2.5 2.6 2.9 3.0 3.1} exec score-play(gen-score(start-times2)) exec score-play(gen-score(start-times2)) ~ 2