Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sound syncing in emulator  (Read 1610 times)
0 Members and 1 Guest are viewing this topic.
Offline coramoor

Senior Newbie

Java games rock!

« Posted 2003-10-25 11:19:32 »

I'm working on an NES emulator, and having problems with syncing the sound.. I'm using the Perf-timer to time the framerate, but it seems I'm still producing the audio too fast.
The sourcedataline buffer will very quickly fill up, and introduce considerable lag from when an event takes place in the game until it's eventually reflected in the music/sound fx.

The amount of samples produced each frame is constant, so it seems I'm producing a little bit too much. The problem now is writing the produced samples to the line without the buffer emptying, and also without writing too much.

What would be the best way to sync this? I've tried adjusting the sleep time between frames, and also sleep some extra when too many frames are produced, but it seems that no matter how I do it, I either get lots of buffer underflows, or a horrible lag.. Cry

I guess the real problem is, I don't know how much data is in the buffer at any given moment. The method for getting the available bytes before blocking doesn't seem to be updated frequently enough, so I can't rely on it. Is there any solution to this? Any sound package I can use that is better at this?
Any help will be appreciated :-)
Offline erikd

JGO Ninja

Medals: 16
Projects: 4
Exp: 14 years


« Reply #1 - Posted 2003-10-29 08:31:01 »

Yes, that is a difficult one which I haven't totally solved myself.
What I did was:
- Have a seperate thread with the sound chip emulator in a tight loop that fills the sound buffer.
- The sound buffer is as small as possible (to avoid underruns and to minimize lag)
- The separate thread just keeps filling the buffer until it blocks (when the buffer is full).
- The main thread feeds the sound thread with commands for the sound chip (usually using CPU ports or memory mapped I/O).

This approach works, but has some problems (feeding many commands in quick succession is sometimes a problem, sound timing is sometimes unstable). Maybe I need to sync the threads but I'm not sure...
It looks like sometimes the thread blocks for longer periods than other times causing the instabilities where I expected that blocking to be of more or less fixed periods.

Offline coramoor

Senior Newbie

Java games rock!

« Reply #2 - Posted 2003-10-29 09:35:37 »

The sound chip emulation and the sampling is clocked by the CPU emulation,  so I can't put it in a separate thread. If I don't sample at the correct time, the produced sound won't be accurate, especially at high frequencies. I've tried getting a smaller sound buffer, but it stays at 65536, which is way too big (is this device-dependent?).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja

Medals: 16
Projects: 4
Exp: 14 years


« Reply #3 - Posted 2003-10-29 13:06:48 »

Hmm I dunno, I set up a line buffer of 4096 and that seems to work fine here.

You might want to check out NESCafe (, another NES emulator with sound. As far as I remember, sound was emulated ok.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (32 views)
2015-08-01 03:58:20

Jesse (22 views)
2015-07-29 04:35:27

Riven (42 views)
2015-07-27 16:38:00

Riven (23 views)
2015-07-27 15:35:20

Riven (26 views)
2015-07-27 12:26:13

Riven (17 views)
2015-07-27 12:23:39

BurntPizza (38 views)
2015-07-25 00:14:37

BurntPizza (47 views)
2015-07-24 22:06:39

BurntPizza (32 views)
2015-07-24 06:06:53

NoxInc (39 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!