Re: generating sound with createJavaScriptNode in an emulator

Hi Antonio,

I tried running this on the Mac and it's also rendering quite badly (left
channel only, distorted, etc.):

I'm forwarding my previous reply to the general list and think these
problems need to be solved:

Hi Antonio,

I can see couple of problems:

1. I notice that you're changing the "onaudioprocess" function, but in the
first function you're setting "data".  The audio data buffer will change
each and every callback, so you need to set "data" by calling
getChannelData(0) again in each call to your buffer function.

2. I don't see where you're initializing the "sample" variable.  Also, it
looks like you're doing a bit operation with "sample ^= 0x40".  The sample
values should be floating point in a range from -1.0 -> +1.0, not an integer
value.

3. Once you have everything else fixed, you'll probably want to increase the
buffer size to be greater than 1024

4. The current JavaScriptAudioNode implementation is ignoring the 3rd
argument to createJavaScriptNode(), so it's expecting stereo output.  That
  explains why you only hear output on the left channel.

Cheers,
Chris
- Hide quoted text -


On Wed, May 18, 2011 at 8:47 AM, Antonio José Villena Godoy <_@
antoniovillena.es> wrote:

>
>  Hello Chris
>
>  I am new in this mailing list. My name is Antonio, I am a Spanish
> developer. I have created this emulator:
>
> http://jbacteria.antoniovillena.es/
>
>  I try to put sound in the emulator with this init code:
>
> if(typeof webkitAudioContext == 'function')
>  cts= new webkitAudioContext(),
>  paso= 69888*50/cts.sampleRate,
>  node= cts.createJavaScriptNode(1024, 0, 1),
>  node.onaudioprocess= function (e){
>    data= e.outputBuffer.getChannelData(0);
>    node.onaudioprocess= buffer;
>  },
>  node.connect(cts.destination);
>
>  and the buffer function:
>
> function buffer(e) {
>  play= flash*69888 + st - paso*1024;
>  j= 0;
>  while( j<1024 )
>    if( data[j++]= sample
>      , (play+= paso) > vb[playp] )
>      sample^= 0x40,
>      playp= playp+1 & 0xff;
> }
>
>  But the quality of the sound is very bad. I have tried to increase
> the buffer to 2048 and 4096 with worse result. I don't know if the
> problem is mine or a bad Chrome implementation.
>
>  As you can see, the 48K sound is very simple, as the PC beeper. I
> would like to implement 128K AY-8912 sound, later, if I can fix this issue.
>
>  The source code is here:
> http://jbacteria.antoniovillena.es/jBacteriaSourceCode.zip
>
>  And a game when you can hear the bad sound is for example:
> http://jbacteria.antoniovillena.es/48?fernando
>
>  Regards (and sorry for my bad English)
> --
>   ,       _            _       __   ___ _ _
>  /_\  _ _| |_ ___ _ _ (_)___   \ \ / (_) | |___ _ _  __ _
>  / _ \| ' \  _/ _ \ ' \| / _ \   \ V /| | | / -_) ' \/ _` |
> /_/ \_\_||_\__\___/_||_|_\___/    \_/ |_|_|_\___|_||_\__,_|
>




On Wed, May 18, 2011 at 8:43 AM, Antonio José Villena Godoy <_@
antoniovillena.es> wrote:

>  Hello Chris
>
>  I am new in this mailing list. My name is Antonio, I am a Spanish
> developer. I have created this emulator:
>
> http://jbacteria.antoniovillena.es/
>
>  I try to put sound in the emulator with this init code:
>
> if(typeof webkitAudioContext == 'function')
>  cts= new webkitAudioContext(),
>  paso= 69888*50/cts.sampleRate,
>  node= cts.createJavaScriptNode(1024, 0, 1),
>  node.onaudioprocess= function (e){
>    data= e.outputBuffer.getChannelData(0);
>    node.onaudioprocess= buffer;
>  },
>  node.connect(cts.destination);
>
>  and the buffer function:
>
> function buffer(e) {
>  play= flash*69888 + st - paso*1024;
>  j= 0;
>  while( j<1024 )
>    if( data[j++]= sample
>      , (play+= paso) > vb[playp] )
>      sample^= 0x40,
>      playp= playp+1 & 0xff;
> }
>
>  But the quality of the sound is very bad. I have tried to increase the
> buffer to 2048 and 4096 with worse result. I don't know if the problem is
> mine or a bad Chrome implementation.
>
>  As you can see, the 48K sound is very simple, as the PC beeper. I would
> like to implement 128K AY-8912 sound, later, if I can fix this issue.
>
>  The source code is here:
> http://jbacteria.antoniovillena.es/jBacteriaSourceCode.zip
>
>  And a game when you can hear the bad sound is for example:
> http://jbacteria.antoniovillena.es/48?fernando
>
>  Regards (and sorry for my bad English)
> --
>   ,       _            _       __   ___ _ _
>  /_\  _ _| |_ ___ _ _ (_)___   \ \ / (_) | |___ _ _  __ _
>  / _ \| ' \  _/ _ \ ' \| / _ \   \ V /| | | / -_) ' \/ _` |
> /_/ \_\_||_\__\___/_||_|_\___/    \_/ |_|_|_\___|_||_\__,_|
>
>
>

Received on Friday, 20 May 2011 17:28:08 UTC