A few weeks ago, after many hours of patience, I managed to get the Sound Blaster blasting audio for the first time! I've since made some huge advancements, from getting a single sound to play, to mixing 4 sounds in real-time, while other code is running. It's exactly what I wished to do from the very beginning, and I finally managed to pull it off. However, because .com files have a limit of 64kb (this seems to be a running theme here), you can only fit so many sounds. What if I wanted long music loops to play in the background, or play a sound effect that won't fit?
The solution: streaming! The way I approached it might seem a bit strange, but it was the easiest to figure out. First, you need to open the file, and store the resulting handle in a variable. When you open this file, use the handle you saved, and read one byte at a time. This is important, because reading multiple at a time can actually be slower in the long run (like... significantly slower!). It's important to use 32-bit registers here, because that allows for files bigger than 65,536 bytes. In fact, your file can be up to 4,294,967,296 bytes large (4.2gb!) which is super excessive, but necessary to see the advantage of streaming from disk.
The result's pretty incredible, because you can have a super long music loop playing in the background, all while mixing 3 other sounds! For example, a .com file containing Blastlib on its own, streaming an extremely long file, is only 1.6kb in size. I strongly advise streaming one sound at a time though, because the disk access might go a bit mad. I've only tested it on a USB drive, but I plan to try it on my old laptop with a hard drive. I'll update this post when that happens!
View the source code for Blastlib here, and a jazzy example of streaming audio here! (make sure to download the appropriate file alongside it)
No comments:
Post a Comment