forked from DDRBoxman/RepRapRingtone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
179 lines (139 loc) · 3.37 KB
/
main.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function convert() {
console.log("Starting Conversion");
rttltextarea = document.getElementById('rttl');
data = rttltextarea.value;
data = data.replace(' ','');
dataarray = data.split(':');
var output = "";
//gcode comment for ringtone name
output += ";" + dataarray[0] + "\n";
//Get rttl settings
settings = new Settings();
settings.parseSettingsString(dataarray[1]);
songData = parseSong(dataarray[2], settings);
output += songData;
gcodetextarea = document.getElementById('gcode');
gcodetextarea.value = output;
}
function parseSong(songString, settings) {
//Note that octave 4: A=440Hz, 5: A=880Hz, 6: A=1.76 kHz, 7: A=3.52 kHz
//The lowest note on the Nokia 61xx is A4, the highest is B7
var pattern = /(\d*?)?([A-Ga-gPp]#?)(\d|\.)?/;
var beatDuration = 60 / settings.getTempo() * 1000;
var gcodeSong = "";
songData = songString.split(",");
for (var i=0; i < songData.length; i++) {
var frequency;
var duration = settings.getDuration();
var octave = settings.getOctave();
var songNote = songData[i].replace(".", "");
var match = pattern.exec(songNote);
var dot = 0;
var oct = songData[i].slice(-1);
//octave manager
if(isNaN(oct) == false){
octave = oct*1;
}else{
octave = settings.getOctave();
}
if(songData[i].indexOf('.') !== -1) {
dot = 1;
}
if (typeof match[1] != "undefined") {
duration = match[1];
}
duration = getDuration(beatDuration, duration);
if(dot == 1) {
duration += duration / 2;
}
if (typeof match[3] != "undefined") {
octave = parseInt(match[3]);
}
frequency = getFrequency(match[2], octave);
gcodeSong += "M300 S" + Math.floor(frequency) + " P" + Math.floor(duration) + "\n";
}
return gcodeSong;
}
function getLineFrequency(line) {
var pattern = /S(\d*)/;
var match = pattern.exec(line);
if (match != null)
return match[1];
return -1;
}
function getLineDuration(line) {
var pattern = /P(\d*)/;
var match = pattern.exec(line);
if (match != null)
return match[1];
return 0;
}
var notes = ['a', 'a#', 'b', 'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#'];
function getFrequency(note, octave) {
if (note == 'p' || note == 'P')
return 0;
var halfsteps = 0;
halfsteps += (octave - 4) * 12;
for (var i=0; i < notes.length; i++) {
if (note.toLowerCase() == notes[i]) {
halfsteps += i;
}
}
return 440 * Math.pow(Math.pow(2, (1/12)), halfsteps);
}
function getDuration(beatDuration, duration) {
if (duration == ".") {
return beatDuration * 1.5;
}
switch (parseInt(duration)) {
case 1:
return beatDuration * 4;
break;
case 2:
return beatDuration * 2;
break;
case 4:
return beatDuration;
break;
case 8:
return beatDuration / 2;
break;
case 16:
return beatDuration / 4;
break;
case 32:
return beatDuration / 8;
break;
}
}
function Settings() {
var duration;
var octave;
var tempo;
}
Settings.prototype.getOctave = function() {
return octave;
}
Settings.prototype.getDuration = function() {
return duration;
}
Settings.prototype.getTempo = function() {
return tempo;
}
Settings.prototype.parseSettingsString = function(settingsString) {
split = settingsString.split(',');
for (var i = 0; i < split.length; i++) {
pair = split[i].split('=');
switch(pair[0]) {
case 'd':
duration = pair[1];
break;
case 'o':
octave = pair[1];
break;
case 'b':
tempo = pair[1];
break;
}
}
}