PCM Playback Not Quite Right

So I have this job I'm doing for a client who wants "better quality audio" out of a children's toy, which with the current hardware there's only so much of an improvement I can make. Nonetheless, I majorly upped the sample rate and got significantly better quality. However, it seems that the sample is playing slower than what it's supposed to play at.

It's simple: a button is pushed on Pin 9 and a sound is played on Pin 11 through a 2N3904 for maximum loudness.

I've been trying to modify this to use timers instead because I'm going to assume that's probably going to be more along the lines of what needs to happen to make this work right. But I'm throwing this out for thoughts. Maybe someone can offer insight because honestly the Arduino IDE is not my first choice for writing MCU code, so I'm lacking in skills with it. Thanks!

PCM_Playback.ino (101 KB)

Holy cow. Can't you put the audio data into an include file?

dominicluciano:
So I have this job I'm doing for a client who wants "better quality audio" out of a children's toy, which with the current hardware there's only so much of an improvement I can make. Nonetheless, I majorly upped the sample rate and got significantly better quality. However, it seems that the sample is playing slower than what it's supposed to play at.

It's simple: a button is pushed on Pin 9 and a sound is played on Pin 11 through a 2N3904 for maximum loudness.

I've been trying to modify this to use timers instead because I'm going to assume that's probably going to be more along the lines of what needs to happen to make this work right. But I'm throwing this out for thoughts. Maybe someone can offer insight because honestly the Arduino IDE is not my first choice for writing MCU code, so I'm lacking in skills with it. Thanks!

Don't know if this will help you, but I recently built an R-2R ladder DAC (see diagram). Using it on my MEGA2560 board, I can get about 30 seconds of really decent quality audio at 8000 hz sample rate (or any combination of time times sample rate that fits into the flash memory).

Only problem is, you cannot make a .h file with the data - you have to take the raw audio data and convert it into one huge .hex file and upload it separately at a known address, then use "pgm_read_byte_far" to access all of it and dump the data to the port.

Attached is the source and a Windows CONSOLE MODE executable for the binary to hex converter.

The way to use it is to take your audio file (an 8 bit WAV file without a header) and convert it to a hex file:

[b]bin2hex raw_audio.wav raw_audio.hex 0x1000
[/b]

the "0x1000" at the end of the command line is the load offset (you can use any value you wish) - the idea is to place the load address above your audio playback code. Say you wrote code like this to play the audio:

    #define DLY (1e6/8000) // delay for an 8000 hz audio sample rate
    uint32_t x;
    DDRK = 0xFF; // all 8 bits as outputs
    for (x = 0; x < audiosize; x++) {
        PORTK = pgm_read_byte_far (0x1000 + x); // send the audio data to the port
        delayMicroseconds (DLY);
    }

Now, build the sketch and see how large it is, then adjust the "0x1000" constant up or down as required to load the audio right after the sketch code (make sense?).

Hope this is of some help to you.......

bin2hex.zip (21.4 KB)