download mix.rb
Language: Ruby
LOC: 607
Project Info
Snd
Server: SourceForge
Type: cvs
SourceForge\s\snd\snd\cvs‑snd\
   _new_snd-test.rb
   _sndlib.h
   aclocal.m4
   analog-filter.scm
   audinfo.c
   audio.c
   autosave.scm
   bess.rb
   bess.scm
   bess1.rb
   bess1.scm
   bird.rb
   bird.scm
   clm-ins.rb
   clm-ins.scm
   clm-strings.h
   clm.c
   clm.h
   clm23.scm
   clm2xen.c
   clm2xen.h
   cmn-glyphs.lisp
   config.guess
   config.h.in
   config.rpath
   config.sub
   configure.ac
   debug.scm
   dlocsig.rb
   draw.rb
   draw.scm
   dsp.rb
   dsp.scm
   edit-menu.scm
   edit123.scm
   effects-utils.scm
   effects.rb
   env.rb
   env.scm
   enved.scm
   eval-c.scm
   event.scm
   examp.rb
   examp.scm
   extensions.rb
   extensions.scm
   fade.scm
   fft-menu.scm
   fix-optargs.scm
   fmv.scm
   freeverb.rb
   freeverb.scm
   gettext.h
   gl.c
   grani.scm
   gtk-effects.scm
   gtk-popup.scm
   gui.scm
   headers.c
   help-snd-fm.pd
   help-snd.pd
   HISTORY.Snd
   hooks.rb
   hooks.scm
   index.rb
   index.scm
   inf-snd.el
   io.c
   jcrev.scm
   ladspa-help.scm
   ladspa.scm
   makefile.in
   makefile.no-gettext
   maraca.rb
   maraca.scm
   marks-menu.scm
   marks.rb
   marks.scm
   maxf.rb
   maxf.scm
   midi.c
   misc.scm
   mix-menu.scm
   mix.rb
   mix.scm
   mixer.scm
   moog.scm
   musglyphs.rb
   musglyphs.scm
   nb.rb
   nb.scm
   new-backgrounds.scm
   new-buttons.scm
   new-effects.scm
   new-icons.scm
   noise.rb
   noise.scm
   old-mac-audio.c
   old-snd.spec
   oo.scm
   oscope.scm
   panic.scm
   pd-add.scm
   pd-any.scm
   pd-fm.scm
   pd-global.scm
   pd-inout.scm
   pd-local.scm
   pd-mozilla.scm
   pd-send_receive.scm
   peak-env.scm
   piano.rb
   piano.scm
   play.rb
   play.scm
   poly.rb
   poly.scm
   popup.rb
   popup.scm
   prc95.rb
   prc95.scm
   pvoc.rb
   pvoc.scm
   README.Snd
   rgb.rb
   rgb.scm
   rmsgain.scm
   rt-compiler.scm
   rt-engine.scm
   rt-examples.scm
   rt.tex
   rtio.rb
   rtio.scm
   rubber.rb
   rubber.scm
   saw.c
   singer.rb
   singer.scm
   snd-0.h
   snd-1.h
   snd-axis.c
   snd-chn.c
   snd-completion.c
   snd-dac.c
   snd-data.c
   snd-draw.c
   snd-edits.c
   snd-env.c
   snd-error.c
   snd-fft.c
   snd-file.c
   snd-file.h
   snd-find.c
   snd-g0.h
   snd-g1.h
   snd-gchn.c
   snd-gdraw.c
   snd-gdrop.c
   snd-genv.c
   snd-gfft.c
   snd-gfile.c
   snd-gfind.c
   snd-ghelp.c
   snd-gl.scm
   snd-glistener.c
   snd-gmain.c
   snd-gmenu.c
   snd-gmix.c
   snd-gprefs.c
   snd-gprint.c
   snd-grec.c
   snd-gregion.c
   snd-gsnd.c
   snd-gtk.scm
   snd-gutils.c
   snd-gxbitmaps.c
   snd-gxcolormaps.c
   snd-gxen.c
   snd-gxutils.c
   snd-help.c
   snd-hobbit.scm
   snd-io.c
   snd-kbd.c
   snd-ladspa.c
   snd-listener.c
   snd-main.c
   snd-marks.c
   snd-menu.c
   snd-menu.h
   snd-mix.c
   snd-motif.scm
   snd-nogui.c
   snd-nogui0.h
   snd-nogui1.h
   snd-prefs.c
   snd-print.c
   snd-rec.c
   snd-rec.h
   snd-region.c
   snd-run.c
   snd-select.c
   snd-sig.c
   snd-snd.c
   snd-strings.h
   snd-trans.c
   snd-utils.c
   snd-x0.h
   snd-x1.h
   snd-xchn.c
   snd-xdraw.c
   snd-xdrop.c
   snd-xen.c
   snd-xenv.c
   snd-xfft.c
   snd-xfile.c
   snd-xfind.c
   snd-xhelp.c
   snd-xlistener.c
   snd-xm.rb
   snd-xmain.c
   snd-xmenu.c
   snd-xmix.c
   snd-xprefs.c
   snd-xprint.c
   snd-xrec.c
   snd-xref.c
   snd-xregion.c
   snd-xsnd.c
   snd-xutils.c
   snd-xxen.c
   snd.1
   Snd.ad
   snd.c
   Snd.gtkrc
   snd.h
   snd.spec
   snd_conffile.scm
   snd_pd_external.c
   snd_pd_external.h
   snd4.scm
   snd5.scm
   snd6.scm
   snd7.scm
   sndctrl.c
   sndinfo.c
   sndlib-strings.h
   sndlib.h.in
   sndlib2xen.c
   sndlib2xen.h
   sndplay.c
   sndrecord.c
   sndwarp.scm
   sound.c
   special-menu.scm
   spectr.rb
   spectr.scm
   strad.rb
   strad.scm
   TODO.Snd
   track-colors.scm
   v.rb
   v.scm
   vct.c
   vct.h
   ws.rb
   ws.scm
   xen.c
   xen.h
   xg-x11.h
   xm-enved.rb
   xm-enved.scm
   zip.rb
   zip.scm

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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# mix.rb -- mix.scm --> mix.rb -*- snd-ruby -*-

