Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: DSP option: mix chanels #106

Open
jagjordi opened this issue Nov 3, 2024 · 8 comments
Open

Feature: DSP option: mix chanels #106

jagjordi opened this issue Nov 3, 2024 · 8 comments

Comments

@jagjordi
Copy link

jagjordi commented Nov 3, 2024

I want to suggest an option for the DSP processor. I am using this for a multi-room audio setup and in some rooms I have a single speaker. It would be very good to be able to merge both left and right channels so that the speaker works as a MONO.
Right now I am using a simple DAC plus a separate audio amplifier. So what I am doing is that I am joining both audio channels after the DAC with a simple two resistor circuit. I am thinking to creating a new board that uses one of the amplifiers with a built-in DAC. For that setup, it would not be possible to merge the two channels after they have been amplified. Without having to use big and expensive power resistors, This is why I think it would be a very nice option to be able to do this merging of the channels in the digital domain on the DSP

@CarlosDerSeher
Copy link
Owner

Shouldn't be too hard. Mixing PCM samples is as simple as adding two samples, L+R. The question is, how we should handle int16_t over and underflows. Just simple clipping maybe?

@normanalie
Copy link

Some DACs offer an option to sum the L+R audio channels.
However, if you'd prefer to handle it DSP side, the approach taken by professional audio consoles is typically either a true L+R summing - where if the same signal is present in both L and R, the resulting sum will be 6dB louder - or an L+R-6dB summing, which maintains the same signal level as the original sources.
For this use case, I think L+R-6dB would be the best approach, as we're not aiming for a real summation but rather to create a mono signal that retains maximum information in a single-speaker setup.

@jagjordi
Copy link
Author

jagjordi commented Nov 4, 2024 via email

@CarlosDerSeher
Copy link
Owner

CarlosDerSeher commented Nov 4, 2024

Why not divide the signal by2 before summing?

You'll lose a lot of information that way, especially if there are low volume parts in one channel. Also it won't be necessary most of the time. Altough this will definitly be the easiest and fastest approach to implement I guess. Maybe we should convert to float before reducing gain?

true L+R summing - where if the same signal is present in both L and R, the resulting sum will be 6dB louder - or an L+R-6dB summing, which maintains the same signal level as the original sources.

As I am no audio expert, how do you do this implementation wise? Using some FIR / IIR filter structure. How do you analyze the audo sample before summing them? Is this done in the frequncy or time domain?

@CarlosDerSeher
Copy link
Owner

CarlosDerSeher commented Nov 4, 2024

After giving it another thought wouldn't it be best to just sum L+R using an int32, then divide it by 2 and cast back to int16? So int16_t out = ((int32_t)L + (int32_t)R) >> 1

@jagjordi
Copy link
Author

jagjordi commented Nov 4, 2024

2 things:

For the addition I am more in favor of doing the /2 mix because the "perceived loudness" would be correct. If you add without dividing then you are amplifiying by a factor of 2.

For the implementation, I don;t know much about how the low level implementation on the ESP platform but if you divide by 2 before adding you dont need to cast to int32. A int16 divided by two has a maximum range of 15 bits, so when you add 2 of them the maximum range is 16 bit again. There is no possibility of overflow. you can also implement the /2 by right shifting 2. the >> operator should perform arithmetic shift (preserving the sign bit) when it shifts a signed number

@CarlosDerSeher
Copy link
Owner

Watch out! x/2 == x >> 1

@DerPicknicker
Copy link

Maybe for some of you are interested in real DSP hardware. The TAS5805M DAC has a DSP builtin.

The board designer of those boards enabled the DSP features for snapcast see here: https://sonocotta.github.io/esparagus-snapclient/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants