Problem:

You want to read and manipulate sound (*.wav) files but since you are on a company-controlled system, you are not allowed to install pyaudio or other excellent audio packages.

How do you solve this using basic standard python package?

Solution:

from scipy.io.wavfile import read, write

Example:

from scipy.io.wavfile import read, write

fname="some.wav"
rate, data = read(fname)

# data is a numpy array which can be manipulated as needed.
# here  multiplying it 20 time
data = data*20
# let's write it out

write("output.wav", rate, data)

These two functions recently helped me in a personal machine learning project. I love it when such simple solutions are available just out of the box.

Help on the two functions.

Signature: read(filename, mmap=False)
Docstring:
Open a WAV file

Return the sample rate (in samples/sec) and data from a WAV file.

Parameters
----------
filename : string or open file handle
    Input wav file.
mmap : bool, optional
    Whether to read data as memory-mapped.
    Only to be used on real files (Default: False).

    .. versionadded:: 0.12.0

Returns
-------
rate : int
    Sample rate of wav file.
data : numpy array
    Data read from wav file.  Data-type is determined from the file;
Signature: write(filename, rate, data)

Docstring:
Write a numpy array as a WAV file.

Parameters
----------
filename : string or open file handle
    Output wav file.
rate : int
    The sample rate (in samples/sec).
data : ndarray
    A 1-D or 2-D numpy array of either integer or float data-type.

Notes
-----
* Writes a simple uncompressed WAV file.
* To write multiple-channels, use a 2-D array of shape
  (Nsamples, Nchannels).
* The bits-per-sample and PCM/float will be determined by the data-type.

Leave a comment

Trending