# Translator: Michael Scholz <scholz-micha@gmx.de>
# Created: Tue Feb 22 13:40:33 CET 2005
# Changed: Thu Sep 29 14:02:34 CEST 2005

# Commentary:
#
# various mix and track related utilities
#
# module Mix (see mix.scm)
#  mix_sound(file, start)
#  delete_all_mixes
#  find_mix(sample, snd, chn)
#  pan_mix(name, beg, envelope, snd, chn, auto_delete)
#  pan_mix_selection(beg, envelope, snd, chn)
#  pan_mix_region(reg, beg, envelope, snd, chn)
#  pan_mix_vct(v, beg, envelope, snd, chn)
#  mix2vct(id)
#  save_mix(id, filename)
#  mix_maxamp(id)
#  snap_mix_to_beat(at_tag_position)
#
#  mix_properties(id)
#  set_mix_properties(id, new_val)
#  mix_property(key, id)
#  set_mix_property(key, val, id)
#  mix_name(id)
#  set_mix_name(id, name)
#  mix_name2id(name)
#  mix_click_sets_amp
#
#  delete_all_tracks
#  reverse_track(trk)
#  track2vct(trk, chn)
#  save_track(trk, filename, chn)
#  track_maxamp(id, chn)
#  transpose_track(trk, semitones)
#  retempo_track(trk, tempo)
#  filter_track(trk, fir_filter_coeffs)
#
#  track_properties(id)
#  set_track_properties(id, new_val)
#  track_property(key, id)
#  set_track_property(key, val, id)
#  mix_click_info(id)
#
# module Mixer_matrix (see mixer.scm)
#  mixer2matrix(mx)
#  matrix2mixer(mat)
#  mixer_equal?(mx1, mx2)
#  mixer_diagonal?(mx)
#  mixer_symmetric?(mx)
#  mixer_hermitian?(mx)
#  mixer_determinant(mx)
#  mixer_transpose(mx)
#  mixer_inverse(mx)
#  mixer_poly(mx, *coeffs)
#  mixer_normal?(mx)
#  mixer_orthogonal?(mx)
#  mixer_unitary?(mx)
#  mixer_trace(mx)
#  mixer_solve(a, b)
#  frame_reverse(fr)
#  make_zero_mixer(n)
#  

