Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | 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,
85
    '8+1/2+1/4' => .3 + 5/4,
86
    '16' => .25,
87
    '32' => .125,
88
);
89
 
90
file_put_contents($_SERVER['argv'][2], '');
91
 
92
$file = file($_SERVER['argv'][1]);
93
$file = array_map('trim', $file);
94
$file = array_map('strtoupper', $file);
95
 
96
foreach ($file as $line)
97
{
98
    if (strlen($line) < 1 || '#' === $line[0])
99
    {
100
        continue;
101
    }
102
    elseif ('T' == $line[0])
103
    {
104
        $beat_len = 60 / trim(substr($line, 1));
105
        continue;
106
    }
107
 
108
    if (empty($beat_len))
109
    {
110
        continue;
111
    }
112
    $line = preg_replace('/\s+/', ' ', $line);
113
    @list($note, $oct, $len) = explode(' ', $line);
114
 
115
    $note = $note_names[$note];
116
 
117
    if (-2 === $note)
118
    {
119
        exit;
120
    }
121
 
122
    $len = $note_lengths[$len] * $beat_len;
123
 
124
    file_put_contents($_SERVER['argv'][2], pack('VV', round($notes[$oct][$note] ? 91225 / $notes[$oct][$note] : 0), round($len * 1000000)), FILE_APPEND);
125
}
126
?>