JSIDPlay - Emulating the SID Chip in JavaThe implementation of JSIDPlay is heavily based on JaC64 since
SIDs are basically C64 code playing a SID-tune. The CPU, CIA Timers,
parts of the VIC emulation and the SID chip from JaC64 are used.
Emulating SID in Java
Emulation of the SID chip have been done on several platforms
(which of Amiga was the first, I believe). The challenge to get
it working in Java was both performance and the Sound API, especially
when I starded before J2SE1.3 when there was no JavaSound ;-). Since
JavaSound (javax.sound) it was a much easier task.
Outputing the Sound in Java
The current design
is based on using a DataLine with 16 bits resolution and just write
to it. To allocate such a data line use something like:
// Allocate Audio resources
mixer = AudioSystem.getMixer(null);
AudioFormat af = new AudioFormat(44100, 16, 1, true, false);
DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af, 16384);
try {
dataLine = (SourceDataLine) AudioSystem.getLine(dli);
[...]
These DataLines take bytes as their inputs, and I used ints to represent
the SID generated sounds, so writing to the DataLine is done like this:
(intBuffer contains the SID generated data, GENLEN is typically 44 samples).
int bIndex = 0;
for (int i = 0, n = SID6581.GENLEN; i < n; i++) {
buffer[bIndex++] = (byte) (intBuffer[i] & 0xff);
buffer[bIndex++] = (byte) ((intBuffer[i] >> 8));
}
dataLine.write(buffer, 0, SID6581.GENLEN * 2)
Just a conversion from ints to bytes and the a write to the DataLine.
The General Design
The SID Chip in the Commodore 64 has three voices and one mixer with
filters and volume control. JSIDPlay follows that design, a SIDMixer class
which mixes the sound from the three voices plus a PSID sample channel.
The filter (HP/LP/BP) with resonance filters the three SID voices and
pass the result on to an effect unit (which was not a part of the C64
but is an add-on just for "fun"). After filters and effects everything
is passed on to the DataLine as described above.
The three voices are in the class SID6581 and all generate 12 bit
waves multiplicated with 8 bit ADSR level (all taken down to 13 bits
before going into the mixer). Each voice can be configured for
generating triangle, saw, pulse or noise. One odd feature of the SID
chip is that when selecting more than one of the waveforms the result
will be a combinated wave. This have been used in several SID tunes,
for example Yie Kung Fu II. Other nice features are:
- Ring Modulation, listen to
Thrust which is only working on the triangle waveform and multiplicates two triangles with eachother in order to get a "bell" like or metallic sound.
- Synchronization, listen to Rolands Ratrace (a few seconds into the tune)
Implementation details on the SID Voices
... to be written ...
|