Jump to content
  • Advertisement
Sign in to follow this  
travikk

Sound mixing

This topic is 2864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, it's good to be back!

I am working on a project this time. In theory, we have to create an application which is going to be given couple of sound streams and it's purpose is to mix those streams into one.

Could you point me please at a correct direction on how to tackle this problem?

Share this post


Link to post
Share on other sites
Advertisement
The simplest way to mix two sounds is to simply add up the samples (making sure you clamp the values to avoid overflow).

Unfortunately clamping doesn't sound great when it happens so there are more advanced algorithms that try to avoid it.

However you're probably better off using a library that will handle all the low level sound mixing and output for you. FMOD for example which last time I checked was free for non-commercial use.

Share this post


Link to post
Share on other sites
Quote:
so there are more advanced algorithms that try to avoid it.


could you please elaborate this topic?
FMOD unfortunately won't work for me for some reasons.

Share this post


Link to post
Share on other sites
The mixing of the two sound streams is simply a linear combination:

output = volume1 * input1 + volume2 * input2

I haven't done audio programming in ages, but I think these days I would use a 32-bit floating-point type for the samples, so I don't have to worry much about overflow or loss of precision in the intermediate stages. At the end of the processing, you have the problem of mapping whatever you get to the limited range that the hardware can handle. The situation is similar to what you do in HDR graphics, where at the end you need to map down to a limited dynamic range for rendering.

If your signal is so loud that it would overflow, you could simply lower the volume(s). You could instead clamp the signal, as Adam suggested. You could also use a soft-clamping function (something like tanh?). Anything you do will distort the signal in some way. For more information, perhaps you should start by reading about compression.

Share this post


Link to post
Share on other sites
Quote:
FMOD unfortunately won't work for me for some reasons.
Is it just FMOD you can't use? Or can you not use any external sound API?

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
The mixing of the two sound streams is simply a linear combination:

output = volume1 * input1 + volume2 * input2

I haven't done audio programming in ages, but I think these days I would use a 32-bit floating-point type for the samples, so I don't have to worry much about overflow or loss of precision in the intermediate stages. At the end of the processing, you have the problem of mapping whatever you get to the limited range that the hardware can handle. The situation is similar to what you do in HDR graphics, where at the end you need to map down to a limited dynamic range for rendering.

If your signal is so loud that it would overflow, you could simply lower the volume(s). You could instead clamp the signal, as Adam suggested. You could also use a soft-clamping function (something like tanh?). Anything you do will distort the signal in some way. For more information, perhaps you should start by reading about compression.


one thing to keep in mind when mixing audio like this is that using a linear scale for volume doesn't map very well to how the human ear perceives volume.

Volume sliders typically use something like: volumefactor = linear_volume^3

For avoiding hard clipping, you'll want to look into limiting. You can use some subtle waveshaping as well to keep everything in the range -1..1..i.e. saturation.

edit: didn't notice the compression article was already linked to! my bad for the dupe.

Share this post


Link to post
Share on other sites
Thanks guys for youor answers! I've already done it with simple sum, as you suggested, but just wanted to discover other ways to do it ;)

Quote:
Is it just FMOD you can't use? Or can you not use any external sound API?


I can't use any external API.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!