-
Notifications
You must be signed in to change notification settings - Fork 0
/
piano.rb
65 lines (44 loc) · 1.5 KB
/
piano.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
require_relative "music_common"
def gen_chord_event(chord,len)
events = []
chord_root, chord_quality = get_intervals(chord)
invert(chord_quality,1) while ((chord_quality.sum / chord_quality.count) < 45)
chord_quality.each do |i|
events << MIDI::NoteOn.new(0, chord_root + i, 127, 0)
end
chord_quality.each_with_index do |e,i|
events << MIDI::NoteOff.new(0, chord_root + e, 127, i == 0 ? len : 0)
end
events
end
def add_piano_track(json,seq)
# Create a track to hold the notes. Add it to the sequence.
track = MIDI::Track.new(seq)
seq.tracks << track
# Give the track a name and an instrument name (optional).
track.name = 'Piano'
track.instrument = MIDI::GM_PATCH_NAMES[0]
# Add a volume controller event (optional).
track.events << MIDI::Controller.new(0, MIDI::CC_VOLUME, 127)
# Add events to the track: a major scale. Arguments for note on and note off
# constructors are channel, note, velocity, and delta_time. Channel numbers
# start at zero. We use the new Sequence#note_to_delta method to get the
# delta time length of a single quarter note.
track.events << MIDI::ProgramChange.new(0, 1, 0)
qnl = seq.note_to_delta('quarter')
json["structure"].flatten.each do |s|
if s.include? "A"
json["a_section"].each do |bar|
bar.each do |chord|
track.events += gen_chord_event(chord,(4/bar.length)*qnl)
end
end
else
json["b_section"].each do |bar|
bar.each do |chord|
track.events += gen_chord_event(chord,(4/bar.length)*qnl)
end
end
end
end
end