Searching \ for 'Music Tones' in subject line. ()
Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=music+tones
Search entire site for: 'Music Tones'.

Truncated match.
'Music Tones'
1999\01\03@225842 by

Eric Borcherding e-mailed the following code that he used to produce a tone
with a speaker:

001  void Dealer::jacktone()
002  {  // This function sends a short sinewave tone to the PC speaker
003    float t, f = 0.01;
004    for(int i = 1; i < 158; i+=1)                 // sin(157) = 0.999
005    {  t = 1000 * sin(f*i);
006       sound(t);                            // from 1 to 999Hz in 157c
007       delay(20);                                 // about 1 second long
008       nosound();
009    }
010  }

I'm a Predko-newbie and wondered if I could figure out how to use this.
Someone pls let me know if I'm on the right track:

Language is Pascal?  (I added the line numbers to facilitate discussion.)
Lines 001-003 are the subroutine name, a comment & variable definitions
respectively.
Line 004 sets up a loop, each iteration thru the loop will execute lines
005-008.
Line 006 is call(?) to a function that creates the tone of frequency t.
Line 008 turns off the sound.
Line 007 is a delay.  The approx. 1 second length comes from executing the
delay() plus lines 5,6&8 - each 157 times.

I think I understand that but it seems there are other important points.
Somehow we have to 1) generate the signal and 2) drive the speaker.  As far
as driving the speaker, can you simply hang one off a PIC I/O pin to
ground?  (Ohms law with 5V and 8 ohms = 0.625A?)  I could see using a small
audio amp but still need a signal from somewhere.

The sound() routine produces the signal by toggling the pin at the desired
frequency?  Would this best be done using PWM?  (But now there's two
variables:  frequency and pulse width.  Would you use frequency of t and a
pulse width for 50% of the cycle?  How will different %s affect the sound?)

If you don't use PWM, the other possibilities are delays based on
decrementing a counter (which ties up program execution) or using an
interrupt based on the WDT (that doesn't sound practical...interrupt
program flow that often?.....also, with the prescaler at 1:1, 18ms would
limit t to 55Hz?).

I already have put a piezo buzzer on a PIC and that works.  It uses a
simple circuit to create the resonance.  The PIC just turns it on/off.
What if I replaced one of the resistors with a transistor and then drove
the transistor with an analog output from the PIC?

Also, how would I approximate sin() in a PIC?

TIA  Dave Scott

Hi Dave,

>Eric Borcherding e-mailed the following code that he used to produce a tone
>with a speaker:
>
>001  void Dealer::jacktone()
>002  {  // This function sends a short sinewave tone to the PC speaker
>003    float t, f = 0.01;
>004    for(int i = 1; i < 158; i+=1)                 // sin(157) = 0.999
>005    {  t = 1000 * sin(f*i);
>006       sound(t);                            // from 1 to 999Hz in 157c
>007       delay(20);                                 // about 1 second long
>008       nosound();
>009    }
>010  }
>
>I'm a Predko-newbie

You're a "Predko-Newbie"?  Hmmm...  I wonder if I should have some T-Shirts
printed up or start my own cult.

>and wondered if I could figure out how to use this.
>Someone pls let me know if I'm on the right track:
>
>Language is Pascal?  (I added the line numbers to facilitate discussion.)

It looks like C++ to me.  The Class Name is "Dealer" and the Member Function
(which may be a "method") is jacktone.

>Lines 001-003 are the subroutine name, a comment & variable definitions
>          respectively.

If the language is Visual C++, which I suspect it is, it is a "Member
Function" and most likely a "method" (which is the term applied to functions
which are used to access data or function within an object).

>Line 004 sets up a loop, each iteration thru the loop will execute lines
>          005-008.

Correct.

>Line 006 is call(?) to a function that creates the tone of frequency t.

Correct.

>Line 007 is a delay.  The approx. 1 second length comes from executing the
>          delay() plus lines 5,6&8 - each 157 times.

I would agree with you.

>Line 008 turns off the sound.

I would agree with you.

Looking at the code (and not the your comments), I should point out two things:

