Teensy mixing audio file and control with light sensor


I want to mix 3 audio files (play them together) with Teensy, and change the GAIN and VOLUME with two photo-resister. I already figuring out the code for changing volume with the change of light, but I don’t know how to combine this volume control code with the mixing code.

*And I really want to explore more with the light sensor, maybe add more light sensor and control on audio file separately.
Does anyone try to do more with light sensor on audio control?

(Below is the code I want to combine, mixing code and the code of playing audio file separately but with the volume control by light sensor)


sketch_mar27a.ino (3.33 KB)

sketch_mar27b_mix_together.ino (2.81 KB)

I've never used the Teensy...

Volume is adjusted by multiplication. If you want to cut the amplitude in half (-6dB) you multiply all of the samples by 0.5.

Mixing is done by summation. (Analog mixers are built-around summing amplifiers.)

If you want to mix 3 files digitally you add sample-1 from all 3 files together, sample-2 from all 3 files together, etc. You'll probably want to do that in real-time as the audio plays. (Of course the sample rates have to match and if they don't one or more of the files has to be re-sampled.)

But in reality, you have to beware of [u]clipping[/u] (or worse*) so you need to make an average or a weighted average. i.e. Divide all of the values by 3 before summing. (An analog mixer has volume faders for each input-channel, plus a master fader and analog mixers usually have extra "headroom".)

Most "real world" DSP is done if floating point (the Teensy is probably not fast-enough for that) and that helps with clipping, but you still have to careful of clipping your DAC or having a clipped file if you save in a regular-integer WAV file.

  • Clipping (distortion) is bad but if you don't handle clipping properly you can get integer-overflow/roll-over (where you loose the most-significant bits) and you'll get really-nasty distortion.

Looks like your program should work, to pan between two WAV files as the light sensor changes. Using the sensor to also control volume should be a fairly "simple" matter of just deleting the volume control code you have now (reading analog A2) and adapt it to use the number you're light sensor reading from analog A6.

If you could describe more clearly how you want the light to control the volume, we could maybe recommend ways to do that math. But you're already using the map() function pretty well....

@DVDdoug - In this case, all that tough DSP work is being done by a library I wrote. :)