home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_4962a735dd848dde4ff1af4c002bbd92
< prev
next >
Wrap
Text File
|
2000-03-23
|
15KB
|
434 lines
<HTML>
<HEAD>
<TITLE>Win32::Sound - An extension to play with Windows sounds</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Win32::Sound - An extension to play with Windows sounds</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#functions">FUNCTIONS</A></LI>
<LI><A HREF="#the waveout package">THE WaveOut PACKAGE</A></LI>
<UL>
<LI><A HREF="#functions">FUNCTIONS</A></LI>
<LI><A HREF="#the sound format">THE SOUND FORMAT</A></LI>
<LI><A HREF="#example">EXAMPLE</A></LI>
</UL>
<LI><A HREF="#version">VERSION</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Win32::Sound - An extension to play with Windows sounds</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use Win32::Sound;
Win32::Sound::Volume('100%');
Win32::Sound::Play("file.wav");
Win32::Sound::Stop();
</PRE>
<PRE>
# ...and read on for more fun ;-)</PRE>
<P>
<HR>
<H1><A NAME="functions">FUNCTIONS</A></H1>
<DL>
<DT><STRONG><A NAME="item_Play"><STRONG>Win32::Sound::Play(SOUND, [FLAGS])</STRONG></A></STRONG><BR>
<DD>
Plays the specified sound: SOUND can the be name of a WAV file
or one of the following predefined sound names:
<PRE>
SystemDefault
SystemAsterisk
SystemExclamation
SystemExit
SystemHand
SystemQuestion
SystemStart</PRE>
<P>Additionally, if the named sound could not be found, the
function plays the system default sound (unless you specify the
<A HREF="#item_SND_NODEFAULT"><CODE>SND_NODEFAULT</CODE></A> flag). If no parameters are given, this function
stops the sound actually playing (see also Win32::Sound::Stop).</P>
<P>FLAGS can be a combination of the following constants:</P>
<DL>
<DT><STRONG><A NAME="item_SND_ASYNC"><CODE>SND_ASYNC</CODE></A></STRONG><BR>
<DD>
The sound is played asynchronously and the function
returns immediately after beginning the sound
(if this flag is not specified, the sound is
played synchronously and the function returns
when the sound ends).
<P></P>
<DT><STRONG><A NAME="item_SND_LOOP"><CODE>SND_LOOP</CODE></A></STRONG><BR>
<DD>
The sound plays repeatedly until it is stopped.
You must also specify <A HREF="#item_SND_ASYNC"><CODE>SND_ASYNC</CODE></A> flag.
<P></P>
<DT><STRONG><A NAME="item_SND_NODEFAULT"><CODE>SND_NODEFAULT</CODE></A></STRONG><BR>
<DD>
No default sound is used. If the specified <EM>sound</EM>
cannot be found, the function returns without
playing anything.
<P></P>
<DT><STRONG><A NAME="item_SND_NOSTOP"><CODE>SND_NOSTOP</CODE></A></STRONG><BR>
<DD>
If a sound is already playing, the function fails.
By default, any new call to the function will stop
previously playing sounds.
<P></P></DL>
<DT><STRONG><A NAME="item_Stop"><STRONG>Win32::Sound::Stop()</STRONG></A></STRONG><BR>
<DD>
Stops the sound currently playing.
<P></P>
<DT><STRONG><A NAME="item_Volume"><STRONG>Win32::Sound::Volume()</STRONG></A></STRONG><BR>
<DD>
Returns the wave device volume; if
called in an array context, returns left
and right values. Otherwise, returns a single
32 bit value (left in the low word, right
in the high word).
In case of error, returns <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> and sets
$!.
<P>Examples:</P>
<PRE>
($L, $R) = Win32::Sound::Volume();
if( not defined Win32::Sound::Volume() ) {
die "Can't get volume: $!";
}</PRE>
<P></P>
<DT><STRONG><STRONG>Win32::Sound::Volume(LEFT, [RIGHT])</STRONG></STRONG><BR>
<DD>
Sets the wave device volume; if two arguments
are given, sets left and right channels
independently, otherwise sets them both to
LEFT (eg. RIGHT=LEFT). Values range from
0 to 65535 (0xFFFF), but they can also be
given as percentage (use a string containing
a number followed by a percent sign).
<P>Returns <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> and sets $! in case of error,
a true value if successful.</P>
<P>Examples:</P>
<PRE>
Win32::Sound::Volume('50%');
Win32::Sound::Volume(0xFFFF, 0x7FFF);
Win32::Sound::Volume('100%', '50%');
Win32::Sound::Volume(0);</PRE>
<P></P>
<DT><STRONG><A NAME="item_Format"><STRONG>Win32::Sound::Format(filename)</STRONG></A></STRONG><BR>
<DD>
Returns information about the specified WAV file format;
the array contains:
<UL>
<LI><STRONG><A NAME="item_rate">sample rate (in Hz)</A></STRONG><BR>
<LI><STRONG><A NAME="item_sample">bits per sample (8 or 16)</A></STRONG><BR>
<LI><STRONG><A NAME="item_channels">channels (1 for mono, 2 for stereo)</A></STRONG><BR>
</UL>
<P>Example:</P>
<PRE>
($hz, $bits, $channels)
= Win32::Sound::Format("file.wav");</PRE>
<DT><STRONG><A NAME="item_Devices"><STRONG>Win32::Sound::Devices()</STRONG></A></STRONG><BR>
<DD>
Returns all the available sound devices;
their names contain the type of the
device (WAVEOUT, WAVEIN, MIDIOUT,
MIDIIN, AUX or MIXER) and
a zero-based ID number: valid devices
names are for example:
<PRE>
WAVEOUT0
WAVEOUT1
WAVEIN0
MIDIOUT0
MIDIIN0
AUX0
AUX1
AUX2</PRE>
<P>There are also two special device
names, <CODE>WAVE_MAPPER</CODE> and <CODE>MIDI_MAPPER</CODE>
(the default devices for wave output
and midi output).</P>
<P>Example:</P>
<PRE>
@devices = Win32::Sound::Devices();</PRE>
<P></P>
<DT><STRONG><A NAME="item_DeviceInfo">Win32::Sound::DeviceInfo(DEVICE)</A></STRONG><BR>
<DD>
Returns an associative array of information
about the sound device named DEVICE (the
same format of Win32::Sound::Devices).
<P>The content of the array depends on the device
type queried. Each device type returns <STRONG>at least</STRONG>
the following information:</P>
<PRE>
manufacturer_id
product_id
name
driver_version</PRE>
<P>For additional data refer to the following
table:</P>
<PRE>
WAVEIN..... formats
channels
</PRE>
<PRE>
WAVEOUT.... formats
channels
support</PRE>
<PRE>
MIDIOUT.... technology
voices
notes
channels
support</PRE>
<PRE>
AUX........ technology
support</PRE>
<PRE>
MIXER...... destinations
support</PRE>
<P>The meaning of the fields, where not
obvious, can be evinced from the
Microsoft SDK documentation (too long
to report here, maybe one day... :-).</P>
<P>Example:</P>
<PRE>
%info = Win32::Sound::DeviceInfo('WAVE_MAPPER');
print "$info{name} version $info{driver_version}\n";</PRE>
<P></P></DL>
<P>
<HR>
<H1><A NAME="the waveout package">THE WaveOut PACKAGE</A></H1>
<P>Win32::Sound also provides a different, more
powerful approach to wave audio data with its
<A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> package. It has methods to load and
then play WAV files, with the additional feature
of specifying the start and end range, so you
can play only a portion of an audio file.</P>
<P>Furthermore, it is possible to load arbitrary
binary data to the soundcard to let it play and
save them back into WAV files; in a few words,
you can do some sound synthesis work.</P>
<P>
<H2><A NAME="functions">FUNCTIONS</A></H2>
<DL>
<DT><STRONG><A NAME="item_WaveOut">new Win32::Sound::WaveOut(FILENAME)</A></STRONG><BR>
<DD>
<DT><STRONG>new Win32::Sound::WaveOut(SAMPLERATE, BITS, CHANNELS)</STRONG><BR>
<DD>
<DT><STRONG>new Win32::Sound::WaveOut()</STRONG><BR>
<DD>
This function creates a <A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> object; the
first form opens the specified wave file (see
also <A HREF="#item_Open"><CODE>Open()</CODE></A> ), so you can directly <A HREF="#item_Play"><CODE>Play()</CODE></A> it.
<P>The second (and third) form opens the
wave output device with the format given
(or if none given, defaults to 44.1kHz,
16 bits, stereo); to produce something
audible you can either <A HREF="#item_Open"><CODE>Open()</CODE></A> a wave file
or <A HREF="#item_Load"><CODE>Load()</CODE></A> binary data to the soundcard
and then <A HREF="#item_Write"><CODE>Write()</CODE></A> it.</P>
<P></P>
<DT><STRONG><A NAME="item_Close"><CODE>Close()</CODE></A></STRONG><BR>
<DD>
Closes the wave file currently opened.
<P></P>
<DT><STRONG><A NAME="item_CloseDevice"><CODE>CloseDevice()</CODE></A></STRONG><BR>
<DD>
Closes the wave output device; you can change
format and reopen it with <A HREF="#item_OpenDevice"><CODE>OpenDevice()</CODE></A>.
<P></P>
<DT><STRONG><A NAME="item_GetErrorText"><CODE>GetErrorText(ERROR)</CODE></A></STRONG><BR>
<DD>
Returns the error text associated with
the specified ERROR number; note it only
works for wave-output-specific errors.
<P></P>
<DT><STRONG><A NAME="item_Load"><CODE>Load(DATA)</CODE></A></STRONG><BR>
<DD>
Loads the DATA buffer in the soundcard.
The format of the data buffer depends
on the format used; for example, with
8 bit mono each sample is one character,
while with 16 bit stereo each sample is
four characters long (two 16 bit values
for left and right channels). The sample
rate defines how much samples are in one
second of sound. For example, to fit one
second at 44.1kHz 16 bit stereo your buffer
must contain 176400 bytes (44100 * 4).
<P></P>
<DT><STRONG><A NAME="item_Open"><CODE>Open(FILE)</CODE></A></STRONG><BR>
<DD>
Opens the specified wave FILE.
<P></P>
<DT><STRONG><A NAME="item_OpenDevice"><CODE>OpenDevice()</CODE></A></STRONG><BR>
<DD>
Opens the wave output device with the
current sound format (not needed unless
you used <A HREF="#item_CloseDevice"><CODE>CloseDevice()</CODE></A>).
<P></P>
<DT><STRONG><A NAME="item_Pause"><CODE>Pause()</CODE></A></STRONG><BR>
<DD>
Pauses the sound currently playing;
use <A HREF="#item_Restart"><CODE>Restart()</CODE></A> to continue playing.
<P></P>
<DT><STRONG>Play( [FROM, TO] )</STRONG><BR>
<DD>
Plays the opened wave file. You can optionally
specify a FROM - TO range, where FROM and TO
are expressed in samples (or use FROM=0 for the
first sample and TO=-1 for the last sample).
Playback happens always asynchronously, eg. in
the background.
<P></P>
<DT><STRONG><A NAME="item_Position"><CODE>Position()</CODE></A></STRONG><BR>
<DD>
Returns the sample number currently playing;
note that the play position is not zeroed
when the sound ends, so you have to call a
<A HREF="#item_Reset"><CODE>Reset()</CODE></A> between plays to receive the
correct position in the current sound.
<P></P>
<DT><STRONG><A NAME="item_Reset"><CODE>Reset()</CODE></A></STRONG><BR>
<DD>
Stops playing and resets the play position
(see <A HREF="#item_Position"><CODE>Position()</CODE></A>).
<P></P>
<DT><STRONG><A NAME="item_Restart"><CODE>Restart()</CODE></A></STRONG><BR>
<DD>
Continues playing the sound paused by <A HREF="#item_Pause"><CODE>Pause()</CODE></A>.
<P></P>
<DT><STRONG><A NAME="item_Save">Save(FILE, [DATA])</A></STRONG><BR>
<DD>
Writes the DATA buffer (if not given, uses the
buffer currently loaded in the soundcard)
to the specified wave FILE.
<P></P>
<DT><STRONG><A NAME="item_Status"><CODE>Status()</CODE></A></STRONG><BR>
<DD>
Returns 0 if the soundcard is currently playing,
1 if it's free, or <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> on errors.
<P></P>
<DT><STRONG><A NAME="item_Unload"><CODE>Unload()</CODE></A></STRONG><BR>
<DD>
Frees the soundcard from the loaded data.
<P></P>
<DT><STRONG>Volume( [LEFT, RIGHT] )</STRONG><BR>
<DD>
Gets or sets the volume for the wave output device.
It works the same way as Win32::Sound::Volume.
<P></P>
<DT><STRONG><A NAME="item_Write"><CODE>Write()</CODE></A></STRONG><BR>
<DD>
Plays the data currently loaded in the soundcard;
playback happens always asynchronously, eg. in
the background.
<P></P></DL>
<P>
<H2><A NAME="the sound format">THE SOUND FORMAT</A></H2>
<P>The sound format is stored in three properties of
the <A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> object: <CODE>samplerate</CODE>, <CODE>bits</CODE> and
<A HREF="#item_channels"><CODE>channels</CODE></A>.
If you need to change them without creating a
new object, you should close before and reopen
afterwards the device.</P>
<PRE>
$WAV->CloseDevice();
$WAV->{samplerate} = 44100; # 44.1kHz
$WAV->{bits} = 8; # 8 bit
$WAV->{channels} = 1; # mono
$WAV->OpenDevice();</PRE>
<P>You can also use the properties to query the
sound format currently used.</P>
<P>
<H2><A NAME="example">EXAMPLE</A></H2>
<P>This small example produces a 1 second sinusoidal
wave at 440Hz and saves it in <EM>sinus.wav</EM>:</P>
<PRE>
use Win32::Sound;
</PRE>
<PRE>
# Create the object
$WAV = new Win32::Sound::WaveOut(44100, 8, 2);</PRE>
<PRE>
$data = "";
$counter = 0;
$increment = 440/44100;</PRE>
<PRE>
# Generate 44100 samples ( = 1 second)
for $i (1..44100) {</PRE>
<PRE>
# Calculate the pitch
# (range 0..255 for 8 bits)
$v = sin($counter/2*3.14) * 128 + 128;</PRE>
<PRE>
# "pack" it twice for left and right
$data .= pack("cc", $v, $v);</PRE>
<PRE>
$counter += $increment;
}
</PRE>
<PRE>
$WAV->Load($data); # get it
$WAV->Write(); # hear it
1 until $WAV->Status(); # wait for completion
$WAV->Save("sinus.wav"); # write to disk
$WAV->Unload(); # drop it</PRE>
<P>
<HR>
<H1><A NAME="version">VERSION</A></H1>
<P>Win32::Sound version 0.45, 09 Apr 1999.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Aldo Calpini, <CODE>dada@divinf.it</CODE></P>
<P>Parts of the code provided and/or suggested by Dave Roth.</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Win32::Sound - An extension to play with Windows sounds</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>