# Code:

require "examp"

module Mix
  # 
  # === MIX ===
  #
  add_help(:mix_sound,
           "mix(sound(file, start) \
mixes file (all chans) at start in the currently selected sound.")
  def mix_sound(file, start)
    mix(file, start, true)
  end

  add_help(:delete_all_mixes, "delete_all_mixes() removes all mixes (sets all amps to 0)")
  def delete_all_mixes
    as_one_edit_rb(get_func_name) do (mixes or []).flatten.each do |id| delete_mix(id) end end
  end

  add_help(:find_mix,
           "find_mix(sample, [snd=false, [chn=false]]) \
returns the id of the mix at the given sample, or nil")
  def find_mix(sample, snd = false, chn = false)
    (mixes(Snd.snd(snd), Snd.chn(chn)) or []).detect do |n| mix_position(n) == sample end
  end
  
  # 
  # === PAN-MIX ===
  # 
  add_help(:pan_mix,
           "pan_mix(file, [start=0, [envelope=1.0, [snd=false, [chn=0, auto_delete=false]]]]) \
mixes 'file' into the sound 'snd' starting at start (in samples) \
using 'envelope' to pan (0: all chan 0, 1: all chan 1).
So, pan_mix(\"oboe.snd\", 0.1, [0, 0, 1, 1]) goes from all chan 0 to all chan 1.  \
If 'envelope' is a scaler, it is turned into an evelope at that value.  \
'auto_delete' determines whether the in-coming file should be treated as a temporary file \
and deleted when the mix is no longer accessible.")
  def pan_mix(name, beg = 0, envelope = 1.0, snd = false, chn = 0, auto_delete = false)
    index = Snd.snd(snd)
    old_with_mix_tags = with_mix_tags
    Snd.raise(:no_such_sound, snd) unless sound?(index)
    Snd.raise(:no_such_file, name) unless File.exists?(name)
    new_mix = nil
    begin
      set_with_mix_tags(true)
      incoming_chans = mus_sound_chans(name)
      receiving_chans = channels(index)
      old_sync = sync(index)
      track_func = if array?(envelope)
                     envelope
                   else
                     [0, envelope, 1, envelope]
                   end
      if receiving_chans == 1
        if incoming_chans == 1
          id = mix(name, beg, 0, index, 0, true, auto_delete)
          if array?(envelope)
            set_mix_amp_env(id, 0, envelope)
          else
            set_mix_amp(id, 0, envelope)
          end
          new_mix = id
        else
          as_one_edit_rb(get_func_name) do
            trk = make_track
            mix0 = mix(name, beg, 0, index, 0, true, auto_delete, trk)
            mix1 = mix(name, beg, 1, index, 0, true, auto_delete, trk)
            set_mix_inverted?(mix1, true)
            set_track_amp_env(trk, track_func)
            new_mix = mix0
          end
        end
      else
        chan0 = chn
        chan1 = (chn + 1) % receiving_chans
        if incoming_chans == 1
          begin
            set_sync(false, index)
            as_one_edit_rb(get_func_name) do
              trk = make_track
              mix0 = mix(name, beg, 0, index, chan0, true, auto_delete, trk)
              mix1 = mix(name, beg, 0, index, chan1, true, auto_delete, trk)
              set_mix_inverted?(mix1, true)
              set_track_amp_env(trk, track_func)
              new_mix = mix0
            end
          rescue
            raise
          ensure
            set_sync(old_sync, index)
          end
        else
          new_sync = 0
          Snd.sounds.each do |s|
            if (sn = sync(s)) >= new_sync
              new_sync = sn + 1
            end
          end
          begin
            set_sync(new_sync, index)
            as_one_edit_rb(get_func_name) do
              trk = make_track
              mix0 = mix(name, beg, 0, index, chan0, true, auto_delete, trk)
              mix1 = mix(name, beg, 1, index, chan1, true, auto_delete, trk)
              set_mix_inverted?(mix1, true)
              set_track_amp_env(trk, track_func)
              new_mix = mix0
            end
          rescue
            raise
          ensure
            set_sync(old_sync, index)
          end
        end
      end
    rescue
      raise
    ensure
      set_with_mix_tags(old_with_mix_tags)
    end
    if mix?(new_mix) and (not old_with_mix_tags)
      if track?(trk = mix_track(new_mix))
        lock_track(trk)
      else
        set_mix_locked?(new_mix, true)
      end
    end
    new_mix
  end

  add_help(:pan_mix_selection,
           "pan_mix_selection([beg=0, [envelope=1.0, [snd=false, [chn=0]]]]) \
mixes the current selection  into the sound 'snd' starting at 'start' (in samples) \
using 'envelope' to pan (0: all chan 0, 1: all chan 1).")
  def pan_mix_selection(beg = 0, envelope = 1.0, snd = false, chn = 0)
    Snd.raise(:no_active_selection) unless selection?
    pan_mix(save_selection(snd_tempnam), beg, envelope, snd, chn, true)
  end

  add_help(:pan_mix_region,
           "pan_mix_region(reg, [beg=0, [envelope=1.0, [snd=false, [chn=0]]]]) \
mixes the given region into the sound 'snd' starting at 'start' (in samples) \
using 'envelope' to pan (0: all chan 0, 1: all chan 1).")
  def pan_mix_region(reg, beg = 0, envelope = 1.0, snd = false, chn = 0)
    Snd.raise(:no_such_region, reg) unless region?(reg)
    pan_mix(save_region(reg, snd_tempnam), beg, envelope, snd, chn, true)
  end

  add_help(:pan_mix_vct,
           "pan_mix_vct(v, [beg=0, [envelope=1.0, [snd=false, [chn=0]]]]) \
mixes the vct data into the sound 'snd' starting at 'start' (in samples) \
using 'envelope' to pan (0: all chan 0, 1: all chan 1).")
  def pan_mix_vct(v, beg = 0, envelope = 1.0, snd = false, chn = 0)
    temp_file = snd_tempnam
    fd = open_sound_file(temp_file, 1, srate(snd), "")
    vct2sound_file(fd, v, v.length)
    close_sound_file(fd, 4 * v.length)
    pan_mix(temp_file, beg, envelope, snd, chn, true)
  end

  add_help(:mix2vct, "mix2vct(id) returns mix's data in vct")
  def mix2vct(id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    len = mix_frames(id)
    v = make_vct(len)
    rd = make_mix_sample_reader(id)
    len.times do |i| v[i] = read_mix_sample(rd) end
    free_sample_reader(rd)
    v
  end

  add_help(:save_mix, "save_mix(id, filename) saves mix data (as floats) in file filename")
  def save_mix(id, filename)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    v = mix2vct(id)
    fd = open_sound_file(filename, 1, srate, "")
    vct2sound_file(fd, v, v.length)
    close_sound_file(fd, 4 * v.length)
  end

  add_help(:mix_maxamp, "mix_maxamp(id) returns the max amp in the given mix")
  def mix_maxamp(id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    len = mix_frames(id)
    rd = make_mix_sample_reader(id)
    peak = read_mix_sample(rd).abs
    (1...len).each do peak = [peak, read_mix_sample(rd).abs].max end
    free_sample_reader(rd)
    peak
  end

  add_help(:snap_mix_to_beat,
           "snap_mix_to_beat() \
forces a dragged mix to end up on a beat (see beats-per-minute).  \
reset $mix_release_hook to cancel")
  def snap_mix_to_beat(at_tag_position = false)
    $mix_release_hook.add_hook!(get_func_name) do |id, samps_moved|
      offset = at_tag_position ? mix_tag_position(id) : 0
      samp = samps_moved + mix_position(id) + offset
      snd = mix_home(id)[0]
      chn = mix_home(id)[1]
      bps = beats_per_minute(snd, chn) / 60.0
      sr = srate(snd).to_f
      beat = ((samp * bps) / sr).floor
      lower = ((beat * sr) / bps).floor
      higher = (((beat + 1) * sr) / bps).floor
      set_mix_position(id, if (samp - lower) < (higher - samp)
                             [0, lower - offset].max
                           else
                             higher - offset
                           end)
      true
    end
  end

  #
  # === Mix Properties ===
  #
  $all_mix_properties = Array.new

  def mix_properties(id)
    property(id, :mix_property)
  end

  def set_mix_properties(id, new_val)
    set_property(id, :mix_property, new_val)
  end

  def remove_mix_properties(id)
    if hash?(mix_properties(id))
      properties.delete(id)
      $all_mix_properties.delete(id)
    end
  end

  add_help(:mix_property,
           "mix_property(key, id) \
returns the value associated with 'key' in the given mix's property list, or false")
  def mix_property(key, id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    hash?(h = mix_properties(id)) and h[key]
  end

  add_help(:set_mix_property,
           "set_mix_property(key, val, id) \
sets the value 'val' to 'key' in the given mix's property list")
  def set_mix_property(key, val, id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    unless hash?(h = mix_properties(id)) and h.store(key, val)
      $all_mix_properties.push(id)
      set_mix_properties(id, {key, val})
    end
  end

  add_help(:remove_mix_property,
           "remove_mix_property(key, id) \
removes the key-value pair in the given mix's property list")
  def remove_mix_property(key, id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    if hash?(h = mix_properties(id))
      h.delete(key)
    else
      $all_mix_properties.delete(id)
    end
  end
  
=begin  
  $close_hook.add_hook!("remove-mix-properties") do |snd|
    $all_mix_properties.each do |id| (not mix?(id)) and remove_mix_properties(id) end
  end
=end

  def mix_name(id)
    mix_property(:name, id)
  end

  def set_mix_name(id, name)
    set_mix_property(:name, name, id)
  end

  def mix_name2id(name)
    (mixes or []).detect do |mx| mix_name(mx) == name end or Snd.raise(:no_such_mix, name)
  end

  def mix_click_sets_amp
    $mix_click_hook.add_hook!(get_func_name) do |id|
      unless mix_property(:zero, id)
        amps = (0...mix_chans(id)).map do |i| mix_amp(id, i) end
        set_mix_property(:amps, amps, id)
        mix_chans(id).times do |i| set_mix_amp(id, i, 0.0) end
        set_mix_property(:zero, true, id)
      else
        (mix_property(:amps, id) or []).each_with_index do |amp, i| set_mix_amp(id, i, amp) end
        set_mix_property(:zero, false, id)
      end
      true
    end
  end
  # $mix_click_hook.add_hook!("mix-click-info") do |id| mix_click_info(id) end
  
  # 
  # === Track ===
  # 
  add_help(:delete_all_tracks,
           "delete_all_tracks() \
removes all mixes that have an associated track (sets all amps to 0)")
  def delete_all_tracks
    as_one_edit_rb(get_func_name) do
      (mixes or []).flatten.each do |id| delete_mix(id) unless mix_track(id).zero? end
    end
  end

  add_help(:reverse_track,
           "reverse_track(trk) \
reverses the order of its mixes (it changes various mix begin times)")
  def reverse_track(trk)
    if track(trk).detect do |t| mix?(t) end
      ids_in_order = track(trk).sort do |a, b|
        if mix_position(a) > mix_position(b)
          1
        elsif mix_position(a) < mix_position(b)
          -1
        else
          0
        end
      end
      as_one_edit_rb(get_func_name) do
        ids_in_order.map do |id| mix_position(id) end.reverse.zip(ids_in_order).each do |pos, id|
          set_mix_position(id, pos)
        end
      end
    else
      Snd.raise(:no_such_track, trk)
    end
  end

  add_help(:track2vct, "track2vct(track, [chan=0]) places track data in vct") 
  def track2vct(trk, chn = 0)
    Snd.raise(:no_such_track, trk) unless track?(trk)
    Snd.raise(:no_such_channel, chn) unless chn < track_chans(trk)
    len = track_frames(trk, chn)
    v = make_vct(len)
    rd = make_track_sample_reader(trk, chn)
    len.times do |i| v[i] = read_track_sample(rd) end
    free_sample_reader(rd)
    v
  end

  add_help(:save_track,
           "save_track(track, filename, [chan=true]) saves track data (as floats) in file filename")
  def save_track(trk, filename, chn = true)
    Snd.raise(:no_such_track, trk) unless track?(trk)
    chans = track_chans(trk)
    if chn == true and chans == 1 or number?(chn) and chn < chans
      v = track2vct(trk, (chn == true ? 0 : chn))
      fd = open_sound_file(filename, 1, srate, format("written by %s", get_func_name))
      vct2sound_file(fd, v, v.length)
      close_sound_file(fd, 4 * v.length)
    else
      if chn == true and chans > 0
        fd = open_sound_file(filename, chans, srate, format("written by %s", get_func_name))
        len = track_frames(trk)
        pos = track_position(trk)
        v = make_vct(chans * len)
        chans.times do |i|
          chan_len = track_frames(trk, i)
          chan_pos = track_position(trk, i) - pos
          rd = make_track_sample_reader(trk, i)
          chan_len.times do |j| v[i + chans * (chan_pos + j)] = read_track_sample(rd) end
          free_sample_reader(rd)
        end
        vct2sound_file(fd, v, v.length)
        close_sound_file(fd, 4 * v.length)
      else
        Snd.raise(:no_such_channel, chn)
      end
    end
  end

  add_help(:track_maxamp, "track_maxamp(id, chan) returns the max amp in the given track")
  def track_maxamp(id, chn)
    Snd.raise(:no_such_track, id) unless track?(id)
    len = track_frames(id)
    rd = make_track_sample_reader(id)
    peak = read_track_sample(rd).abs
    (1...len).each do peak = [peak, read_track_sample(rd).abs].max end
    free_sample_reader(rd)
    peak
  end

  add_help(:transpose_track,
           "transpose_track(track, semitones) transposes each mix in track by semitones")
  def transpose_track(trk, semitones)
    set_track_speed(trk, track_speed(trk) * 2 ** (semitones / 12.0))
  end

  add_help(:retempo_track,
           "retempo_track(track, tempo) \
changes the inter-mix begin times of mixes in track by tempo (> 1.0 is faster)")
  def retempo_track(trk, tempo)
    set_track_tempo(trk, track_tempo(trk) * tempo)
  end

  add_help(:filter_track,
           "filter_track(track, coeffs) \
filters track data using FIR filter coeffs: filter_track(track-id, [0.1, 0.2, 0.3, 0.3, 0.2, 0.1])")
  def filter_track(trk, fir_filter_coeffs)
    Snd.raise(:no_such_track, trk) unless track?(trk)
    order = fir_filter_coeffs.length
    chans = track_chans(trk)
    chans.times do |chn|
      beg = track_position(trk, chn)
      dur = track_frames(trk, chn)
      flt = make_fir_filter(order, list2vct(fir_filter_coeffs))
      rd = make_track_sample_reader(trk, chn, 0)
      map_channel(lambda do |y|
                    val = read_track_sample(rd)
                    y + fir_filter(flt, val) - val
                  end, beg, dur + order, false, false, false, get_func_name)
    end
  end

  # 
  # === Track Properties ===
  # 
  $all_track_properties = Array.new

  def track_properties(id)
    property(id, :track_property)
  end

  def set_track_properties(id, new_val)
    set_property(id, :track_property, new_val)
  end

  def remove_track_properties(id)
    if hash?(track_properties(id))
      properties.delete(id)
      $all_track_properties.delete(id)
    end
  end

  add_help(:track_property,
           "track_property(key, id) \
returns the value associated with 'key' in the given track's property list, or false")
  def track_property(key, id)
    Snd.raise(:no_such_track, id) unless track?(id)
    hash?(h = track_properties(id)) and h[key]
  end

  add_help(:set_track_property,
           "set_track_property(key, val, id) \
sets the value 'val' to 'key' in the given track's property list")
  def set_track_property(key, val, id)
    Snd.raise(:no_such_track, id) unless track?(id)
    unless hash?(h = track_properties(id)) and h.store(key, val)
      $all_track_properties.push(id)
      set_track_properties(id, {key, val})
    end
  end

  add_help(:remove_track_property,
           "remove_track_property(key, id) \
removes the key-value pair in the given track's property list")
  def remove_track_property(key, id)
    Snd.raise(:no_such_track, id) unless track?(id)
    if hash?(h = track_properties(id))
      h.delete(key)
    else
      $all_track_properties.delete(id)
    end
  end
  
=begin  
  $close_hook.add_hook!("remove-track-properties") do |snd|
    $all_track_properties.each do |id| (not track?(id)) and remove_track_properties(id) end
  end
=end

  # 
  # === Mix Click Info ===
  # 
  add_help(:mix_click_info,
           "mix_click_info(n) \
is a $mix_click_hook function that describes a mix and its properties")
  def mix_click_info(id)
    Snd.raise(:no_such_mix, id) unless mix?(id)
    info_dialog("Mix info", format("\
      mix id: %d%s
    position: %d (%1.3f secs)
      length: %d (%1.3f secs)
          in: %s[%d]%s%s%s
     scalers: %s
       speed: %s
        envs: %s%s%s",
                                   id,
                                   ((s = mix_name(id)) ?
                                    format("\n    mix name: %s", s.inspect) :
                                      ""),
                                   mix_position(id),
                                   mix_position(id) / srate(mix_home(id)[0]).to_f,
                                   mix_frames(id),
                                   mix_frames(id) / srate(mix_home(id)[0]).to_f,
                                   short_file_name(mix_home(id)[0]),
                                   mix_home(id)[1],
                                   (mix_locked?(id) ? " (locked)" : ""),
                                   (mix_inverted?(id) ? " (inverted)" : ""),
                                   (mix_track(id).nonzero? ?
                                    format("\n       track: %s", mix_track(id)) :
                                      ""),
                                   (0...mix_chans(id)).map do |i| mix_amp(id, i) end.inspect,
                                   mix_speed(id),
                                   (0...mix_chans(id)).map do |i| mix_amp_env(id, i) end.inspect,
                                   (mix_tag_position(id).nonzero? ?
                                    format("\ntag-position: %d", mix_tag_position(id)) :
                                      ""),
                                   ((props = mix_properties(id)) ?
                                    format("\n  properties: %s", props.inspect) :
                                      "")))
    true
  end
end

include Mix

# 
# === MIXER.SCM ===
# 
require "matrix"

class Matrix
  with_silence do
    # local variable rows renamed to rs
    def Matrix.diagonal(*values)
      size = values.size
      rs = (0...size).collect do |j|
        row = Array.new(size).fill(0, 0, size)
        row[j] = values[j]
        row
      end
      rows(rs, false)
    end
    # private :init_rows doesn't work any longer
    public :init_rows
  end
end

module Mixer_matrix
  def mixer2matrix(mx)
    if mixer?(mx)
      a = make_array(mx.channels) do |i|
        make_array(mx.channels) do |j|
          mixer_ref(mx, i, j)
        end
      end
      Matrix.columns(a)
    else
      false
    end
  end

  def matrix2mixer(mat)
    if mat.kind_of?(Matrix)
      mx = make_mixer(mat.row_size)
      mat.to_a.each_with_index do |row, i|
        row.each_with_index do |val, j|
          mixer_set!(mx, j, i, val)
        end
      end
      mx
    else
      false
    end
  end

  def mixer_equal?(mx1, mx2)
    a = if mixer?(mx1)
          mixer2matrix(mx1)
        else
          mx1
        end
    b = if mixer?(mx2)
          mixer2matrix(mx2)
        else
          mx2
        end
    a == b or callcc do |flag|
      a.to_a.zip(b.to_a) do |a1, b1|
        flag.call(false) unless vequal(a1, b1)
      end
      true
    end
  end

  def mixer_diagonal?(mx)
    if mx.length == 1
      true
    else
      callcc do |flag|
        mx.channels.times do |i|
          mx.channels.times do |j|
            flag.call(false) if i != j and mixer_ref(mx, i, j).nonzero?
          end
        end
        true
      end
    end
  end

  def mixer_symmetric?(mx)
    mixer_equal?(mx, mixer_transpose(mx))
  end
  alias mixer_hermitian? mixer_symmetric?

  def mixer_determinant(mx)
    mixer2matrix(mx).determinant
  end

  def mixer_transpose(mx)
    matrix2mixer(mixer2matrix(mx).transpose)
  end

  def mixer_inverse(mx)
    matrix2mixer(mixer2matrix(mx).inverse)
  rescue
    false
  end

  def mixer_poly(mx, *coeffs)
    n = coeffs.length
    nmx = make_scalar_mixer(mx.length, coeffs[-1])
    x = mixer_multiply(mx, 1.0)
    (n - 2).downto(0) do |i|
      nmx = mixer_add(nmx, mixer_multiply(x, coeffs[i]))
      x = mixer_multiply(mx, x)
    end
    nmx
  end

  def mixer_normal?(mx)
    mixer_equal?(mixer_multiply(mx, mixer_transpose(mx)),
                 mixer_multiply(mixer_transpose(mx), mx))
  end

  def mixer_orthogonal?(mx)
    mixer_equal?(mixer_transpose(mx),
                 mixer_inverse(mx))
  end
  alias mixer_unitary? mixer_orthogonal?

  def mixer_trace(mx)
    mixer2matrix(mx).trace
  end

  # Ax=b where A is mixer and b is frame, returns frame
  def mixer_solve(a, b)
    if a.length == 1
      if mixer_ref(a, 0, 0) != 0.0
        make_frame(1, frame_ref(b, 0) / mixer_ref(a, 0, 0))
      else
        false
      end
    else
      if mixer?(imx = mixer_inverse(a))
        frame2frame(imx, b)
      else
        false
      end
    end
  end

  def frame_reverse(fr)
    len = fr.length
    j = len - 1
    (0...(len / 2)).each do |i|
      temp = frame_ref(fr, i)
      frame_set!(fr, i, frame_ref(fr, j))
      frame_set!(fr, j, temp)
      j -= 1
    end
    fr
  end

  def frame_copy(fr)
    len = fr.length
    nfr = make_frame(len)
    len.times do |i| frame_set!(nfr, i, frame_ref(fr, i)) end
    nfr
  end

  def mixer_copy(mx)
    len = mx.length
    nmx = make_mixer(len)
    len.times do |i|
      len.times do |i|
        mixer_set!(nmx, i, j, mixer_ref(mx, i, j))
      end
    end
    nmx
  end

  alias make_zero_mixer make_mixer

end

# mix.rb ends here

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us