-
Notifications
You must be signed in to change notification settings - Fork 2
/
soundgenerator.rb
206 lines (176 loc) · 5.12 KB
/
soundgenerator.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
require 'sndlib'
require 'fileutils'
require 'yaml'
def midi_to_freq(note)
440.0 * ( 2.0 ** ((note.to_f - 69.0) / 12))
end
# pattern = {
# :notes => [
# 64, nil, nil, nil,
# 60, nil, 58, nil,
# 64, nil, nil, 65,
# 64, nil, nil, nil
# ],
# :length => [
# 16, nil, nil, nil,
# 16, nil, 4, nil,
# 8, nil, nil, 1,
# 8, nil, nil, nil,
# ],
# :sound => [
# 8, nil, nil, nil,
# 2, nil, 5, nil,
# 4, nil, nil, 12,
# 4, nil, nil, nil,
# ]
# }
#
# File.open( 'pattern.yaml', 'w' ) do |out|
# YAML.dump( pattern, out )
# end
class PatternMem
def initialize
reload
end
def reload
@mem = YAML.load_file('pattern.yml')
end
def[](type, num, field)
@mem[type.to_s][num.to_i].nil? ? nil : @mem[type.to_s][num.to_i][field.to_s]
end
def note?(num)
@mem['not'][num]
end
end
class MoogFilter
def initialize
@in1 = @in2 = @in3 = @in4 = 0
@out1 = @out2 = @out3 = @out4 = 0
end
def filter(input, fc, res)
f = fc * 1.16;
fb = res * (1.0 - 0.15 * f * f);
input -= @out4 * fb;
input *= 0.35013 * (f*f)*(f*f);
@out1 = input + 0.3 * @in1 + (1 - f) * @out1; # Pole 1
@in1 = input;
@out2 = @out1 + 0.3 * @in2 + (1 - f) * @out2; # Pole 2
@in2 = @out1;
@out3 = @out2 + 0.3 * @in3 + (1 - f) * @out3; # Pole 3
@in3 = @out2;
@out4 = @out3 + 0.3 * @in4 + (1 - f) * @out4; # Pole 4
@in4 = @out3;
return @out4;
end
end
class BufferWriter
include FileUtils
def initialize(basename, file_size)
puts "opening new stream with name #{basename}, filesize #{file_size}"
@basename = basename
@file_size = file_size
@segment = 0
@pointer_in_file = 0
@pointer_in_buffer = 0
end
def update(buffer)
puts "updating from buffer with size #{buffer.size}"
open_file
words_to_write = [(@file_size - @pointer_in_file), (buffer.length - @pointer_in_buffer)].min
return if words_to_write == 0
puts "words_to_write: #{words_to_write}"
puts "writing #{@basename}_#{@segment} #{@pointer_in_buffer} > #{@pointer_in_buffer + words_to_write - 1}"
mus_sound_write(@file, @pointer_in_buffer, @pointer_in_buffer + words_to_write - 1, 1, buffer)
@pointer_in_buffer += words_to_write
@pointer_in_file += words_to_write
if @pointer_in_file >= @file_size
close_file
end
open_file
update(buffer) # writing out rests of buffer, if needed
if (@pointer_in_buffer >= buffer.size)
@pointer_in_buffer = 0
end
end
def open_file
unless @file
@segment += 1
@file = mus_sound_open_output("public/media/#{@basename}_#{@segment}.wav", 44100, 1, Mus_lshort, Mus_riff)
end
end
def close_file
mus_sound_close_output(@file, @file_size * 2)
system "/usr/bin/ffmpeg -er 4 -y -i public/media/#{@basename}_#{@segment}.wav -f mpegts -acodec mp3 -ac 1 -ar 44100 -ab 64k public/media/#{@basename}_#{@segment}.ts"
#system "/usr/bin/lame --preset"
rm("public/media/#{@basename}_#{@segment}.wav")
@file = nil
@pointer_in_file = 0
write_m3u8
cleanup_old_files
end
def write_m3u8
File.open("#{@basename}.m3u8", "w") do |file|
file.puts "#EXTM3U"
file.puts "#EXT-X-TARGETDURATION:5"
file.puts "#EXT-X-ALLOW-CACHE:yes"
min = 0
if (@segment > 5)
file.puts "#EXT-X-MEDIA-SEQUENCE:#{@segment - 5}"
min = @segment - 5
end
min.upto(@segment) do |seg|
file.puts "#EXTINF:5,"
# you probably need to change this here:
file.puts "http://halfmac.local:4567/media/#{@basename}_#{seg}.ts"
end
end
end
def cleanup_old_files
if @segment > 5
0.upto(@segment - 5) do |i|
file_name = "public/media/#{@basename}_#{i}.ts"
if File.exists?(file_name)
rm(file_name)
end
end
end
end
end
STREAM_SEGMENT_LENGTH = 44_100 * 10
BPM = 125
freq = BPM.to_f / 60.to_f
buffer_length = (4 * 44_100 / freq).to_i
puts buffer_length
buffer = SoundData.new(1, buffer_length.to_i+1);
mus_srate = 44100
wave = make_sawtooth_wave(440)
env = make_env([0,1,1,0], 1, 0.3)
filter = MoogFilter.new
current_note = -1
filter_freq = 0
resonance = 0
volume = 0
bw = BufferWriter.new("test", 44_100 * 5)
pattern = PatternMem.new
loop do
pattern.reload
0.upto(buffer_length - 1) do |i|
pos = i.to_f / (buffer_length / 16).to_f
if (pos.to_i > current_note)
current_note = pos.to_i
if pattern.note?(current_note)
env = make_env([0,1,0.2,0], 1, (pattern[:env, current_note, :x] || 4).to_f / 16.to_f)
midi_note = pattern[:not, current_note, :x] * 12 + pattern[:not, current_note, :y]
freq = midi_to_freq(midi_note)
freq = 22050 if freq > 22050
wave = make_sawtooth_wave(freq)
filter_freq = (pattern[:snd, current_note, :y] || 8).to_f / 16.to_f
resonance = (pattern[:snd, current_note, :x] || 8).to_f / 4.to_f
volume = (pattern[:env, current_note, :y] || 10).to_f / 16.to_f
end
end
buffer[0,i] = filter.filter(sawtooth_wave(wave) * env(env) * volume, filter_freq, resonance)
end
bw.update(buffer)
current_note = -1
end