Sound

Simply read and write wave files for sound editing.

Wav File Functions

sound.read_sound(file_name)

Returns the samples and sample rate of a wav file.

Parameters:file_name (str) – The name of the wav file to read. The wav file must not use any compression.
Returns:(list(float), int) – The wav file’s samples as a list of floats in the range -1 to 1 and the wav file’s sample rate in samples per second.
sound.write_sound(file_name, samples, sample_rate)

Write a list of samples in the range -1 to 1 as a 16-bit mono wav.

Parameters:
  • file_name (str) – The name of the wav file to write.
  • samples (list(float)) – The list of samples to write to the file. The values must be in the range -1 to 1.
  • sample_rate (int) – The number of samples per second that the wav file should play.

Examples

Here is an example of using the Sound module to lower the volume of a sound file:

import sound

sound_file_name = 'pretty_sound.wav'
fraction_decrease = 0.5
(samples, sample_rate) = sound.read_sound(sound_file_name)
for i in range(len(samples)):
    samples[i] = samples[i] * fraction_decrease
sound.write_sound(sound_file_name, samples, sample_rate)

Table Of Contents

Previous topic

Welcome to Sound’s documentation!

This Page