-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·148 lines (128 loc) · 3.14 KB
/
index.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
#!/usr/bin/env node
var fs = require('fs')
, program = require('commander')
, baudio = require('baudio')
, musicbits = require('musicbits')
, Instrument = musicbits.Instrument
, Piano = musicbits.Piano
, Flute = musicbits.Flute
var file = ''
program
.version('0.2.0')
.option('-i --instrument <instrument>', 'instrument to use (piano or flute)', /^(piano|flute)$/i, 'piano')
.option('-d --duration <duration>', 'duration of a full length note (in seconds)', parseFloat, 0.3)
.option('-o --outfile <outfile>', 'save output to outfile, don\'t play it directly')
.arguments('<file>')
.action(function(f) {
file = f
})
.parse(process.argv)
if (!file) {
program.help()
}
if (isNaN(program.duration)) {
console.error('Duration must be a valid decimal number.')
program.help()
}
try {
var text = fs.readFileSync(file).toString()
}
catch (e) {
if (e.code === 'ENOENT') {
console.error("Error: File '" + file + "' not found.")
} else {
console.error('error:', e.message)
}
process.exit(1)
}
var melody = parseText(text)
switch (program.instrument) {
case 'flute':
var instrument = new Flute(program.duration)
break
case 'piano':
var instrument = new Piano(program.duration)
break
default:
var instrument = new Piano(program.duration)
break
}
instrument.play(melody)
var endIndex = 0
var startIndex = 0
instrument.on('end', function() {
endIndex = 1
})
var b = baudio(function(t) {
if (startIndex < 4400 ) {
startIndex++
return 0
}
if (endIndex && endIndex < 16000) {
endIndex++
if (endIndex == 16000) {
this.end()
}
return 0
}
return instrument.value(t)
})
var sox = null
if (program.outfile) {
sox = b.record(program.outfile)
}
else {
sox = b.play()
}
sox.stderr.on('data', function (data) {
console.error(data.toString())
})
sox.on('exit', function (code) {
if (code !== 0) {
console.log('Error: sox did not exit normally!')
process.exit(code)
}
})
b.on('error', function (error) {
console.error(error.message)
})
function parseText(text) {
var inputLines = text.split('\n')
var melody = []
for (var i = 0; i < inputLines.length; ++i) {
var line = inputLines[i]
if (!line.trim()) {
continue
}
var input = line.split(/\s+/)
if (input.length === 1) {
melody.push(parseVal(input[0]))
}
else if (input.length === 2) {
melody.push([parseVal(input[0]), parseDuration(input[1])])
}
else {
throw new Error('Line ' + i + ' has more than two values.')
}
}
return melody
}
function parseVal(input) {
if (input == 's') {
return 0
}
else if (isNaN(+input)) {
return new Error('Input', input, 'is not a number.')
}
else {
return +input
}
}
function parseDuration(duration) {
if (isNaN(+duration)) {
return new Error('Duration', duration, 'is not a number.')
}
else {
return +duration
}
}