-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratioscore.pl
executable file
·152 lines (124 loc) · 3.4 KB
/
ratioscore.pl
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
#!/usr/bin/perl
##
## Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
## Creation Date: Sat 03 Apr 2021 01:32:58 PM PDT
## Last Modified: Sat 03 Apr 2021 09:12:27 PM PDT
## Filename: /var/www/cgi-bin/ratioscore.pl
## Syntax: Perl 5; CGI
## $Smake: cp -f %f /var/www/cgi-bin/%b
## vim: ts=3
##
## Description: Convert Ratioscores into MIDI or MP3 files.
## Backend for https://ratioscore.humdrum.org
##
use strict;
use IPC::Open2;
use CGI;
my $cgi_form = new CGI;
# Configuration variables:
my $basedir = "/project/ratioscore";
# Command-line tools:
my $ratioscore = "$basedir/ratioscore/bin/ratioscore";
my $timidity = "/usr/bin/timidity";
my $lame = "/usr/bin/lame";
##########################################################################
############################## ### ##
############### ###############
my %OPTIONS;
$OPTIONS{'inputdata'} = $cgi_form->param('inputdata');
$OPTIONS{'outputformat'} = $cgi_form->param('outputformat');
# outputformat == "mid" for MIDI
# outputformat == "mp3" for MP3
if ($OPTIONS{'inputdata'} =~ /^\s*$/) {
errorMessage("Error: no input score");
} elsif ($OPTIONS{'outputformat'} =~ /mid/i) {
generateMidiFile();
} elsif ($OPTIONS{'outputformat'} =~ /mp3/i) {
generateMp3File();
} else {
printParameters();
}
exit(0);
############### ###############
############################## ### ##
##########################################################################
##############################
##
## generateMidiFile --
##
sub generateMidiFile {
my $inputdata = $OPTIONS{"inputdata"};
local (*READ, *WRITE);
my $command = "$ratioscore -r";
my $outputdata = "";
my $pid = open2(\*READ, \*WRITE, "$command") or $outputdata = "ERROR";
if ($outputdata ne "ERROR") {
print WRITE $inputdata;
close(WRITE);
binmode(READ);
my $udata;
while (read(READ, $udata, 4096)) {
$outputdata .= $udata
}
}
if ($outputdata =~ /^MThd/) {
print "Content-Type: audio/midi\n\n";
print "$outputdata";
} else {
errorMessage($outputdata);
}
}
##############################
##
## generateMp3File --
##
sub generateMp3File {
my $inputdata = $OPTIONS{"inputdata"};
local (*READ, *WRITE);
my $command = "$ratioscore -r -x 630";
$command .= " | $timidity - -m60000 --preserve-silence -Ow -o -";
$command .= " | $lame - -b64 -";
my $outputdata = "";
my $pid = open2(\*READ, \*WRITE, "$command") or $outputdata = "ERROR";
if ($outputdata ne "ERROR") {
print WRITE $inputdata;
close(WRITE);
binmode(READ);
my $udata;
while (read(READ, $udata, 4096)) {
$outputdata .= $udata
}
}
if ($outputdata =~ /[^\t\n\x20-x7e]/) {
print "Content-Type: audio/mpeg\n\n";
print "$outputdata";
} else {
errorMessage("ERROR: " . $outputdata);
}
}
##############################
##
## errorMessage --
##
sub errorMessage {
my ($message) = @_;
print "Content-Type: text/plain\n\n";
print "$message\n";
}
##############################
##
## printParameters --
##
sub printParameters {
my $output = "";
my $name;
my $value;
foreach my $name ($cgi_form->param) {
foreach my $value ($cgi_form->param($name)) {
$output .= "$name ==\t$value\n";
}
}
print "Content-Type: text/plain\n\n";
print "CGI parameters:\n";
print "$output\n";
}