SOUND
PROCESSING.
LESSON OBJECTIVES
- USE COMMANDS OF THE WAVE LIBRARY TO PROCESS SOUND FILES
Assessment criteria:
- Can use commands of the Wave library to process sound files: play, copy
and write the sound file
Install wave library via command line
Install pyaudio library via command line
• The wave module provides a convenient
interface to the WAV sound 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:
• If file is a string, open the file by that name, otherwise treat it as a file-like object. mode can be:
• 'rb'
• Read only mode.
• 'wb'
• Write only mode.
• 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, [Link] 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.
Read frames in *.wav file
Struct library
• Wave_read.close()
• Close the stream if it was opened by wave, and make the instance unusable. This is
called automatically on object collection.
• Wave_read.getnchannels()
• Returns number of audio channels (1 for mono, 2 for stereo).
• Wave_read.getsampwidth()
• Returns sample width in bytes.
• Wave_read.getframerate()
• Returns sampling frequency.
• Wave_read.getnframes()
Returns number of audio frames.
• Wave_read.getcomptype()
Returns compression type ('NONE' is the only supported type).
• Wave_read.getcompname()
Human-readable version of getcomptype(). Usually 'not compressed' parallels 'NONE'.
• Wave_read.getparams()
Returns a namedtuple() (nchannels, sampwidth, framerate, nframes, comptype,
compname), equivalent to output of the get*() methods.
• Wave_read.readframes(n)
Reads and returns at most n frames of audio, as a bytes object.
• Wave_read.rewind()
Rewind the file pointer to the beginning of the audio stream.
• Wave_write.close()
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.
• Wave_write.setnchannels(n)
Set the number of channels.
• Wave_write.setsampwidth(n)
Set the sample width to n bytes.
• Wave_write.setframerate(n)
Set the frame rate to n.
Changed in version 3.2: A non-integral input to this method is rounded to the nearest integer.
• Wave_write.setnframes(n)
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)
Task 1: Write the code to play a sound in
Python
№ Criteria Descriptors
Can use commands of the Wave library to Write the code to connect wave library;
Write the code for open file;
process sound files: play, copy and write Write code to play the sound
the sound file
Task 2: Record your voice as audio
№ Criteria Descriptors
Can use commands of the Wave library to Write the code to connect wave library;
Write the code for open file;
process sound files: play, copy and write Write code to play the sound
the sound file Write the code to record audio from microphone