Skip to content | Skip to navigation
Powered by RSPowered by RSPowered by RS

mbed now does audio

d_worrall

United Kingdom

The mbed has a hidden I2S port, it is only now that they are making it fully public on the new demo board, soon to be released by RS Components. I investigate with the TLV320AIC23B Audio CODEC.

mbed audio board

The mbed is capable of a lot of things, many of which have not yet been published on the internet. One of those things is that the mbed possesses a high quality audio port called an I2S port. This supports various sampling frequencies, word sizes, wiring layouts, master/slave configurations, mono/stereo output, clocking speeds etc. So it’s highly flexible and most of all enables us to add an entirely new dimension to the mbed libraries.

The I2S port also supports interrupts and DMA requests. I’ve chosen to write a class with just the interrupts, since mbed does not yet supply any DMA functionality to any of its classes. Otherwise check out Andy Kirkham’s DMA library. For the really keen, why not try and write your own abstraction libraries? It’s a fantastic way to really understand how some of the mbed libraries work!

Anyway, let’s talk about the TLV320. It’s a great little audio CODEC and DAC, which like most hi-spec chips has n different configurations and takes 2n hours to set up! So, in the usual mbed style I’ve tried to simplify the number of configurations, so that you’re left with something that just works. Luckily, mbed are releasing a new demo board through RS Components, which should be available to buy in the not too distant future. This has a built-in TLV320 with all the extra hardware done for you.

TLV320AIC23B

Despite that, there are still a couple of things to set up. Why? Because audio files come in a variety of flavours. So what does the mbed user need to specify before using this device? Well I’ve whittled it down to the following:

  • word width
  • mono/stereo mode,
  • sample rate
  • devices to power

These can all be loaded from the header of the file, so you could have a program which parses the header and automatically updates the TLV320 and I2S port settings as per the file.

The I2S port uses a FIFO buffer for both input and output. Each FIFO is 8 x 32-bit words deep and at a certain depth an interrupt is triggered. Upon interrupt it’s up to the user to do something with the audio data. Note the FIFOs have to be cleared in order to escape the interrupt service routine.

PROJECTS

This is really what the mbed is about – projects! So here are a couple.

1. Simple playback – by creating a circular buffer, it is possible to load data continually into some flash from, say, an SD card and then feed it, 8 words at a time to the I2S port. If you have CD quality tracks, you definitely get CD quality playback! I used several of my own .wav files with 16bit stereo at a sample rate of 32kHz, so just sub-CD quality, but it still sounds good :-)

 Circular buffer

If you've never encountered circular buffers, it's basically a buffer which loops back on itself (like in the diagram), such that you can continually write to it and read from it. If this is not to your liking, you could use a system of double buffering, or even triple buffering, which is where you constantly switch between buffers in synchrony, such that you write to one whilst you read from another. I prefer the circular buffer though, just because I think it's elegant . You can find my program here.

Okay, playback is a bit samey. My iPod can do that. What can my iPod not do? Well it can’t do text-to-speech synthesis. Sound tough? Well, yes, but there’s a nice little cheat which you can use to get around this and strictly speaking this next project is not text-to-speech, but more numbers-to-speech.

2. FTSE100 readout – you may have read some of my previous blogs, in particular one about displaying the FTSE100 on an alphanumeric display. I decided seeing the FTSE100 at work every day was not enough to satisfy my addiction to real-time data, I needed to cover more senses! So I got the old FTSE100 display program and after a little jiggery-pokery I managed to rearrange the program. It parses the value of the current FTSE100 into six digits and a decimal point and calls an audio file corresponding to the value of the digit or decimal point. All in all I recorded 12 files; ten for the numbers 0-9, one for the decimal point and one to introduce the FTSE100. Neat!

PHP logo

The program takes advantage of a PHP script, which I uploaded to a bit of web-space. It looks at this website and in the top right hand corner, there's a little number, which is the current value of the FTSE100 delayed by 15mins. Every time you poll the website where the script is stored, it in turn polls the Yahoo Finance website and then parses the source code for this number, which is possible because the number is wrapped inside two distinct HTML id tags. So the mbed then parses the source code of my own webpage, but only needs to read one line, so programming-wise it's easier for me. As I wrote in the previous paragraph, it splits the number up and loads the appropriate files, which I named 0.wav, 1.wav, 2.wav ... 9.wav and then there's one for the decimal point and one for the introduction. Watch the video!

 

The program is here.

3. Recording - So, I've pretty much covered reading from files, but I haven't looked at writing them. The audio board, which I’m using requires a powered input, say my iPod, mobile phone, pc, anything that could be used with a pair of headphones really. I've also decided that it may be easier to write to a USB stick, so I can then transfer my audio files onto my pc without too much SD Micro to USB converter bother.

The internal RAM on the mbed is far too small to store an audio file and then write it to memory after recording has finished, so I run a quick diagnostic. It turns out that I'm limited to a sampling rate of 32kHz, but then I find every 66 writes of 512 bytes the USB system hangs for 0.2 seconds - I assume this is to do with internal memory allocation/buffer shunting etc. Anyway, in this amount of time I would fill 40% of my total RAM! Okay, that's not a problem, but I have to be able to empty that onto the USB stick before the next long wait as well as all the data which streams in during the periods without the hang.

 LPC1768

But the write speed is just as fast as my input data speed, so I can't run at 32kHz, I'm limited to 8kHz...bother! Nonetheless, this has the potential to still sound better than some of Dad's old BeeGees cassettes (yes, he still listens to tape casettes!).

My record data will come from my pc, I can run a BBC Radio1 pop-out and connect the audio output from the computer to the audio board via a male-male TRS Jack.

Something, which I'm going to have to do is populate a header. I only need the following info:

  • Sampling rate
  • Word width
  • Number of channels

I found that the rest of the data is either fixed for this kind of file, arbitrary or just a function of those three parameters.

The final program can be found here.

 Record

 

I'd be intrigued as to if anybody could get a program to record at CD quality i.e. 44.1kHz. I know how it would be done, but my knowledge of file systems is a bit weak. Feel free to comment :-) Also, here's some links to my mbed posts:

TLV320 

I2S


 

 

 

Comments

R_Phoenix

United States

39 weeks ago

Where is that eval board available? (the one the mBed is plugged into)

 

Edit; RS Components nm

 

Gerrit Gehnen

Germany

40 weeks ago

Can't wait for the audio board.

Do you only sell the boards or do you release the design files.

I have the TLV320 as sample already in my dry-storage....

Related Pages

26 May 2011

While in the previous post we describe the main idea of the HD Audio together with the DAC se ...