Sound is at least two seconds behind...

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
_jamesbrown_
Posts: 2
Joined: Sun May 07, 2017 7:26 pm

Sound is at least two seconds behind...

Post by _jamesbrown_ »

I created a very rudimentary implementation of the APU in my emulator. I currently sample at 48000Hz and every 41st cycle of the cpu, I go ahead and spew the current tone out for the two pulse waves (basically nearest neighbor algorithm). I don't do anything else fancy like envelopes or handling duty cycles, everything is at a constant volume. As a result, the music/sound never turns off (I'm only testing with Donkey Kong) which is expected.

What I noticed is that the PPU doesn't match up with the APU. I'm testing with Donkey Kong and if Mario gets hit by a barrel, the kill noise is delayed and doesn't output for a few seconds.

My implementation of the PPU is to currently draw out the whole frame at the end (89,342 PPU cycles). I don't think I'm bound by my computer as it's able to keep up fine. Also there's zero input lag between keyboard input (controlling mario) and picture output.

Based on my implementation, does it make sense that the sound doesn't match up with the picture? There is still a lot of work to do but for now I'm more curious as to what could cause the delay. Thanks.
User avatar
Memblers
Site Admin
Posts: 4044
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Re: Sound is at least two seconds behind...

Post by Memblers »

That's what would happen if the audio buffer is too long, can you make it shorter? What system/library are you using for audio? I've only used SDL myself, but that info might allow someone else to help.

edit: PPU and audio synchronization seems to be a recurring problem with making an NES emulator, so there will surely be more to it than just using a shorter buffer. If you can find other threads about that, it might help.
_jamesbrown_
Posts: 2
Joined: Sun May 07, 2017 7:26 pm

Re: Sound is at least two seconds behind...

Post by _jamesbrown_ »

Memblers wrote:That's what would happen if the audio buffer is too long, can you make it shorter? What system/library are you using for audio? I've only used SDL myself, but that info might allow someone else to help.

edit: PPU and audio synchronization seems to be a recurring problem with making an NES emulator, so there will surely be more to it than just using a shorter buffer. If you can find other threads about that, it might help.
I'm also using SDL... I can believe that my audio buffer is too long. Is this the "samples" member in SDL_AudioSpec as an argument to SDL_OpenAudioDevice? (https://wiki.libsdl.org/SDL_OpenAudioDevice) I'm using 1600 here, which I calculated from:

4800hz * sizeof(short) / 30 / 2 (30 being arbitrary, in this case 30fps -- NTSC).

I stole that boilerplate calculation from some stackoverflow post so I may be off track here.
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Sound is at least two seconds behind...

Post by Quietust »

_jamesbrown_ wrote:(30 being arbitrary, in this case 30fps -- NTSC)
As far as the NES is concerned, NTSC is 60fps, not 30.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Post Reply