• No results found

A more advanced VDP: Genesis

Chapter 7. Sound Emulation

4. Sample based sound generation

Sample based sound emulation is basically the easiest to implement. It must just to take into account a few things as the format of the sample values, the number of channels that must be mixed and the envelope and filters applied to the channels. The sound hardware and sound API that we will use for emulating other systems will be a sample based system. Therefore it just a question of correctly

converting the sample data from the emulated system format to the target system format and then mixing the different channels correctly.

There are many systems which implement sample based sound hardware. The Genesis has a very low quality DAC channel as we already said. The Mega CD (or Sega CD) has 8 PCM channels with a good sound quality (38KHz). The samples are stored in a special area of the Mega CD memory which is

Figure 68. YM2413 Schematics (Envelope Generator, Operator and Phase Generator).

organized in banks. For each channel it is selected the bank that will be played. The Sega CD also has a CD audio source which is mixed with the other sound fonts. Other systems also have similar systems, the CPS2 uses QSound ADPCM system with the samples stored in separated sound ROMs and the sound chip being controlled by a Z80 (the main CPU is a 68K).

The case of the emulation of a CD sound track is rather easy if the emulator works with original CD-ROMs. The target system CD-ROM and audio card just must be told to play the same sound track than in the emulated machine. CD sound tracks are a standard and work in the same way in all the systems. The emulation is just the conversion from the emulated machine command to the target machine command to play CD tracks. If rather than the original CD-ROM we are using a CD-ROM image (a copy in the hard disk of the full CD-ROM) we should emulate it through the sample based services of the target sound hardware. It will be just a matter of reading the raw data from the image and copy it (and convert it may be) to the playback buffer.

There are different formats for storing the samples as we already said. The samples can be in raw format, can be compressed or not, can be signet or not, use 8-bit samples or 16-bit samples, be in different sample frequencies and so. It is important to know what are sample characteristics of the emulated sound hardware and the sample capacities offered by the target sound hardware. For example the PC standard sound hardware can play from 8KHz to 44KHz samples, either 8-bit or 16-bit and stereo (two channels) or mono sound. The sample bit size, the sample frequency and if it is a mono or a stereo sample

determines the size of the sound buffers needed. Stereo samples are usually stored coupled, two samples together one from each channel (right or left) which are played together.

There are different ways of compressing sound data (similarly what happens with graphic data), some are loosely forms of compression and other are not. For example MP3 compression format can decrease in an order of magnitude the size of the sound data but it is loosely and needs a lot of CPU power to decompress and compress the data. Other formats as ADPCM (adaptative PCM) are not loosely but reduce the amount of memory needed. ADPCM is based in store the changes between two samples rather than the samples themselves. This way the sample size can be reduced (or the quality increased). For example two 16-bit samples with a difference between them less than 127 can be stored as the first sample as a 16-bit value and the second as an 8-bit signed value which is the difference between the two original samples.

The way the samples are played is really easy. The sound sample data is stored in RAM or other form of memory (ROM could work). The sound chip is attached to the bus and can read the memory. When we want to play that sound the sound hardware is tell to play it from the given address (or it can be fixed if the sound hardware is attached to fixed memory addresses). The sound hardware begins to read data from this position and playing the sound. When the buffer is exhausted it raises a signal (an interrupt usually) to the CPU demanding for more data. The sound hardware can then stop playing data or automatically loop back to the start of the buffer. This later feature could either work for repetitive sounds or for reusing the same buffer. The program code could, with the proper synchronization, be filling the sound buffer from the start while the sound hardware is already playing ahead of it. When the sound hardware arrives to the end of the buffer the start will be already filled with proper data by the program code. The sound sample system is basically implemented in all the systems using a callback.

This is basically as it works such a system. Different sound APIs and sound hardware can have additional features as different output channels and hardware (or automatic) mixing, filter and volume controls, but with the basic features almost any sound system can be emulated. We could talk about different filters effects and other features that can be found in particular systems, but as it is easy to find information about sound generation (even easier than information about graphic programming) we will end this chapter here.

5. How to find information.

The information about how to produce sound and the way the sound hardware of the emulated machine works can be found more or less in the same places as the information about the graphics. Datasheets of the original hardware (if they are freely available), source code from demos and source code emulators of the machine. Some reverse engineering could work but only if you have a good knowledge about the kind of hardware you are emulating. It is hard to figure how something must sound without the original system for example.

Information about sound format, sound filter, sound effects and such topics is easily found searching the web for sound programming or similar words. The programming APIs use to have a lot of examples and explanations as well (DirectSound). And the basic sample system is really easy to use.

Figure 69. Sample based sound generation.

SOUND BUFFER

new sound data

DAC (Digital to Analog

Converter)

SPEAKERS Buffer empty

interrupt