From d0662682e19c281fad073850e061bb403f82df7f Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sat, 4 Feb 2017 15:08:16 +0100 Subject: [PATCH] examples/source_reader.js: added a simple source reader --- examples/source_reader.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/source_reader.js diff --git a/examples/source_reader.js b/examples/source_reader.js new file mode 100644 index 0000000..b5f8213 --- /dev/null +++ b/examples/source_reader.js @@ -0,0 +1,46 @@ +var aubio = require('../'); +var ref = require('ref'); + +if (process.argv[2]) { + var inputfile = process.argv[2]; +} else { + console.error('a command line is required.'); + console.log('usage examples:'); + console.log(' ' + process.argv[0] + ' ' + process.argv[1] + ' '); + return; +} + +var samplerate = 0; // this tells aubio_source to use test.wav samplerate +var hop_s = 512; // number of samples to be read at each aubio_source_do call + +var source = aubio.new_aubio_source(inputfile, samplerate, hop_s); +try { + source.readPointer(); +} catch (e) { + console.error('failed opening ' + inputfile + ' for reading'); + return; +} +// get the actual samplerate of the input file +var samplerate = aubio.aubio_source_get_samplerate(source); + +var readPtr = ref.alloc('int'); // a pointer to and int +var total_frames = 0; // frame counter + +var samples = aubio.new_fvec(hop_s); +while (true) { + // read at most hop_s frames from inputfile + aubio.aubio_source_do(source, samples, readPtr); + // increment the number of frames read + total_frames += readPtr.deref(); + // stop at end of file + if (readPtr.deref() != hop_s) { break; } +} +// compute the duration in seconds +var cur_time = total_frames / samplerate; +// print duration and number of frames read +console.log('read %d seconds (%d frames) from %s', cur_time.toFixed(3), + total_frames, inputfile); + +// clean up memory (each new_ should have a corresponding del_) +aubio.del_aubio_source(source); +aubio.del_fvec(samples); -- 2.11.0