-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhilbert-notes
95 lines (77 loc) · 2.18 KB
/
hilbert-notes
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
#!/usr/bin/env perl
use strict;
use warnings;
use Math::Curve::Hilbert;
use lib '/Users/gene/sandbox/MIDI-Util/lib';
use MIDI::Util qw(setup_score);
use lib '/Users/gene/sandbox/Music-ScaleNote/lib';
use Music::ScaleNote;
use Music::Note;
my $max = shift || 64;
my $bpm = shift || 120;
my $t_scale = shift || 'dorian';
my $b_scale = shift || 'pminor';
my $score = setup_score( bpm => $bpm );
my $format = 'midinum';
my $treb = 60;
my $bass = 48;
my $t_note = Music::Note->new( $treb, $format );
my $b_note = Music::Note->new( $bass, $format );
my $t_msn = Music::ScaleNote->new(
scale_note => $t_note->format('isobase'),
scale_name => $t_scale,
note_format => $format,
# verbose => 1,
);
my $b_msn = Music::ScaleNote->new(
scale_note => $b_note->format('isobase'),
scale_name => $b_scale,
note_format => $format,
# verbose => 1,
);
my $hilbert = Math::Curve::Hilbert->new(
direction => 'up',
max => 8,
clockwise => 1,
step => 4,
);
my $count = 0;
# Start the Hilbert curve
my ( $x1, $y1 ) = $hilbert->CoordinatesFromPoint( $count++ );
# Draw the Hilbert curve
while ( ( $hilbert->CoordinatesFromPoint($count) )[0] ) {
$score->n( 'qn', $treb, $bass );
# Get a new point on the curve
my ( $x2, $y2 ) = $hilbert->CoordinatesFromPoint( $count++ );
# Inc/Decrement the treble note
if ( $x2 - $x1 > 0 ) {
$treb = $t_msn->get_offset(
note_name => $treb,
offset => 1,
)->format($format);
}
elsif ( $x2 - $x1 < 0 ) {
$treb = $t_msn->get_offset(
note_name => $treb,
offset => -1,
)->format($format);
}
# Inc/Decrement the bass note
if ( $y2 - $y1 > 0 ) {
$bass = $b_msn->get_offset(
note_name => $bass,
offset => 1,
)->format($format);
}
elsif ( $y2 - $y1 < 0 ) {
$bass = $b_msn->get_offset(
note_name => $bass,
offset => -1,
)->format($format);
}
# Increment the line segment
( $x1, $y1 ) = ( $x2, $y2 );
# End the loop if we have reached the maximum
last if $count > $max;
}
$score->write_score("$0.mid");