-
Notifications
You must be signed in to change notification settings - Fork 5
/
buzz
118 lines (94 loc) · 2.92 KB
/
buzz
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
#!/usr/bin/env perl
use strict;
use warnings;
# https://www.youtube.com/watch?v=_WviF3f1VLM
use MIDI::Util qw(setup_score set_chan_patch midi_format);
use Music::Chord::Progression ();
use Music::Duration::Partition ();
use Music::Scales qw(get_scale_MIDI);
use Music::VoiceGen ();
my $max = shift || 16; # number of measures to play
my $bpm = shift || 90; # beats per minute
my $note = shift || 'C'; # tonic
my $tpatch = shift || 69; # electric piano=4&5, warm synth=89, sweep synth=95, fretless bass=35, English horn=69
my $cpatch = shift || 5;
my $bpatch = shift || 35;
my $tscale = shift || 'major';
my $bscale = shift || 'pentatonic';
my $toctave = shift || 4;
my $boctave = shift || 2;
my $channel = 0; # initial midi channel
my $score = setup_score(
lead_in => 0,
bpm => $bpm,
);
$score->synch(
\&bottom,
\&top,
\&chords,
);
$score->write_score("$0.mid");
sub bottom {
set_chan_patch($score, $channel++, $bpatch);
my $mdp = Music::Duration::Partition->new(
size => 4, # 4 quarter notes = 1 measure
pool => [qw/ wn dhn hn qn /],
);
my @motifs = map { $mdp->motif } 1 .. 4;
my @pitches = get_scale_MIDI($note, $boctave, $bscale);
my @intervals = qw(-3 -2 -1 1 2 3);
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => \@intervals,
);
for my $n (1 .. $max) {
my $motif = $motifs[ int rand @motifs ];
my @notes = map { $voice->rand } @$motif;
for my $duration (0 .. $#$motif) {
$score->n($motif->[$duration], $notes[$duration]);
}
}
$score->n('wn', $pitches[0]);
}
sub top {
set_chan_patch($score, $channel++, $tpatch);
$score->Volume(100);
my $mdp = Music::Duration::Partition->new(
size => 4, # 4 quarter notes = 1 measure
pool => [qw/ hn dqn qn dqn en /],
);
my @motifs = map { $mdp->motif } 1 .. 6;
my @pitches = (
get_scale_MIDI($note, $toctave, $tscale),
get_scale_MIDI($note, $toctave + 1, $tscale)
);
my @intervals = qw(-3 -2 -1 1 2 3);
my $voice = Music::VoiceGen->new(
pitches => \@pitches,
intervals => \@intervals,
);
for my $n (1 .. $max) {
my $motif = $motifs[ int rand @motifs ];
my @notes = map { $voice->rand } @$motif;
for my $duration (0 .. $#$motif) {
$score->n($motif->[$duration], $notes[$duration]);
}
}
}
sub chords {
set_chan_patch($score, $channel++, $cpatch);
$score->Volume(100);
my $prog = Music::Chord::Progression->new(
max => 4,
chord_map => ['', 'm', 'm', '', '', 'm', 'm'],
# tonic => 0,
resolve => 0,
verbose => 1,
);
my $progressions = [ map { $prog->generate } 1 .. $max / $prog->max ];
for my $chords (@$progressions) {
for my $chord (@$chords) {
$score->n('wn', midi_format(@$chord));
}
}
}