Work with WAV files#

A WAV file is a header specifying format information, followed by a sequence of bytes, representing the state of some audio signal over a length of time.

A WAV file may have any number of channels. Typically, they have 1 (mono) or 2 (for stereo). The data of a WAV file is given as a sequence of frames. A frame consists of samples. There is one sample per channel, per frame. Every wav file has a sample width, or, the number of bytes per sample. Typically this is either 1 or 2 bytes.

The wav module supplies more convenient access to this data. In particular, see the docstring for Wave.channel_data().

The header contains information necessary for playing the WAV file, including the number of frames per second, the number of bytes per sample, and the number of channels in the file.

AUTHORS:

  • Bobby Moretti and Gonzalo Tornaria (2007-07-01): First version

  • William Stein (2007-07-03): add more

  • Bobby Moretti (2007-07-03): add doctests

This module (and all of sage.media) is deprecated.

EXAMPLES:

sage: import sage.media
doctest:warning...
DeprecationWarning: the package sage.media is deprecated
See http://trac.sagemath.org/12673 for details.
class sage.media.wav.Wave(data=None, **kwds)#

Bases: SageObject

A class wrapping a wave audio file.

INPUT:

You must call Wave() with either data = filename, where filename is the name of a wave file, or with each of the following options:

  • channels – the number of channels in the wave file (1 for mono, 2 for stereo, etc…

  • width – the number of bytes per sample

  • framerate – the number of frames per second

  • nframes – the number of frames in the data stream

  • bytes – a string object containing the bytes of the data stream

Slicing:

Slicing a Wave object returns a new wave object that has been trimmed to the bytes that you have given it.

Indexing:

Getting the \(n\)-th item in a Wave object will give you the value of the \(n\)-th frame.

channel_data(n)#

Get the data from a given channel.

INPUT:

n – the channel number to get

OUTPUT:

A list of signed ints, each containing the value of a frame.

convolve(right, channel=0)#

NOT DONE!

Convolution of self and other, i.e., add their fft’s, then inverse fft back.

domain(npoints=None)#

Used internally for plotting. Get the x-values for the various points to plot.

getframerate()#

Return the number of frames per second in this wave object.

OUTPUT:

The frame rate of this sound file.

getlength()#

Return the length of this file (in seconds).

OUTPUT:

The running time of the entire WAV object.

getnchannels()#

Return the number of channels in this wave object.

OUTPUT:

The number of channels in this wave file.

getnframes()#

Return the total number of frames in this wave object.

OUTPUT:

The number of frames in this WAV.

getsampwidth()#

Return the number of bytes per sample in this wave object.

OUTPUT:

The number of bytes in each sample.

listen()#

Listen to (or download) this wave file.

Creates a link to this wave file in the notebook.

plot(npoints=None, channel=0, plotjoined=True, **kwds)#

Plots the audio data.

INPUT:

  • npoints – number of sample points to take; if not given, draws all known points.

  • channel – 0 or 1 (if stereo). default: 0

  • plotjoined – whether to just draw dots or draw lines between sample points

OUTPUT:

a plot object that can be shown.

plot_fft(npoints=None, channel=0, half=True, **kwds)#
plot_raw(npoints=None, channel=0, plotjoined=True, **kwds)#
readframes(n)#

Read out the raw data for the first \(n\) frames of this wave object.

INPUT:

n – the number of frames to return

OUTPUT:

A list of bytes (in string form) representing the raw wav data.

save(filename='sage.wav')#

Save this wave file to disk, either as a Sage sobj or as a .wav file.

INPUT:

filename – the path of the file to save. If filename ends

with ‘wav’, then save as a wave file, otherwise, save a Sage object.

If no input is given, save the file as ‘sage.wav’.

set_values(values, channel=0)#

Used internally for plotting. Get the y-values for the various points to plot.

slice_seconds(start, stop)#

Slice the wave from start to stop.

INPUT:

start – the time index from which to begin the slice (in seconds) stop – the time index from which to end the slice (in seconds)

OUTPUT:

A Wave object whose data is this object’s data, sliced between the given time indices

values(npoints=None, channel=0)#

Used internally for plotting. Get the y-values for the various points to plot.

vector(npoints=None, channel=0)#