Play 2 second audio with arduino

Hi,

I have a really small speaker connected to my arduino and I can make noises, play melodies... using the tone library. It's very easy and powerful. But I would like to play a 2 second audio of me saying a phrase like "NO, YOU ARE A BAD DOG". I can record it in windows and save it to wav/mp3 file. I would like to know how can I get this audio and convert to some "melody" I can play with tone library?

I dont want to use SD card reader, I dont want to make my arduino play songs... no, I want to embed in arduino ode this 2 seconds of audio in some constant so arduino play this 2 second audio whenever my dog come to a room inside my house that I dont want it to go inside.

Thanks!

(deleted)

Thank you so much man! I didnt know this simple thing was too complex for arduino.

Anyway, I searched at google for how to convert MP3 to 8-bit PCM audio and it looked not simple to me. So I provided you the file in the link http://quemfazsite.com.br/temp/nao-sai-dai.mp3

It's less than 1 sec, cant I set this audio to some constante instead of using EPROM? I am pretty sure some slots of my EPPROM are weared out cause everytime I try to use it I get arduino stuck or different problems. Could you tell me how to assign this audio to a variable and play it?

EDIT: I found this webiste http://g711.org/ but it only converts to 16bit PCM not 8 bit :frowning:

(deleted)

Ok I thought flash progem and epprom were the same thing I didnt know it was different things. If it gets saved to the memory of arduino (not the epprom) I think it would be nice to know how to save that audio to a constant and play it.

Wow thanks so much, I will take a look at that as soon as I get home :slight_smile:

Just one thought: why is that so hard to reproduce a voice using arduino? A voice isnt just a sequence of tones played one after another very fast? I mean, if I could make something like this very fast in arduino:

tone(100);
delayMicroseconds(1);
tone(350);
delayMicroseconds(1);
tone(88);
delayMicroseconds(1);
tone(2909);
delayMicroseconds(1);
tone(1320);
...

It should work like a voice speaking right? Of course, the delays and the values of tone above should match my audio/voice sample, of course. But a sound of a voice isnt just many tones being played shortly? Should I be able to reproduce any sound of the universe using the code above? Why it does not work?

Just get this and be done in one eveving.

Then later you can use it for other things after the dog's been trained.

-jim lee

You can halve the sample rate to 4KHz from 8 and halve the storage space required. For voice, you could probably get away with an even lower sample rate. It won't exactly be high fidelity, but it'll be recognizable.

(deleted)

batata004:
Just one thought: why is that so hard to reproduce a voice using arduino?

  1. No proper analog outputs (mostly)
  2. Not much storage
  3. Not much processing power

Audio is the exact kind of application that microprocessors aren't good at.

A voice isnt just a sequence of tones played one after another very fast?

Even if it was, you'd have to store all those tones somewhere, somehow, and recall them all really fast, then send them to the right kind of output.

BTW, it's more like a varying array (a spectrum) of simultaneous tones played one after another really fast.

@jimLee in brazil I cant find that expensive module, and even if I could find it it would cost at least US$ 100. I am actually using an ultrassonic sensor to measure a distance between two close walls and if my dog passes between those 2 walls my alarm rings. It is working really fine and the dog gets scaried with the loud sound and runs away, but after about 5 seconds the dog gets curious about the sound and comes back to exactly where it shouldnt be and pisses in my carpet. On the other hand, my dog hates when I say a very strong "NÃO" (no) and it respects my "NÃO" so it would be nice to have my speaker to say "NÃO"!

@BobKay when you say I could cut in half the size of the sound, would you mind telling me how could I do that? I could only find in the internet 8khz PCM converter, not any 4khz PCM converter. In one of those converters it outputed a raw file but I cant open it in notepad to extract the data to paste in my sketch. Could any of you tell me how to convert an audio to a string raw data?

Thanks to other people for pointing that arduino is not good to play sounds on its own, I will not keep trying even further to accomplish this, I will only try to play a 1 second sound and not bother you anymore.

And yet -- the TRS-80 Model I with its 4K ROM and 4K RAM was capable of playing back speech.

The TRS-80 used an off-the-shelf audio cassette player (CRT-41 specifically, although others would work) for program/data storage. The electrical interface was extremely simple; basically a series of digital on-off pulses with pulse width representing a "1" or "0". People discovered that by plugging the jack into a small speaker/amp instead of the cassette deck they could produce sound. Radio Shack even sold a program called "Micro Music" that was a sound synthesizer.

One could record voice on tape, read the tape through the cassette input as digital data, save the data, and play it back after unplugging the cassette deck and reconnecting the speaker.

I mention all this because if it could be done on a Model I, I'm sure it's possible to do this on the Arduino. I'm just not sure how I'd go about it.

@batata004 -- you'd have to write your own routine to halve the frequency. You'd need to know the data format, write something to read it, manipulate it, and write it back out again. Note that you could also downsize the sample size from 16 bits to 8 or even 4. Not an easy beginner's project, but eminently doable.

Crossroads did an Arduino audio project years ago. Sound quality wasn't great but it did work.

Dog will probably know the difference but being a dog, it's going to mess with him. You can't make it smell like you.

But I would like to play a 2 second audio of me saying a phrase like "NO, YOU ARE A BAD DOG".

That bit is fine. You can get about 3.5 seconds of audio inside the flash memory, see https://youtu.be/WShVFcrFpwU, no extra hardware needed.

I didnt know this simple thing was too complex for arduino.

No what you didn't know was how complex this seemingly simple thing actually is.

Yes @jurs I wanted exactly that file! The file you generated should be a string, right? A string that I can copy to my skecth, correctly? Would you mind pasting that string into http://pastebin.com? Thanks!

Thank you @jurs but the problem so far is that I tried my best and looked around but I didnt find any tool to convert a MP3 audio to string in the format:

const unsigned char sounddata_data[] PROGMEM = {128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, .....};

Do you recommend any tool? I still cant find an easy way to convert MP3 audio to the string format above.

Hi, thanks !!! I will use from now on audacity to convert audio to 8-bit PCM. The command line c tool you provided, how can I use it in my Windows? What parameters should I set in the command line in order to output string format from the 8bit PCM audio?

Regarding the player, I think I wont need you to spend much more time into this. I found this player Arduino Playground - PCMAudio which looks good and easy to use, just change the variable of the audio and it should work, right?

Ok, I thought when you said it's command line I should execute the program using parameters like:

file.c --output-directoy c:\teste --output-filename teste --source-file c:\teste\lkj.wav ...

I didnt you I should execute the file in my windows. When I get home I will give it a try, now I am in a company computer and it has lots of blocked programs.

thanks!

Buy one of those birthday cards that let's you record a message ($10?), and hack the switch that triggers the message when the card is opened.