Passing an SB pointer to a JS function

Just starting out? Need help? Post your questions and find answers here.
erion
Posts: 9
Joined: Fri Mar 03, 2017 8:16 pm

Passing an SB pointer to a JS function

Post by erion »

Hello,

I would like to load an encrypted sound, then later an entire databank into a WebAudio buffer, via the 'context.decodeAudioData' function.

At the moment, I'm using SB's ReadFile() and ReadData() functions to load a file, then decrypt the loaded buffer with the AES cypher. This works.

My problem is that the decodeAudioData() function expects an ArrayBuffer, which should be an Uint8Array type, but this is not true for the SB pointer. Is there a way to convert it to the appropriate Uint8Array format? Here's a simplified version of what I'm using, without decryption.

Code: Select all

Procedure createSoundWithBuffer(buffer)
EnableJS
var sound;
v_sonoobject.context.decodeAudioData(v_buffer, function(buff) {
	sound = v_sonoobject.createSound(buff);
	sound.play();
	});  
DisableJS
EndProcedure

Procedure fread(Status, Filename$, File, SizeRead)
    Select Status
      Case #PB_Status_Loaded
        *sbuf=AllocateMemory(SizeRead,#PB_Memory_NoClear)
        ReadData(file,*sbuf,0,SizeRead)
!f_createsoundwithbuffer(p_sbuf);
      Case #PB_Status_Progress
        ; File loading in progress, use FileProgress() get the current progress

      Case #PB_Status_Error
        ; File loading has failed
    EndSelect
  EndProcedure
  
ReadFile(0,"file.mp3", @fread())
erion
Posts: 9
Joined: Fri Mar 03, 2017 8:16 pm

Re: Passing an SB pointer to a JS function

Post by erion »

As a followup to this, SB pointers seem to be that of the ArrayBuffer type, however, even though a WebAudio context expects an ArrayBuffer, it is unable to decode when I am passing an SB pointer directly. I am not sure why. Could it be because SB adds a few elements to it, like DataView, viewu8, etc?

At the moment, I'm trying to accomplish this without the use of SB function calls, i.e. loading the file into an ArrayBuffer via XMLHttpRequest(). Not using javascript for anything serious so far, finding an appropriate encryption library is going to be quite the challenge, I think. Any help, still, is graciously appreciated.

Erion
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: Passing an SB pointer to a JS function

Post by Fred »

AllocateMemory() returns an array buffer, so if your function expect an Uint8Array, you can cast it like that:

Code: Select all

u8view = new Uint8Array(buffer);
You can also use directly the field ".viewu8", but it's internal and can be subject to change.
erion
Posts: 9
Joined: Fri Mar 03, 2017 8:16 pm

Re: Passing an SB pointer to a JS function

Post by erion »

Hi Fred,
Thank you very much. The irony of this is that I've tried this already, but now I spent a few hours trying to figure out why it still did not work.
Turns out that no matter what file I tried to load, it always read 6056440 bytes. I suspect that this was a caching issue somewhere.
Now I can pass p_pointer.viewu8.buffer to my decode function and it works like a charm. Off to code my encrypted Databank format.
Huge thanks, Fred, for pointing me in the right direction!

Erion
Post Reply