Package: wavfile
wavfileA lightweight package to read/write wave audio files to/from lists of
native Python types.The package currently supports PCM (integer) and IEEE float formats, and
supports arbitrary integer precision, including: 16-, 24-, 32-, and
64-bit samples.View online documentation athttps://pywavfile.readthedocs.io/.Usage: reading wave filesThe package provideswavfile.opento open audio files for reading and
writing. For reading:f = wavfile.open(file, 'r')wherefileis either a path to a wave file or a pointer to an open
file. This returns awavfile.wavread.WavReadobject with the following properties:num_channelsThe number of audio channels in the stream.sample_rateThe sampling rate/frequency of the audio stream.bits_per_sampleThe number of bits per audio sample.num_framesThe total number of audio frames in the audio stream. A frame is a
block of samples, one for each channel, corresponding to a single
sampling point.durationThe duration of the audio file in seconds.hmsThe duration of the audio file formatted as hh:mm:ss.tt.audio_fmtThe audio sample format.formatThe wave format code.metadataA dictionary containing metadata encoded in the file.The object also has the following methods:read([N])Read, at most,Nframes from the audio stream in their unmodified
native format. The method returns a list of lists with size(N,C), whereCis the number of audio channels. ExcludingN, choosingN = NoneorN < 0will read all remaining
samples.read_int([N])This method is identical toread()except that it returns the samples
as integers using the specified bit depth.read_float([N])This method is identical toread()except that it returns the samples
as floats in the range [-1, 1).iter([N])Similar toread()but used in iterator contexts to read successive
groups of audio frames.iter_int([N])Similar toread_int()but used in iterator contexts to read successive
groups of audio frames.iter_float([N])Similar toread_float()but used in iterator contexts to read
successive groups of audio frames.seek(N [, whence])Move to theNth frame in the audio stream;whencesets the
positioning: 0 (default) = absolute positioning, 1 = relative to
current position, 2 = relative to end of last frame.tell()Return the current frame in the audio stream.close()Close the instance.Alternatively, thewavfile.readshortcut function is provided:audio, sample_rate, bits_per_sample = wavfile.read(file, fmt='int')wherefmtis'int','float', or'native'; andaudiois the audio data. The function reads all audio data in the file.Usage: writing wave filesf = wavfile.open(file, 'w',
                 sample_rate=44100,
                 num_channels=None,
                 bits_per_sample=16,
                 fmt=wavfile.chunk.WavFormat.PCM)wheresample_rateis the sampling rate for the new file,num_channelsis the number of audio channels,bits_per_sampleis
the number of bits used to encode each sample, andfmtis the wave
format code. Ifnum_channelsis unspecified it will be determined
automatically from the first block of samples that are written (see
below). This returns awavfile.wavwrite.WavWriteobject. The object shares its
properties with thewavfile.wavread.WavReadclass.The format code is eitherwavfile.chunk.WavFormat.PCM,wavfile.chunk.WavFormat.IEEE_FLOAT, orwavfile.chunk.WavFormat.EXTENSIBLE. The extensible code corresponds
to a variation of the wave file format intended for audio with: a bit
depth of greater than 16 bits, or more than two channels. The file will
be updated automatically to use the extensible format as appropriate. If
the extensible format is specified explicitly, then the audio data will
be PCM encoded. Theaudio_fmtproperty always reports the audio sample
format, whereas theformatproperty reports the format code that may
be any of the three aforementioned values.The object also offers the sameseek(),tell(), andclose()methods.
In addition, the following methods are provided for writing audio data:write(audio)Write frames of audio data to the audio file. The data should be
contained in a list of lists with size(N,C), whereNis the
number of frames andCis the number of audio channels. The data
may beintorfloat. The data may be converted if they do
match the format of the destination file.write_int(audio)Write frames of integer audio data to the audio file. The data may be
converted if they do match the format of the destination file.write_float(audio)Write frames of float audio data to the audio file. The data may be
converted if they do match the format of the destination file.add_metadata(**kwargs)Add metadata to the .wav file.Alternatively, thewavfile.writeshortcut function is provided:wavfile.write(file,
              audio,
              sample_rate=44100,
              bits_per_sample=16,
              fmt=wavfile.chunk.WavFormat.PCM,
              metadata=None)whereaudiois the audio data to write to the file.InstallationFrom source:python -m pip install -e --user .From PyPI:python -m pip install --user wavfileLicensewavfileis available under the MIT license. See LICENSE.txt for more
information.