Mixing Digital Audio

about | archive


[ 2005-March-12 09:02 ]

Have you ever wondered how to blend together multiple streams of digital audio? Hm... Well, probably not, but I am a big geek so I have. A few years ago, I hacked Abuse-SDL to work on Mac OS X (aside: it is a very fun game that is worth checking out if you like blasting aliens and don't mind dated graphics). The sound was not working, and in fixing it I discovered that the mixing code was very stupid. What it would do is reduce the volume of both streams in half, and then add them together. This was fine if the streams were at maximum volume, but if one was silent, the other stream would end up half as loud. This was really bad when the game switched from playing one sound to playing two sounds, because the volume of the first sound would suddenly drop. Unfortunately, I could not figure out a better way. Well, yesterday I found an excellent page that explains how to mix digital audio streams together in a much better way that is still very simple. It is very interesting to read, if you are curious about this sort of thing. I'm sure there are probably tons of research papers describing even better ways to do this, but I bet this simple approach is good enough in most situations.