Shop OBEX P1 Docs P2 Docs Learn Events
SPDIF (digital audio) output demo — Parallax Forums

SPDIF (digital audio) output demo

scanlimescanlime Posts: 106
edited 2009-06-03 20:21 in Propeller 1
Hi,

I've been experimenting with generating SPDIF digital audio signals from the propeller. If you aren't familiar with SPDIF, it's the protocol used by the optical and coax digital audio inputs on all modern stereo systems and home theater receivers.

It's a pretty demanding signal: 6 megabits, precise sampling rate, manchester-like "Biphase Mark" encoding. But, the Propeller is up to the task smile.gif The biphase encoding is done using a pretty simple unrolled loop (2 instructions per bit) while the cog's video generator simultaneously clocks out the signal at the proper rate, as determined by the CTRA PLL.

The net result: You can get 48 KHz 16-bit stereo audio, reproduced in digital clarity, using only a Propeller and an LED.

Attached is my "spdifOut" object, and two demos:
- A modified version of HSS, with 48 KHz SPDIF output.
- A simple WAV file player which plays CD-quality audio from an SD card in real-time.

For the HSS demo, all you need is a Propeller, an LED, and of course some kind of audio equipment that has a TOSLINK (optical digital) input. I just used one of the LEDs on the PropRPM board, and pointed the exposed end of a TOSLINK cable at it. For the WAV demo, you'll also need an SD card with some audio on it.

I tested this with an Onkyo receiver, as well as a cheap USB audio adapter based on the PCM2902 chip. Works fine for me, but YMMV. Please be sure to test at a low volume, to avoid damaging your speakers if anything is wrong.

--Micah

