AC-3 (aka "Dolby Digital") 5.1 over spdif / toslink

Frank B

Senior Member
Hi all,

i have a new idea for a little project:

TOSLINK is working now, and it is no problem to output PCM Stereo.
The same Interface is able to transport more sophicsticated formats like DTS or AC-3. At first, i'd like to try AC-3, because i have the hardware to play it in my livingroom... my old receiver is not able to play DTS, so i have no chance to test anything :)

On the low-level hardware side, AC-3 uses the same fomat we are using already. This means, our Teensy 3 is able to play it.
NOT to encode it, this is way too heavy (It would mean to compress (like Mp3/aac) 6 channels in realtime!))
But, we are happy to play PCM-Wave-files from SD-Card. Why not extend it, and play 5.1 wavefiles ? I hope we don't need to decode anything, this is done by the receiver. I hope, it is sufficiant to "copy the wavefile to toslink" (pls excuse the simplification in this case).

Here are examples for such files:
http://www.diatonis.com/surround_sound_music.html

(Maybe it is the same format as for DVD ?)

You can play, for example "A Night full of stars AC-3" with "VLC media player". Most other players do not understand it.

Now to the details:
The spdif bitrate is the same as with PCM. That means we can use most of the already written code.
There is a flag in the SPDIF bitstream which tells the format. Currently, it is set to PCM.
I hope it is sufficiant to set it to AC-3..
We transmit 2 channels currently..with 16 bits.
I do not know how this would be for AC-3. Do we need 20 Bits ? 24 ? One Channel ?
These details are important to know.

This is the AC-3 specification:
http://atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/

Until now, i have no read it..

You see, until now, it's not more than an Idea. But I am confident that it is possible
If anyone has more information or ideas , I would be very pleased .
 
Last edited:
No Joke...

No Joke...believe me..

It works out of the box, without changing anything!

Just edit the example wav player, change the name of the first wavefile, switch to SPDIF, and you are done!
(welll copy the example ac-3 to the sd-card first)
5.1 Dolby-Digital surround with the Teensy!!!

Unfortuantely i can't test DTS, but this should work too.
 
Last edited:
Pauls example code.
Switched to SPIF & added "dolby.wav", nothing else changed.

Code:
// Simple WAV file player example
//
// Three types of output may be used, by configuring the code below.
//
//   1: Digital I2S - Normally used with the audio shield:
//         http://www.pjrc.com/store/teensy3_audio.html
//
//   2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
//         https://www.oshpark.com/shared_projects/KcDBKHta
//
//   3: Analog DAC - Connect the DAC pin to an amplified speaker
//         http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
//
// To configure the output type, first uncomment one of the three
// output objects.  If not using the audio shield, comment out
// the sgtl5000_1 lines in setup(), so it does not wait forever
// trying to configure the SGTL5000 codec chip.
//
// The SD card may connect to different pins, depending on the
// hardware you are using.  Uncomment or configure the SD card
// pins to match your hardware.
//
// Data files to put on your SD card can be downloaded here:
//   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
//
// This example code is in the public domain.

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

AudioPlaySdWav           playWav1;
// Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
//AudioOutputI2S           audioOutput;
AudioOutputSPDIF       audioOutput;
//AudioOutputAnalog      audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
//AudioControlSGTL5000     sgtl5000_1;

// Use these with the audio adaptor board
//#define SDCARD_CS_PIN    10
//#define SDCARD_MOSI_PIN  7
//#define SDCARD_SCK_PIN   14

// Use these for the SD+Wiz820 or other adaptors
#define SDCARD_CS_PIN    4
#define SDCARD_MOSI_PIN  11
#define SDCARD_SCK_PIN   13

