-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlocsig.rb
2938 lines (2729 loc) · 90.4 KB
/
dlocsig.rb
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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# dlocsig.rb -- CLM -> Snd/Ruby translation of dlocsig.lisp
# Translator/Author: Michael Scholz <mi-scholz@users.sourceforge.net>
# Copyright (c) 2003-2012 Michael Scholz <mi-scholz@users.sourceforge.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# Original Copyright of Fernando Lopez Lezcano:
# ;;; Copyright (c) 92, 93, 94, 98, 99, 2000, 2001 Fernando Lopez Lezcano.
# ;;; All rights reserved.
# ;;; Use and copying of this software and preparation of derivative works
# ;;; based upon this software are permitted and may be copied as long as
# ;;; no fees or compensation are charged for use, copying, or accessing
# ;;; this software and all copies of this software include this copyright
# ;;; notice. Suggestions, comments and bug reports are welcome. Please
# ;;; address email to: nando@ccrma.stanford.edu
# ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
# ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
# ;;; Dynamic multichannel three-dimentional signal locator
# ;;; (wow that sound good! :-)
# ;;;
# ;;; by Fernando Lopez Lezcano
# ;;; CCRMA, Stanford University
# ;;; nando@ccrma.stanford.edu
# ;;;
# ;;; Thanks to Juan Pampin for help in the initial coding of the new version
# ;;; and for prodding me to finish it. To Joseph L. Anderson and Marcelo Perticone
# ;;; for insights into the Ambisonics coding and decoding process.
# ;;; http://www.york.ac.uk/inst/mustech/3d_audio/ambison.htm for more details...
# Commentary:
# Tested with Snd 7.10, Motif 2.2.2, Gtk+ 2.2.1, Ruby 1.6.6, 1.6.8 and 1.9.0.
#
# The code is a translation of the Lisp code of Fernando Lopez Lezcano
# found in clm-2/dlocsig of the CLM distribution. An extensive
# documentation of the purpose and usage of it can be found in
# clm-2/dlocsig/dlocsig.html.
#
# Note: dlocsig.rb handles not more rev_channels than out_channels;
# B_format_ambisonics handles only 4 out_channels and 0, 1, or 4
# rev_channels.
#
# The simple example
#
# [[-10, 10], [0, 5], [10, 10]].to_path.snd_plot
#
# draws trajectory, velocity, doppler curve, and the acceleration in
# Snd's lisp-graph. If you have gnuplot installed, the example
#
# [[-10, 10], [0, 5], [10, 10]].to_path.pplot
#
# draws all four curves in one gnuplot window.
# DL.make_path
# DL.make_polar_path
# DL.make_closed_path
# and to_path take the following options
#
# Open_bezier_path.new(path, *args)
# :d3, true
# :polar, false
# :error, 0.01
# :curvature, nil
# :initial_direction, [0.0, 0.0, 0.0]
# :final_direction, [0.0, 0.0, 0.0]
#
# Closed_bezier_path.new(path, *args)
# :d3, true
# :polar, false
# :error, 0.01
# :curvature, nil
#
# DL.make_literal_path
# DL.make_literal_polar_path take the following options
#
# Literal_path.new(path, *args)
# :d3, true
# :polar, false
#
# DL.make_spiral_path takes these options
#
# Spiral_path.new :start_angle, 0
# :turns, 2
#
# The make_locsig-replacement make_dlocsig takes these arguments:
#
# DL.make_dlocsig(startime, dur, *args)
# :path, nil
# :scaler, 1.0
# :reverb_amount, 0.05
# :rbm_output, $output
# :rbm_reverb, $reverb
# :output_power, 1.5
# :reverb_power, 0.5
# :render_using, :amplitude_panning
# or :b_format_ambisonics
# or :decoded_ambisonics
#
# Sample instruments (sinewave() and move() below) show how to replace
# the usual make_locsig() and locsig() by DL.make_dlocsig() and
# DL.dlocsig().
# Example functions at the end of the file:
# class Instrument
# sinewave(start, dur, freq, amp, path, amp_env, *dlocsig_args)
# move(start, file, path, *dlocsig_args)
# move_sound(path, *dlocsig_args) do ... end
#
# class With_sound
# run_dlocsig(start, dur, *dlocsig_args) do |samp| ... end
# Classes and Modules:
# module Inject
# inject(n)
# sum(initial)
# product(initial)
#
# class Array
# to_trias
# to_path(*args)
#
# class Sndplot
# initialize(chns)
# snd
# open
# close
#
# class Gnuplot
# initialize
# open
# close
# command(*args)
# reset
# set_autoscale
# set_x_range(range)
# set_y_range(range)
# set_z_range(range)
# set_grid
# set_surface
# set_parametric
# set_ticslevel(level)
# set_title(title)
# set_label(label)
# set_margins(margin)
# set_border(border)
# start_multiplot
# end_multiplot
# size(xorigin, yorigin, xsize, ysize)
# data(data, *args)
# plot_2d_curve(curve, *args)
# plot_2d_curves(curves, *args)
# plot_3d_curve(curve, *args)
#
# module DL
# class Dlocsig < Dlocs
# initialize(start, dur, *args)
# one_turn
# one_turn=(val)
# speed_of_sound
# speed_of_sound=(val)
# run_beg
# run_end
# angles_in_degree
# angles_in_radians
# angles_in_turns
# distance_in_meters
# distance_in_feet
#
# class Path
# initialize(path, *args)
# path_x
# path_y
# path_z
# path_time
# scale_path(scaling)
# translate_path(translation)
# rotate_path(rotation, *args)
#
# path_trajectory
# path_2d_trajectory
# path_velocity
# path_doppler
# path_acceleration
#
# plot_open
# plot_close
# cmd(*args)
# plot_trajectory(*args)
# plot_velocity(reset)
# plot_doppler(reset)
# plot_acceleration(reset)
# pplot(normalize)
#
# snd_open(chns)
# snd_close
# snd_trajectory(chn, label)
# snd_velocity(chn, label)
# snd_doppler(chn, label)
# snd_acceleration(chn, label)
# snd_plot
#
# DL.make_dlocsig(start, dur, *args)
# DL.dlocsig(dl, loc, input)
#
# DL.make_path(path, *args)
# DL.make_polar_path(path, *args)
# DL.make_closed_path(path, *args)
# DL.make_literal_path(path, *args)
# DL.make_literal_polar_path(path, *args)
# DL.make_spiral_path(*args)
#
# class Dlocsig_menu
# initialize(label, snd_p)
# post_dialog
# Code:
require "ws"
require "matrix"
include Math
provided?(:snd_motif) and (not provided?(:xm)) and require("libxm.so")
provided?(:snd_gtk) and (not provided?(:xg)) and require("libxg.so")
class DlocsigError < StandardError
end
Ruby_exceptions[:dlocsig_error] = DlocsigError
def dl_error(*msg)
Snd.raise(:dlocsig_error, (msg.empty? ? "" : format(*msg)))
end
# module Inject, see Thomas, David, Hunt, Andrew: Programming Ruby --
# The Pragmatic Programmer's Guide, 2001 Addison-Wesley, page 102n
module Inject
def inject(n)
each do |x| n = yield(n, x) end
n
end
def sum(initial = 0)
inject(initial) do |n, v| n + v end
end
def product(initial = 1)
inject(initial) do |n, v| n * v end
end
end unless defined? Inject
# used by plotting curves
# to_trias: [0, 1, 2, 3, 4, 5] --> [[0, 1, 2], [3, 4, 5]]
# to_path: [[-10, 10], [0, 5], [10, 10]].to_path <=> DL.make_path([[-10, 10], [0, 5], [10, 10]])
# uses the same options as DL.make_path()
class Array
include Inject
def to_trias
ary = []
unless self.length.divmod(3).last.nonzero?
0.step(self.length - 2, 3) do |i|
ary.push([self[i], self[i + 1], self[i + 2]])
end
end
ary
end
def to_path(*args)
DL.make_path(self, *args)
end
end
class Sndplot
def initialize(chns = 1)
@chns = chns
@snd = open
end
attr_reader :snd
def inspect
format("#<%s: snd: %d, chns: %d>", self.class, @snd, @chns)
end
def open
if snds = sounds()
snds.each do |s| set_sound_property(:selected, false, s) end
set_sound_property(:selected, true, selected_sound)
end
if @snd = snds.detect do |s| channels(s) >= @chns end
set_sound_property(:dlocsig_created, false, @snd)
select_sound(@snd)
else
@snd = new_sound(snd_tempnam, @chns, default_output_srate,
default_output_sample_type,
default_output_header_type)
set_sound_property(:dlocsig_created, true, @snd)
end
channels(@snd).times do |chn|
set_channel_property(:time_graph, time_graph?(@snd, chn), @snd, chn)
set_channel_property(:transform_graph, transform_graph?(@snd, chn), @snd, chn)
set_channel_property(:lisp_graph, lisp_graph?(@snd, chn), @snd, chn)
set_time_graph?(false, @snd, chn)
set_transform_graph?(false, @snd, chn)
set_lisp_graph?(true, @snd, chn)
end
$exit_hook.add_hook!("dlocsig-hook") do | |
close
false
end
@snd
end
def close
if snds = sounds()
snds.each do |snd|
set_sound_property(:selected, false, snd)
unless sound_property(:dlocsig_created, snd).nil?
if sound_property(:dlocsig_created, snd)
close_sound_extend(snd)
else
channels(snd).times do |chn|
set_time_graph?(channel_property(:time_graph, snd, chn), snd, chn)
set_transform_graph?(channel_property(:transform_graph, snd, chn), snd, chn)
set_lisp_graph?(channel_property(:lisp_graph, snd, chn), snd, chn)
end
end
end
end
end
$exit_hook.remove_hook!("dlocsig-hook")
self
end
end
class Gnuplot
@@plot_stream = nil
def initialize
if (not @@plot_stream) or @@plot_stream.closed?
open
end
end
def inspect
format("#<%s: plot_stream: %s>", self.class, @@plot_stream.inspect)
end
def open
if (gnuplot = `which gnuplot`).empty?
dl_error("gnuplot not found?")
else
@@plot_stream = IO.popen(gnuplot, "w")
end
end
def close
unless @@plot_stream.closed?
@@plot_stream.puts("quit")
@@plot_stream.close
@@plot_stream = nil
end
end
def command(*args)
open if @@plot_stream.closed?
@@plot_stream.printf(*args)
format(*args).chomp
rescue
Snd.warning("%s#%s", self.class, get_func_name)
end
def reset
command "reset\n"
end
def set_autoscale
command "set autoscale\n"
end
def set_x_range(range = [])
command("set xrange [%f:%f]\n", range[0], range[1]) if range.length == 2
end
def set_y_range(range = [])
command("set yrange [%f:%f]\n", range[0], range[1]) if range.length == 2
end
def set_z_range(range = [])
command("set zrange [%f:%f]\n", range[0], range[1]) if range.length == 2
end
def set_grid
command "set grid xtics; set grid ytics; set grid ztics\n"
end
def set_surface
command "set surface\n"
end
def set_parametric
command "set parametric\n"
end
def set_ticslevel(level = 0)
command("set ticslevel %.2f\n", level)
end
def set_title(title = "")
command("set title \"%s\"\n", title) unless title.empty?
end
def set_label(label = "")
command("set label \"%s\"\n", label) unless label.empty?
end
def set_margins(margin = 1)
command("set tmargin %f\n", margin)
command("set lmargin %f\n", margin)
command("set rmargin %f\n", margin)
command("set bmargin %f\n", margin)
end
def set_border(border = nil)
command("set border %d\n", border.to_i) if border
end
def start_multiplot
command "set multiplot\n"
end
def end_multiplot
command "set nomultiplot\n"
end
def size(xorigin, yorigin, xsize, ysize)
command("set origin %f,%f\n", xorigin.to_f, yorigin.to_f)
command("set size %f,%f\n", xsize.to_f, ysize.to_f)
end
def data(data, *args)
style, label = nil
optkey(args, binding,
[:style, "linespoints"],
[:label, ""])
command("plot '-' %s %s\n", label.empty? ? "" : "title \"#{label}\"",
style.empty? ? "" : "with #{style}")
data.each_with_index do |y, x| command("%f %f\n", x, y) end
command "e\n"
end
def plot_2d_curve(curve, *args)
style, label = nil
optkey(args, binding,
[:style, "linespoints"],
[:label, ""])
set_grid()
command("plot '-' %s %s\n", label.empty? ? "" : "title \"#{label}\"",
style.empty? ? "" : "with #{style}")
curve.each_pair do |x, y| command("%.8f %.8f\n", x, y) end
command "e\n"
end
def plot_2d_curves(curves, *args)
styles, labels = nil
optkey(args, binding,
[:styles, "linespoints"],
[:labels, ""])
set_grid()
styles = curves.map do |i| styles end unless array?(styles)
labels = curves.map do |i| labels end unless array?(labels)
command "plot"
curves.each_with_index do |x, i|
style = styles[i]
label = labels[i]
command " '-' "
command(" title \"%s\"", label) if label or (not label.empty?)
command(" with %s", style) if style or (not style.empty?)
command(", ") if i != (curves.length - 1)
end
command "\n"
curves.each do |curve|
curve.each_pair do |x, y| command("%.8f %.8f\n", x, y) end
command "e\n"
end
end
def plot_3d_curve(curve, *args)
style, label, zstyle, xrot, zrot, scale, zscale = nil
optkey(args, binding,
[:style, "linespoints"],
[:label, ""],
[:zstyle, "impulses"],
:xrot,
:zrot,
:scale,
:zscale)
set_border(127 + 256 + 512)
set_grid()
set_surface()
set_parametric()
set_ticslevel(0)
if xrot or zrot or scale or zscale
command("set view %s,%s,%s,%s\n", xrot, zrot, scale, zscale)
end
command "splot '-'"
command(" title \"%s\"", label) unless label.empty?
command(" with %s 1", style) unless style.empty?
command(", '-' notitle with %s 1", zstyle) unless zstyle.empty?
command "\n"
curve.to_trias.each do |x, y, z| command("%.8f %.8f %.8f\n", x, y, z) end
command "e\n"
if zstyle
curve.to_trias.each do |x, y, z| command("%.8f %.8f %.8f\n", x, y, z) end
command "e\n"
end
end
end
module DL
Path_maxcoeff = 8
Point707 = cos(TWO_PI / 8.0)
Amplitude_panning = 1
B_format_ambisonics = 2
Decoded_ambisonics = 3
def which_render(val)
case val
when :amplitude_panning, Amplitude_panning
Amplitude_panning
when :b_format_ambisonics, B_format_ambisonics
B_format_ambisonics
when :decoded_ambisonics, Decoded_ambisonics
Decoded_ambisonics
else
Amplitude_panning
end
end
def cis(r)
Complex(cos(r), sin(r))
end
def distance(x, y, z)
sqrt(x * x + y * y + z * z)
end
def nearest_point(x0, y0, z0, x1, y1, z1, px, py, pz)
if same?(x0, y0, z0, px, py, pz)
[x0, y0, z0]
elsif same?(x1, y1, z1, px, py, pz)
[x1, y1, z1]
elsif same?(x0, y0, z0, x1, y1, z1)
[x0, y0, z0]
else
xm0 = x1 - x0
ym0 = y1 - y0
zm0 = z1 - z0
xm1 = px - x0
ym1 = py - y0
zm1 = pz - z0
d0 = distance(xm0, ym0, zm0)
d1 = distance(xm1, ym1, zm1)
p = d1 * ((xm0 * xm1 + ym0 * ym1 + zm0 * zm1) / (d0 * d1))
ratio = p / d0
[x0 + xm0 * ratio, y0 + ym0 * ratio, z0 + zm0 * ratio]
end
end
def same?(a0, b0, c0, a1, b1, c1)
a0 == a1 and b0 == b1 and c0 == c1
end
def rotation_matrix(x, y, z, angle)
mag = distance(x, y, z)
dx = x / mag
dy = y / mag
dz = z / mag
ri = Matrix.I(3)
ra = Matrix.rows([[0.0, dz, -dy], [-dz, 0.0, dx], [dy, -dx, 0.0]])
raa = ra * ra
sn = sin(-angle)
omcs = 1 - cos(-angle)
raa = raa.map do |xx| omcs * xx end
ra = ra.map do |xx| sn * xx end
(ri + ra + raa)
end
class Dlocsig_base
include DL
def initialize
@one_turn = 360.0
@speed_of_sound = 344.0
end
attr_accessor :one_turn, :speed_of_sound
def angles_in_degree
@one_turn = 360.0
end
def angles_in_radians
@one_turn = TWO_PI
end
def angles_in_turns
@one_turn = 1.0
end
def distances_in_meters
@speed_of_sound = 344.0
end
def distances_in_feet
@speed_of_sound = 1128.0
end
end
class Speaker_config < Dlocsig_base
Groups = Struct.new("Groups", :size, :vertices, :speakers, :matrix)
def initialize
super
@number = nil
@coords = nil
@groups = nil
end
protected
def set_speakers(channels, d3)
if channels.between?(1, 8)
d3 = false if channels < 4
arrange_speakers(unless d3
case channels
when 1
[[0]]
when 2
[[-60, 60]]
when 3
[[-45, 45, 180]]
when 4
[[-45, 45, 135, 225]]
when 5
[[-45, 0, 45, 135, -135]]
when 6
[[-60, 0, 60, 120, 180, 240]]
when 7
[[-45, 0, 45, 100, 140, -140, -100]]
when 8
[[-22.5, 22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5]]
end
else
case channels
when 4
[[[-60, 0], [60, 0], [180, 0], [0, 90]],
[[0, 1, 3], [1, 2, 3], [2, 0, 3], [0, 1, 2]]]
when 5
[[[-45, 0], [45, 0], [135, 0], [-135, 0], [0, 90]],
[[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [0, 1, 2], [2, 3, 0]]]
when 6
[[[-45, 0], [45, 0], [135, 0], [-135, 0], [-90, 60], [90, 60]],
[[0, 1, 4], [1, 4, 5], [1, 2, 5], [2, 3, 5],
[3, 4, 5], [3, 0, 4], [0, 1, 2], [2, 3, 0]]]
when 7
[[[-45, 0], [45, 0], [135, 0], [-135, 0],
[-60, 60], [60, 60], [180, 60]],
[[0, 1, 4], [1, 4, 5], [1, 2, 5], [2, 6, 5], [2, 3, 6],
[3, 4, 6], [3, 0, 4], [4, 5, 6], [0, 1, 2], [2, 3, 0]]]
when 8
[[[-45, 10], [45, -10], [135, -10], [225, -10],
[-45, 45], [45, 45], [135, 45], [225, 45]],
[[0, 4, 5], [0, 5, 1], [5, 1, 2], [2, 6, 5], [6, 7, 2], [2, 3, 7],
[3, 7, 4], [3, 0, 4], [4, 7, 6], [6, 5, 4], [0, 1, 2], [2, 3, 0]]]
end
end)
else
dl_error(channels, "only 1 to 8 channels possible")
end
end
private
def arrange_speakers(args)
speakers = args.shift
groups = args.shift
@number = speakers.length
@coords = speakers.map do |s|
a = (array?(s) ? s[0] : s).to_f
e = (array?(s) ? s[1] : 0).to_f
evec = cis((e / @one_turn) * TWO_PI)
dxy = evec.real
avec = cis((a / @one_turn) * TWO_PI)
x = (dxy * avec.imag)
y = (dxy * avec.real)
z = evec.imag
mag = distance(x, y, z)
[x / mag, y / mag, z / mag]
end
unless groups
if @number == 1
groups = [[0]]
else
groups = make_array(@number) do |i| [i, i + 1] end
groups[-1][-1] = 0
end
end
@groups = groups.map do |group|
size = group.length
vertices = group.map do |vertice| @coords[vertice] end
matrix = case size
when 3
if (m = Matrix[vertices[0], vertices[1], vertices[2]]).regular?
m.inverse.to_a
else
nil
end
when 2
if (m = Matrix[vertices[0][0, 2], vertices[1][0, 2]]).regular?
m.inverse.to_a
else
nil
end
else
nil
end
Groups.new(size, vertices, group, matrix)
end
end
end
class Dlocsig < Speaker_config
def initialize
super
@render_using = Amplitude_panning
@output_power = 1.5
@reverb_power = 0.5
@rbm_output = nil
@rbm_reverb = nil
@out_channels = 4
@rev_channels = 1
@clm = true
@delay = []
@prev_time = @prev_dist = @prev_group = @prev_x = @prev_y = @prev_z = false
@first_dist = @last_dist = 0.0
@min_dist = @max_dist = 0.0
@start = nil
@end = nil
@output_gains = nil
@reverb_gains = nil
@path = nil
@run_beg = @run_end = nil
end
attr_reader :run_beg, :run_end, :out_channels, :rev_channels
def inspect
format("#<%s: channels: %d, reverb: %s>", self.class, @out_channels, @rev_channels.inspect)
end
def each
(@run_beg...@run_end).each do |i| yield(i) end
end
alias run each
# general clm version
# dl.dlocsig(loc, val)
def dlocsig(loc, input)
if loc < @start
delay(@path, (loc >= @end ? 0.0 : input), 0.0)
@out_channels.times do |chn| out_any(loc, 0.0, chn, @rbm_output) end
else
sample = delay(@path, (loc >= @end ? 0.0 : input), env(@delays))
@out_channels.times do |chn|
out_any(loc, sample * env(@output_gains[chn]), chn, @rbm_output)
end
@rev_channels.times do |chn|
out_any(loc, sample * env(@reverb_gains[chn]), chn, @rbm_reverb)
end
end
end
# dl.ws_dlocsig do |loc| ...; val; end
# @clm == true @rbm_output/@rbm_reverb: sample2files
# @clm == false @rbm_output/@rbm_reverb: sound index numbers
# With_sound#run_dlocsig below uses this method
def ws_dlocsig
len = @run_end - @run_beg
out_data = make_vct(len)
len.times do |i|
loc = i + @run_beg
input = yield(loc)
if loc < @start
delay(@path, (loc >= @end ? 0.0 : input), 0.0)
else
out_data[i] = delay(@path, (loc >= @end ? 0.0 : input), env(@delays))
end
end
@out_channels.times do |chn|
if @clm
(@run_beg...@run_end).each do |i|
out_any(i, out_data[i - @run_beg] * env(@output_gains[chn]), chn, @rbm_output)
end
else
out = vct_multiply!(vct_copy(out_data),
Vct.new(len) do |x| env(@output_gains[chn]) end)
mix_vct(out, @run_beg, @rbm_output, chn, false)
end
end
@rev_channels.times do |chn|
if @clm
(@run_beg...@run_end).each do |i|
out_any(i, out_data[i - @run_beg] * env(@reverb_gains[chn]), chn, @rbm_reverb)
end
else
out = vct_multiply!(vct_copy(out_data),
Vct.new(len) do |x| env(@reverb_gains[chn]) end)
mix_vct(out, @run_beg, @rbm_reverb, chn, false)
end
end
end
# :amplitude_panning
# :b_format_ambisonics
# :decoded_ambisonics
def make_dlocsig(startime, dur, *args)
path, scaler, reverb_amount, output_power, reverb_power, render_using = nil
rbm_output, rbm_reverb, out_channels, rev_channels, clm = nil
optkey(args, binding,
:path,
[:scaler, 1.0],
[:reverb_amount, 0.05],
[:output_power, 1.5],
[:reverb_power, 0.5],
[:render_using, Amplitude_panning],
[:rbm_output, $output],
[:rbm_reverb, $reverb],
[:out_channels, 4],
[:rev_channels, 1],
[:clm, true])
@output_power = output_power
@reverb_power = reverb_power
@render_using = which_render(render_using)
@rbm_output = rbm_output
@rbm_reverb = rbm_reverb
@out_channels = out_channels
@rev_channels = rev_channels
@clm = clm
if @render_using == B_format_ambisonics and @out_channels != 4
dl_error("B_format_ambisonics requires 4 output channels")
end
if @render_using == B_format_ambisonics and
@rev_channels.nonzero? and
(@rev_channels != 1 and @rev_channels != 4)
dl_error("B_format_ambisonics accepts only 0, 1 or 4 rev_channels")
end
if @rev_channels > @out_channels
dl_error("more rev_channels than out_channels")
end
if @render_using == B_format_ambisonics
scaler *= 0.8
end
unless path.kind_of?(Path)
if array?(path) and !path.empty?
path = make_path(path)
else
dl_error(path, "sorry, need a path")
end
end
xpoints = path.path_x
ypoints = path.path_y
zpoints = path.path_z
tpoints = path.path_time
@channel_gains = make_array(@out_channels) do [] end
@channel_rev_gains = make_array(@rev_channels) do [] end
self.set_speakers(@out_channels, (not zpoints.detect do |x| x.nonzero? end.nil?))
@speed_limit = (@speed_of_sound * (tpoints[-1] - tpoints[0])) / dur
if xpoints.length == 1
walk_all_rooms(xpoints[0], ypoints[0], zpoints[0], tpoints[0])
else
xb = yb = zb = tb = 0.0
(tpoints.length - 1).times do |i|
xa, xb = xpoints[i, 2]
ya, yb = ypoints[i, 2]
za, zb = zpoints[i, 2]
ta, tb = tpoints[i, 2]
minimum_segment_length(xa, ya, za, ta, xb, yb, zb, tb)
end
walk_all_rooms(xb, yb, zb, tb)
end
@start = dist2samples(@first_dist - @min_dist)
@end = seconds2samples(startime + dur)
min_delay = dist2samples(@min_dist)
@run_beg = seconds2samples(startime)
@run_end = @end + dist2samples(@last_dist) - min_delay
real_dur = dur + dist2seconds(@last_dist - @first_dist)
min_dist_unity = [@min_dist, 1.0].max
unity_gain = scaler * min_dist_unity ** @output_power
@output_gains = make_array(@number) do |i|
make_env(:envelope, @channel_gains[i], :scaler, unity_gain, :duration, real_dur)
end
if @rev_channels.nonzero?
unity_rev_gain = reverb_amount * scaler * min_dist_unity ** @reverb_power
@reverb_gains = make_array(@rev_channels) do |i|
make_env(:envelope, @channel_rev_gains[i], :scaler, unity_rev_gain, :duration, real_dur)
end
end
@delays = make_env(:envelope, @delay, :offset, -min_delay, :duration, real_dur)
@path = make_delay(:size, 1, :max_size, [1, dist2samples(@max_dist)].max)
self
end
private
def dist2samples(d)
(d * (mus_srate() / @speed_of_sound)).round
end
def dist2seconds(d)
d / @speed_of_sound.to_f
end
def transition_point_3(vert_a, vert_b, xa, ya, za, xb, yb, zb)
line_b = vct(xa, ya, za)
line_m = tr3_sub(vct(xb, yb, zb), line_b)
normal = tr3_cross(vert_a, vert_b)
if (denominator = tr3_dot(normal, line_m)).abs <= 0.000001
false
else
vct2list(tr3_add(line_b, tr3_scale(line_m, -tr3_dot(normal, line_b) / denominator)))
end
end
def tr3_cross(v1, v2)
vct(v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0])
end
def tr3_dot(v1, v2)
dot_product(v1, v2)
end
def tr3_sub(v1, v2)
vct_subtract!(vct_copy(v1), v2)
end
def tr3_add(v1, v2)
vct_add!(vct_copy(v1), v2)
end
def tr3_scale(v1, c)
vct_scale!(vct_copy(v1), c)
end
def transition_point_2(vert, xa, ya, xb, yb)
ax = vert[0]
bx = xa - xb
ay = vert[1]
by = ya - yb
cx = -xa
cy = -ya
d = by * cx - bx * cy
f = ay * bx - ax * by
if f.zero?
false
else
[(d * ax) / f, (d * ay) / f]
end
end
def calculate_gains(x, y, z, group)
zero_coord = 1e-10
zero_gain = 1e-10
size = group.size
if mat = group.matrix
if x.abs < zero_coord and y.abs < zero_coord and z.abs < zero_coord
[true, [1.0, 1.0, 1.0]]