Photon play sampled music (wav file) using DAC

This is the sound I’m hearing:
https://drive.google.com/open?id=1bNEC3RiCswO9UR_VDpXrtfP0QRCbs5Bv

This is my sound that I was able to hear correctly (sounds like the start of a doorbell):
https://drive.google.com/open?id=1JMgE_VuntZjQqfItSi69eEi8qD5h2YYm

Your sound data is only a bit over 10% of the length of the demo sound in the library, so given that you said this is 20% of your original data the length of the sample shouldn’t be the issue.

Can you post the entire sound?
Can you also provide the exact type of your amp module?

Not sure of link allowances… This is the amp that I’m using:

Here’s the entire sound I’m attempting to load. I’m certain that it’s too big.

And here is my rough circuit design… Apologies…
Photon%20Wiring

I’ve also tried an 8 ohm speaker now, with the same results as before… I really don’t know why the sound isn’t playing correctly, unless it’s a compiler issue of some sort.

I’ve been compiling this using the Desktop IDE. Is there a preferred method of compiling by chance?

What sample rate was that WAV file? It’s huge (670KB)
Was it definetly mono?

That entire file seems to be sampled at 44.1kHz which is definetly overkill. You won’t get CD quality out of a Photon.
22050 Hz should be more than enough and will immediately half your data.
Next trim all the lead-in and lead-out off your sound, you don’t want to waste space for more or less silence.

If you don’t see yourself doing that you can also post the WAV file itself and I’ll try what I can do.

Good catches. I thought that I had accounted for all that when I made my RAW file, but apparently not. I re-created the file now and it’s 1/6th the size. Here’s a link to the new audio:
https://drive.google.com/open?id=1rm9hUMz0da94YI5riORC1trFQM6xM0BS

Oddly, when referencing this file, the compiler gives the message “Compiler timed out or encountered an error”, which I’ve been seeing a lot of while working on this. I’m not sure what else to try, besides continuing to click on “Compile and Flash” and hope for the best.

Update: I tried the Web IDE instead and this time the compiler finished and flashed the photon fine. Sadly, the audio was still the same as before. The only difference was a larger gap between the loops.

When I flash this I can hear the DingDong quite clearly.
https://go.particle.io/shared_apps/5dcec7212fa0050009259a7b

1 Like

Amazing! I figured it had to be my code. Once I changed it to your code it worked perfectly as well. I had coded it per your post from April 2018, using a different initialization for the Speaker var and using the memcpy function.

Now I need to figure out how to stream the audio from the SD card to the DAC pin… That will be another rodeo.

Thank you!

In order to stream it from SD you’d need to get my code from above (plus some SD stuff and buffer swapping) and read the data in smaller chunks as you won’t have 100+ KB RAM to fit as many samples in.


Update:
I like a useful challenge, so I couldn’t resist going ahead and came up with this
https://go.particle.io/shared_apps/5dd021a464486e00058d84ac
particle-speaker/playWavFromSD.ino at master · monkbroc/particle-speaker · GitHub

This should take “any” 16 bit WAV file stored in the root directory of an SD card, parses the header and plays its left/mono/primary channel via the speaker library on the DAC pin.
To select a file you’d send the file name (including file extension) to the exposed Particle.function("playWav"). To see what files are stored on the SD you can send dir

Hey guys, apologies again for reviving this old thread, but it seems the app you wrote isn’t available anymore, @ScruffR. I’m making a photon based chime doorbell as well, and I want it to have multiple sounds, so the SD card app would be great to have! Could you share the code again?
Edit: the code is available on github: particle-speaker/examples/playWavFromSD at master · monkbroc/particle-speaker · GitHub

1 Like