;; code_6.sal -- sampling ;; PLAYING A SOUND FROM A FILE ;; ;; first, find a tone in a file and play it ;; set the sound file directory set *default-sf-dir* = "/Users/rbd/class/icm2013/" ;; play the file ;play s-read("cello.wav") ;; play pitch-shifted sample with envelope ;; we need an envelope that is 1 until fade-out in the last 0.1s ;; Also, we'll enforce a maximum duration function fadeout(maxdur) begin with d = min(get-duration(1), maxdur) return pwlv(1, d - 0.1, d) ~~ 1 end set sample-dur = 0.87 ; duration of sample ;; Now multiply cello sample by fadeout: function cello(pitch: c4) begin with factor = 357.0 / step-to-hz(pitch), maxdur = sample-dur * factor display "cello", factor, maxdur ;; read sample and stretch to obtain pitch return sound(s-read("cello.wav")) ~~ factor * ;; maximum duration is stretched by factor fadeout(sample-dur * factor) end ;; play some cello notes ;play seq(cello(pitch: c4), cello(pitch: d4), cello(pitch: e4)) ~ 0.5 ;; MAKING SPECIFICATIONS FOR SAMPLER FUNCTION WHICH LOOPS ;; from Audacity, find some possible loop points. startt is where ;; to start the loop. endt is where to end the loop. These are ;; read from Audacity in samples and divided by 44100 to get seconds. function cello2(pitch: c4) begin set startt = 23217 / 44100.0 set endt = 30267 / 44100.0 ;; read only to the end of the loop set sample = s-read("cello.wav", time-offset: 0, dur: endt) ;; try it out. The parameters are: ;; pitch -- desired pitch in steps ;; const(0, 4) -- frequency modulation and duration ;; no modulation for 4 seconds here ;; list(...) -- sample specification with three elements: ;; sample -- the actual sound ;; hz-to-step(357) -- step of unstretched sample ;; startt -- the point to loop to return sampler(pitch, const(0, 1), list(sample, hz-to-step(357), startt)) * fadeout(100) end ;; try some cello2 notes: play seq(cello2(pitch: c4), cello2(pitch: d4), cello2(pitch: e4)) ~ 2 ;; NOTE: there are some pops in the sound. This might be improved ;; with a better splice point. Better yet, the end of the sample ;; might be cross-faded to the beginning to mask any discontinuity