1.  If this is a PC Speaker, then it cannot output a sine wave.  I don't
understand where these comments are coming from.  The PC's speaker is the
timer overflow output driven to a cap and a speaker.  The actual output
waveform is a square wave, which gets filtered mightily by the cap and
speaker's coil.

2.  The period of the square wave driving the speaker is produced by the
8253/8254 timer built into the PC's motherboard.  The timer is normally
loaded with a "reload" value that determines the frequency of the timer's
overflow, which is the square wave output.

When I look at the code above, I have to believe that the speaker is used as
I describe above the "t = 1000 * sin(f*i);" statement is loading the timer
overflow value with a non-linearly changing value to give a
rising/falling/rising/etc. tone.

>I think I understand that but it seems there are other important points.
>Somehow we have to 1) generate the signal and 2) drive the speaker.  As far
>as driving the speaker, can you simply hang one off a PIC I/O pin to
>ground?  (Ohms law with 5V and 8 ohms = 0.625A?)  I could see using a small
>audio amp but still need a signal from somewhere.

Have you looked at my PICMicro Web page at:

http://www.myke.com/PICMicro

And select "Example Applications"?  I have a tonal output application that
you can take a look at.

>The sound() routine produces the signal by toggling the pin at the desired
>frequency?  Would this best be done using PWM?  (But now there's two
>variables:  frequency and pulse width.  Would you use frequency of t and a
>pulse width for 50% of the cycle?  How will different %s affect the sound?)

I have only used a square wave (just like the PC) without any problems.  My
routine simply toggles a pin when a timer overflows.

>If you don't use PWM, the other possibilities are delays based on
>decrementing a counter (which ties up program execution) or using an
>interrupt based on the WDT (that doesn't sound practical...interrupt
>program flow that often?.....also, with the prescaler at 1:1, 18ms would
>limit t to 55Hz?).

You'll have to use the timer (and not the WDT).  Again, look at the sample
application to see how I did it.

>I already have put a piezo buzzer on a PIC and that works.  It uses a
>simple circuit to create the resonance.  The PIC just turns it on/off.
>What if I replaced one of the resistors with a transistor and then drove
>the transistor with an analog output from the PIC?

Sorry, I don't understand where you're going with this.  If you want to make
noise with the PIC and you're already doing it with a Piezo Buzzer, then I
would think that you're done.

>Also, how would I approximate sin() in a PIC?

You could do it a number of different ways:

1.  If you're going to be using a single frequency, how about a low pass
filter that removes all the upper harmonics from the square wave output?  In
this case, the filtered signal would have to be amplified.
2.  A DAC with a table of sine values could be used - the output from one of
the I/O ports could be used.
3.  Has anybody tried a series (ie multiple square wave outputs) and
combined them into a sine (or other) waveform?
4.  Go to Don Lancaster's site and look up the "magic sinewave" stuff.  I
haven't had a chance to play with them, but they look kind of interesting.

Good luck,

Bhagwan myke

Would you be able to abide by the rules after winning \$100 Million?  Find
out more in this week's book at:

http://www.myke.com/Book_Room/book1a.htm

Coming Very Soon - "Programming and Customizing the 8051 Microcontroller".
Find out more at:

http://www.myke.com/My_Books/pac8051.htm

Nit.

myke predko wrote:
> 1.  If this is a PC Speaker, then it cannot output a sine wave.

True, but what you will hear with your ears from a distance will be a
set of sine waves with the most prevalent frequency being the one you
selected.  Because of the square wave drive, there will be masses of
harmonics.

--
James Cameron                                      (cameronstl.dec.com)

OpenVMS, Linux, Firewalls, Software Engineering, CGI, HTTP, X, C, FORTH,
COBOL, BASIC, DCL, csh, bash, ksh, sh, Electronics, Microcontrollers,
Disability Engineering, Netrek, Bicycles, Pedant, Farming, Home Control,
Remote Area Power, Greek Scholar, Tenor Vocalist, Church Sound, Husband.

"Specialisation is for insects." -- Robert Heinlein.

Dave

