Understanding the PSG

Using Arkos Tracker efficiently requires a minimum knowledge of our beloved AY-3-8912/YM-2149F. We are going to have a quick overview of their capabilities, with sounds and drawings for a better understanding.

As a simplification, most of this documentation will refer to the AY, but all the explanations also concern the YM. The difference between the two will be explained below.

Overview

The channels

The AY/YM are an old Programmable Sound Generator (PSG) which can produce sound in a rather limited, yet expressive way.

This PSG has 3 channels. This means we can produce three sounds at the same time. The output may be mono or stereo depending on the hardware. The CPC has only one speaker, but also a stereo output for plugging an amplified system. The CPC Plus has two speakers, the Atari STF only one (arh arh).

Waves, amplitudes and periods

By default, the AY generates square waves. This is the most basic, saturated sound you can find.

Listening to three square waves is quite tiring, but fortunately, each wave has its own amplitude, so changing it in real-time allows creating more natural sounds.

There are 16 amplitude steps, from 0 (no sound) to 15 (full amplitude). The amplitude curve is logarithmic: this means that the amplitude difference between amplitude 10 and 11 is not as important as the difference between amplitude 14 and 15.

In order to play music, one must play notes. Each channel can be given its own frequency. The little example above, on top of decaying the amplitude, also changes their frequency over time.

Important: the PSG does not work with frequencies (440 Hz for example), but with periods. Periods are the invert of frequencies. The highest the frequency, the lowest the period. Mathematically speaking: period = 1 / frequency. However, we won’t be using maths here so don’t worry if you don’t understand this. Just be aware that periods will be used, and thus by decreasing the period, the pitch of a sound will actually increase. For now on, we will talk about periods, not frequencies.

Noise

What about producing some drums? Well the PSG got you covered: it has one noise generator. It can be coupled to any of the three channels, or two of them, or even the three of them. The period of the noise varies thanks to a value going from 1 to 31 (0 producing the same result as 1, 1 being a very high-pitched noise, 31 a low-pitched one). Here is an example of the noise going from 0 to 31.

Noise can be used alone as seen above, or mixed with a square wave:

I told you there was only one noise generator. If you apply the noise on two channels, both will use the same noise value. This is quite limiting, which is why, most of the time, you will not use two noises at the same time. It simply sounds ugly. Forget your dreams of simulating a hihat (noise to 1) and big explosion sound (noise to 31) at the same time.

The hardware envelope

All that is above is enough to make 99.9% of the game music from the 80’s. You may want to go one step beyond and use another neat feature called “the hardware envelope”, which is very useful to produce peculiar sounds, mostly used in basses, but which can have great effects in melodies or for special effects.

As you have seen, in order to have more expressive sounds, you would use the amplitude to create attack and decay to simulate real (or not) instruments. The hardware envelope allows you to do it automatically.

One envelope to rule them all

Note the use of the singular when I talk about hardware envelope. Just like there is only one single noise generator, there is only one single hardware envelope generator. And just like noise, it can be used on one, two, or the three channels at once. But just like the noise, it will probably sound ugly if you try to use it in more than one channel at the time.

The shapes

Two parameters defines the hardware envelope:

  • Its shape (going up, or down, cycling or not)
  • Its period: how fast it goes.

There are 8 shapes available. Here are they, using a high period for you to hear them, and with the software wave on.

Shape 8: sawtooth from 15 to 0, loops.

Shape 9: from 15 to 0, loops at 0.

Shape 10 (0xA): triangle (15 to 0 to 15 and loops).

Shape 11 (0xB): 15 to 0, loops to 15.

Shape 12 (0xC): sawtooth from 0 to 15, loops (opposite to 8).

Shape 13 (0xD): 0 to 15, loops to 15.

Shape 14 (0xE): triangle (0 to 15 to 0 and loops) (opposite to 0xA).

Shape 15 (0xF): 0 to 15, loops to 0.

