-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_noise.rb
190 lines (148 loc) · 3.14 KB
/
gen_noise.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
require "wavefile"
include WaveFile
params = {}
params["1"] = {
seed: 1,
envelope: [
[0.0 , 0.5],
[0.02, 0.0],
[0.02, 1.0],
[0.1 , 0.0],
]
}
params["1_2"] = {
seed: 1,
envelope: [
[0.0 , 0.0],
[0.005 , 0.5],
[0.02, 0.0],
[0.025, 1.0],
[0.1 , 0.0],
]
}
params["2"] = {
seed: 1,
envelope: [
[0.0 , 0.0],
[0.005, 1.0],
[0.08, 0.0],
[0.085, 0.5],
[0.1 , 0.0],
]
}
params["3"] = {
seed: 1,
envelope: [
[0.0 , 0.5],
[0.01, 0.5],
[0.01, 0.0],
[0.07, 0.0],
[0.07, 1.0],
[0.1 , 0.0],
]
}
PARAM = params["1"]
# --------------------------------
class NoiseOscillator
def initialize(seed)
r = Random.new(seed)
@values = []
10000.times {
@values << r.rand * 2 - 1
}
end
def generate(ratio)
i = ((@values.size - 1) * ratio).floor
@values[i]
end
def self.instance
@@instance ||= NoiseOscillator.new(PARAM[:seed])
@@instance
end
end
def osc_noise(ratio)
NoiseOscillator.instance.generate(ratio)
end
# --------------------------------
class Envelope
class Block
attr_reader :sec_beg, :v_beg
attr_reader :sec_end, :v_end
def initialize(
sec_beg, v_beg,
sec_end, v_end
)
@sec_beg = sec_beg
@sec_end = sec_end
@v_beg = v_beg
@v_end = v_end
end
def include?(sec)
@sec_beg <= sec && sec < @sec_end
end
def duration
@sec_end - @sec_beg
end
end
def initialize(param)
@param = param
@blocks = param.each_cons(2).map { |a, b|
Block.new(a[0], a[1], b[0], b[1])
}
end
def get_amp(sec)
if sec == @param.last[0]
return @param.last[1]
end
block = @blocks.find { |bl| bl.include?(sec) }
x_ratio = (sec - block.sec_beg) / block.duration
v_delta = (block.v_end - block.v_beg) * x_ratio
block.v_beg + v_delta
end
end
# --------------------------------
params = {}
ARGV.map { |arg|
md = /^(.+?)=(.+)/.match(arg)
k = md[1].to_sym
v = md[2]
params[k] =
case k
when :msec, :hz, :amp
v.to_f
when :out
v
else
raise "invalid key (#{k})"
end
}
# --------------------------------
# サンプリングレート(サンプル数 / 秒)
srate = 44100
# 増幅率 (0.0 <= x <= 1.0)
amp = params[:amp] || 0.1
duration_msec = params[:msec] || 100.0
# 全体のサンプル数
num_samples = srate * (duration_msec.to_f / 1000)
hz = params[:hz] || 440.0
# 1周期あたりのサンプル数
num_samples_per_cycle = srate / hz
out_file = params[:out] || "output.wav"
# --------------------------------
samples = []
envelope = Envelope.new(PARAM[:envelope])
(0 ... num_samples).each { |t|
t_in_cycle = t % num_samples_per_cycle
ratio = t_in_cycle / num_samples_per_cycle
sec = t.to_f / srate
sample = osc_noise(ratio)
env_amp = envelope.get_amp(sec)
samples << sample * env_amp * amp
}
# --------------------------------
buffer_format = Format.new(:mono, :float, srate)
file_format = Format.new(:mono, :pcm_16, srate)
buffer = Buffer.new(samples, buffer_format)
Writer.new(out_file, file_format) do |writer|
writer.write(buffer)
end