Getting Waveplay to work

I first tried to get WavePlay from  Vlad Cazan to work. I couldn't. Here's what I found out:

First, I was reading from the mbed internal USB memory (what is it, flash?), so I didn't need any of Vlad's SD card library. That was easy.

Then I had to get some audio samples to play with. I looped out my PC's audio back into the line input and recorded with windows' built-in "Sound Recorder". The great part about this tool is that you can specify the sampling rate, number of channels, and bit depth really easily. Just remember to save the original, because once you save as a lower depth, you can't get it back.

I worked at 8000 samples per second, mono, and 16 bit depth. That was the least I could still hear intelligbly the audio at.

I got noise from Vlad's software. Then I found someone else had made some changes, and it was better, but still no good. In particular, remember there is a difference between AudioOut.write and AudioOut.write_u16 !

So I started from scratch. The header information is pretty easy to bang through. There was an extra 2 bytes somewhere in my header and there was a funny chunk called "fact" where I expected "data", but I just ignored it.

Then getting the cast correct was hard. My malloc'd array was just single bytes (chars), but the audio was 16 bit. So I did this:

int data = ((short*)(sound_buffer)[DAC_ptr]) + 32000;

because, remember, audio is stored as 2's complement in WAVE format, but we need 0-65535 unsigned AnalogOut.

Then I ran out of memory on the mbed. I could only malloc 16000 bytes at a time, but the audio sample was much, much longer. If I tried to malloc the entire chunk size, the program would freeze with no warning to me.

So you have to double-buffer. I see that Vlad is using a 0xFF rolling buffer, but I don't understand how, exactly. I just created two 8000 byte buffers and loaded one while one read. Whatever. I never got it working, and I think I understand enough now just to move on. I can play ~1/4 second samples with long pauses between while the buffer refills. :)

One of the key debugging tools is to printf your array of data and capture it with Hyperterminal out to a log file, open it in Excel, and line-plot it. Audio has a very distinctive waveform. You can't printf while playing audio (too slow?), but uncomment it when you want to check where you are!

 

Later,

-Owen Piette

 


0 comments

You need to log in to post a comment