I'm interested in sound generation right at the moment. The specific task is
building a metronome that has a decent sounding, high volume "TOCK" sound.
But I figure since there are quite a few issues in terms of generating audio,
that I may as well examine them for this project. My thoughts:

1) My goal is to use some form of PWM for output. Presuming that I want to
generate 4000 8 bit samples/sec, would using each sample as the duty cycle
be sufficient. By this I mean if I have three successive samples of 64,128,
and 192, that for the first PWM cycle I drive the output high for 62 uS,
then low for 187 uS, a hi/low cycle of 125/125 uS, then a cycle of 187/62 uS?

2) Is it possible to configure the hardware PWM so that it changes the duty
cycle each cycle?

3) Where would I start in terms of the values for a low pass filter for the
PWM output?

4) I want to drive a cheap 8 ohm speaker. I've seen a couple of push/pull
transistor amplifiers, but it seemed they were digital in nature. Since the
output of the low pass filter is an analog value should I be looking at
a transistor amp? op-amp? LM386?

5) And finally the metronome question: Any pointers on how to generate a
quality TOCK sound?

It is possible to take a raw .wav file and playing it back by simply
writing successive values straight to the PWM register at the
sample frequency.

When I did this (it was a Philips 80c552 chip, but same principles
apply), I just used an RC filter and then AC coupled it into an audio
amp. I ignored the header info at the start of the .wav file and
played it like it was audio data, the result was just a slight click at
the start of the sound. I set the PWM frequency as high as it would
go. When not producing a sound set the PWM output to 50% (2.5V),
as the data in the wave file is also biased at "half way".

On my second attempt I used the PWM signal to directly control a
MOSFET H bridge driving a loudspeaker. This was quite a bit louder
(which is what I wanted).

On Wed, Oct 17, 2001 at 08:46:29AM +1300, Brent Brown wrote:
> BAJ,
>
> It is possible to take a raw .wav file and playing it back by simply
> writing successive values straight to the PWM register at the
> sample frequency.

That's why I love this list. A quick, great, easy to implement suggestion.

>
> When I did this (it was a Philips 80c552 chip, but same principles
> apply), I just used an RC filter and then AC coupled it into an audio
> amp. I ignored the header info at the start of the .wav file and
> played it like it was audio data, the result was just a slight click at
> the start of the sound. I set the PWM frequency as high as it would
> go. When not producing a sound set the PWM output to 50% (2.5V),
> as the data in the wave file is also biased at "half way".

Understood. After a quick read of the Midrange manual, I realize that the
highest speed I'll be able to run the PWM is 19.5Khz with a 20 Mhz Fosc.
The reason is that you start to lose resolution as you crank up the frequency.
Since the PWM is 10 bits and I'm not real thrilled at having to fight with
splitting the sample to store the lower two bits in the config register. So
I plan to zero out the lower two bits and store the sample in the upper 8 bits.

>
> On my second attempt I used the PWM signal to directly control a
> MOSFET H bridge driving a loudspeaker. This was quite a bit louder
> (which is what I wanted).

> Since the PWM is 10 bits and I'm not real thrilled at having to fight with
> splitting the sample to store the lower two bits in the config register.
So
> I plan to zero out the lower two bits and store the sample in the upper 8
bits.

Can anyone explain why uC people decided to put those last two bits in an
odd place? Even register banking would be simpler than stuffing the extra
bits where they belong in the current implementation. Register banking is
how they chose to address the last two bits in A to D so it looks
inconsistent from that angle.

Byron A Jeff wrote:
>
> I'm interested in sound generation right at the moment. The specific task is
> building a metronome that has a decent sounding, high volume "TOCK" sound.
> But I figure since there are quite a few issues in terms of generating audio,

> 5) And finally the metronome question: Any pointers on how to generate a
> quality TOCK sound?

Ever attached a 555 timer with a square wave
output to a speaker?? You get a real good tock
sound. :o)

If you output a single square wave pulse, driven
by one transistor, to the speaker through a 0.1uF
capacitor you will get a decent loud tock. Then you
can simply adjust the length of the pulse and this
will adjust the volume of the tock, over a pretty
good range too if I remember right.
-Roman

> 1) My goal is to use some form of PWM for output. Presuming that I want to
> generate 4000 8 bit samples/sec, would using each sample as the duty cycle
> be sufficient. By this I mean if I have three successive samples of
64,128,
> and 192, that for the first PWM cycle I drive the output high for 62 uS,
> then low for 187 uS, a hi/low cycle of 125/125 uS, then a cycle of 187/62
uS?

I haven't checked your numbers, but that's what PWM is all about. For PIC
PWM the period remains constant and the duty cycle is changed. You low pass
filter the resulting signal to recover the "average", which is the audio
signal. It's a cheap man's D/A. It works fine as long as the PWM frequency
is several times the highest frequency you want. This allows it to be
filtered out easily. I've done this several times.

> 2) Is it possible to configure the hardware PWM so that it changes the
duty
> cycle each cycle?

Yes, you can have the CCP interrupt every PWM period. This allows you the
opportunity to update the PWM output value.

> 3) Where would I start in terms of the values for a low pass filter for
the
> PWM output?

