The wave module in Python's standard library is an easy interface to the audio WAV format. The functions in this module can write audio data in raw format to a file like object and read the attributes of a WAV file.
The file is opened in 'write' or read mode just as with built-in open() function, but with open() function in wave module
wave.open()
This function opens a file to read/write audio data. The function needs two parameters - first the file name and second the mode. The mode can be 'wb' for writing audio data or 'rb' for reading.
obj = wave.open('sound.wav','wb')A mode of 'rb' returns a Wave_read object, while a mode of 'wb' returns a Wave_write object.
Wave_write object has following methods
| close() | Close the file if it was opened by wave. |
| setnchannels() | Set the number of channels. 1 for Mono 2 for stereo channels |
| setsampwidth() | Set the sample width to n bytes. |
| setframerate() | Set the frame rate to n. |
| setnframes() | Set the number of frames to n. |
| setcomptype() | Set the compression type and description. At the moment, only compression type NONE is supported, meaning no compression. |
| setparams() | accepts parameter tuple (nchannels, sampwidth, framerate, nframes, comptype, compname) |
| tell() | Retrieves current position in the file |
| writeframesraw() | Write audio frames, without correcting. |
| writeframes() | Write audio frames and make sure they are correct. |
Following code creates a WAV file with random short integer bytes of 99999 seconds duration.
import wave, struct, math, random
sampleRate = 44100.0 # hertz
duration = 1.0 # seconds
frequency = 440.0 # hertz
obj = wave.open('sound.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2)
obj.setframerate(sampleRate)
for i in range(99999):
value = random.randint(-32767, 32767)
data = struct.pack('<h', value)
obj.writeframesraw( data )
obj.close()Wave_read object methods
| close() | Close the stream if it was opened by wave module. |
| getnchannels() | Returns number of audio channels (1 for mono, 2 for stereo). |
| getsampwidth() | Returns sample width in bytes. |
| getframerate() | Returns sampling frequency. |
| getnframes() | Returns number of audio frames. |
| getcomptype() | Returns compression type ('NONE' is the only supported type). |
| getparams() | Returns a namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname), equivalent to output of the get*() methods. |
| readframes(n) | Reads and returns at most n frames of audio, as a bytes object. |
| rewind() | Rewind the file pointer to the beginning of the audio stream. |
Following code reads some of the parameters of WAV file.
import wave
obj = wave.open('sound.wav','r')
print( "Number of channels",obj.getnchannels())
print ( "Sample width",obj.getsampwidth())
print ( "Frame rate.",obj.getframerate())
print ("Number of frames",obj.getnframes())
print ( "parameters:",obj.getparams())
obj.close()Output
Number of channels 1 Sample width 2 Frame rate. 44100 Number of frames 99999 parameters: _wave_params(nchannels=1, sampwidth=2, framerate=44100, nframes=99999, comptype='NONE', compname='not compressed')