forked from rochars/wavefile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavefile.js
More file actions
executable file
·187 lines (176 loc) · 5.9 KB
/
wavefile.js
File metadata and controls
executable file
·187 lines (176 loc) · 5.9 KB
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
180
181
182
183
184
185
186
187
#!/usr/bin/env node
/*
* Copyright (c) 2017-2019 Rafael da Silva Rocha.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
/**
* @fileoverview wavefile CLI.
* @see https://github.com/rochars/wavefile
*/
const WaveFile = require('../dist/wavefile.js').WaveFile;
const fs = require('fs');
/** @type {string} */
const presentation = " WaveFile 8.1.0\n" +
" Copyright (c) 2017-2018 Rafael da Silva Rocha.\n";
/** @type {string} */
const help = " Usage:\n" +
"\n" +
" wavefile path/to/input.wav [...options] path/to/output.wav\n" +
"\n" +
" Available options:\n" +
"\n" +
" --resample Ex: wavefile input.wav --resample=16000 output.wav\n" +
" Change the sample rate. The input file is not affected\n" +
" Use with --method to change the interpolation method:\n" +
" Ex: wavefile in.wav --resample=8000 --method=sinc out.wav\n" +
" If --method is ommited, cubic interpolation will be used.\n" +
"\n" +
" --bitdepth Ex: wavefile input.wav --bitdepth=32f output.wav\n" +
" Change the bit depth.\n" +
" The input file is not affected.\n" +
" Possible values: 8, 16, 24, 32, 32f, 64\n" +
"\n" +
" --compress Ex: wavefile input.wav --compress=adpcm output.wav\n" +
" Apply compression to the file.\n" +
" The input file is not affected.\n" +
" Possible values: adpcm, alaw, mulaw\n" +
"\n" +
" --tag Ex: wavefile input.wav --tag=ICRD\n" +
" Print the value of tag if the tag exists.\n" +
"\n" +
" --list-tags Ex: wavefile input.wav --list-tags\n" +
" Print all tags of the file.\n" +
"\n" +
" --list-cue Ex: wavefile input.wav --list-cue\n" +
" Print all the cue points of the file.\n" +
"\n" +
" --bits Ex: wavefile input.wav --bits\n" +
" Print the bit depth of the file.\n" +
"\n" +
" --rate Ex: wavefile input.wav --rate\n" +
" Print the sample rate of the file.\n" +
"\n" +
" --help Ex: --help\n" +
" Show this help page.\n" +
"\n";
// Help
if (process.argv[2] == '-h' ||
process.argv[2] == '--help' || process.argv.length < 4) {
console.log(presentation);
console.log(help);
process.exit();
}
/**
* Anything that is not a command will be considered a file reference;
* the first item in the list will be considered the input, all
* others will be considered output.
* @type {!Array<string>}
*/
let files = [];
/**
* Anything that is not a file will be considered a command;
* All commands should be executed against the input file.
* @type {Object}
*/
let commands = {};
// parse args
for (let arg=2; arg<process.argv.length; arg++) {
if (process.argv[arg].slice(0, 1) == '-') {
commands[process.argv[arg].split("=")[0]] = process.argv[arg].split("=")[1];
} else {
files.push(process.argv[arg]);
}
}
// Load the input file
/** @type {!Object} */
let wav = new WaveFile(fs.readFileSync(files[0]));
files.splice(0, 1);
// Run the commands
/** @type {boolean} */
let shouldWrite = false;
for (let command in commands) {
// --bitdepth
if (command == '--bitdepth') {
wav.toBitDepth(commands[command]);
shouldWrite = true;
// --resample
} else if (command == '--resample') {
/** @type {!Object} */
let options = {
'method' : commands['--method'] ? commands['--method'] : 'cubic'
};
wav.toSampleRate(parseInt(commands[command], 10), options);
shouldWrite = true;
// --compress
} else if (command == '--compress') {
if (commands[command] == 'adpcm') {
wav.toIMAADPCM();
shouldWrite = true;
} else if (commands[command] == 'alaw') {
wav.toALaw();
shouldWrite = true;
} else if (commands[command] == 'mulaw') {
wav.toMuLaw();
shouldWrite = true;
}
// --tag
} else if (command == '--tag') {
console.log(wav.getTag(commands[command]));
// --list-tag
} else if (command == '--list-tags') {
/** @type {!Object} */
tags = wav.listTags();
for (var tag in tags) {
if (tags.hasOwnProperty(tag)) {
console.log(tag + ': ' + tags[tag]);
}
}
// --list-cue
} else if (command == '--list-cue') {
console.log(wav.listCuePoints());
// --bits
} else if (command == '--bits') {
if (wav.fmt.validBitsPerSample) {
console.log(wav.fmt.validBitsPerSample);
} else {
console.log(wav.fmt.bitPerSample);
}
// --rate
} else if (command == '--rate') {
console.log(wav.fmt.sampleRate);
// error
} else {
if (!(command === '--method' && commands['--resample'])) {
console.log('ERROR: Bad option. Run wavefile -h to check the available'+
' options.');
process.exit();
}
}
}
// Write the output file
if (shouldWrite) {
if (files.length) {
for (let file in files) {
fs.writeFileSync(files[file], wav.toBuffer());
}
}
}