Reading audio files from CD-ROM

In this new episode, I tackle a problem that may seem trivial, but actually caused me quite a bit of trouble for several days. On the PlayStation 1, audio files — both for sound effects and music — can be read in various ways. You can use .XA files, which are a sort of compilation of audio tracks. In most cases, they’re used for music, but as I explain in the video linked below — and in the previous episode — this technique can also be used for commentary in games like FIFA 98.

With this technique, switching from one track to another is easy, since the CD-ROM is continuously being read. However, a problem arises when you also need to read other data from the CD-ROM to load game assets — like during a screen transition. The drive is already busy, so the music has to pause briefly. To get around this issue, we need to use a different method. We'll use .VAG files, converted from .WAV files using the VAG Edit tool included in the PS1 SDK.

A VAG audio file is certainly lighter than a WAV file, but it still takes up at least 4 MB — often more. On average, an audio track weighs around 6 MB. The RAM is only 2 MB, and the audio memory is 512 KB, so loading an entire VAG into memory is impossible.

What I implemented is a system that, through an interrupt, detects which byte the audio chip is currently reading — and thus determines the current second of playback. Basically, the first 300 KB of the music is loaded into memory. When the chip reaches KB 100, I load the next 100 KB of the VAG into audio memory (from 0 to 100 KB). When the chip reaches 200 KB, I do the same for bytes 100 to 200. Once it gets to 300, I repeat the process for bytes 200 to 300. At that point, the audio chip loops back to byte 0, where it finds another 300 KB of audio ready to be played.

This way, with just 300 KB continuously in use, the entire audio file can be read. The CD drive remains constantly engaged, but we also gain a time buffer — those 300 KB already in memory equal several seconds of playback — enough time to read other necessary data from the CD-ROM for the next screen.

And that’s the secret behind why, in Final Fantasy games, the music never stopped — you could run around the streets of Midgar or the Balamb Garden all you wanted, and the soundtrack kept playing seamlessly.

Back to articles