When do i play sound?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
nesemu101
Posts: 9
Joined: Sun Jan 10, 2021 1:04 am

When do i play sound?

Post by nesemu101 »

Utterly confused by this. If i output a sample every 40 apu cycles, and i have a buffer of 4096 then do i wait 40*4096 cycles to play sound? in other words do i play sound after i fill the buffer or what...? i feel like the apu and ppu won' t be in sync as a result of this
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: When do i play sound?

Post by Quietust »

nesemu101 wrote: Fri May 28, 2021 11:25 pm Utterly confused by this. If i output a sample every 40 apu cycles, and i have a buffer of 4096 then do i wait 40*4096 cycles to play sound? in other words do i play sound after i fill the buffer or what...? i feel like the apu and ppu won' t be in sync as a result of this
Ideally, your buffer should be reasonably short so that the delay between video output and audio playback is minimized, but not so short that sound playback runs out of data before you're ready to give it more. A good rule of thumb would be to make the buffer length a multiple of 1 frame in length - for example, if you're doing 44100Hz (which you probably are, since that would give ~40.5844 CPU cycles per sample for NTSC), your buffer length should be a multiple of 735 samples for NTSC or 882 samples for PAL. Depending on what audio library you're using for playback (e.g. SDL, OpenAL, DirectSound), you might need to make it at least 2 or 3 frames long.

The problem arises, though, that the PPU does not output video at exactly 60Hz or 50Hz - NTSC is actually ~60.0988Hz, and PAL is ~50.00698Hz, so if you synchronize your emulator's speed to your monitor's refresh rate (which will likely only "work" with NTSC) then you'll need to adjust your audio emulation to output samples at a different rate (e.g. NTSC at 1786830 Hz instead of 1789772.7 Hz, which would be 40.5177 cycles per sample). On the other hand, if you synchronize your emulator's speed against audio playback then you might run into occasional video stutter.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
nesemu101
Posts: 9
Joined: Sun Jan 10, 2021 1:04 am

Re: When do i play sound?

Post by nesemu101 »

Quietust wrote: Sat May 29, 2021 7:00 am
nesemu101 wrote: Fri May 28, 2021 11:25 pm Utterly confused by this. If i output a sample every 40 apu cycles, and i have a buffer of 4096 then do i wait 40*4096 cycles to play sound? in other words do i play sound after i fill the buffer or what...? i feel like the apu and ppu won' t be in sync as a result of this
Ideally, your buffer should be reasonably short so that the delay between video output and audio playback is minimized, but not so short that sound playback runs out of data before you're ready to give it more. A good rule of thumb would be to make the buffer length a multiple of 1 frame in length - for example, if you're doing 44100Hz (which you probably are, since that would give ~40.5844 CPU cycles per sample for NTSC), your buffer length should be a multiple of 735 samples for NTSC or 882 samples for PAL. Depending on what audio library you're using for playback (e.g. SDL, OpenAL, DirectSound), you might need to make it at least 2 or 3 frames long.

The problem arises, though, that the PPU does not output video at exactly 60Hz or 50Hz - NTSC is actually ~60.0988Hz, and PAL is ~50.00698Hz, so if you synchronize your emulator's speed to your monitor's refresh rate (which will likely only "work" with NTSC) then you'll need to adjust your audio emulation to output samples at a different rate (e.g. NTSC at 1786830 Hz instead of 1789772.7 Hz, which would be 40.5177 cycles per sample). On the other hand, if you synchronize your emulator's speed against audio playback then you might run into occasional video stutter.
I'm sorry but I don't quite understand the second paragraph. Could you please explain it again? I understand that since the NTSC doesn't run exactly at 60 hz but why is it exactly going to be a problem? What if my ppu runs at say 500 fps, can't i cap it so that it gives my 60.0988HZ? also why does syncing to refresh monitor only work for NTSC and not for PAL? is it because the refresh rates of most monitors in 60hz? Also I'm planning to run my emu from my apu unit, will this cause stutters?

I'm sorry for asking so many [dumb]questions but i'm trying to wrap my head around this because i still don't where to start with the apu unit really since i have absolutely 0 experience in sound or sound api. I hope you don't mind me asking more stupid questions and taking up more of your precious time.

Thank you for your response
Post Reply