Some important remarks must be made :

  • Some shapes seem to loop endlessly, some to stop after one cycle. However, they actually all loop on their last cycle.
  • Whatever the shape is, the amplitude always goes between 0 and 15 (or the opposite). You can’t ask a shape to go from 14 to 6 and then cycle or stop.

The latter limitation makes the hardware envelope as though it is useless. Why bother using an envelope when I can simply not use them but change the amplitude at will in real-time, choosing the exact values I want? In a sense, you are right, but the real interest is explained below.

Speed it up!

So hardware envelopes are limited and boring. Now let’s try something. Let’s play a sound using a cycling hardware envelope (like 8) and progressively decrease the period of the hardware envelope. In this example, the software envelope is fixed to a certain period and won’t change.

Did you hear that? Isn’t it awesome? By using a low period of the hardware envelope, a whole new sound is created from a boring square wave. Note that most of the sample sounded like rubbish, up to the end, where it sounded right. Why? This is explained below.

Synchronization

Why did it sound right? Because the period of the (square) software wave is proportional to the one of the hardware envelope. A good result is to have the latter being 8, 16 or 32 times (a power of 2) faster than the square wave:

Hardware period 8 times faster than the software period, shape 8.

Hardware period 16 times faster than the software period, shape 8.

Hardware period 8 times faster than the software period, shape 10.

Hardware period 16 times faster than the software period, shape 10.

So this sounds great, but you will probably very quickly encounter a limitation: the higher the pitch of the sound, the less accurate the periods are. Plus, the hardware envelope is more accurate than the software. So there *will* be times where envelopes will be desynchronized. Most of the time, with low sounds, it won’t sound too disgracious. But go higher and…

The faster the internal clock of the AY is, the more accurate this will be. Which explains why some Atari ST music converted to the CPC can have its hardware sounds sound crappy: the Atari ST has a 2 mHz YM, the CPC a 1 mHz AY.

Also, don’t worry about the ratio calculations we talked about earlier. Unless you specifically want to, everything is automatically calculated by AT! You don’t have to worry about the technicalities, only fiddle with some parameters and have fun with what you hear.

AY or YM?

The AY is used on many computers (Amstrad CPC, ZX Spectrum, MSX, etc.). The YM is mostly used on the Atari ST and MSX 2 and is almost exactly the same as AY (from which it copied its design). AT fully supports both.
There are two subtle differences between the AY and YM:

  • The logarithm curves are slightly different. The YM sounds “smoother” because the steps between the highest volumes aren’t as “steep”.
  • The hardware envelopes on the YM have double steps, which means that intermediate volumes are used when ramping up or down. This once again improves the “smoothness” of the hardware sounds. This is especially noticeable when using “hardware only” sounds, where the hardware envelope is distinctly heard. However, these more accurate volumes are only used when using hardware envelope (you don’t have control over them). You can not use them with the usual basic volumes… Too bad.

Samples

You may wonder how samples can be played on such limited chips. Well, depending on the hardware, it may be very easy to not-so-easy. But the basic is as this: very quickly, the amplitude of one (or more!) channel(s) is changed very quickly (8000 times per seconds for a 8 kHz sound), thus producing a richer sound than the PSG can normally produce by itself. Hardware like Atari ST have timers allowing to do that “in the background”. But on more limited hardware (like 8-bit computers), it is up to the coder to perform these changes: synchronization in itself is not hard to do, but things can get very complex if you want the computer to perform other actions while playing the sample.

As of now, except for the MOD player, no player in AT handle samples, but a digi-drum player may be done if people ask me to.

SIDs

SIDs have been created on Atari ST to emulate (a bit) the fantastic sound chip of the C64. Without the use of the Atari ST timers, they are hard to do, and the needed accuracy will require 100% of the CPU, which make their use pretty limited (and I personally think the resulting sound is not so great, making the trouble to create such complex player rather useless). They are also many ways to produce SIDs (ST-SID, Sid samples, etc.). As of now, SIDs are not supported by AT.