Emulator audio...how?
Hi all.
I am in the process of writing a Spectrum emulator (c#) for my final year project at university. I've got it all working, apart from sound. It's easy enough to trap the ROM routines when (for instance) a BASIC program uses the BEEP command, but obviously this doesn't work in most games.
So I was wondering if anyone could give me an idea of how to approach this? I'm sure its much easier than it seems to me. My current system involves synchronising every frame, and from the loudspeaker OUT commands over that timeframe, trying to recreate the pitch and duration - I get sound to output, but inaccurately.
Anyway, thanks in advance for any suggestions.
Cheers
James
I am in the process of writing a Spectrum emulator (c#) for my final year project at university. I've got it all working, apart from sound. It's easy enough to trap the ROM routines when (for instance) a BASIC program uses the BEEP command, but obviously this doesn't work in most games.
So I was wondering if anyone could give me an idea of how to approach this? I'm sure its much easier than it seems to me. My current system involves synchronising every frame, and from the loudspeaker OUT commands over that timeframe, trying to recreate the pitch and duration - I get sound to output, but inaccurately.
Anyway, thanks in advance for any suggestions.
Cheers
James
Post edited by johara on
Comments
1) Take the average of the sound value (the last OUT to BEEP i.e) every 79 tstates (this is for a 44100 Hz audio, which samples 882 sound values over 50 frames. If you are using 22.1 KHz, adjust the value accordingly). Push this sound value to the sound buffer.
2) Once 882 samples are taken, play the sound buffer. You will need a few sound buffers chained together (circularly) to keep things flowing smoothly.
The synchronization is achieved by waiting for the current sound buffer to finish playing 882 samples before proceeding with the emulation.
That's the theory anyway. Hope that makes sense!
Bytes:Chuntey - Spectrum tech blog.
The sound is produced every time the Spectrum sets bit 4 of 0xFE port. so you need to use that to fill your sound buffer.
When no sound is playing, you will know that this is the case and so you can send a prefilled buffer of 0's (or 1's). I wouldn't suggest monitoring the state every few t-states as this could be heavy work and may not provide an accurate reproduction.
I'm still getting some clicks in the audio, but I'm hopeful some tweaking of the buffers can fix that.
Cheers
James
Good luck with your project! Remember to make your emulator available on WoS when you're done! :)
Bytes:Chuntey - Spectrum tech blog.
http://www.worldofspectrum.org/forums/showthread.php?t=39647
http://zxmak2.codeplex.com/
"...Project written in C#. Currently it works on Windows platform and using Managed DirectX. .."