-
Notifications
You must be signed in to change notification settings - Fork 5
/
h-chord.rb
59 lines (45 loc) · 1.32 KB
/
h-chord.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
require 'bundler'
Bundler.require
twelth_root_of_two = (2 ** (1.0/12))
current = 220.0
note_frequencies = [current]
50.times do
current *= twelth_root_of_two
note_frequencies.push current
end
T = 2
S = 1
scale_pattern = [T, T, S, T, T, T, S]
current = 0
all_notes = [note_frequencies.first]
scale_pattern.in_groups_of(2, false) do |group|
current += group.sum
all_notes << note_frequencies[current]
end
sample_rate = 22050 # frames per second
note_duration = 3 # seconds
frames_per_note = (sample_rate * note_duration).to_i
samples = []
all_notes.each do |frequency|
cycles_per_frame = frequency / sample_rate
increment = 2 * Math::PI * cycles_per_frame
phase = 0
frames_per_note.times.map do |i|
# This time we add the samples to play multiple notes
samples[i] ||= 0
samples[i] += Math.sin(phase)
phase += increment
end
end
# Normalize the samples as the maximum can be above 1 now
max = samples.map{ |s| s.abs }.max
multiplier = 1.0 / max
samples.map!{ |s| multiplier * s }
filename = 'h.wav'
format = WaveFile::Format.new :mono, :pcm_16, sample_rate
buffer_format = WaveFile::Format.new :mono, :float, sample_rate
WaveFile::Writer.new filename, format do |writer|
buffer = WaveFile::Buffer.new samples, buffer_format
writer.write buffer
end
`afplay h.wav`