Audio mixing strategies

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Audio mixing strategies

Post by mathieu » Fri Nov 10, 2017 9:59 pm

Dear all,

First time poster here. I have plenty of experience with regular Python but none with microcontrollers. I've recently started playing with a pyboard, and so far it's been fun and stimulating. My first project is to build a Star-Wars-style lightsaber (officially, for my kids to play with). I can now successfully play back arbitrary-length wav files with acceptable quality, but eventually I'll need to mix some dynamically generated background noise (the famous lightsaber "hum", modified by readings from a MPU6050 inertial measurement unit) with some occasional pre-recorded sound effects. My attempts so far have not been very successful, mainly because of the immutable nature of the bytes type returned by 'wave.open().readframes()' and the memory constraints which make it difficult to convert everything to regular arrays for mixing.

I can see several option to move forward. I could eschew digital mixing, use the two DACs independently, and mix the two analog ouptputs using an analog mixer (do commercial pcb audio mixers even exist?). Less code, more physical components to cram into a tight space. Alternatively, I could keep looking for a way to combine digital signals, but maybe my expectation of what can realistically be achieved by a pyboard alone is too high? In that case, are there external components that could take care of digital mixing? Option #3 is the least satisfying, where I would have to pause the huming during short-term sound effects. I suspect that 90% of the "magic" of such toys is in the subtle sensory details, so I'd really like to avoid that kind of hack.

Any suggestions welcome, I look forward to hearing what you think.

- Mathieu

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Audio mixing strategies

Post by pythoncoder » Sat Nov 11, 2017 9:39 am

Analog mixing can be very simple. The Pyboard DAC outputs are buffered so can drive loads down to 5KΩ. Depending on the load you are driving a mixer could be as simple as a pair of 5.6KΩ resistors or above. A buffer in the form of an op-amp could be added if necessary but if you plan to drive a Pyboard ADC then the two resistors may suffice.
Peter Hinch
Index to my micropython libraries.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Audio mixing strategies

Post by SpotlightKid » Mon Nov 13, 2017 5:09 pm

Digital mixing is just adding the sample of each channel and optionally scaling the result back by multiplying with 0..1.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Audio mixing strategies

Post by pythoncoder » Tue Nov 14, 2017 6:33 am

Quite. Addition is addition in either domain. Digital makes more sense to me, but @mathieu did ask ;)

In his OP I think @mathieu was trying to mix the sound samples in RAM before outputting the combined result. The analog and digital solutions involve doing the addition in real time as the sound is output. Both avoid the need to modify data stored in RAM. Unless there is a compelling reason against I would add and scale immediately before writing to the DAC.
Peter Hinch
Index to my micropython libraries.

Post Reply