Just brainstorming.
Do you think one could implement a Sound like an shoot, or an explosision in a Pic?
Let's say a table which contents the 300ms duration of Noise?
A shoot is no more than a white noise whose Amplitude is going fast high and somewhat slower down.
Or is generating noise on the fly and changing the Level of Output as envelope possible?
But what can be the Output device? a micro Loudspeaker would sound like a toy. I need something impressive.
Just brainstorming.... How big must a speaker be to sound not like a toy?
I can not use a megaphone which is a way to big
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I did some experiments on this a few years back, it can be done if you have a chip with enough memory, the speaker on a hardware PWM pin, and can tolerate sound that isn't great.
Using a data table is the best way to store the sound. I used Audacity to convert the sound to 8 bit, 8000 Hz sampling rate and to save that as a raw file. Back then I had to use a hex editor to extract the values and format them correctly for a data table, but now you should be able to import the raw audio file with something like this:
Table MySoundTable from "soundfile.raw"
Then, I used this code to play it:
#define HPWM_FAST
Dim Length As Word
Dim Pos As Word
ReadTable MySoundTable, 0, Length
For Pos = 1 to Length
ReadTable MySoundTable, Pos, Sample
HPWM 1, 50, Sample
Wait 90 us 'For 8 kHz
Next
Since the PWM frequency is a constant 50 kHz, some calculations can be skipped each time HPWM is called. Adding the constant HPWM_FAST does this, and makes HPWM work fast enough to produce audio. The rest of it is hopefully fairly clear - get the table length, then output the appropriate signal level for each sample. The 90 us delay was found through trial and error, but you could calculate how long each sample needs to be used for (1/sampling rate, then minus the time for the code to execute).
I used an 18F26K22 running at 16 MHz for this code, but I think an 8 MHz PIC could probably just do it. The biggest limitation is memory, with an 8 kHz sampling rate you need 8 kB of memory for a second of sound, so even with a relatively large program memory you'll only get a few seconds in. You may also be able to try a higher sampling rate, it may sound better.
For the hardware, you might want to add some sort of filter to remove the 50 kHz PWM signal and leave you with just the audio. I used a 1k resistor and a 10 nF capacitor to create a basic low pass filter with a cutoff around 15 kHz from the PWM pin on the PIC, then an op-amp as a buffer to connect that to the speaker.
Can't help with the not sounding like a toy part, sorry!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
But this are great news and I will give it a try. My 18F25k22 has about 50% Memory left, so why not?
In fact it's for a toy for adults, no, not that thing, but for a NERF Gun
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can get some impessive noises from a pic if you amplify it.
Can anyone remember sinclair spectrum manic miner. The spectrum sound was just a port, pin, that went on or off so interrupts were used to turn the port on/off and play a tune while the game ran.
Try sending random to a pin and you get hiss/white noise.
Sounds worth trying for fun. doh!...re,mi,fa
Just brainstorming.
Do you think one could implement a Sound like an shoot, or an explosision in a Pic?
Let's say a table which contents the 300ms duration of Noise?
A shoot is no more than a white noise whose Amplitude is going fast high and somewhat slower down.
Or is generating noise on the fly and changing the Level of Output as envelope possible?
But what can be the Output device? a micro Loudspeaker would sound like a toy. I need something impressive.
Just brainstorming.... How big must a speaker be to sound not like a toy?
I can not use a megaphone which is a way to big
I did some experiments on this a few years back, it can be done if you have a chip with enough memory, the speaker on a hardware PWM pin, and can tolerate sound that isn't great.
Using a data table is the best way to store the sound. I used Audacity to convert the sound to 8 bit, 8000 Hz sampling rate and to save that as a raw file. Back then I had to use a hex editor to extract the values and format them correctly for a data table, but now you should be able to import the raw audio file with something like this:
Then, I used this code to play it:
Since the PWM frequency is a constant 50 kHz, some calculations can be skipped each time HPWM is called. Adding the constant HPWM_FAST does this, and makes HPWM work fast enough to produce audio. The rest of it is hopefully fairly clear - get the table length, then output the appropriate signal level for each sample. The 90 us delay was found through trial and error, but you could calculate how long each sample needs to be used for (1/sampling rate, then minus the time for the code to execute).
I used an 18F26K22 running at 16 MHz for this code, but I think an 8 MHz PIC could probably just do it. The biggest limitation is memory, with an 8 kHz sampling rate you need 8 kB of memory for a second of sound, so even with a relatively large program memory you'll only get a few seconds in. You may also be able to try a higher sampling rate, it may sound better.
For the hardware, you might want to add some sort of filter to remove the 50 kHz PWM signal and leave you with just the audio. I used a 1k resistor and a 10 nF capacitor to create a basic low pass filter with a cutoff around 15 kHz from the PWM pin on the PIC, then an op-amp as a buffer to connect that to the speaker.
Can't help with the not sounding like a toy part, sorry!
Hugh,
How does that .raw file get incorporated into the code?
Can that be used with an simple text file to produce a table or just that file format?
The instruction will handle any ASCII format file. The Help shows other examples.
But this are great news and I will give it a try. My 18F25k22 has about 50% Memory left, so why not?
In fact it's for a toy for adults, no, not that thing, but for a NERF Gun
You can get some impessive noises from a pic if you amplify it.
Can anyone remember sinclair spectrum manic miner. The spectrum sound was just a port, pin, that went on or off so interrupts were used to turn the port on/off and play a tune while the game ran.
Try sending random to a pin and you get hiss/white noise.
Sounds worth trying for fun. doh!...re,mi,fa
Last edit: stan cartwright 2017-10-22