Search This Blog

Wednesday, August 1, 2018

Automatic audio source switching

Projects / Audio Switching  Original post date:06/08/2016

This is an automatic source switch between my computer or TV source to my desktop amplifier. A mixer or a simple audio detection switch won't work as I sometimes watch short videos on my computer during commercial on TV. The switching is based on threshold detection and a state machine implemented on STM32F030F4. It automatically reduces volume of loud audio.

What I am trying to do:

  • I want a box that automate the switching audio source between my TV and computer as I am manually doing that a few times a day with a DPDT switch.
  • A lot of times, obnoxious installed computer games intro or youtube or unexpected webpage advertising popup volume is set too high. I would like to reduce the audio level automatically.
  • I sometimes watches video clips on my computer during a TV commercial break, so the audio source needs to be switched to the computer and back to the TV when I am done. A mixer solution won't work here.
  • The TV doubles as my 2nd monitor. By also connecting the audio that runs through a different cable,I have created a "ground loop". Right now, I also have a SPDT switch to select the ground connections for certain peripherals that causes noise, but this is not ideal.

The ground loop becomes a big antenna for magnetically picking up noise within the loop area. One way to break the ground loop is to use balanced signals for the audio.

Block diagram

Differential Amplifier

The differential amplifiers helps to remove the Common Mode noise even for a quasi-balanced with the ground acting as the "cold" wire. By breaking the ground connections, this also help to remove the ground loop. Initially, I was going to place the differential amp after the mux. The mux I have are very old and their high R(on) resistance is going to strongly reduce the common mode rejection ratio. (see here for explanation) So I am better off with more amplifiers.

Bipolar low noise/distortion opamp e.g. El Cheapo NE5532 (30+ years old!) are good enough for this project, unfortunately they require +/-5V rails.

Analog Mux

A dual 4:1 Analog Mux is used for switching the audio signals. Voltage dividers are used to provide an attenuated version of the input signals when the firmware detected loud audio input.

ADC

The ADC in the ARM is used to continuously scan the audio input from the two audio sources to detect active audio source. The data can also be used for optional visualization such as a Spectrum Analyzer, VU meter etc.

A simple voltage divider is used to attenuate and bias the bipolar audio signal to the 0-3.3V range. This extends the ADC range to +/- 3.3V which is about 2.33V RMS for a pure sine wave. The Green trace is the audio input and the Blue one is what the ADC sees.


1.8nF capacitors are added to the input. They act as anti-alias filter and lower AC source impedance for the sampling ADC.


The -3dB point for the filter is around 18kHz. ADC sampling rate is set to 38.4k samples/s which has a Nyquist frequency of fs/0.5 = 19.2kHz.

Volume Meter

Basically, a VU meter is a full wave rectifier driving a moving coil meter with a capacitor for averaging. The capacitor sets the rise time to 300 milliseconds. The long time constant averaged values reflects the volume of the audio input. 1.228V RMS is defined as 0VU. This is also the audio output level from my PC.


The summing and rectifier is implemented in firmware. The pseudo code is as below.


Offset_L, Offset_R are the input offset. They are computed by taking an average of the input sample. Any values above it is a positive voltage, so the offset is subtracted off. Voltages below that are negative, so the difference is used. Thus the loop adds up the absolute values.

This produce an average for a sample period at a point in time. This is used to detect the present of audio. I used a IIR filter for averaging the values over time which is used to drive a log scale bar graph on the LCD.

Volume Meter

The volume data is used for threshold audio detection for the input switching control. It is also for eye candy like a VU meter. It is a good opportunity to learn how to code one from scratch.

From what I read is that the time constant for the VU meter is 300 milliseconds. It is an average of the input voltage levels representing the input volume.


I have been looking at the volume levels and decided to extend the range from -51 to +3VU using 3dB steps. I am plotting it using a linear spacing for the scale. I am using a lookup table of pre-computed threshold values to convert the average value to the dB scale.

True RMS value would use up a bit of extra processor time in ARM M0, but might not make a lot of visual differences. Analog VU meters in consumer grade electronics are likely to use a scaled peak to peak value for RMS. (The same lookup table could hold the sum of squares to avoid the costly square root operation needed for RMS calculation.)

I am using a simple IIR filter for averaging the magnitude of the input voltage. I adjust coefficient α for 300ms worth of averaging. α = 89/90

I have also implemented a peak meter with a full scale fall time of 1.5 seconds.

References:
http://sound.westhost.com/project55.htm
http://music.columbia.edu/pipermail/music-dsp/2007-June/066267.html
http://www.embedded.com/design/configurable-systems/4025591/Digital-filtering-without-the-pain

Bar Graph Display

The following piece of code is used for log scale conversion and output rendering. dB_Table stores the threshold value for dBV.


The code does a linear search in the table and decides whether the corresponding bar graph segment should be on or off by comparing against the thresholds in dB scale.

Control Logic

Simply looking at the video source selection at the TV isn't good enough as sometime I might watch short video on my computer during the commercial breaks.

I came to the conclusion that I would need a state machine and time delays. This pushes the complexity into realm of a microcontroller. I am using the STM32F030F4 ARM M0 chip as the 32-bit architecture asd well as DMA are useful for simple digital signal processing.

Power Supply

The audio switch requires the following voltages:

  • Diff amplifier, MUX: +5V/-5V @10mA
  • Power for class D amplifier: 5V @1.5A
  • Control circuits: 3.3V @60mA

Analog supplies and switch mode supply for class D amplifier

I am using a small Buck converter from Aliexpress for the Class D amplifier for my speakers. I take advantage of the Buck module by running a negative charge pump (C13, D1, C11) off the switching node. A pair of 78L05 and 79L05 regulators are used to produce the +/-5V rail for the analog circuits. They also provide modest amount (60dB) of power supply rail ripple attenuation within the audio frequency range. Additional filter and careful layout is used to control the noise level. See Analog power supply for noise measurements.

A LDO is used to provide +3.3V for the ARM microcontroller.
3,3V supply for the microcontroller

Mechanical Design


The main board consists of power supplies, 3.5mm stereo connectors and micrcontroller. I have fabricated this at home using toner transfer. I am using a split plane for the single sided design. I may eventually get it fabricated on a doubled PCB next time when I need to fill up unused space on a PCB.


Project logs

Project logs part-1 - Plan of attack, Project progress,
Project logs part-2 - proto build
Analog power supply - with noise measurements.
Control Logic - State machine - code for selecting audio source
Daughtercard assembly - also glitchless switching

Youtube demos: 

Spectrum analyzer

This was recorded from my camera with the music redub from downloaded source / re-levelled.
(My camera's mono audio isn't that great.)

Music: "The Day I Die Remastered" by TeknoAXE
Music License: Creative Commons Attribution 4.0 International 
URL: http://teknoaxe.com/Link_Code_3.php?q=812


Here is the audio switch in action switching between TV and PC. (The clicking noise is from my mouse click.)


Licenses:  See github

References
http://sound.westhost.com/project55.htm VU And PPM Audio Metering
http://www.jensign.com/RMAA/RMAAOpAmpTests.html RMAA Testing of Audio IC Op Amps

Elm-chan's "AC Load Analyzer"
Code: http://elm-chan.org/works/heco/rc/heco_src.zip (integer FFT: intfft.c and intfft.h)

Another similar project: http://www.zea.jp/audio/dvol/dvol_01.htm in Japanese  (google translate)
This project uses analog circuit for rectifier, relay for switching audio channels with mute for the switching noise which are the alternate approaches.

I am using the 16-bit Integer FFT library from Elm-chan and wrote my own DMA LCD routines.




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.