Last active
February 25, 2020 11:00
-
-
Save MayamaTakeshi/2aeaa8cbd1455e04637aea9bc03c6fae to your computer and use it in GitHub Desktop.
Plot audio data from wav file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
$ sox -V -r 16000 -n -b 32 -c 1 -e float test.wav synth 30 sin 440 vol -10dB | |
sox: SoX v14.4.2 | |
Input File : '' (null) | |
Channels : 1 | |
Sample Rate : 16000 | |
Precision : 32-bit | |
Output File : 'test.wav' | |
Channels : 1 | |
Sample Rate : 16000 | |
Precision : 25-bit | |
Sample Encoding: 32-bit Floating Point PCM | |
Endian Type : little | |
Reverse Nibbles: no | |
Reverse Bits : no | |
Comment : 'Processed by SoX' | |
sox INFO sox: effects chain: input 16000Hz 1 channels | |
sox INFO sox: effects chain: synth 16000Hz 1 channels | |
sox INFO sox: effects chain: vol 16000Hz 1 channels | |
sox INFO sox: effects chain: output 16000Hz 1 channels | |
$ sox --i test.wav | |
Input File : 'test.wav' | |
Channels : 1 | |
Sample Rate : 16000 | |
Precision : 25-bit | |
Duration : 00:00:30.00 = 480000 samples ~ 2250 CDDA sectors | |
File Size : 1.92M | |
Bit Rate : 512k | |
Sample Encoding: 32-bit Floating Point PCM | |
*/ | |
const pl = require( 'nodeplotlib') | |
const fs = require('fs'); | |
const wav = require('wav'); | |
const rs = fs.createReadStream('./test.wav') | |
const reader = new wav.Reader() | |
const buf = [] | |
var samples = 512 | |
reader.on('format', function(format) { | |
console.log(format) | |
reader.on('data', (data) => { | |
buf.push(data) | |
}) | |
reader.on('end', () => { | |
console.log('end') | |
var data = Buffer.concat(buf) | |
var y = new Array() | |
for(var i=0 ; i<samples ; i++) { | |
y.push( data.readFloatLE(i*4) ) | |
} | |
var x = [...Array(y.length).keys()]; | |
//console.log(y) | |
//console.log(x) | |
var data = [{x: x.slice(0, samples), y: Array.from(y).slice(0, samples), type: 'line'}] | |
pl.plot(data) | |
}) | |
}) | |
rs.pipe(reader) | |
// CLI output: | |
/* | |
{ audioFormat: 3, | |
endianness: 'LE', | |
channels: 1, | |
sampleRate: 16000, | |
byteRate: 64000, | |
blockAlign: 4, | |
bitDepth: 32, | |
signed: true, | |
float: true } | |
end | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment