I am working on a hobby project to generate sound effects with 89c2051 using PWM. I am able to generate keyboard notes with it by generating pwm signals of different frequencies. But I am at loss as how to generate other effects like car crashing,bird chirping,machine gun sound etc. ?
To do that you would need to record those sounds and play back the samples (under Windows these would be stored in a .wav file) using 1-bit PWM. Haven't done it myself, but I've seen C source code which plays sounds through the PC speaker, which uses similar technology. Google for that.
Not really required. For a chirping bird, for example, try a couple of short bursts of a high-pitched tone followed by a slightly longer burst of a lower tone, the whole sequence repeating four or five times. A machine-gun-ish sound could be two bursts in rapid succession, low and lower, and repeat.
I'd also recommend at least considering linear predictive coding on occasion, using one or more individual sets of coefficients and maybe even sometimes not bothering to keep every bit of data (residuals, starting values); plus huffman coding can be applied at times, as well.
But the basic idea is that there are many different useful methods and people have probably found uses for _more_ than all of them. ;)
Some ideas for search terms: Digital sound synthesis, fm sound synthesis, adsr envelope. A nice program for experiment with different ideas before implementing it in a microcontroller is Csound (
) But creating a realistic car crashing sound would be difficult in software, maybe a simpler solution would be to attach a SD card to your microcontroller and just play some samples.
But if you like to do some really challenging, you could try to implement a realistic sound synthesis in software with your MCU. At least on PC it is possible to create a nearly raytraced looking realtime 3D animation of some snowy mountains with cool music in just a 4k program and no other files (yes, that's really just 4096 bytes :-)
Of course, the 3D functions of the OS are used for displaying the landscape (which was nevertheless calculated by the program), but I assume the sound was created by sound synthesis by the program.
Frank Buss, email@example.com
I do not understand this. could you please explain the process to do this. I do not want to use a DAC. Can i implement what you are suggesting with just pwm? Currently I am generating square waves of 50% duty cycle with different frequencies to produce different notes of keyboard.Do you mean i must also vary duty cycle of the pulses.
One other problem which I am facing is that I am unable to find for different sounds as mentioned earlier what is the base frequencies for the sound effects. For example how to find out for crashing sound what is the base frequency which has to be applied in decaying fashion.
You need to 'detect' peaks and then use those peaks to control gain. A very simple gain formula (or a more complex one -- you get to decide it) can use that value together with a 'control' value. You will set up some constants to use in your gain formula to set the minimum and maximum gains and scaling, etc. (Simple algebra, but something along the lines of 1/(K+L*control) where you pick K and L.)
A fast attack/slow decay approach makes an assumption that high peaks are likely to be followed by more high peaks and that it takes more than just a few low peaks to assume a quiet phrase. You get to pick the times (or filter coefficients) for that attack and decay.
For a fast attack/slow decay, the control signal might be handled:
if peak is less than or equal to the control value, set control = control + 0.2*(peak - control) else (if the peak is greater than the control value), set control = control + 0.9*(peak - control)
Here, it's about adding 90% of the difference when the peak is higher and subtracting 20% of the difference when the peak is lower. Obviously, you get to pick the 90%/20% values as you like. Or use a more complex formula. It's your choice, really.
How often you update this control value is your choice, too. Experiment.
Also, I suggest you use google or some other search engine. It really wouldn't have been hard for you to use the words Vladimir already gave you to look up much. If you are unable to pick out what is important to read from that point on, you are probably too far behind the curve yet to implement it. If you know enough to get close, then google would provide almost all of what you need, I think.
I'm frustrated that the OP didn't take your comments further before asking that question, but this comment from you also makes me think this is the primary reason you post. It wouldn't have been hard to go another inch or two for the OP.
You seem confused on what PWM is - you claimed to be using it, then state here that you always use 50% ?.
If you want to vary the amplitude, you need other than 50% - Varying the duty cycle is a cheap way to vary the energy, but it does also affect the spectrum - if you want to preserve harmonic shape, then you need an external means of Amplitude control. In a simple uC, you could use one PWM as a slower DAC for envelope shape, and a 2nd PWM driving an external SPCO Analog switch, to give frequency control.
Or, you can just move up in frequency, and use single PWM as a faster DAC, and use the uC to 'play back' a recorded waveshape. That will give the best sound-effect quality by far, and large flash memory really is cheap.