-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaytone.js
143 lines (125 loc) · 2.58 KB
/
playtone.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
// playtone.js
/* Tone Generation Utility (using PC speaker, not sound card) */
const REVISION = "$Revision: 1.4 $".split(' ')[1];
const NO_VISUAL =(1<<3)
var mode=0; /* Optional modes */
var t=1; /* Timing */
var s=0; /* Stacato */
var octave=4; /* Default octave */
var stop=false;
var pitch=523.50/32.0; /* low 'C' */
function play(freq, dur)
{
var notes=['c',' ','d',' ','e','f',' ','g',' ','a',' ','b'];
var sharp=['B','C',' ','D',' ','E','F',' ','G',' ','A',' '];
var i,n,o,d;
var len;
var f;
// writeln("play " + freq + ", " + dur);
if(dur==undefined)
dur="0";
d=parseFloat(dur);
if(isNaN(f=parseInt(freq)))
switch(freq.charAt(0).toUpperCase()) {
case 'O': /* default octave */
if(Number(dur.charAt(0)))
octave=d;
else
octave+=d;
return;
case 'P': /* pitch variation */
if(Number(dur.charAt(0)))
pitch=parseFloat(dur)/32.0;
else
pitch+=parseFloat(dur);
return;
case 'R': /* rest */
f=0;
break;
case 'S': /* stacato */
if(Number(dur.charAt(0)))
s=d;
else
s+=d;
return;
case 'T': /* time adjust */
t=d;
return;
case 'V':
if(mode&NO_VISUAL)
return;
truncsp(dur);
if(dur.charAt(dur.length-1)=='\\')
write(truncstr(dur,'\\'));
else
writeln(dur);
return;
case 'Q':
case 'X': /* exit */
stop=true;
break;;
default:
for(n=0;notes[n];n++)
if(freq.charAt(0)==notes[n] || freq.charAt(0)==sharp[n])
break;
if(parseInt(freq.charAt(1)))
o=(freq.charAt(1)&0xf);
else
o=octave;
f=pitch*Math.pow(2,o+n/12);
break;
}
if(t>10)
len=(d*t)-(d*s);
else
len=d*t;
if(isNaN(len))
return;
if(f)
beep(f,len);
else
sleep(len);
if(s) {
if(t>10)
sleep(d*s);
else
sleep(s);
}
}
printf("\r\nSynchronet Tone Generation Module %s\r\n\r\n", REVISION);
var filename;
var debug=false;
for(i=0;i<argc;i++) {
switch(argv[i]) {
case "-d":
debug=true;
break;
default:
filename=argv[i];
break;
}
}
if(filename==undefined) {
alert("No filename specified");
exit();
}
if(!file_exists(filename))
filename = system.exec_dir + filename;
file = new File(filename);
if(!file.open("r")) {
alert("Error " + file.error + " opening " + file.name);
exit();
}
text=file.readAll();
file.close();
stop=false;
for(i in text) {
if(js.terminated || stop)
break;
if(debug)
writeln(text[i]);
if(text[i].charAt(0)==':') /* comment */
continue;
token=truncsp(text[i]).split(/\s+/);
play(token.shift(), token.join(' '));
}