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 >
Text File  |  2000-03-23  |  15KB  |  434 lines

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Win32::Sound - An extension to play with Windows sounds</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
  6. <LINK REV="made" HREF="mailto:">
  7. </HEAD>
  8.  
  9. <BODY>
  10. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  11. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  12. <STRONG><P CLASS=block> Win32::Sound - An extension to play with Windows sounds</P></STRONG>
  13. </TD></TR>
  14. </TABLE>
  15.  
  16. <A NAME="__index__"></A>
  17. <!-- INDEX BEGIN -->
  18.  
  19. <UL>
  20.  
  21.     <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
  22.  
  23.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  24.     <LI><A HREF="#functions">FUNCTIONS</A></LI>
  25.     <LI><A HREF="#the waveout package">THE WaveOut PACKAGE</A></LI>
  26.     <UL>
  27.  
  28.         <LI><A HREF="#functions">FUNCTIONS</A></LI>
  29.         <LI><A HREF="#the sound format">THE SOUND FORMAT</A></LI>
  30.         <LI><A HREF="#example">EXAMPLE</A></LI>
  31.     </UL>
  32.  
  33.     <LI><A HREF="#version">VERSION</A></LI>
  34.     <LI><A HREF="#author">AUTHOR</A></LI>
  35. </UL>
  36. <!-- INDEX END -->
  37.  
  38. <HR>
  39. <P>
  40. <H1><A NAME="name">NAME</A></H1>
  41. <P>Win32::Sound - An extension to play with Windows sounds</P>
  42. <P>
  43. <HR>
  44. <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
  45. <UL>
  46. <LI>Windows</LI>
  47. </UL>
  48. <HR>
  49. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  50. <PRE>
  51.     use Win32::Sound;
  52.     Win32::Sound::Volume('100%');
  53.     Win32::Sound::Play("file.wav");
  54.     Win32::Sound::Stop();
  55. </PRE>
  56. <PRE>
  57.  
  58.     # ...and read on for more fun ;-)</PRE>
  59. <P>
  60. <HR>
  61. <H1><A NAME="functions">FUNCTIONS</A></H1>
  62. <DL>
  63. <DT><STRONG><A NAME="item_Play"><STRONG>Win32::Sound::Play(SOUND, [FLAGS])</STRONG></A></STRONG><BR>
  64. <DD>
  65. Plays the specified sound: SOUND can the be name of a WAV file
  66. or one of the following predefined sound names:
  67. <PRE>
  68.     SystemDefault
  69.     SystemAsterisk
  70.     SystemExclamation
  71.     SystemExit
  72.     SystemHand
  73.     SystemQuestion
  74.     SystemStart</PRE>
  75. <P>Additionally, if the named sound could not be found, the 
  76. function plays the system default sound (unless you specify the 
  77. <A HREF="#item_SND_NODEFAULT"><CODE>SND_NODEFAULT</CODE></A> flag). If no parameters are given, this function 
  78. stops the sound actually playing (see also Win32::Sound::Stop).</P>
  79. <P>FLAGS can be a combination of the following constants:</P>
  80. <DL>
  81. <DT><STRONG><A NAME="item_SND_ASYNC"><CODE>SND_ASYNC</CODE></A></STRONG><BR>
  82. <DD>
  83. The sound is played asynchronously and the function 
  84. returns immediately after beginning the sound
  85. (if this flag is not specified, the sound is
  86. played synchronously and the function returns
  87. when the sound ends).
  88. <P></P>
  89. <DT><STRONG><A NAME="item_SND_LOOP"><CODE>SND_LOOP</CODE></A></STRONG><BR>
  90. <DD>
  91. The sound plays repeatedly until it is stopped.
  92. You must also specify <A HREF="#item_SND_ASYNC"><CODE>SND_ASYNC</CODE></A> flag.
  93. <P></P>
  94. <DT><STRONG><A NAME="item_SND_NODEFAULT"><CODE>SND_NODEFAULT</CODE></A></STRONG><BR>
  95. <DD>
  96. No default sound is used. If the specified <EM>sound</EM>
  97. cannot be found, the function returns without
  98. playing anything.
  99. <P></P>
  100. <DT><STRONG><A NAME="item_SND_NOSTOP"><CODE>SND_NOSTOP</CODE></A></STRONG><BR>
  101. <DD>
  102. If a sound is already playing, the function fails.
  103. By default, any new call to the function will stop
  104. previously playing sounds.
  105. <P></P></DL>
  106. <DT><STRONG><A NAME="item_Stop"><STRONG>Win32::Sound::Stop()</STRONG></A></STRONG><BR>
  107. <DD>
  108. Stops the sound currently playing.
  109. <P></P>
  110. <DT><STRONG><A NAME="item_Volume"><STRONG>Win32::Sound::Volume()</STRONG></A></STRONG><BR>
  111. <DD>
  112. Returns the wave device volume; if 
  113. called in an array context, returns left
  114. and right values. Otherwise, returns a single
  115. 32 bit value (left in the low word, right 
  116. in the high word).
  117. In case of error, returns <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> and sets
  118. $!.
  119. <P>Examples:</P>
  120. <PRE>
  121.     ($L, $R) = Win32::Sound::Volume();
  122.     if( not defined Win32::Sound::Volume() ) {
  123.         die "Can't get volume: $!";
  124.     }</PRE>
  125. <P></P>
  126. <DT><STRONG><STRONG>Win32::Sound::Volume(LEFT, [RIGHT])</STRONG></STRONG><BR>
  127. <DD>
  128. Sets the wave device volume; if two arguments
  129. are given, sets left and right channels 
  130. independently, otherwise sets them both to
  131. LEFT (eg. RIGHT=LEFT). Values range from
  132. 0 to 65535 (0xFFFF), but they can also be
  133. given as percentage (use a string containing 
  134. a number followed by a percent sign).
  135. <P>Returns <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> and sets $! in case of error,
  136. a true value if successful.</P>
  137. <P>Examples:</P>
  138. <PRE>
  139.     Win32::Sound::Volume('50%');
  140.     Win32::Sound::Volume(0xFFFF, 0x7FFF);
  141.     Win32::Sound::Volume('100%', '50%');
  142.     Win32::Sound::Volume(0);</PRE>
  143. <P></P>
  144. <DT><STRONG><A NAME="item_Format"><STRONG>Win32::Sound::Format(filename)</STRONG></A></STRONG><BR>
  145. <DD>
  146. Returns information about the specified WAV file format;
  147. the array contains:
  148. <UL>
  149. <LI><STRONG><A NAME="item_rate">sample rate (in Hz)</A></STRONG><BR>
  150.  
  151. <LI><STRONG><A NAME="item_sample">bits per sample (8 or 16)</A></STRONG><BR>
  152.  
  153. <LI><STRONG><A NAME="item_channels">channels (1 for mono, 2 for stereo)</A></STRONG><BR>
  154.  
  155. </UL>
  156. <P>Example:</P>
  157. <PRE>
  158.     ($hz, $bits, $channels) 
  159.         = Win32::Sound::Format("file.wav");</PRE>
  160. <DT><STRONG><A NAME="item_Devices"><STRONG>Win32::Sound::Devices()</STRONG></A></STRONG><BR>
  161. <DD>
  162. Returns all the available sound devices;
  163. their names contain the type of the
  164. device (WAVEOUT, WAVEIN, MIDIOUT,
  165. MIDIIN, AUX or MIXER) and 
  166. a zero-based ID number: valid devices
  167. names are for example:
  168. <PRE>
  169.     WAVEOUT0
  170.     WAVEOUT1
  171.     WAVEIN0
  172.     MIDIOUT0
  173.     MIDIIN0
  174.     AUX0
  175.     AUX1
  176.     AUX2</PRE>
  177. <P>There are also two special device
  178. names, <CODE>WAVE_MAPPER</CODE> and <CODE>MIDI_MAPPER</CODE>
  179. (the default devices for wave output
  180. and midi output).</P>
  181. <P>Example:</P>
  182. <PRE>
  183.     @devices = Win32::Sound::Devices();</PRE>
  184. <P></P>
  185. <DT><STRONG><A NAME="item_DeviceInfo">Win32::Sound::DeviceInfo(DEVICE)</A></STRONG><BR>
  186. <DD>
  187. Returns an associative array of information 
  188. about the sound device named DEVICE (the
  189. same format of Win32::Sound::Devices).
  190. <P>The content of the array depends on the device
  191. type queried. Each device type returns <STRONG>at least</STRONG> 
  192. the following information:</P>
  193. <PRE>
  194.     manufacturer_id
  195.     product_id
  196.     name
  197.     driver_version</PRE>
  198. <P>For additional data refer to the following
  199. table:</P>
  200. <PRE>
  201.     WAVEIN..... formats
  202.                 channels
  203. </PRE>
  204. <PRE>
  205.  
  206.     WAVEOUT.... formats
  207.                 channels
  208.                 support</PRE>
  209. <PRE>
  210.  
  211.     MIDIOUT.... technology
  212.                 voices
  213.                 notes
  214.                 channels
  215.                 support</PRE>
  216. <PRE>
  217.  
  218.     AUX........ technology
  219.                 support</PRE>
  220. <PRE>
  221.  
  222.     MIXER...... destinations
  223.                 support</PRE>
  224. <P>The meaning of the fields, where not
  225. obvious, can be evinced from the 
  226. Microsoft SDK documentation (too long
  227. to report here, maybe one day... :-).</P>
  228. <P>Example:</P>
  229. <PRE>
  230.     %info = Win32::Sound::DeviceInfo('WAVE_MAPPER');
  231.     print "$info{name} version $info{driver_version}\n";</PRE>
  232. <P></P></DL>
  233. <P>
  234. <HR>
  235. <H1><A NAME="the waveout package">THE WaveOut PACKAGE</A></H1>
  236. <P>Win32::Sound also provides a different, more
  237. powerful approach to wave audio data with its 
  238. <A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> package. It has methods to load and
  239. then play WAV files, with the additional feature
  240. of specifying the start and end range, so you
  241. can play only a portion of an audio file.</P>
  242. <P>Furthermore, it is possible to load arbitrary
  243. binary data to the soundcard to let it play and
  244. save them back into WAV files; in a few words,
  245. you can do some sound synthesis work.</P>
  246. <P>
  247. <H2><A NAME="functions">FUNCTIONS</A></H2>
  248. <DL>
  249. <DT><STRONG><A NAME="item_WaveOut">new Win32::Sound::WaveOut(FILENAME)</A></STRONG><BR>
  250. <DD>
  251. <DT><STRONG>new Win32::Sound::WaveOut(SAMPLERATE, BITS, CHANNELS)</STRONG><BR>
  252. <DD>
  253. <DT><STRONG>new Win32::Sound::WaveOut()</STRONG><BR>
  254. <DD>
  255. This function creates a <A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> object; the
  256. first form opens the specified wave file (see
  257. also <A HREF="#item_Open"><CODE>Open()</CODE></A> ), so you can directly <A HREF="#item_Play"><CODE>Play()</CODE></A> it.
  258. <P>The second (and third) form opens the
  259. wave output device with the format given
  260. (or if none given, defaults to 44.1kHz,
  261. 16 bits, stereo); to produce something
  262. audible you can either <A HREF="#item_Open"><CODE>Open()</CODE></A> a wave file
  263. or <A HREF="#item_Load"><CODE>Load()</CODE></A> binary data to the soundcard
  264. and then <A HREF="#item_Write"><CODE>Write()</CODE></A> it.</P>
  265. <P></P>
  266. <DT><STRONG><A NAME="item_Close"><CODE>Close()</CODE></A></STRONG><BR>
  267. <DD>
  268. Closes the wave file currently opened.
  269. <P></P>
  270. <DT><STRONG><A NAME="item_CloseDevice"><CODE>CloseDevice()</CODE></A></STRONG><BR>
  271. <DD>
  272. Closes the wave output device; you can change
  273. format and reopen it with <A HREF="#item_OpenDevice"><CODE>OpenDevice()</CODE></A>.
  274. <P></P>
  275. <DT><STRONG><A NAME="item_GetErrorText"><CODE>GetErrorText(ERROR)</CODE></A></STRONG><BR>
  276. <DD>
  277. Returns the error text associated with
  278. the specified ERROR number; note it only
  279. works for wave-output-specific errors.
  280. <P></P>
  281. <DT><STRONG><A NAME="item_Load"><CODE>Load(DATA)</CODE></A></STRONG><BR>
  282. <DD>
  283. Loads the DATA buffer in the soundcard.
  284. The format of the data buffer depends
  285. on the format used; for example, with
  286. 8 bit mono each sample is one character,
  287. while with 16 bit stereo each sample is
  288. four characters long (two 16 bit values
  289. for left and right channels). The sample
  290. rate defines how much samples are in one
  291. second of sound. For example, to fit one
  292. second at 44.1kHz 16 bit stereo your buffer
  293. must contain 176400 bytes (44100 * 4).
  294. <P></P>
  295. <DT><STRONG><A NAME="item_Open"><CODE>Open(FILE)</CODE></A></STRONG><BR>
  296. <DD>
  297. Opens the specified wave FILE.
  298. <P></P>
  299. <DT><STRONG><A NAME="item_OpenDevice"><CODE>OpenDevice()</CODE></A></STRONG><BR>
  300. <DD>
  301. Opens the wave output device with the
  302. current sound format (not needed unless
  303. you used <A HREF="#item_CloseDevice"><CODE>CloseDevice()</CODE></A>).
  304. <P></P>
  305. <DT><STRONG><A NAME="item_Pause"><CODE>Pause()</CODE></A></STRONG><BR>
  306. <DD>
  307. Pauses the sound currently playing; 
  308. use <A HREF="#item_Restart"><CODE>Restart()</CODE></A> to continue playing.
  309. <P></P>
  310. <DT><STRONG>Play( [FROM, TO] )</STRONG><BR>
  311. <DD>
  312. Plays the opened wave file. You can optionally
  313. specify a FROM - TO range, where FROM and TO
  314. are expressed in samples (or use FROM=0 for the
  315. first sample and TO=-1 for the last sample).
  316. Playback happens always asynchronously, eg. in 
  317. the background.
  318. <P></P>
  319. <DT><STRONG><A NAME="item_Position"><CODE>Position()</CODE></A></STRONG><BR>
  320. <DD>
  321. Returns the sample number currently playing;
  322. note that the play position is not zeroed
  323. when the sound ends, so you have to call a
  324. <A HREF="#item_Reset"><CODE>Reset()</CODE></A> between plays to receive the
  325. correct position in the current sound.
  326. <P></P>
  327. <DT><STRONG><A NAME="item_Reset"><CODE>Reset()</CODE></A></STRONG><BR>
  328. <DD>
  329. Stops playing and resets the play position
  330. (see <A HREF="#item_Position"><CODE>Position()</CODE></A>).
  331. <P></P>
  332. <DT><STRONG><A NAME="item_Restart"><CODE>Restart()</CODE></A></STRONG><BR>
  333. <DD>
  334. Continues playing the sound paused by <A HREF="#item_Pause"><CODE>Pause()</CODE></A>.
  335. <P></P>
  336. <DT><STRONG><A NAME="item_Save">Save(FILE, [DATA])</A></STRONG><BR>
  337. <DD>
  338. Writes the DATA buffer (if not given, uses the 
  339. buffer currently loaded in the soundcard) 
  340. to the specified wave FILE.
  341. <P></P>
  342. <DT><STRONG><A NAME="item_Status"><CODE>Status()</CODE></A></STRONG><BR>
  343. <DD>
  344. Returns 0 if the soundcard is currently playing,
  345. 1 if it's free, or <A HREF="../../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> on errors.
  346. <P></P>
  347. <DT><STRONG><A NAME="item_Unload"><CODE>Unload()</CODE></A></STRONG><BR>
  348. <DD>
  349. Frees the soundcard from the loaded data.
  350. <P></P>
  351. <DT><STRONG>Volume( [LEFT, RIGHT] )</STRONG><BR>
  352. <DD>
  353. Gets or sets the volume for the wave output device.
  354. It works the same way as Win32::Sound::Volume.
  355. <P></P>
  356. <DT><STRONG><A NAME="item_Write"><CODE>Write()</CODE></A></STRONG><BR>
  357. <DD>
  358. Plays the data currently loaded in the soundcard;
  359. playback happens always asynchronously, eg. in 
  360. the background.
  361. <P></P></DL>
  362. <P>
  363. <H2><A NAME="the sound format">THE SOUND FORMAT</A></H2>
  364. <P>The sound format is stored in three properties of
  365. the <A HREF="#item_WaveOut"><CODE>WaveOut</CODE></A> object: <CODE>samplerate</CODE>, <CODE>bits</CODE> and
  366. <A HREF="#item_channels"><CODE>channels</CODE></A>.
  367. If you need to change them without creating a 
  368. new object, you should close before and reopen 
  369. afterwards the device.</P>
  370. <PRE>
  371.     $WAV->CloseDevice();
  372.     $WAV->{samplerate} = 44100; # 44.1kHz
  373.     $WAV->{bits}       = 8;     # 8 bit
  374.     $WAV->{channels}   = 1;     # mono
  375.     $WAV->OpenDevice();</PRE>
  376. <P>You can also use the properties to query the
  377. sound format currently used.</P>
  378. <P>
  379. <H2><A NAME="example">EXAMPLE</A></H2>
  380. <P>This small example produces a 1 second sinusoidal
  381. wave at 440Hz and saves it in <EM>sinus.wav</EM>:</P>
  382. <PRE>
  383.     use Win32::Sound;
  384. </PRE>
  385. <PRE>
  386.  
  387.     # Create the object
  388.     $WAV = new Win32::Sound::WaveOut(44100, 8, 2);</PRE>
  389. <PRE>
  390.  
  391.     $data = ""; 
  392.     $counter = 0;
  393.     $increment = 440/44100;</PRE>
  394. <PRE>
  395.  
  396.     # Generate 44100 samples ( = 1 second)
  397.     for $i (1..44100) {</PRE>
  398. <PRE>
  399.         # Calculate the pitch 
  400.         # (range 0..255 for 8 bits)
  401.         $v = sin($counter/2*3.14) * 128 + 128;</PRE>
  402. <PRE>
  403.         # "pack" it twice for left and right
  404.         $data .= pack("cc", $v, $v);</PRE>
  405. <PRE>
  406.         $counter += $increment;
  407.     }
  408. </PRE>
  409. <PRE>
  410.  
  411.     $WAV->Load($data);       # get it
  412.     $WAV->Write();           # hear it
  413.     1 until $WAV->Status();  # wait for completion
  414.     $WAV->Save("sinus.wav"); # write to disk
  415.     $WAV->Unload();          # drop it</PRE>
  416. <P>
  417. <HR>
  418. <H1><A NAME="version">VERSION</A></H1>
  419. <P>Win32::Sound version 0.45, 09 Apr 1999.</P>
  420. <P>
  421. <HR>
  422. <H1><A NAME="author">AUTHOR</A></H1>
  423. <P>Aldo Calpini, <CODE>dada@divinf.it</CODE></P>
  424. <P>Parts of the code provided and/or suggested by Dave Roth.</P>
  425. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  426. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  427. <STRONG><P CLASS=block> Win32::Sound - An extension to play with Windows sounds</P></STRONG>
  428. </TD></TR>
  429. </TABLE>
  430.  
  431. </BODY>
  432.  
  433. </HTML>
  434.