Wednesday 10 May 2023

Super long streaming sounds, and system timer!

This is a follow-up to a post I made yesterday regarding Blastlib, my Sound Blaster library. I mentioned how 4-voice playback was tied to the vertical retrace, and how big a disadvantage it was. Today, I experimented with different methods, and tried using the system timer, which I briefly mentioned. Turns out it's an extremely good solution, and it works so much better than using the retrace period. Of course, it does have a catch, but it's a very small one. More on that later.

By default, the system timer runs at 18.2Hz, which at the time, didn't seem like enough to me. So, I ended up trying different frequencies, and after some experimenting, I discovered that the default speed actually gives the best results! I couldn't believe it; it was the first time I'd ever heard the mixing routine sound so good. Clicks and pops were a huge issue before, probably because the retrace and the buffer didn't match up perfectly. Using the timer is way more accurate, providing a much cleaner sound. It also means that if you're running a game and the main loop slows down, the timer keeps on streaming sounds without stuttering once. A huge improvement!

However, the timer does have a small catch, and it depends on the amount of code that's running at once. You'll have to make very fine adjustments to the buffer size if you want to get the cleanest sound, so more code will require the buffer to be minutely bigger, but only by a few samples!

I've also been making Blastlib way more dynamic. Before, mixing was fixed to 11025Hz, but I've discovered how to use %define in NASM, so I've made the sample rate changeable. Now, all you need to do is add a %define at the beginning of your code, and it'll handle all the buffer-related things for you. I also made an appropriate timer library, so you can use the libraries together to get the best possible sound. All of these things open up some huge possibilities previously unknown to me!

To finish off, here's a demo I made, streaming a 3-minute song at 22kHz with minimal stuttering, using the system timer. Check it out, it's pretty incredible:



No comments:

Post a Comment

Amiga module player for DOS - the first draft!

After one week, I've finally pulled off what I previously thought impossible - an entire module player, running in real mode DOS! I'...