javascript - Web Audio- streaming file from server to client -


i'm trying stream audio server containing audio file client using binaryjs. code inspired code in question: playing pcm stream web audio api on node.js

here's server code looks like:

// create binaryserver using binaryjs var binaryserver = require('binaryjs').binaryserver; // gotta able access filesystem var fs = require('fs');  // create our server listening on specific port var server = binaryserver({port: 8080});  // when client makes request server.on('connection', function(client){     // audio file     var file = fs.createreadstream(__dirname + '/jdillalife.mp3');      // convert int16     var len = file.length;     var buf = new int16array(len);      while(len--){         buf[len] = data[len]*0xffff;     }      var stream = client.send();     stream.write(buf.buffer);      console.log("server contacted , file sent"); }); console.log('server running on port 8080'); 

and client code:

var speaker = require('speaker'); var speaker = new speaker({     channels: 2,     bitdepth: 32,     samplerate: 44100,     signed: true });  var binaryclient = require('binaryjs').binaryclient; var client = binaryclient('http://localhost:8080'); client.on('stream', function(stream, meta){     stream.on('data', function(data){         speaker.write(data);     }); }); 

this rough draft , i'm won't play nicely right away, right it's throwing error when run seems take issue line var buf = new int16array(len);, , i'm not sure why happening. says there's "type error," i'm not sure why happen when assigning new object empty variable. i'm new js (and whole untyped languages thing in general) issue i'm assigning?

i think issue here you're accessing file.length, while file stream object don't think have length property. you're doing saying

 new int16array(undefined); 

and hence type error.

fs.createreadstream documented here; https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

you can use stream object read data in chunks using stream.read(256), documented here; https://nodejs.org/api/stream.html#stream_readable_read_size

hopefully gives enough pointers proceed!


Comments

Popular posts from this blog

html - How can i make an element from a bottom stacking context stays in front of another higher stacking context? -

python - TypeError: start must be a integer -

html - href attribute breaking an element -