Searching \ for '[EE]: Using PICs for simple audio tasks' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ios.htm?key=audio
Search entire site for: 'Using PICs for simple audio tasks'.

Exact match. Not showing close matches.
PICList Thread
'[EE]: Using PICs for simple audio tasks'
2001\10\16@151421 by Byron A Jeff

face picon face
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?

Thanks for any help you can offer.

BAJ

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\10\16@154902 by Brent Brown

picon face
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.

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).

Hope this gives you some ideas to experiment with.

Brent Brown
Electronic Design Solutions
16 English Street
Hamilton, New Zealand
Ph/fax: +64 7 849 0069
Mobile/text: 025 334 069
eMail:  spam_OUTbrent.brownTakeThisOuTspamclear.net.nz

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\10\16@183649 by Byron A Jeff

face picon face
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).

I think I'm going to try an LM386 for starters. The circuit here:

http://www.reprise.com/host/circuits/lm386.asp

Seems simple enough.

Thanks for the info.

BAJ

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\10\16@193930 by Jeff DeMaagd

flavicon
face
----- Original Message -----
From: Byron A Jeff <.....byronKILLspamspam@spam@CC.GATECH.EDU>

> 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.

Jeff

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\10\17@040627 by Roman Black

flavicon
face
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

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamKILLspammitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@090835 by 742-9014

face picon face
> 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.

For example code, see my HAL PIC project at
http://www.embedinc.com/pic/hal.htm.  The PWM output is updated in the
interrupt routine in the HAL_INTR.ASPIC module.

> 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.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, .....olinKILLspamspam.....embedinc.com, http://www.embedinc.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspam_OUTspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@090849 by 742-9014

face picon face
> 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.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinspamspam_OUTembedinc.com, http://www.embedinc.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email @spam@listservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@122709 by Philip Galanter

flavicon
face
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?

thanks, Philip

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@125208 by Dan Larson

flavicon
face
On Wed, 17 Oct 2001 12:24:00 -0400, Philip Galanter wrote:

>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...

Dan



>
>thanks, Philip
>
>--
>http://www.piclist.com#nomail Going offline? Don't AutoReply us!
>email RemoveMElistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body
>
>
>

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spamBeGonelistservspamBeGonespammitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@155823 by Brent Brown

picon face
> > 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.

Brent Brown
Electronic Design Solutions
16 English Street
Hamilton, New Zealand
Ph/fax: +64 7 849 0069
Mobile/text: 025 334 069
eMail:  TakeThisOuTbrent.brownEraseMEspamspam_OUTclear.net.nz

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@172441 by 742-9014

face picon face
> 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.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinEraseMEspam.....embedinc.com, http://www.embedinc.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspammitvma.mit.edu with SET PICList DIGEST in the body


2001\10\17@190353 by Byron A Jeff

face picon face
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.

I'll test it.

Thanks,

BAJ

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservEraseMEspamEraseMEmitvma.mit.edu with SET PICList DIGEST in the body


2001\10\20@144213 by Dwayne Reid
flavicon
face
At 07:01 PM 10/17/01 -0400, Byron A Jeff wrote:
{Quote hidden}

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.

dwayne



Dwayne Reid   <RemoveMEdwaynerspam_OUTspamKILLspamplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 17 years of Engineering Innovation (1984 - 2001)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do NOT send unsolicited commercial email to this email address.
This message neither grants consent to receive unsolicited
commercial email nor is intended to solicit commercial email.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...