I believe you need some help, I am Eric Borcherding the codes author.
The program was in C+ of r a PC, and this was an excerp of a function
prototype.  You can convert it to C and compile with CCS C of HiTechC to
a PIC.  You may want to make the sin function converted to a table lookup.
the conversion to C can be done by changing the void Dealer::jacktone to
void main(void).   Then it will compile... to a small program that can be
executed on a PC by command prompt ?.exe invocation.   ? is the name
of you program.

Some corrections for you Dave:

Lines 1 and are opening of function
Line 7 is a 20 mSor 0.02 Second Delay
sound(?) provides a 1KHz to 15.7KHz tone to the speaker, and most
likely would be better written to do 500Hz to 8KHz for the general acoustic
response of small PM speakers.
Many of these variable's timings can be changed to tune it to your system.

To implement on a PIC you will need to have the sound "pseudo keyword"
replaced with a PWM driver program out to a LM386 0.4W Audio Amp
and then to a PM speaker.   You would have better luck to couple the Amp
thru a Capacitor to eliminate the DC bais you were talking about. Good luck.

Eric Borcherding

1.  If this is a PC Speaker, then it cannot output a sine wave.  I
don't understand where these comments are coming from.  The PC's
speaker is the timer overflow output driven to a cap and a speaker.
The actual output waveform is a square wave, which gets filtered
mightily by the cap and speaker's coil.

Audio SW hackers are very clever - I've seen software that outputs
intelligible VOICE on the PC's internal speaker, along with "easy" stuff
like multi-tone harmonies that ought to be impossible with the hardware
that is present in the PC.

I assume it works by re-biasing the PC's speaker cone to a new 'center'
position by outputting (for quiet) something like a 40kHz square wave from
the timer output (averaging wise, this is 2.5V or so.)  Then you can move
it back and forth (presumably at audio frequencies) by doing PWM on the
40KHz square wave to favor either the low or high states...

(but I don't see how the code segment posted can do what it's comments
claim it does, without a lot of other stuff happening elsewhere.)

BillW

James,

It is not quite a "sinewave" more like a sin function freq. modulated
squarewave

Thanks for clarifiy this;  when I wrote the code I wanted this exact effect.
Note for all PICers it was for a PC not a PIC.   Someone a few threads back
wanted the code so I threw it in to PICLIST by reply instead of a specific
foward.    If anyone wants the whole program Ask...

I have noticed the games sounds different on different PCs.   It was part of a
program for a senior level C++ course that I took which had only one program
required and no exams.    The program had to have original video graphics
and sound.   The selection of the 1KHz to 15.8KHz and the delay was quite
"try, listen test, recompile" and was bent on the PC I used at the time and my
ears.   I used EXTENDED ASCII an the cards suits for graphics.   Esstentially
wildjack was a loaded deck of extra Aces and Jacks from blackjack.

Eric Borcherding

part 0 544 bytes content-type:application/octet-stream;Yea it said sinewave....   It is pseudo sine at harmonics...
Essentiallt it sounded good on my PC at the time the code
comment is in error.   As the part of intelligible voice thru a PC
speaker

the attached COM file has your PC speaker say Ohh SHIT...

Have fun - it is great for a laptop boot...

Eric Borcherding

Content-ID: <0_915439049inet_out.mail.aol.com.2>
Content-type: application/octet-stream;
name="OSHIT.COM"
Content-disposition: inline

Attachment converted: wonderland:OSHIT.COM (????/----) (00025018)
Pursue this code no further before you try out the PIC basic compiler from
Micro Engineering Labs.
To produce sound is very straight forward as there is a SOUND command built
in.

DONT connect a speaker less than 200 ohms (if there is such an impedance
speaker) directly to a pic pin, it wont like it ! - use buffering. A simple
emitter follower transistor will do.

Darren

> PC speaker talking

FYI There is a PD program on Simtel called TRAN. It 'speaks' ASCII text
through the PC speaker. It's pretty lousy in voice quality and needs
tuning to match your computer speed, but it works, and I'd used it once or
twice. There is no source for it, though.

Peter

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