-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.rb
40 lines (34 loc) · 851 Bytes
/
mixer.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
class Mixer
attr_reader :output
attr_reader :size
def initialize
@inputs = []
@output = []
@channels = 0.0
@size = 0
end
def add_input(input)
@inputs << input
@inputs.sort! { |a, b| a.size <=> b.size }
@channels += 1.0
@size += input.size
end
def process
len = @inputs.last.size
len.times.map.with_index do |sample, i|
sum = 0.0
@inputs.map(&:output).each do |ip|
next if ip.empty?
sum += ip.shift
end
@output[i] = sum / @channels
end
len
end
def inspect
puts "num_inputs: #{@channels}, length: #{@output.size / 44100}"
end
def to_s
puts "num_inputs: #{@channels}, length: #{@output.size / 44100}"
end
end