Montag, 25. Januar 2016

Noisy Converters - Adventures with Optical Audio, Part I

I'm quite positive that everyone who once tried to transmit analog audio over a long distance encountered sound quality issues in some form. When I wanted to hook up a pair of speakers across the room to my computer, I went for a digital solution right away: TOSLINK. A simple standardized audio interface using a fiber optic cable. Its signals are identical to S/PDIF, an elektrical digital audio interface. Both TOSLINK and S/PDIF have gotten pretty common nowadays in all kinds of audio equipment such as TVs, CD players, HiFi systems and even PC sound cards. No interference, crystal clear sound, no path loss. Sounds great so far? (Pun intended)
I bought a cheap sound card for my PC with optical output, a 10 m fiber cable and a digital to analog converter.


However, whenever the converter doesn't receive an optical input, such as when the sender is turned off, it started to output ugly noise. There wasn't really much you could do against it than shutting the converter off when my PC was off. So I made a little device with a monostable circuit triggered by the optical signal that switched to power for the converter on whenever there was an active input and shut it back of when the input was inactive. The circuit was very simple, just a TOSLINK receiver, a monostable cuircuit and a transistor. Since I also had to feed to signal trough to the converter, I used the S/PDIF coax input of the DA-converter rather than to TOSLINK in order to save an optical splitter. And it was awesome in its simplicity and it also did its job.
However there is a reason why I talk in the past tense about this device and I don't have pictures of it: It isn't alive anymore because it had a major drawback: Start up clicks on my speakers. Nasty, loud clicks. And that wasn't caused by my circuit, it was the converter itself. Probably uncharged decoupling capacitors at the output. You could use some relays  at the output of the converter to disconnect the speakers until the caps are charged, but that's a third circuit and a third box. I decided to take another path in order to combine all three circuits (the power switch, the converter itself and the relays) into one - and it turned out that with the ability to disconnect the speakers from the converter output, the power switch circuit becomes obsolete.
Rather than using relays I used an analog multiplexer CD4052 and a charge pump for a negative supple voltage, since the output is AC centered around ground. An ATtiny84 does the switching for the charge pump and decides whether to turn on or off the output. A 10 k resistor is used to keep the outputs at well defined level when shut off.



Here is the code that runs on the little AVR. Basically just 50% duty-cycle high frequency pwm on the charge pump for the charge pump and an interrupt triggered "monostable" circuit with predefined time constant. I know that the same thing could also be achieved by a smaller microcontroller, the ATtiny84 was the smallest I had lying around though.


//pin definitions: 
const byte pwm = 7;  
 const byte signalin = 2;  
 const byte sel1 = 1;  
 const byte sel2 = 0;  
 const unsigned int tresholdtime = 64; // 1 ms  
 bool volatile triggered;  

 void setup(){  
  pinMode(pwm,OUTPUT);  
  //timer0 multiply by 64  
  //millis runs 64 times as fast.  
  TCCR0B |= (1<<CS00);  
  TCCR0B &= ~((1<<CS01) | (1<<CS02));  
  analogWrite(pwm,128);  
  pinMode(sel1,OUTPUT);  
  pinMode(sel2,OUTPUT);  
  digitalWrite(sel1,HIGH);  
  digitalWrite(sel2,LOW);  
  pinMode(signalin,INPUT);  
  PCMSK0 = (1 << PCINT2); //Enable interrupts on PCINT2 (signalin)  
  GIMSK = (1 << PCIE0);  //Enable interrupts period for PCI0 (PCINT7:0)  
 }  

 void loop(){  
  triggered = false;  
  sei(); //enable interrupts  
  delay(tresholdtime); //not nice but works  
  if (triggered){  
   digitalWrite(sel1,LOW);  
  } else {  
   digitalWrite(sel1,HIGH);  
  }  
 }  

 ISR(PCINT0_vect){  
  cli(); //disable interrupts  
  triggered = true;  
 }  
I happened to be able to fit everything in the original box when I desoldered the coax jack. Even with all parts being through hole and the ICs socketed!!



And this thing got rid of all the noise. I am sure the manufacturer could have captured equivalent measures without much effort or price hike.

This is part one of a little three (or more) part series about my experiences with TOSLINK and S/PDIF.
Click here to read part two of the story.
- Marv