![](/uploads/1/2/7/3/127342314/415680540.jpg)
Hello,I was hoping if someone could give me some guidance on the following.I have a piezo speaker connected to Arduino, and I can play homophonic tones on it using code e.g. Like PlayMelody sample here. Everything works nicely.Now, to play my own kind of tunes, I have captured audio in PCM.wav format, which is (nearly) perfectly homophonic. For an example, see the audio here: should one go about converting such PCM.wav file to a PWM sequence of pitch-duration pairs so that it could be reproduced on a piezo speaker?
For homophonic audio such as in the above Outrun tune which was originally played back on a PC speaker, such conversion could be more or less perfect(?) (For arbitrary PCM data, it can of course sound very gross and approximate)I've tried searching for algorithms/tools/discussion on how to write such a converter, but I'm somewhat new to audio, and have not been able to quite figure out what this kind of conversion would be called and what to search for. Oh, sorry, let me clarify.The reason why I am not trying to trying to push the PCM data to the chip is that PCM data takes up a lot of memory, e.g. If I have ten seconds of a square wave in PCM wave form in 8-bit 8kHz, it takes up 8000samples/sec. 1byte/sample.
10seconds = 80000 bytes, whereas in PWM tone-duration pair form, one can use just two bytes to encode the same information, first being the tone frequence, and second being the duration (e.g. In seconds or 1/16th of seconds or similar). That's why it would be desirable to have a sequence of pitch-duration pairs of PWM data instead of PCM to store the audio, and why I'd like to do it offline. I just don't know what would be a proper algorithm to do this kind of conversion? 'whereas in PWM tone-duration pair form, one can use just two bytes to encode the same information, first being the tone frequence, and second being the duration (e.g.
Edit: I was focused on the PWM to frequency question. Last year, I used what was effectively the PWM duty cycle from an AS5048A to drive a stepper in quarter step mode. That was simply based on the ratio between the number of steps per revolution of the stepper motor and the number of counts/PWM percentage of the encoder. Arduino I2C to PWM converter. Greetings, For a while now, I've had one of my MKs (Mikrokopters) set up with Turnigy Plush 18A ESCs, using Arduino boards to do the I2C to PWM conversion. The programming was so easy that I couldn't resist trying it, and was surprised to see that the ESC didn't seem to have any problems with the 500 Hz update rate.
Vatsyayana in tamil pdf download. In seconds or 1/16th of seconds or similar).' Maybe you don't understand what PWM is.It does not allow you to define the tone (frequency), it allows you to define the ratio of the high part of the wave to the total wave. 100 means the high part will be 100/255 of the wave. The frequency is set at about 500hz.Maybe you want to look at tone.
Clbri,I don't think anybody is angry. Maybe just grumpy.
![Pwm to voltage converter circuit Pwm to voltage converter circuit](http://www.thanksbuyer.com/image/cache/data/201610/51039/1476158877-3-200x200.jpg)
I think he's trying to make sure he has your attention.You are confusing a couple of things. If you use something like PlayMelody it takes no more memory to generate PCM than to generate PWM. It just takes different hardware.There's a difference between generating a sound and storing a sound. If I store a 1kHz tone on an audio CD, I can store store a little more than an hour of sound and it takes about 700MB. But, it only takes a few bytes to store a 'message' that says '1Khz, 24 hours'.You could write a little program for the Arduino that plays randomized melodies for year and it would take no more memory than a program that plays a song for 1 minute.
It doesn't take much memory to generate a randomized series of notes or loop your program (with randomized changes every time through the loop).A practical example is a file, which contains 'notes & timing' (and some other information) and is MUCH smaller than a digital audio recording. (A MIDI file can be very high quality depending on the quality of your virtual instruments but it can't generate vocals/lyrics.)In practice, I think PWM requires a much higher sample rate than PCM (for the same quality). I'm not an expert on PWM and philosophically, there's no more information so it shouldn't require a bigger file but it practice, it might require a bigger file. But, nobody stores audio in PWM in files. There are a couple of formats similar to PWM, and class-D amplifiers convert analog to high frequency PWM. (Class-D amplifiers don't have any software or memory.)And of course, you have lossless and lossy audio compression.
FLAC (lossless) compresses PCM data and MP3 (lossy) works in the frequency domain which is a whole 'nother story.
![](/uploads/1/2/7/3/127342314/415680540.jpg)