|
Win32::Sound - An extension to play with Windows sounds |
Win32::Sound - An extension to play with Windows sounds
use Win32::Sound;
Win32::Sound::Volume('100%');
Win32::Sound::Play("file.wav");
Win32::Sound::Stop();
# ...and read on for more fun ;-)
SystemDefault
SystemAsterisk
SystemExclamation
SystemExit
SystemHand
SystemQuestion
SystemStart
Additionally, if the named sound could not be found, the
function plays the system default sound (unless you specify the
SND_NODEFAULT flag). If no parameters are given, this function
stops the sound actually playing (see also Win32::Sound::Stop).
FLAGS can be a combination of the following constants:
SND_ASYNCSND_LOOPSND_ASYNC flag.
SND_NODEFAULTSND_NOSTOPundef and sets
$!.
Examples:
($L, $R) = Win32::Sound::Volume();
if( not defined Win32::Sound::Volume() ) {
die "Can't get volume: $!";
}
Returns undef and sets $! in case of error,
a true value if successful.
Examples:
Win32::Sound::Volume('50%');
Win32::Sound::Volume(0xFFFF, 0x7FFF);
Win32::Sound::Volume('100%', '50%');
Win32::Sound::Volume(0);
Example:
($hz, $bits, $channels)
= Win32::Sound::Format("file.wav");
WAVEOUT0
WAVEOUT1
WAVEIN0
MIDIOUT0
MIDIIN0
AUX0
AUX1
AUX2
There are also two special device
names, WAVE_MAPPER and MIDI_MAPPER
(the default devices for wave output
and midi output).
Example:
@devices = Win32::Sound::Devices();
The content of the array depends on the device type queried. Each device type returns at least the following information:
manufacturer_id
product_id
name
driver_version
For additional data refer to the following table:
WAVEIN..... formats
channels
WAVEOUT.... formats
channels
support
MIDIOUT.... technology
voices
notes
channels
support
AUX........ technology
support
MIXER...... destinations
support
The meaning of the fields, where not obvious, can be evinced from the Microsoft SDK documentation (too long to report here, maybe one day... :-).
Example:
%info = Win32::Sound::DeviceInfo('WAVE_MAPPER');
print "$info{name} version $info{driver_version}\n";
Win32::Sound also provides a different, more
powerful approach to wave audio data with its
WaveOut 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.
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.
WaveOut object; the
first form opens the specified wave file (see
also Open() ), so you can directly Play() it.
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 Open() a wave file
or Load() binary data to the soundcard
and then Write() it.
Close()CloseDevice()OpenDevice().
GetErrorText(ERROR)Load(DATA)Open(FILE)OpenDevice()CloseDevice()).
Pause()Restart() to continue playing.
Position()Reset() between plays to receive the
correct position in the current sound.
Reset()Position()).
Restart()Pause().
Status()undef on errors.
Unload()Write()
The sound format is stored in three properties of
the WaveOut object: samplerate, bits and
channels.
If you need to change them without creating a
new object, you should close before and reopen
afterwards the device.
$WAV->CloseDevice();
$WAV->{samplerate} = 44100; # 44.1kHz
$WAV->{bits} = 8; # 8 bit
$WAV->{channels} = 1; # mono
$WAV->OpenDevice();
You can also use the properties to query the sound format currently used.
This small example produces a 1 second sinusoidal wave at 440Hz and saves it in sinus.wav:
use Win32::Sound;
# Create the object
$WAV = new Win32::Sound::WaveOut(44100, 8, 2);
$data = "";
$counter = 0;
$increment = 440/44100;
# Generate 44100 samples ( = 1 second)
for $i (1..44100) {
# Calculate the pitch
# (range 0..255 for 8 bits)
$v = sin($counter/2*3.14) * 128 + 128;
# "pack" it twice for left and right
$data .= pack("cc", $v, $v);
$counter += $increment;
}
$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
Win32::Sound version 0.46, 25 Sep 1999.
Aldo Calpini, dada@divinf.it
Parts of the code provided and/or suggested by Dave Roth.
|
Win32::Sound - An extension to play with Windows sounds |