1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
# prc95.rb -- Translation of prc95.scm/prc-toolkit95.lisp to Snd/Ruby
# Perry Cook's Physical Modelling Toolkit
# Translator/Author: Michael Scholz <mi-scholz@users.sourceforge.net>
# Changed: Mon Nov 22 13:28:27 CET 2010
require "ws"
module PRC
def play_all(dur = 1)
with_sound(:clm, false, :play, 1, :statistics, true, :output, "cook.snd") do
beg = 0
plucky(beg, dur, 440, 0.2, 1.0)
beg += dur + 0.2
bow(beg, dur, 440, 0.2, 1.0)
beg += dur + 0.2
brass(beg, dur, 440, 0.2, 1.0)
beg += dur + 0.2
clarinet(beg, dur, 440, 0.2, 1.0)
beg += dur + 0.2
flute(beg, dur, 440, 0.2, 1.0)
end
end
def make_reed(*args)
offset, slope = nil
optkey(args, binding,
[:offset, 0.6],
[:slope, -0.8])
lambda do |samp| [1.0, offset + slope * samp].min end
end
def reedtable(r, sample)
r.call(sample)
end
def make_bowtable(*args)
offset, slope = nil
optkey(args, binding,
[:offset, 0.0],
[:slope, 1.0])
lambda do |samp| [0.0, 1.0 - (slope * (samp + offset)).abs].max end
end
def bowtable(b, sample)
b.call(sample)
end
def jettable(sample)
[-1.0, [1.0, sample * (sample * sample - 1.0)].min].max
end
def make_onezero(*args)
gain, zerocoeff = nil
optkey(args, binding,
[:gain, 0.5],
[:zerocoeff, 1.0])
make_one_zero(gain, gain * zerocoeff)
end
def make_onep(*args)
polecoeff = optkey(args, [:polecoeff, 0.9])
make_one_pole(1.0 - polecoeff, -polecoeff)
end
def set_pole(p, val)
set_mus_b1(p, -val)
set_mus_a0(p, 1.0 - val)
end
def set_gain(p, val)
set_mus_a0(p, mus_a0(p) * val)
end
def lip_set_freq(b, freq)
set_mus_frequency(b, freq)
end
def lip(b, mouthsample, boresample)
temp = formant(b, mouthsample - boresample)
temp = [1.0, temp * temp].min
temp * mouthsample + ((1.0 - temp) * boresample)
end
def make_dc_block
input = output = 0.0
lambda do |samp|
output = samp + (0.99 * output - input)
input = samp
output
end
end
def dc_block(b, sample)
b.call(sample)
end
def make_delaya(len, lag)
lastin = output = 0.0
input = make_delay(len)
outpointer = 2.0 - lag
outpointer += len while outpointer <= 0.0
outpoint = outpointer.floor
alpha = outpointer - outpoint
coeff = (1.0 - alpha) / (1.0 + alpha)
outpoint = -outpoint
lambda do |samp|
delay(input, samp)
temp = tap(input, outpoint)
output = -coeff * output + lastin + temp * coeff
lastin = temp
output
end
end
def delaya(d, sample)
d.call(sample)
end
def make_delayl(len, lag)
input = make_delay(len)
outpointer = 1 - lag
outpointer += len while outpointer <= 0.0
outpoint = outpointer.floor
alpha = outpointer - outpoint
omalpha = 1.0 - alpha
outpoint = -outpoint
lambda do |samp|
delay(input, samp)
tap(input, outpoint - 1) * omalpha + tap(input, outpoint) * alpha
end
end
def delayl(d, sample)
d.call(sample)
end
# sample instruments
def plucky(start, dur, freq, amp, maxa)
len = (mus_srate / 100.0).floor + 1
delayline = make_delaya(len, mus_srate / freq.to_f - 0.5)
filter = make_onezero()
dout = 0.0
len.times do |i| dout = delaya(delayline, 0.99 * dout + maxa * (1.0 - random(2.0))) end
run_instrument(start, dur) do
dout = delaya(delayline, one_zero(filter, dout))
amp * dout
end
end
def bowstr(start, dur, freq, amp, maxa)
len = (mus_srate / 100.0).floor + 1
ratio = 0.8317
temp = mus_srate / freq.to_f - 4.0
neckdelay = make_delayl(len, temp * ratio)
bridgedelay = make_delayl((len / 2.0).floor, temp * (1.0 - ratio))
bowtab = make_bowtable(:slope, 3.0)
filt = make_onep()
rate = 0.001
bowing = true
bowvelocity = rate
maxvelocity = maxa
attackrate = rate
durlen = seconds2samples(dur)
ctr = 0
release = (0.8 * durlen).floor
bridgeout = 0.0
neckout = 0.0
set_pole(filt, 0.6)
set_gain(filt, 0.3)
run_instrument(start, dur) do
bridgerefl = nutrefl = veldiff = stringvel = bowtemp = 0.0
if bowing
unless maxvelocity == bowvelocity
if bowvelocity < maxvelocity
bowvelocity += attackrate
else
bowvelocity -= attackrate
end
end
else
if bowvelocity > 0.0
bowvelocity -= attackrate
end
end
bowtemp = 0.3 * bowvelocity
filt_output = one_pole(filt, bridgeout)
bridgerefl = -filt_output
nutrefl = -neckout
stringvel = bridgerefl + nutrefl
veldiff = bowtemp - stringvel
veldiff = veldiff * bowtable(bowtab, veldiff)
neckout = delayl(neckdelay, bridgerefl + veldiff)
bridgeout = delayl(bridgedelay, nutrefl + veldiff)
result = amp * 10.0 * filt_output
if ctr == release
bowing = false
attackrate = 0.0005
end
ctr += 1
result
end
end
def brass(start, dur, freq, amp, maxa)
len = (mus_srate / 100.0).floor + 1
delayline = make_delaya(len, 1.0 + mus_srate / freq.to_f)
lipfilter = make_formant()
dcblocker = make_dc_block()
blowing = true
rate = 0.001
breathpressure = 0.0
maxpressure = maxa
attackrate = rate
durlen = seconds2samples(dur)
release = (0.8 * durlen).floor
ctr = 0
dout = 0.0
lip_set_freq(lipfilter, freq)
run_instrument(start, dur) do
if blowing
unless maxpressure == breathpressure
if breathpressure < maxpressure
breathpressure += attackrate
else
breathpressure -= attackrate
end
end
else
if breathpressure > 0.0
breathpressure -= attackrate
end
end
dout = delaya(delayline,
dc_block(dcblocker, lip(lipfilter, 0.3 * breathpressure, 0.9 * dout)))
result = amp * dout
if ctr == release
blowing = false
attackrate = 0.0005
end
ctr += 1
result
end
end
def clarinet(start, dur, freq, amp, maxa)
len = (mus_srate / 100.0).floor + 1
delayline = make_delayl(len, 0.5 * (mus_srate / freq.to_f) - 1.0)
rtable = make_reed(:offset, 0.7, :slope, -0.3)
filter = make_onezero()
blowing = true
breathpressure = 0.0
rate = 0.001
maxpressure = maxa
attackrate = rate
durlen = seconds2samples(dur)
release = (0.8 * durlen).floor
ctr = 0
dout = 0.0
run_instrument(start, dur) do
pressurediff = 0.0
if blowing
unless maxpressure == breathpressure
if breathpressure < maxpressure
breathpressure += attackrate
else
breathpressure -= attackrate
end
end
else
if breathpressure > 0.0
breathpressure -= attackrate
end
end
pressurediff = one_zero(filter, -0.95 * dout) - breathpressure
dout = delayl(delayline,
breathpressure + pressurediff * reedtable(rtable, pressurediff))
result = amp * dout
if ctr == release
blowing = false
attackrate = 0.0005
end
ctr += 1
result
end
end
def flute(start, dur, freq, amp, maxa)
len = (mus_srate / 100.0).floor + 1
ratio = 0.8
temp = mus_srate / freq.to_f - 0.5
jetdelay = make_delayl((len / 2.0).floor, temp * (1.0 - ratio))
boredelay = make_delayl(len, ratio * temp)
filter = make_onep()
dcblocker = make_dc_block()
jetrefl = 0.6
sinphase = 0.0
blowing = true
breathpressure = 0.0
rate = 0.0005
maxpressure = maxa
attackrate = rate
durlen = seconds2samples(dur)
release = (0.8 * durlen).floor
ctr = 0
dout = 0.0
set_pole(filter, 0.8)
set_gain(filter, -1.0)
run_instrument(start, dur) do
randpressure = 0.1 * breathpressure * random(1.0)
temp = 0.0
pressurediff = 0.0
sinphase += 0.0007
sinphase -= 6.28 if sinphase > 6.28
randpressure = randpressure + 0.05 * breathpressure * sin(sinphase)
if blowing
unless maxpressure == breathpressure
if breathpressure < maxpressure
breathpressure += attackrate
else
breathpressure -= attackrate
end
end
else
if breathpressure > 0.0
breathpressure -= attackrate
end
end
temp = dc_block(dcblocker, one_pole(filter, dout))
pressurediff = jettable(delayl(jetdelay,
breathpressure + (randpressure - jetrefl * temp)))
dout = delayl(boredelay, pressurediff)
result = 0.3 * amp * dout
if ctr == release
blowing = false
attackrate = 0.0005
end
ctr += 1
result
end
end
end
include PRC
# prc95.rb ends here
|