void setup() {
  Serial.begin(9600);

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(8);

  // Comment these out if not using the audio adaptor board.
  // This may wait forever if the SDA & SCL pins lack
  // pullup resistors
  //sgtl5000_1.enable();
  //<sgtl5000_1.volume(0.5);

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here, but print a message repetitively
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void playFile(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playWav1.play(filename);

  // A brief delay for the library read WAV info
  delay(5);

  // Simply wait for the file to finish playing.
  while (playWav1.isPlaying()) {
    // uncomment these lines if you audio shield
    // has the optional volume pot soldered
    //float vol = analogRead(15);
    //vol = vol / 1024;
    // sgtl5000_1.volume(vol);
  }
}


void loop() {
  playFile("DOLBY.WAV");
  delay(500);
  playFile("SDTEST1.WAV");  
  delay(500);
  playFile("SDTEST2.WAV");
  delay(500);
  playFile("SDTEST3.WAV");
  delay(500);
  playFile("SDTEST4.WAV");
  delay(1500);
}


Deep-Link to the testfile (rename wavefile in this zip to dolby.wav):
http://www.diatonis.com/downloads/diatonis_ac3_wav_anfos.zip
 
Last edited:
Hm, there is a short loud "klick" when switching from wav to ac3. That's because the decoder needs to detect the change of dataformat first - until then, the compressed data are interpreted as pcm.
There is a "valid" bit in the protocol, it should help to set it false for compressed audio.
Paul, there are reserved bytes in the "audiomemory" - can we introduce a "valid" or "compressed"-bit there ?
At least for the output-objects.
 
Paul, there are reserved bytes in the "audiomemory" - can we introduce a "valid" or "compressed"-bit there ?

Could this be accomplished by adding a function to the SPDIF output object?

My general preference is to keep the underlying infrastructure as simple as possible, and to expose setting and capabilities as functions which Arduino sketches can call. But if there's a really compelling need, real uses that can't be accomplished with Arduino accessible functions, perhaps we could dig into those reserved bytes. My preference is the leave those reserved bytes alone, if we can.
 
Could this be accomplished by adding a function to the SPDIF output object?

My general preference is to keep the underlying infrastructure as simple as possible, and to expose setting and capabilities as functions which Arduino sketches can call. But if there's a really compelling need, real uses that can't be accomplished with Arduino accessible functions, perhaps we could dig into those reserved bytes. My preference is the leave those reserved bytes alone, if we can.

The only instance wich knows details about the format inside the wavefile is the waveplayer. We need to inform the output about that.
Do you know an other way ?

Long term, maybe, we have compression/decompression inside the audiolibrary?
 
Last edited:
There is a "valid" bit in the protocol, it should help to set it false for compressed audio.

Ok, i can confirm now, that switching "VALID" works as expected. The amplifier mutes for PCM, not for compressed data.
I'll do a pullrequest that allows to set "VALID" from the outside of AudioOutputSPDIF.

Not ideal, because this does not allow syncing with the audiobuffers. But without access to the unused(reserved) bytes, more is not possible.

Anyway, its possible then to mute PCM before playing() a compressed file.

Edit: Usage:
Code:
  playFile("SDTEST1.WAV"); 
  audioOutput.mute_PCM(true);
  playFile("DOLBY.WAV");
  audioOutput.mute_PCM(false);

PullRequest:
https://github.com/PaulStoffregen/Audio/pull/120
 
Last edited:
No Joke...believe me..

It works out of the box, without changing anything!

Just edit the example wav player, change the name of the first wavefile, switch to SPDIF, and you are done!
(welll copy the example ac-3 to the sd-card first)
5.1 Dolby-Digital surround with the Teensy!!!

Unfortuantely i can't test DTS, but this should work too.

hi Frank,

I haven't been able to find an AC3 file that works with teensy, the link you posted with the demo doesn't work anymore.

I've tried other files, changing the number of channels and the samplerate, but no luck.

Can you share with me the AC3 file that you used to do the test?

I would like to use your work to use a logitech Z906

https://github.com/zarpli/Logitech-Z906

first of all, Thanks
 
Back
Top