Run the PWM as fast as you can to still get the desired output resolution.
Hopefully this is several times higher than the highest desired audio output
frequency. Usually a 2 or 3 pole RC filter just above your output frequency
limit is all you need. This isn't rocket science - just do the math.

> 4) I want to drive a cheap 8 ohm speaker. I've seen a couple of push/pull
> transistor amplifiers, but it seemed they were digital in nature. Since
the
> output of the low pass filter is an analog value should I be looking at
> a transistor amp? op-amp? LM386?

I use an op amp driving 4 bipolar transistors in the HAL project. This
works very well for low quality sound.

> 5) And finally the metronome question: Any pointers on how to generate a
> quality TOCK sound?

This is usually done with a few cycles of a low frequency wave. You need to
experiment. Try something like 3 cycles of 50Hz, but there are lots of
choices for different effects. Simulate it on a PC with a sound card.

> When I did this (it was a Philips 80c552 chip, but same principles
> apply), I just used an RC filter and then AC coupled it into an audio
> amp. I ignored the header info at the start of the .wav file and
> played it like it was audio data, the result was just a slight click at
> the start of the sound. I set the PWM frequency as high as it would
> go. When not producing a sound set the PWM output to 50% (2.5V),
> as the data in the wave file is also biased at "half way".

Note that this WAV file hack only works if the WAV data is 8 bit monophonic.
In general this is a bad idea - don't try this at home. Reading a WAV file
properly is really quite easy.

>I think this is the case, but just to be clear...
>
>To play sampled sound using the PWM technique, you just do a simple
>linear mapping of the waveshape amplitudes (the samples) into the
>range 0-255 for the pulse width.
>
>Right?

As long as your integrating capacitor is fed by a constant current source
and you use the PWM output only to discharge the cap and not source current
into it, but rather, let the constant current source charge it. Should be
nice and linear that way. There may be simpler ways to do this though...

> > When I did this (it was a Philips 80c552 chip, but same principles
> > apply), I just used an RC filter and then AC coupled it into an
> > audio amp. I ignored the header info at the start of the .wav file
> > and played it like it was audio data, the result was just a slight
> > click at the start of the sound. I set the PWM frequency as high as
> > it would go. When not producing a sound set the PWM output to 50%
> > (2.5V), as the data in the wave file is also biased at "half way".
>
> Note that this WAV file hack only works if the WAV data is 8 bit
> monophonic. In general this is a bad idea - don't try this at home.
> Reading a WAV file properly is really quite easy.

Yes, 8 bits mono. No, it's a good idea - do try it at home. It is rough
and ready but the point was it's really easy to try and then you can
develop it further. Like you say it shouldn't be too hard to use the
PIC to decode the wave file header.

> Yes, 8 bits mono. No, it's a good idea - do try it at home. It is rough
> and ready but the point was it's really easy to try and then you can
> develop it further. Like you say it shouldn't be too hard to use the
> PIC to decode the wave file header.

I meant have the host properly interpret the WAV file, then send the
converted content to the PIC. WAV files are RIFF files, which are pretty
straight forward to interpret. Write a routine once that reads them and you
can use it for all future projects. Just dumping raw data may work under
the right conditions, but will come back and bite you sooner or later.

On Wed, Oct 17, 2001 at 06:03:58PM +1000, Roman Black wrote:
> Byron A Jeff wrote:
> >
> > I'm interested in sound generation right at the moment. The specific task is
> > building a metronome that has a decent sounding, high volume "TOCK" sound.
> > But I figure since there are quite a few issues in terms of generating audio,
>
> > 5) And finally the metronome question: Any pointers on how to generate a
> > quality TOCK sound?
>
>
> Ever attached a 555 timer with a square wave
> output to a speaker?? You get a real good tock
> sound. :o)

I took 2 minutes to test dumping a cap into a small speaker. I wasn't real
impressed.

>
> If you output a single square wave pulse, driven
> by one transistor, to the speaker through a 0.1uF
> capacitor you will get a decent loud tock. Then you
> can simply adjust the length of the pulse and this
> will adjust the volume of the tock, over a pretty
> good range too if I remember right.

>On Wed, Oct 17, 2001 at 06:03:58PM +1000, Roman Black wrote:
> > Byron A Jeff wrote:
> > >
> > > I'm interested in sound generation right at the moment. The specific
> task is
> > > building a metronome that has a decent sounding, high volume "TOCK"
> sound.
> > > But I figure since there are quite a few issues in terms of
> generating audio,
> >
> > > 5) And finally the metronome question: Any pointers on how to generate a
> > > quality TOCK sound?
> >
> >
> > Ever attached a 555 timer with a square wave
> > output to a speaker?? You get a real good tock
> > sound. :o)
>
>I took 2 minutes to test dumping a cap into a small speaker. I wasn't real
>impressed.

One of my early pyro projects were 'bang simulators' used for doing
rehearsals with actors and such: they used little 4" speakers in a plastic
case. The actual bang is created by discharging a 10 uF cap charged to ~50
Vdc (six 9V batteries in series) into the speaker. The discharge mechanism
is a MOSFET driven by a monostable timer. Its loud but doesn't sound
anything like a metronome.