passing an HTML5 AudioBuffer to nacl

47 views
Skip to first unread message

Chris Weaver

unread,
May 12, 2016, 6:29:59 PM5/12/16
to Native-Client-Discuss
i have the following javascript code which takes an audio File and decodes it. it then passes it to a NACL module in a dictionary

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var fileReader = new (window.FileReader || window.webkitFileReader)();
fileReader
.addEventListener('load', function(event) {
  audioCtx
.decodeAudioData(event.target.result).then(function(decodedData) {
   
SigxModule.postMessage({data: decodedData});
 
});
});
fileReader
.readAsArrayBuffer(file);

i then have the following NACL handleMessage function

virtual void HandleMessage(const pp::Var& var_message) {
 
if (var_message.is_dictionary()) {
    pp
::VarDictionary dictionary(var_message);
    pp
::Var var_data = dictionary.Get('data');
 
}
}

what i can't figure out is how to get 'data' into either a pp::Resource object or a pp::AudioBuffer object. var_data.is_resource() fails. any ideas?

Dennis Kane

unread,
May 13, 2016, 10:39:42 AM5/13/16
to Native-Client-Discuss
This is a basic kind of programming question having everything to do with the fundamentals of the C++ object model, and nothing to do with the Native Client system itself. You just need to keep grinding away with the samples under ~/nacl_sdk/pepper_<VERSION>/examples/, and link all of that logic together with the data structures under ~/nacl_sdk/pepper_<VERSION>/include/. There is no magic bullet that will help you to become an effective C++ programmer. You just need to keep hacking away until everything starts to make sense! 

Ben Smith

unread,
May 13, 2016, 12:58:02 PM5/13/16
to Native-Client-Discuss
Hi Chris,

I don't believe you can pass a JavaScript AudioBuffer to NaCl. You can send an ArrayBuffer, though, so I think you can use AudioBuffer.getChannelData() to get a Float32Array, then use Float32Array.buffer to get the underlying ArrayBuffer. IIRC, you can't send typed arrays directly, so you'll also need to send the byteOffset and the byteLength so you know the range of data to process. Or I suppose you could create a new ArrayBuffer with just the data from the Float32Array.

-Ben


On Thursday, May 12, 2016 at 3:29:59 PM UTC-7, Chris Weaver wrote:

Chris Weaver

unread,
May 13, 2016, 2:33:45 PM5/13/16
to Native-Client-Discuss
thank you, i'll give that a try.
Reply all
Reply to author
Forward
0 new messages