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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
;;; various mix related functions
;;;
;;; (mix->float-vector mix) return mix data in float-vector
;;; (snap-mix-to-beat) forces dragged mix to end up on a beat
;;; (silence-all-mixes) sets all mix amps to 0.0
;;; (find-mix sample snd chn) returns the mix at the given sample, or #f
;;; (mix-maxamp mix) maxamp of mix
;;;
;;; mix-click-sets-amp sets up hook functions so that mix click zeros amps, then subsequent click resets to the before-zero value
;;; check-mix-tags tries to move mix tags around to avoid collisions
(require snd-env.scm)
(provide 'snd-mix.scm)
(define tree-for-each
(let ((+documentation+ "(tree-for-each func tree) applies func to every leaf of 'tree'"))
(lambda (func tree)
(cond ((null? tree) ())
((not (pair? tree)) (func tree))
(else (tree-for-each func (car tree))
(tree-for-each func (cdr tree)))))))
(define tree-for-each-reversed
(let ((+documentation+ "(tree-for-each-reversed func tree) applies func to every leaf of 'tree' moving in reverse through all the lists"))
(lambda (func tree)
(for-each func
(reverse
(let flatten ((lst tree))
(cond ((null? lst) ())
((not (pair? lst)) lst)
((pair? (car lst)) (append (flatten (car lst)) (flatten (cdr lst))))
(else (cons (car lst) (flatten (cdr lst)))))))))))
(define mix-sound
(let ((+documentation+ "(mix-sound file start) mixes file (all chans) at start in the currently selected sound."))
(lambda (file start)
(mix file start #t))))
(define silence-all-mixes
(let ((+documentation+ "(silence-all-mixes) sets all mix amps to 0"))
(lambda ()
(as-one-edit
(lambda ()
(tree-for-each
(lambda (id)
(set! (mix-amp id) 0.0))
(mixes)))))))
(define find-mix
(let ((+documentation+ "(find-mix sample snd chn) returns the mix at the given sample, or #f"))
(lambda* (samp snd chn)
(let ((mix-list (mixes (or snd (selected-sound) (car (sounds))) (or chn (selected-channel snd) 0))))
(call-with-exit
(lambda (found-it)
(for-each
(lambda (n)
(if (= (mix-position n) samp)
(found-it n)))
mix-list)
#f))))))
(define (mix->float-vector id) (samples 0 (mix-length id) id))
;;; 12-Nov-09: moved save-mix to C (snd-mix.c)
(define (mix-maxamp id) (float-vector-peak (mix->float-vector id)))
;;; -------- snap dragged mix(es) to the nearest beat
(define (snap-mix-1 id samps-moved)
(let* ((samp (+ samps-moved (mix-position id)))
(snd (car (mix-home id)))
(bps (/ (beats-per-minute snd (cadr (mix-home id))) 60.0))
(sr (srate snd))
(beat (floor (/ (* samp bps) sr))))
(let ((lower (floor (/ (* beat sr) bps)))
(higher (floor (/ (* (+ 1 beat) sr) bps))))
(set! (mix-position id)
(if (< (- samp lower) (- higher samp))
(max 0 lower)
higher))
#t)))
(define snap-mix-to-beat
(let ((+documentation+ "(snap-mix-to-beat) forces a dragged mix to end up on a beat (see beats-per-minute). (hook-remove mix-release-hook snap-mix-1) to cancel."))
(lambda ()
(hook-push mix-release-hook (lambda (hook) (set! (hook 'result) (snap-mix-1 (hook 'id) (hook 'samples))))))))
(define (snap-syncd-mixes-1 id samps-moved)
(let* ((new-position (let* ((samp (+ samps-moved (mix-position id)))
(snd (car (mix-home id)))
(bps (/ (beats-per-minute snd (cadr (mix-home id))) 60.0))
(sr (srate snd))
(beat (floor (/ (* samp bps) sr))))
(let ((lower (floor (/ (* beat sr) bps)))
(higher (floor (/ (* (+ 1 beat) sr) bps))))
(if (< (- samp lower) (- higher samp))
(max 0 lower)
higher))))
(true-samps-moved (- new-position (mix-position id))))
(if (= (sync id) 0)
(set! (mix-position id) new-position)
(move-mixes (syncd-mixes (sync id)) true-samps-moved))
#t))
(define snap-syncd-mixes-to-beat
(let ((+documentation+ "(snap-mix-to-beat) forces a dragged mix to end up on a beat (see beats-per-minute). \
All mixes sync'd to it are also moved the same number of samples. (hook-remove mix-release-hook snap-syncd-mixes-1) to cancel."))
(lambda ()
(hook-push mix-release-hook (lambda (hook) (set! (hook 'result) (snap-syncd-mixes-1 (hook 'id) (hook 'samples))))))))
;;; -------- mix-click-sets-amp
(define (mix-click-sets-amp)
(hook-push mix-click-hook
(lambda (hook)
(let* ((n (hook 'id))
(zeroed (mix-property :zero n)))
(if zeroed
(begin
(set! (mix-amp n) (mix-property :amp n))
(set! (mix-property :zero n) #f))
(begin
(set! (mix-property :amp n) (mix-amp n))
(set! (mix-amp n) 0.0)
(set! (mix-property :zero n) #t)))
(set! (hook 'result) #t)))))
;(mix-click-sets-amp)
;;; ---------- mix-click-info
(define mix-click-info
(let ((+documentation+ "(mix-click-info n) is a mix-click-hook function that describes a mix and its properties"))
(lambda (n)
(help-dialog "Mix Help"
(format #f "Mix ~A (sync: ~A):~% position: ~D = ~,3F secs~% length: ~D (~,3F secs)~% in: ~A[~D]~% scaler: ~A~% speed: ~A~% env: ~A~A"
(format #f (if (string=? (mix-name n) "")
(values "~A" n)
(values "~S (~A)" (mix-name n) n)))
(mix-sync n)
(mix-position n)
(* 1.0 (/ (mix-position n) (srate (car (mix-home n)))))
(framples n)
(* 1.0 (/ (framples n) (srate (car (mix-home n)))))
(short-file-name (car (mix-home n)))
(cadr (mix-home n))
(mix-amp n)
(mix-speed n)
(mix-amp-env n)
(let ((props (mix-properties n)))
(if (pair? props)
(format #f "~% properties: '~A" props)
""))))
#t)))
;(hook-push mix-click-hook (lambda (hook) (set! (hook 'result) (mix-click-info (hook 'id)))))
;;; -------- mix-name->id
(define mix-name->id
(let ((+documentation+ "(mix-name->id name) returns the mix associated with 'name'"))
(lambda (name)
(call-with-exit
(lambda (return)
(for-each
(lambda (snd)
(do ((chn 0 (+ 1 chn)))
((= chn (channels snd)))
(for-each
(lambda (m)
(if (equal? (mix-name m) name)
(return m)))
(mixes snd chn))))
(sounds))
'no-such-mix)))))
;;; ---------------- backwards compatibilty
(define delete-mix
(let ((+documentation+ "(delete-mix mix) sets the mix's amp to 0.0"))
(lambda (id)
(set! (mix-amp id) 0.0))))
;;; -------- mix lists (used to be called "tracks")
;;;
;;; to use these based on a mix-sync setting, use syncd-mixes below:
;;; (scale-mixes (syncd-mixes 2) 2.0) scales all mixes whose mix-sync field is 2 by 2.0.
(define scale-mixes
(let ((+documentation+ "(scale-mixes mix-list scl) scales the amplitude of each mix in 'mix-list' by 'scl'"))
(lambda (mix-list scl)
(as-one-edit
(lambda ()
(for-each
(lambda (m)
(set! (mix-amp m) (* scl (mix-amp m))))
mix-list))))))
(define silence-mixes
(let ((+documentation+ "(silence-mixes mix-list) sets the amplitude of each mix in 'mix-list' to 0.0"))
(lambda (mix-list)
(scale-mixes mix-list 0.0))))
(define move-mixes
(let ((+documentation+ "(move-mixes mix-list samps) moves each mix in 'mix-list' by 'samps' samples"))
(lambda (mix-list samps)
(as-one-edit
(lambda ()
(for-each
(lambda (m)
(set! (mix-position m) (max 0 (+ (mix-position m) samps))))
mix-list))))))
(define src-mixes
(let ((+documentation+ "(src-mixes mix-list sr) multiplies the speed (resampling ratio) of each mix in 'mix-list' by 'sr'"))
(lambda (mix-list sr)
(if (not (= sr 0.0))
(as-one-edit
(lambda ()
(for-each
(lambda (m)
(set! (mix-speed m) (* (mix-speed m) sr)))
mix-list)))))))
(define transpose-mixes
(let ((+documentation+ "(transpose-mixes mix-list semitones) transposes each mix in mix-list by semitones"))
(lambda (mix-list semitones)
(if (not (= semitones 0))
(src-mixes mix-list (expt 2.0 (/ semitones 12.0)))))))
(define color-mixes
(let ((+documentation+ "(color-mixes mix-list color) sets the tag and waveform color of each mix in 'mix-list' to 'color'"))
(lambda (mix-list col)
(for-each
(lambda (m)
(set! (mix-color m) col))
mix-list))))
(define set-mixes-tag-y
(let ((+documentation+ "(set-mixes-tag-y mix-list new-y) sets the mix tag vertical position of each mix in 'mix-list' to 'new-y'. The \
position is measured from the top of the graph, so higher tag-y values position the tag lower in the graph. For \
example, if you know the frequency of the mix sound, you can reflect that in the tag height with: \n\n\
\n\
(set! (mix-tag-y mix-id) (round (* 100 (- 1.0 (/ (log (/ freq 40.0)) (* (log 2.0) 7))))))\n"))
(lambda (mix-list new-y)
(for-each
(lambda (m)
(set! (mix-tag-y m) new-y))
mix-list))))
(define mixes-maxamp
(let ((+documentation+ "(mixes-maxamp mix-list) returns the maximum amplitude of the data in the mixes in 'mix-list'"))
(lambda (mix-list)
(let ((mx 0.0))
(for-each
(lambda (m)
(set! mx (max mx (mix-maxamp m))))
mix-list)
mx))))
(define scale-tempo
(let ((+documentation+ "(scale-tempo mix-list scl) changes the rate at which the mixes in 'mix-list' occur to reflect \
the tempo scaler 'scl'. If 'scl' is 2.0, for example, the mixes are re-positioned so that they \
happen twice as slowly (the data is not resampled -- each mix is untouched except that its begin time \
may change)"))
(lambda (mix-list tempo-scl)
(let* ((first-beg (mix-position (car mix-list)))
(last-beg first-beg))
(for-each
(lambda (m)
(let ((pos (mix-position m)))
(set! first-beg (min first-beg pos))
(set! last-beg (max last-beg pos))))
(cdr mix-list))
(as-one-edit
(lambda ()
(for-each
(lambda (m)
(let ((diff (round (* tempo-scl (- (mix-position m) first-beg)))))
(if (not (= diff 0))
(set! (mix-position m) (+ first-beg diff)))))
mix-list)))))))
;;; reverse-mix-list is (scale-tempo mix-list -1.0)
(define mixes-length
(let ((+documentation+ "(mixes-length mix-list) returns the number of samples between the start of the earliest mix and the \
last end of the mixes in 'mix-list'"))
(lambda (mix-list)
(- (+ (apply max (map (lambda (m)
(+ (mix-position m) (framples m)))
mix-list))
1)
(apply min (map mix-position mix-list))))))
(define env-mixes
(let ((+documentation+ "(env-mixes mix-list amp-env) applies 'amp-env' as a global amplitude envelope to the mixes in 'mix-list'"))
(lambda (mix-list overall-amp-env)
(let* ((mix-begs (map mix-position mix-list))
(mix-ends (map (lambda (m) (+ (mix-position m) (framples m))) mix-list))
(beg (apply min mix-begs))
(end (apply max mix-ends))
(first-x (car overall-amp-env))
(x-scale (let ((last-x (envelope-last-x overall-amp-env)))
(/ (- last-x first-x) (- (+ end 1) beg)))))
(as-one-edit
(lambda ()
(for-each
(lambda (m)
(let ((wenv (let ((beg-x (+ first-x (* x-scale (- (mix-position m) beg))))
(end-x (+ first-x (* x-scale (- (+ (mix-position m) (framples m)) beg)))))
(window-envelope beg-x end-x overall-amp-env))))
(set! (mix-amp-env m) (if (null? (mix-amp-env m))
wenv
(multiply-envelopes (mix-amp-env m) wenv)))))
mix-list)))))))
(define sync-all-mixes
;; a replacement for set-all-tracks in snd-8
(let ((+documentation+ "(sync-all-mixes (new-sync 1)) sets the mix-sync field of every active mix to new-sync"))
(lambda* ((new-sync 1))
(for-each
(lambda (snd-m)
(for-each
(lambda (chn-m)
(for-each
(lambda (m)
(set! (sync m) new-sync))
chn-m))
snd-m))
(mixes)))))
(define syncd-mixes
(let ((+documentation+ "(syncd-mixes val) returns a list (possibly null) of all mixes whose mix-sync field is set to 'val'"))
(lambda (val)
(if (<= val 0)
()
(let ((mix-list ()))
(for-each
(lambda (snd-m)
(for-each
(lambda (chn-m)
(for-each
(lambda (m)
(if (= (sync m) val)
(set! mix-list (cons m mix-list))))
chn-m))
snd-m))
(mixes))
mix-list)))))
(define play-mixes
(let ((+documentation+ "(play-mixes mix-list) plays the mixes in 'mix-list'"))
(lambda (mix-list)
(let* ((sorted-mixes (sort! (copy mix-list) (lambda (a b) (< (mix-position a) (mix-position b)))))
(now (mix-position (car sorted-mixes))))
(play (lambda ()
(while (and (pair? sorted-mixes)
(= now (mix-position (car sorted-mixes))))
(play (let ((mx (car sorted-mixes)))
(if (integer? mx)
(integer->mix mx)
mx)))
(set! sorted-mixes (cdr sorted-mixes)))
(set! now (+ 1 now))
(and (pair? sorted-mixes)
0.0)))))))
;;; -------- pan-mix --------
(define pan-mix
(letrec ((+documentation+ "(pan-mix file start pan-env snd (auto-delete #f)) mixes 'file' into the sound 'snd'
starting at 'start' (in samples) using 'pan-env' to decide how to split the sound between the output channels (0: all chan 0, 1: all chan 1).
So, (pan-mix \"oboe.snd\" 0 '(0 0 1 1)) goes from all chan 0 to all chan 1.
'auto-delete' determines whether the in-coming file should be treated as a temporary file and deleted when the mix
is no longer accessible. pan-mix returns a list of the mixes performing the
panning operation.")
(invert-envelope
(lambda (e)
(if (null? e)
()
(cons (car e)
(cons (- 1.0 (cadr e))
(invert-envelope (cddr e))))))))
(lambda* (name beg pan snd auto-delete)
(let ((deletion-choice (if auto-delete 3 0))) ; multichannel deletion case
(let ((index (or snd (selected-sound) (and (sounds) (car (sounds)))))
(end-deletion-choice (if (= deletion-choice 3) 4 0)))
(if (not (sound? index))
(error 'no-such-sound (list "pan-mix" snd)))
(if (not (file-exists? name))
(error 'no-such-file (list "pan-mix" name)))
(as-one-edit
(lambda ()
(let ((incoming-chans (channels name))
(receiving-mono (= (channels index) 1)))
(if (= incoming-chans 1)
;; mono input
(if receiving-mono
;; mono to mono = just scale or envelope
(let ((idx (mix name beg 0 index 0 *with-mix-tags* auto-delete))) ; file start in-chan snd chn ...
(and (pair? idx)
(mix? (car idx))
(let ((id (car idx)))
(set! (mix-amp-env id) (invert-envelope pan))
idx)))
;; mono to stereo
(let* ((idx0 (mix name beg 0 index 0 *with-mix-tags* deletion-choice))
(idx1 (mix name beg 0 index 1 *with-mix-tags* end-deletion-choice)))
(and (pair? idx0)
(mix? (car idx0))
(pair? idx1)
(mix? (car idx1))
(let ((id0 (car idx0))
(id1 (car idx1)))
(set! (mix-amp-env id0) (invert-envelope pan))
(set! (mix-amp-env id1) pan)
(list id0 id1)))))
;; stero input
(if receiving-mono
;; stereo -> mono => scale or envelope both input chans into the output
(let* ((idx0 (mix name beg 0 index 0 *with-mix-tags* deletion-choice))
(idx1 (mix name beg 1 index 0 *with-mix-tags* end-deletion-choice)))
(and (pair? idx0)
(mix? (car idx0))
(pair? idx1)
(mix? (car idx1))
(let ((id0 (car idx0))
(id1 (car idx1)))
(set! (mix-amp-env id0) (invert-envelope pan))
(set! (mix-amp-env id1) (invert-envelope pan))
(list id0 id1))))
;; stereo -> stereo => incoming chans are treated equally, each panned into outputs
(let* ((idx00 (mix name beg 0 index 0 *with-mix-tags* deletion-choice))
(idx01 (mix name beg 0 index 1 *with-mix-tags* deletion-choice))
(idx10 (mix name beg 1 index 0 *with-mix-tags* deletion-choice))
(idx11 (mix name beg 1 index 1 *with-mix-tags* end-deletion-choice)))
(and (pair? idx00)
(mix? (car idx00))
(pair? idx01)
(mix? (car idx01))
(pair? idx10)
(mix? (car idx10))
(pair? idx11)
(mix? (car idx11))
(let ((id00 (car idx00))
(id01 (car idx01))
(id10 (car idx10))
(id11 (car idx11)))
(set! (mix-amp-env id00) (invert-envelope pan))
(set! (mix-amp-env id10) (invert-envelope pan))
(set! (mix-amp-env id01) pan)
(set! (mix-amp-env id11) pan)
(list id00 id01 id10 id11))))))))))))))
(define pan-mix-selection
(let ((+documentation+ "(pan-mix-selection start pan-env snd) mixes the current selection into the sound 'snd'
starting at 'start' (in samples) using 'pan-env' to pan (0: all chan 0, 1: all chan 1)."))
(lambda* (beg pan snd)
(if (not (selection?))
(error 'no-active-selection (list "pan-mix-selection"))
(pan-mix (save-selection (snd-tempnam)) beg pan snd #t)))))
(define pan-mix-region
(let ((+documentation+ "(pan-mix-region reg start pan-env snd) mixes the given region into the sound 'snd'
starting at 'start' (in samples) using 'pan-env' to pan (0: all chan 0, 1: all chan 1)."))
(lambda* (reg beg pan snd)
(if (not (region? reg))
(error 'no-such-region (list "pan-mix-region" reg))
(pan-mix (save-region reg (snd-tempnam)) beg pan snd #t)))))
(define pan-mix-float-vector
(let ((+documentation+ "(pan-mix-float-vector v start pan-env snd) mixes the float-vector data into the sound 'snd'
starting at 'start' (in samples) using 'pan-env' to pan (0: all chan 0, 1: all chan 1)."))
(lambda* (v beg pan snd)
(let ((temp-file (snd-tempnam)))
(array->file temp-file v (length v) (srate snd) 1)
(pan-mix temp-file beg pan snd #t)))))
;;; -------- delay-channel-mixes
(define delay-channel-mixes
(let ((+documentation+ "(delay-channel-mixes beg dur snd chn) adds dur (which can be negative) to the \
begin time of each mix that starts after beg in the given channel"))
(lambda* (beg dur snd chn)
(for-each
(lambda (m)
(if (>= (mix-position m) beg)
(set! (mix-position m) (max 0 (+ (mix-position m) dur)))))
(mixes (or snd
(selected-sound)
(car (sounds)))
(or chn 0))))))
;;; -------- check-mix-tags
(define check-mix-tags
(let ((+documentation+ "(check-mix-tags snd chn) tries to move mix tags around to avoid collisions"))
(lambda* (snd chn)
(if (not snd)
(for-each check-mix-tags (sounds))
(if (not chn)
(do ((chns (channels snd))
(i 0 (+ i 1)))
((= i chns))
(check-mix-tags snd i))
(let ((mxs (mixes snd chn))
(changed #f))
(let check-mix ((mx (car mxs))
(trailing-mixes (cdr mxs)))
(when (pair? trailing-mixes)
(let ((pos (mix-position mx))
(ls (left-sample snd chn))
(rs (right-sample snd chn)))
(when (<= ls pos rs)
(let ((x (x->position (/ pos (srate snd))))
(y (mix-tag-y mx)))
(for-each (lambda (other-mix)
(let ((other-pos (mix-position other-mix)))
(if (<= ls other-pos rs)
(let ((other-x (x->position (/ other-pos (srate snd))))
(other-y (mix-tag-y other-mix)))
(when (and (< (abs (- x other-x)) 6)
(< (abs (- y other-y)) 10))
(set! (mix-tag-y other-mix) (+ (mix-tag-y other-mix) 20))
(set! changed #t))))))
trailing-mixes)))
(check-mix (car trailing-mixes) (cdr trailing-mixes)))))
(if changed
(update-time-graph snd chn))))))))
|