-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
Hi,
I would like to play with tensorflow.JS with audio file in pure nodejs / Typescript code.
I convert your code to Typescript, and then test the sample. that having some typo error.
spectro.on should be replace by sp.on
in my case I have this code sample:
import Spectro from './spectro/spectro'; // local Typescript port
import fs from 'fs';
import path from 'path';
var sp = new Spectro();
const src = path.join(__dirname, '..', 'sample', '0', '0.wav');
var audioFile = fs.createReadStream(src, { start: 44 }) // Note: The first 44 bytes are the wav-header
// The file stream can simply be piped into the Spectro instance
audioFile.pipe(sp)
// Check when the file stream completed
var fileRead = false
audioFile.on('end', () => {
console.log('audioFile DONE');
fileRead = true;
}
)
// The data event can be used to work with recently processed data from the workers
sp.on('data', (err, frame: { index: number, data: number[] }) => {
if (err) console.error('Spectro data event has an error', err)
console.log('data index:', frame.index);
// frame contains an index of the processed frame and a data section with the processed data
})
sp.on('end', (err, data: number[][]) => {
if (err) return console.error('Spectro ended with an error', err)
// The 'end' event always fires when spectro has reached the end of the currently processable data
// Therefore we should check if the file was read completely before using the data
if (fileRead !== true)
return;
// Stop spectro from waiting for data and stop all of it's workers
sp.stop()
console.log('event end dataLen:', data.length, 'last datablock Size:' , data[data.length-1].length);
// The spectrogram can e.g. be drawn with third party modules such as pngjs
// Examples therefor can be found in examples/...
})by running this sample I get the folowwing output:
audioFile DONE
data index: 1
data index: 2
data index: 5
data index: 3
data index: 0
data index: 7
data index: 10
data index: 9
data index: 6
data index: 8
data index: 4
event end dataLen: 11 last datablock Size: 1024
data index: 11
event end dataLen: 12 last datablock Size: 1024
data index: 12
event end dataLen: 13 last datablock Size: 1024
data index: 13
event end dataLen: 14 last datablock Size: 1024
data index: 17
data index: 15
data index: 16
data index: 14
event end dataLen: 18 last datablock Size: 1024
data index: 18
event end dataLen: 19 last datablock Size: 1024
data index: 20
data index: 19
event end dataLen: 21 last datablock Size: 1024
data index: 21
event end dataLen: 22 last datablock Size: 1024
data index: 23
data index: 24
data index: 22
event end dataLen: 25 last datablock Size: 1024
data index: 25
event end dataLen: 26 last datablock Size: 1024
data index: 26
event end dataLen: 27 last datablock Size: 1024
data index: 27
event end dataLen: 28 last datablock Size: 1024
I should get this output:
audioFile DONE
data index: 1
data index: 2
data index: 5
data index: 3
data index: 0
data index: 7
data index: 10
data index: 9
data index: 6
data index: 8
data index: 4
data index: 11
data index: 12
data index: 13
data index: 17
data index: 15
data index: 16
data index: 14
data index: 18
data index: 20
data index: 19
data index: 21
data index: 23
data index: 24
data index: 22
data index: 25
data index: 26
data index: 27
event end dataLen: 28 last datablock Size: 1024
the end of file is reached before the end of the FFT.
your sample should use a different aproche to detect the end of process.
Moreover, would you accept a Pull request to convert your project to typescript ?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels