[comp.windows.ms.programmer] Sound/Music in Windows Apps

corfmanr@gtephx.UUCP (Russ Corfman) (02/13/91)

I want to start writing some windows applications that
make use of the speaker. I'm planning on starting off
with something simple like a cuckoo clock/alarm clock.
I've read what is in the reference manual for all the
music/sound functions such as SetVoiceNote, SetVoicePitch,
OpenSound, etc. I am left with a number of questions.

1. How does windows allocate the sound resource? I know
   that there is only one speaker so I assume only one
   application can be using it at a time. How does that
   application reserve the speaker? How does the application
   know if the speaker is already being used?

2. Voice queues are used to place different notes and
   frequencies and such in. In one of the function 
   descripions in the reference manual, it said the
   first queue is queue "1". How many voice queues can
   there be? Is it unlimited? Another question, are the
   queues a system wide resource such as timers or
   are they just a single application's resource?

3. I could more or less follow what the documentation
   meant for (how to use) functions such as SetVoiceNote
   and SetVoicePitch, but I was lost on how to use 
   SetVoiceFrequency. Is there an easy way to figure
   out how the passed frequencies affect the sound? I 
   guess a table of what frequencies to use for common
   sounds such as sirens or bubbling water would be nice.
   Something to point me in the right direction.

4. The "Guide to Programming" and "Programming Windows"
   by Petzold have no mention or examples of making sounds.
   Does anybody know of a book or article that discusses
   this topic? Some example source code would be very
   helpful too.

Thanks,
-- 
Russell Corfman
AG Communication Systems; Phoenix, AZ
UUCP: ...!{ncar!noao!asuvax | uunet!zardoz!hrc}!gtephx!corfmanr
Internet: gtephx!corfmanr@asuvax.eas.asu.edu
(602) 581-4403

bcw@rti.rti.org (Bruce Wright) (02/14/91)

In article <199@maillot_.gtephx.UUCP>, corfmanr@gtephx.UUCP (Russ Corfman) writes:
> I want to start writing some windows applications that
> make use of the speaker. I'm planning on starting off
> with something simple like a cuckoo clock/alarm clock.
> I've read what is in the reference manual for all the
> music/sound functions such as SetVoiceNote, SetVoicePitch,
> OpenSound, etc. I am left with a number of questions.
> 
> 1. How does windows allocate the sound resource? I know
>    that there is only one speaker so I assume only one
>    application can be using it at a time. How does that
>    application reserve the speaker? How does the application
>    know if the speaker is already being used?

Windows allocates the sound resource to the first progam to do
OpenSound ().  Be sure to release the resource with CloseSound () 
when you're through with it!  Only one application can use the
sound at a time - if it's already in use, you get an S_SERDVNA
error return from OpenSound ().

> 2. Voice queues are used to place different notes and
>    frequencies and such in. In one of the function 
>    descripions in the reference manual, it said the
>    first queue is queue "1". How many voice queues can
>    there be? Is it unlimited? Another question, are the
>    queues a system wide resource such as timers or
>    are they just a single application's resource?

The number of voice queues is hardware-dependent.  For a standard
PC-type machine without any add-on hardware, there's only one voice 
queue - because there is only one clock available to send output to 
the speaker (the issue is the number of oscillators availble, not
the number of speakers).  Since the number of voices is (largely)
determined by the hardware, the number of queues is a system-wide 
resource.

> 3. I could more or less follow what the documentation
>    meant for (how to use) functions such as SetVoiceNote
>    and SetVoicePitch, but I was lost on how to use 
>    SetVoiceFrequency. Is there an easy way to figure
>    out how the passed frequencies affect the sound? I 
>    guess a table of what frequencies to use for common
>    sounds such as sirens or bubbling water would be nice.
>    Something to point me in the right direction.

I assume you mean SetVoiceAccent and SetVoiceSound instead of
SetVoicePitch and SetVoiceFrequency.  The frequencies of
common notes are:

	A3	220.00
	A#3	233.08
	B3	246.94
	C4	261.63	<- C below "middle C"
	C#4	277.18
	D4	293.66
	D#4	311.13
	E4	329.63
	F4	349.23
	F#4	369.99
	G4	392.00
	G#4	415.30
	A4	440.00
	A#4	466.16
	B4	493.88
	C5	523.25 <- "middle C"
	C#5	554.37
	D5	587.33
	D#5	622.25
	E5	659.26
	F5	698.46
	F#5	739.99
	G5	783.99
	G#5	830.61
	A5	880.00
	A#5	932.33
	B5	987.77
	C6	1046.50 <- C above "middle C"
		...etc

Note that for each note (assuming I typed the numbers in right),
the frequency is exactly twice that for the corresponding note of
the octave below.  This should allow you to extend the scale in
both directions if you need to.

Sirens are pretty easy by programming a "glissando" up and down
the scale (a lot of short notes going through all the frequencies
[not just the above-named frequencies] between two baseline notes - 
the timing of the glissando and the choice of endpoint notes can 
simulate a number of different types of sirens).  Bubbles are 
harder - I don't have anything specific offhand to simulate them, 
but they should be possible to do by a similar technique, ending 
with a hard "stop".  It wouldn't hurt to experiment if you want to 
produce special effects.

SetVoiceAccent seems to have bugs on Windows 3.0.  It seemed to
work OK on Windows 2.11, but apparently something broke in 3.0
and accents don't work properly.  The nTempo and nMode parameters
to SetVoiceAccent no longer seem to have any effect (especially
bad in the case of nMode since the workaround is particularly ugly).
The nPitch paramter still seems to work OK.  Note that nVolume is
ignored for most PC's - the standard PC architecture has no way
to control the volume of the speaker.

> 4. The "Guide to Programming" and "Programming Windows"
>    by Petzold have no mention or examples of making sounds.
>    Does anybody know of a book or article that discusses
>    this topic? Some example source code would be very
>    helpful too.

Sorry, I don't know any books on how to use the sound functions
under Windows.  They aren't really too hard to use (except for the
bugs in SetVoiceAccent).  I might be able to mail or post the
source to a "fun" program that my wife and I have played around
with (Program displays a Christmas scene and plays Christmas carols -
an adaptation of a well-known Basic version with enhancements for
Windows).  We never really have gotten it to the point of being happy 
with it, and the source is a bit big (probably not written as small 
as possible since it has grown by accretion over several Christmas 
seasons), so I've never posted the program or the source.  I'd have
to see how my wife would feel about it, and I'm also not sure that
the net would appreciate the volume (source is about 60k un-ZIPed).
The sound portion is really a very small part of the code though.

Good luck -

						Bruce C. Wright

strobl@gmdzi.gmd.de (Wolfgang Strobl) (02/15/91)

bcw@rti.rti.org (Bruce Wright) writes:

>In article <199@maillot_.gtephx.UUCP>, corfmanr@gtephx.UUCP (Russ Corfman) writes:
>> I want to start writing some windows applications that
>> make use of the speaker. [...]

>Sorry, I don't know any books on how to use the sound functions
>under Windows. [...]

The Microsoft Programmers Library version 1.1 on CDROM has two
sample programs for sound, in the Windows area. More than nothing,
but not very impressive. 

A question: is there an newer version of the Programmers Library? The
above version is from '89 and is about Windows 2.11, so it is not
very usefull, anymore.

Wolfgang Strobl
#include <std.disclaimer.hpp>