Comments

  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-07-26 13:15
    WOW!

    repeat that...

    WOW!!!

    You coded an SPDIF digital audio player in the Prop?! Man, I wish I had a system to test this out on.

    WOW!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.

    www.brilldea.com·- Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto fo SunSPOT, BitScope
    www.sxmicro.com - a blog·exploring the SX micro
    www.tdswieter.com
  • LeonLeon Posts: 7,620
    edited 2008-07-26 16:02
    Very impressive!

    I think my Delta-44 sound card has SPDIF, I suppose that would do for testing. I think that an ADI Blackfin DSP board I've got has it , as well.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • scanlimescanlime Posts: 106
    edited 2008-07-26 18:52
    Thanks!

    Looks like it does surround-sound too!

    I just tried playing some of the WAV files with AC3 (Dolby Digital) and DTS content from www.kellyindustries.com/sounds.html. Success!

    I did have to swap the left and right channel in spdifOut, by inverting the four shl/shr instructions that unpack the audio. I'm not sure whether the channels are just backwards in my code, or whether this is because I'm not parsing the WAV header at all, so my data might be totally misaligned. I tried adding the "NON_AUDIO" flag, to perhaps tell older receivers that this isn't uncompressed audio.. but it seems like receivers (at least the Sony that I tried it on) will totally ignore an SPDIF signal if it has this bit set. The receiver is perfectly happy to magically reinterpret a 44.1 KHz PCM stream as DTS or AC3 if it looks like compressed data.

    I haven't noticed this problem yet with AC3 audio, but with DTS the receiver occasionally (every 10 seconds or so) momentarily loses a lock on my signal, and the audio cuts out for a fraction of a second. I have two theories about this:
    • The DTS WAV file includes some additional chunks of data (keyframes??) every so often, and my demo isn't smart enough to skip them.
    • The receiver's buffer is over/under-running, because my 44.1 kHz clock isn't accurate enough. Maybe the spdifOut module should be constantly tweaking the PLL frequency in order to get higher long-term frequency accuracy? (This would be kind of like what the utility companies do, periodically changing the power line frequency to ensure all of the line-powered clocks show the right time...)

    So, this is kind of neat, but I still have no idea what it would be useful for. I just wanted to see if it could be done. I wonder if it's possible to do some kind of minimal AC3 encoding on the Propeller, for real non-prerecorded surround sound output? Maybe it's possible to build an HSS-style synthesizer, but entirely in the frequency domain?

    --Micah
  • RaymanRayman Posts: 13,797
    edited 2008-07-26 20:26
    Very interesting!
  • PFloyd36069PFloyd36069 Posts: 135
    edited 2008-07-27 01:58
    now someone needs to make something like the stereo spatializer work in surround.
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-07-27 02:04
    That is a great idea. Can Chip's singing monk demo or "seven" song be done in surround?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.

    www.brilldea.com·- Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto fo SunSPOT, BitScope
    www.sxmicro.com - a blog·exploring the SX micro
    www.tdswieter.com
  • scanlimescanlime Posts: 106
    edited 2008-07-27 08:33
    Well, the AC3 audio specs are public now, thanks to the ATSC: www.atsc.org/standards/a_52b.pdf

    Assuming the Prop is fast enough to do all the bitstream packing and CRC operations in real-time, the tricky part would be writing a useful synthesizer that works in the frequency domain.

    Theoretically SPDIF can do multi-channel PCM audio, but I don't know that any actual consumer hardware supports uncompressed surround-sound. The required SPDIF bit rate would go through the roof...

    --Micah
  • Jimmy W.Jimmy W. Posts: 112
    edited 2008-07-28 03:34
    Look at Master Audio specs, they use uncompressed digital audio for "perfect" reproduction.
  • scanlimescanlime Posts: 106
    edited 2008-07-28 03:53
    Jimmy W. said...
    Look at Master Audio specs, they use uncompressed digital audio for "perfect" reproduction.

    According to Wikipedia, it isn't uncompressed- it's a regular DTS stream (for backwards compatibility) plus an additional stream which encodes the difference between the compressed stream and the original. It's lossless, but it isn't uncompressed. Compressing a DTS Master Audio signal would be at least as hard as compressing a regular DTS signal.

    Uncompressed PCM would be easy, but I don't know if any real audio receivers support it. I think the S/PDIF spec allows any number of channels (you can replicate the secondary channel subframe any number of times) but in practice nobody seems to use anything but 2-channel PCM.

    And again, the data rate would be pretty astronomical. With my current code, I can't *quite* do 96 KHz stereo audio. (I think the max I've tried it at is about 90 KHz) Any kind of multichannel audio would be out of the question unless you used a really low sampling rate.

    But hey.. if we want to do high quality audio on the Prop, we could start by experimenting with higher sample rate and higher precision synthesizers for normal 16-bit stereo audio. I can pipe out pristine digital audio from HSS at 48 KHz, but it's still generating that audio using tiny 8-bit wavetables [noparse]:)[/noparse]

    --Micah
  • evanhevanh Posts: 15,126
    edited 2008-07-28 10:45
    You're being a bit enthusiastic wanting to pump out 5.1ch at 24bit/96kHz! I suggest leaving the high-end to uncompressed stereo only. That makes the most sense to me.

    How about something like 4ch at 32kHz?

    What about mp3 decompression from those SD cards? Making your own personalised mp3 player. [noparse]:)[/noparse]


    Evan
  • scanlimescanlime Posts: 106
    edited 2008-07-28 18:02
    evanh said...
    You're being a bit enthusiastic wanting to pump out 5.1ch at 24bit/96kHz! I suggest leaving the high-end to uncompressed stereo only. That makes the most sense to me.

    How about something like 4ch at 32kHz?

    What about mp3 decompression from those SD cards? Making your own personalised mp3 player. [noparse]:)[/noparse]

    Evan

    Even 4ch at 32kHz is a little faster than I've been able to get my object to run... plus there's the fact that I don't know of any receivers that do multi-channel PCM.

    Real-time MP3 decoding on the Prop itself is not likely to be possible. Many other threads have discussed this. There are cheap co-processor chips that decode MP3s.. but where's the fun in that?

    This project was just a challenge to see if it was possible to output a high-quality digital audio signal using nothing but the Propeller. I still don't know what else, if anything, it's useful for [noparse];)[/noparse]

    --Micah
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-06-01 12:00
    What about spdif decoding? or say I wanted to use the prop to MERGE two SPDIF signals and output one final mixed signal?

    Would making a SPDIF, mixer be a hard thing to do? Many say expensive hardware is needed to mix two spdif 44.1khz, stereo uncompressed signals into one single output?


    You mentioned making a synth, how about starting by combining your spdif object with the singing demo spin object?
    Wouldn't this sound better than the singing demo that outputs to the props pins? Due to the sound being sent digitally to a higher end device that can render the audio with much better hardware?

    I am interested in looking into all this but have no idea how complicated these ideas are, micah, perhaps you can comment a bit on them.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.

    Post Edited (BPM) : 6/1/2009 12:49:29 PM GMT
  • LeonLeon Posts: 7,620
    edited 2009-06-01 12:31
    You'd need a lot more processing power than the Propeller has to mix SPDIF inputs. It would be quite easy with the right hardware, though.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-06-01 12:46
    It has enough power to encode a spdif signal, but can't decode one? why? perhaps someone can go a bit more into why this is not possible.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.
  • LeonLeon Posts: 7,620
    edited 2009-06-01 13:28
    Some software I have (not for the Propeller, of course) samples S/PDIF input at 50 MHz!

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle

    Post Edited (Leon) : 6/1/2009 1:48:51 PM GMT
  • AleAle Posts: 2,363
    edited 2009-06-01 14:34
    Leon: We know that _that_ other processor can do it wink.gif.
    What I did not know was that the propeller can output such a signal. This should go to the Timers and Counters Application note just to show how a clever thought algorithm together with the serializer in the video circuitry can be used as output for other than video. We really need a de-serializer for input now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
  • BeanBean Posts: 8,129
    edited 2009-06-01 14:36
    I guess there is no way to do ADAT, 8 channels of 24-bit 48KHz audio...

    That's like 9.2MHz bit rate.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    There is a fine line between arrogance and confidence. Make sure you don't cross it...

    ·
  • heaterheater Posts: 3,370
    edited 2009-06-01 17:37
    @BPM: I think you'll find the answer to your question is that the SPDIF is actually being shifted out of the Prop at 6MHz by the Props video shifters. Sadly there is no similar hardware support to shift data into the Prop. sampling the an SPDIF input at that rate in software only is not going to happen.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-06-01 18:42
    heater said...
    @BPM: I think you'll find the answer to your question is that the SPDIF is actually being shifted out of the Prop at 6MHz by the Props video shifters. Sadly there is no similar hardware support to shift data into the Prop. sampling the an SPDIF input at that rate in software only is not going to happen.

    I thought it had something to do with the video hardware / pll? I guess I need to read more on exactly how the propeller is going about generating the video and audio signals.

    But perhaps the benefit for us all is to allow us to understand the
    nature of video and audio without going out and buying a dsp fpga combo.

    So really, we can only generate stereo spdif signals, and ones on the fly only.
    I'll take it.

    So...

    I am now off to code, and learn from all your examples here to add to the PILE!

    [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.
  • Clock LoopClock Loop Posts: 2,069
    edited 2009-06-03 20:21
    I have a few questions.


    One, I was not able to get "beuler.hwav" to play back properly, but the .hmus files sound great. Is there something wrong with the .hwav player? (yes the .hwav file is compiling proplerly) and yes i turned the .hwav player on, and disabled the .hmus player/file loader

    Another question,

    This comment...
    "Note that we don't actually run the HSS engine at 44.1 kHz, it's still running at the default 32 kHz.
    We end up duplicating those samples as necessary to generate a 44.1 kHz output signal."


    Why isn't the HSS engine run at 44.1khz or 48khz? If were outputting the signal as 48khz, shouldn't the engine run at the same sample rate?, instead of duplicating samples?

    Is this due to prop limitations, or the fact that the engine needs to be recoded to get higher khz?

    I am still amazed that this little 7$ chip is putting out digital audio which is also being generated on the fly in the same chip.


    And my last question.
    Can other cogs, or hub interaction cause minor (audible)speedups and slowdowns in the playback of .hmus files? (if you play fl-sytrus.hmus, you will notice around 3 seconds into the playback, the bassline drops a bit, and the tempo seems to also slow down a bit when more sound samples get mixed into the playback.... I wonder if this is due to code, or is the way the .hmus file is made?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.

    Post Edited (BPM) : 6/3/2009 10:27:57 PM GMT
Sign In or Register to comment.