Newsgroups: comp.sys.ibm.pc.hardware.misc,alt.pcnews,comp.speech
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.alpha.net!uwm.edu!lll-winken.llnl.gov!enews.sgi.com!decwrl!amd!netcomsv!netcomsv!netcom.com!ebohlman
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: PC speaker volume 
Message-ID: <ebohlmanCy03DB.Hz0@netcom.com>
Followup-To: comp.sys.ibm.pc.hardware.misc,alt.pcnews,comp.speech
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
X-Newsreader: TIN [version 1.2 PL1]
References: <1994Oct18.094649.921@cine88.cineca.it>
Date: Fri, 21 Oct 1994 02:10:23 GMT
Lines: 39
Xref: glinda.oz.cs.cmu.edu comp.sys.ibm.pc.hardware.misc:13255 comp.speech:3573

Studenti (studenti@ing.unife.it) wrote:
: Hello World !

: I'm looking for anyone who knows how to control PC speaker volume
: (software control), if it is possible.

There's no way to control the volume of sounds generated by *other 
programs*.  You can control the volume of sounds that your programs 
generate by using pulse-width modulation techniques.  This requires 
devoting a lot more processor time and attention than simply telling the 
system timer to play a square wave through the speaker, but it can be done.

This isn't intended to be comprehensive (I've seen references on how to 
actually implement it but I don't have them handy), but here's an 
overview.  Let's say you want to generate a 500Hz tone on the speaker.  
The usual way of doing this would be to toggle the speaker's state 1000 
times a second (remember that each cycle requires two toggles).  This 
would put out a tone at the maximum volume possible for that particular 
speaker.  Now suppose that you wanted to to generate that tone at half 
the maximum possible volume.  It turns out that the speaker's cone 
doesn't instantaneously move from its "full off" to its "full on" 
position; it takes time to move.  This means that the speaker acts as a 
mechanical integrator.

Now, if you were to toggle the speaker on and off at a rate of, say, 
100,000 times a second, what would happen is that the speaker cone would 
hold a fairly steady position halfway between its full off and full on 
positions.  If you repeatedly turned the speaker on for 1/100,000 second, 
and then off for 3/100,000 second, the cone would hold a position a 
quarter of the way between full off and full on.

Now we have a way to generate a 500Hz tone at half volume.  For 1/1000 
second, we toggle the speaker on and off every 1/100,000 second.  Then we 
keep the speaker completely off for another 1/1000 second.  Then we repeat.

In real life, you'd probably be using a basic toggle rate of less than 
100,000 times a second.  Note that the more levels of volume you want to 
generate, the faster your basic toggle rate has to be.

