Subversion Repositories freemyipod

Rev

Rev 714 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 user890104 1
<?php
2
//
3
//
4
//    Copyright 2011 user890104
5
//
6
//
7
//    This file is part of emCORE.
8
//
9
//    emCORE is free software: you can redistribute it and/or
10
//    modify it under the terms of the GNU General Public License as
11
//    published by the Free Software Foundation, either version 2 of the
12
//    License, or (at your option) any later version.
13
//
14
//    emCORE is distributed in the hope that it will be useful,
15
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
//    See the GNU General Public License for more details.
18
//
19
//    You should have received a copy of the GNU General Public License along
20
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
21
//
22
//
23
 
24
 
25
if (
26
    empty($_SERVER['argc']) || empty($_SERVER['argv']) ||
27
    !is_int($_SERVER['argc']) || !is_array($_SERVER['argv']) ||
28
    3 !== $_SERVER['argc'] || 3 !== count($_SERVER['argv'])
29
)
30
{
31
    echo 'usage: ', $_SERVER['argv'][0], ' <input> <output>', "\n";
32
    exit;
33
}
34
 
35
$start_oct = 0;
36
$end_oct = 9;
37
 
38
$notes = array();
39
 
40
for ($oct = $start_oct; $oct < $end_oct; ++$oct)
41
{
42
    $notes[$oct] = array();
43
    $notes[$oct][-1] = 0;
44
 
45
    for ($note = 0; $note < 12; ++$note)
46
    {
47
        $freq = pow(2, ($oct * 12 + $note - 45) / 12) * 440;
48
 
49
        $notes[$oct][$note] = $freq;
50
    }
51
}
52
 
53
$note_names = array(
54
    'X' => -2,
55
    'P' => -1,
56
    'C' => 0,
57
    'C#' => 1,
58
    'D' => 2,
59
    'D#' => 3,
60
    'E' => 4,
61
    'F' => 5,
62
    'F#' => 6,
63
    'G' => 7,
64
    'G#' => 8,
65
    'A' => 9,
66
    'A#' => 10,
67
    'B' => 11,
68
);
69
 
70
// ----------------------
71
 
72
$beat_len = 0;
73
 
74
$note_lengths = array(
75
    '1+1+1/4' => 9,
76
    '1+1' => 8,
77
    '1' => 4,
78
    '2.5' => 3,
79
    '2+1/4' => 2.5,
80
    '2' => 2,
81
    '4.5' => 1.5,
82
    '4+1/2' => 1.5,
83
    '4' => 1,
84
    '8' => .5,
764 user890104 85
    '8.5' => .75,
714 user890104 86
    '8+1/2+1/4' => .3 + 5/4,
87
    '16' => .25,
88
    '32' => .125,
89
);
90
 
91
file_put_contents($_SERVER['argv'][2], '');
92
 
93
$file = file($_SERVER['argv'][1]);
94
$file = array_map('trim', $file);
95
$file = array_map('strtoupper', $file);
96
 
97
foreach ($file as $line)
98
{
99
    if (strlen($line) < 1 || '#' === $line[0])
100
    {
101
        continue;
102
    }
103
    elseif ('T' == $line[0])
104
    {
105
        $beat_len = 60 / trim(substr($line, 1));
106
        continue;
107
    }
108
 
109
    if (empty($beat_len))
110
    {
111
        continue;
112
    }
113
    $line = preg_replace('/\s+/', ' ', $line);
114
    @list($note, $oct, $len) = explode(' ', $line);
115
 
116
    $note = $note_names[$note];
117
 
118
    if (-2 === $note)
119
    {
120
        exit;
121
    }
122
 
123
    $len = $note_lengths[$len] * $beat_len;
124
 
125
    file_put_contents($_SERVER['argv'][2], pack('VV', round($notes[$oct][$note] ? 91225 / $notes[$oct][$note] : 0), round($len * 1000000)), FILE_APPEND);
126
}
127
?>