Python воспроизвести wav файл

winsound — Sound-playing interface for Windows¶

The winsound module provides access to the basic sound-playing machinery provided by Windows platforms. It includes functions and several constants.

winsound. Beep ( frequency , duration ) ¶

Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32,767. The duration parameter specifies the number of milliseconds the sound should last. If the system is not able to beep the speaker, RuntimeError is raised.

winsound. PlaySound ( sound , flags ) ¶

Call the underlying PlaySound() function from the Platform API. The sound parameter may be a filename, a system sound alias, audio data as a bytes-like object , or None . Its interpretation depends on the value of flags, which can be a bitwise ORed combination of the constants described below. If the sound parameter is None , any currently playing waveform sound is stopped. If the system indicates an error, RuntimeError is raised.

winsound. MessageBeep ( type = MB_OK ) ¶

Call the underlying MessageBeep() function from the Platform API. This plays a sound as specified in the registry. The type argument specifies which sound to play; possible values are -1 , MB_ICONASTERISK , MB_ICONEXCLAMATION , MB_ICONHAND , MB_ICONQUESTION , and MB_OK , all described below. The value -1 produces a “simple beep”; this is the final fallback if a sound cannot be played otherwise. If the system indicates an error, RuntimeError is raised.

The sound parameter is the name of a WAV file. Do not use with SND_ALIAS .

The sound parameter is a sound association name from the registry. If the registry contains no such name, play the system default sound unless SND_NODEFAULT is also specified. If no default sound is registered, raise RuntimeError . Do not use with SND_FILENAME .

All Win32 systems support at least the following; most systems support many more:

Читайте также:  Адаптивный шаблон

Corresponding Control Panel Sound name

Источник

wave — Read and write WAV files¶

The wave module provides a convenient interface to the Waveform Audio “WAVE” (or “WAV”) file format. Only files using WAVE_FORMAT_PCM are supported. Note that this does not include files using WAVE_FORMAT_EXTENSIBLE even if the subformat is PCM.

The wave module defines the following function and exception:

wave. open ( file , mode = None ) ¶

If file is a string, open the file by that name, otherwise treat it as a file-like object. mode can be:

Note that it does not allow read/write WAV files.

A mode of ‘rb’ returns a Wave_read object, while a mode of ‘wb’ returns a Wave_write object. If mode is omitted and a file-like object is passed as file, file.mode is used as the default value for mode.

If you pass in a file-like object, the wave object will not close it when its close() method is called; it is the caller’s responsibility to close the file object.

The open() function may be used in a with statement. When the with block completes, the Wave_read.close() or Wave_write.close() method is called.

Changed in version 3.4: Added support for unseekable files.

An error raised when something is impossible because it violates the WAV specification or hits an implementation deficiency.

Wave_read Objects¶

Wave_read objects, as returned by open() , have the following methods:

Close the stream if it was opened by wave , and make the instance unusable. This is called automatically on object collection.

Returns number of audio channels ( 1 for mono, 2 for stereo).

Returns sample width in bytes.

Returns sampling frequency.

Читайте также:  Не питона а пейтон

Returns number of audio frames.

Returns compression type ( ‘NONE’ is the only supported type).

Human-readable version of getcomptype() . Usually ‘not compressed’ parallels ‘NONE’ .

Returns a namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname) , equivalent to output of the get*() methods.

Reads and returns at most n frames of audio, as a bytes object.

Rewind the file pointer to the beginning of the audio stream.

The following two methods are defined for compatibility with the aifc module, and don’t do anything interesting.

The following two methods define a term “position” which is compatible between them, and is otherwise implementation dependent.

Set the file pointer to the specified position.

Return current file pointer position.

Wave_write Objects¶

Wave_write objects, as returned by open() .

For seekable output streams, the wave header will automatically be updated to reflect the number of frames actually written. For unseekable streams, the nframes value must be accurate when the first frame data is written. An accurate nframes value can be achieved either by calling setnframes() or setparams() with the number of frames that will be written before close() is called and then using writeframesraw() to write the frame data, or by calling writeframes() with all of the frame data to be written. In the latter case writeframes() will calculate the number of frames in the data and set nframes accordingly before writing the frame data.

Changed in version 3.4: Added support for unseekable files.

Wave_write objects have the following methods:

Make sure nframes is correct, and close the file if it was opened by wave . This method is called upon object collection. It will raise an exception if the output stream is not seekable and nframes does not match the number of frames actually written.

Читайте также:  Что такое html eng

Set the number of channels.

Set the sample width to n bytes.

Changed in version 3.2: A non-integral input to this method is rounded to the nearest integer.

Set the number of frames to n. This will be changed later if the number of frames actually written is different (this update attempt will raise an error if the output stream is not seekable).

Set the compression type and description. At the moment, only compression type NONE is supported, meaning no compression.

The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname) , with values valid for the set*() methods. Sets all parameters.

Return current position in the file, with the same disclaimer for the Wave_read.tell() and Wave_read.setpos() methods.

Write audio frames, without correcting nframes.

Changed in version 3.4: Any bytes-like object is now accepted.

Write audio frames and make sure nframes is correct. It will raise an error if the output stream is not seekable and the total number of frames that have been written after data has been written does not match the previously set value for nframes.

Changed in version 3.4: Any bytes-like object is now accepted.

Note that it is invalid to set any parameters after calling writeframes() or writeframesraw() , and any attempt to do so will raise wave.Error .

Источник

Оцените статью