How can I generate PWM out from a .WAV file?

Hi All,

Ho can I generate PWM out from a .wav file stored in 2 MB SPI Flash storage chip in adafruit metro M0 express board? This board is supported both circuitpython and arduino. When used in Arduino, we can read/write files from 2 MB SPI Flash memory, like a little datalogger or SD card. When used in circuitpython the 2 MB flash acts as storage for all your scripts, libraries and files. See the link-Overview | Adafruit Metro M0 Express | Adafruit Learning System

Anyone can share your idea or give me suggestion how can I generate PWM out from .wav file either arduino or circuitpython. I am new in both, so wondering someone give me response of my query.

Thank You

khossain:
Hi All,

Ho can I generate PWM out from a .wav file stored in 2 MB SPI Flash storage chip in adafruit metro M0 express board? This board is supported both circuitpython and arduino. When used in Arduino, we can read/write files from 2 MB SPI Flash memory, like a little datalogger or SD card. When used in circuitpython the 2 MB flash acts as storage for all your scripts, libraries and files. See the link-Overview | Adafruit Metro M0 Express | Adafruit Learning System

Anyone can share your idea or give me suggestion how can I generate PWM out from .wav file either arduino or circuitpython. I am new in both, so wondering someone give me response of my query.

Thank You

I have no idea what you are proposing. EVERY file, no matter the "type", consists of bytes that are 8 bits long. Your Arduino will read ONE byte at a time from your file.

What is it you want your Ardiuno to do with that byte?

Paul

khossain:
Anyone can share your idea or give me suggestion how can I generate PWM out from .wav file either arduino or circuitpython. I am new in both, so wondering someone give me response of my query.

Thank You

Map the frequency range of the wav file to the range of the pulse width.

Paul_KD7HB:
I have no idea what you are proposing. EVERY file, no matter the "type", consists of bytes that are 8 bits long. Your Arduino will read ONE byte at a time from your file.

What is it you want your Ardiuno to do with that byte?

Paul

I am proposing how to generate pwm out from .wav file. I have a .wav file but need to play as PWM signal not play as audio signal.

Thank you for your response.

Idahowalker:
Map the frequency range of the wav file to the range of the pulse width.

Thank you for your reply. Do you give me some link where I will get some information regarding this issue.

How about read the .wav file one byte at a time and 'analogWrite()' each one to the PWM output pin?

its more complicated than you think. first of all wav files have headers. the header is a number of bytes at the front of the file that contains info about the format. so you would want to skip ahead of the header before playing.

wav files com in different types and quality. this is the type of info contained in the header.

most wav files are going to have 2 channels of audio. left speaker and right speaker.

also each channels sound samples are not usually represented in one byte most recordings are going to be a higher fidelity than 8 bit. you pry would need to add sets of two or three bytes into an int to get the real pulse voltage value.

so in another words, you would need to read sections of 6 or eight bytes at a time. the first half of every section would be the left speaker and the other half of the bytes would represent the right channel.

also you would need the sample rate. the sample rate would tell you the speed you are sopposed to be playing your sound.

There is a library out there for this. i used it a while back. it works for low quality sound. its a little rough but it works. you should be able to find it with a quick google.

gfvalvo:
How about read the .wav file one byte at a time and 'analogWrite()' each one to the PWM output pin?

gfvalvo:
How about read the .wav file one byte at a time and 'analogWrite()' each one to the PWM output pin?

Thank you for your ideam. I am new on microcontroller so I don't have experinece too much. I will try that you suggest. Can you give some example/link where i will get more information if you have?

Thank you

khossain:
Thank you for your reply. Do you give me some link where I will get some information regarding this issue.

Perhaps there does exist internet information about this issue but its something off the top of my head.

Consider that you want to play audio from xLowHertz to xHighHertz and that your PWM is from xPWM_Low to xPWM_High. Those numbers ranges have a mathematical relationship to one another which can be calculated and transformed.

You may find a useful function called map(), try entering into your favorite internet search engine the words "c++ map".

here are some specifacs about wav file break down

http://soundfile.sapp.org/doc/WaveFormat/

if you really want to persue this i would reccomend using something like audacity to format all your wavs down to a matching low quality so you don't have to pick up on the differences between tracks in runtime.

i believe that arduino's analog out pins have a fidelity of 0 to 255. so you wouldnt be able to playback more than 8 bit fidelity without some external module anyways.

as a sidenote,

if you recorded a series of bytes off a pin at a specific speed and simply played them back at a specific speed hooked to an amp and speaker you could pry replay a recorded sound:)