Sound Processing Utilities

This module contains two classes:

Classes

sounds.Sound([inFile, inData, inRate])

Class for working with sound in Python.

sounds.FFMPEG_info()

Class for storing the config-info for FFMPEG.

Methods Sound

sounds.Sound.generate_sound(data, rate)

Set the properties of a Sound-object.

sounds.Sound.get_info()

Return information about the sound.

sounds.Sound.play()

Play the stored sound

sounds.Sound.read_sound(inFile)

Read data from a sound-file.

sounds.Sound.summary()

Display information about the sound.

sounds.Sound.write_wav([full_out_file])

Write sound data to a WAV-file.

Methods FFMPEG_info

sounds.FFMPEG_info.set()

Set the config-filename, and write the FFMPEG_info properties “ffmpeg” and “ffplay” to that config-file.

Details

Python module to read, play, and write sound data. For flexibility, FFMPEG is used for non-WAV files.

If you re-set the location of FFMPEG, please run the following code:

>>> ffmpeg = FFMPEG_info()
>>> ffmpeg.set()
You can obtain it for free from

http://ffmpeg.org

Mac users using Anaconda should follow the instructions on

Note that FFMPEG must be installed externally, not as a Python package!! Please install ffmpeg/ffplay in the following directory:

  • Windows: “C:\Program Files\ffmpeg\bin\”

  • Mac: “/usr/local/bin/” (is already included in the default paths of the Mac terminal.)

  • Linux: “/usr/bin/”

Compatible with Python >=3.5

class sounds.FFMPEG_info[source]

Class for storing the config-info for FFMPEG.

If first checks if FFMPEG is installed. FFMPEG is necessary to read MP3-files etc. Once checked, the corresponding information is saved in “ffmpeg.json”, under “/FFMPEG_info/sksound/”:

FFMPEG_info properties:
  • config_file : JSON-file, with the config-information

  • ffmpeg : Commandline location of the command “ffmpeg”

  • ffplay : Commandline location of the command “ffplay”

set()[source]

Set the config-filename, and write the FFMPEG_info properties “ffmpeg” and “ffplay” to that config-file.

If FFMPEG is not installed, these are set to “None”.

exception sounds.NoFFMPEG_Error[source]
class sounds.Sound(inFile=None, inData=None, inRate=None)[source]

Class for working with sound in Python.

A Sound object can be initialized
  • by giving a filename

  • by providing “int16” data and a rate

  • without giving any parameter; in that case the user is prompted to select an infile

Parameters
  • inFile (string) – path- and file-name of infile, if you get the sound from a file.

  • inData (array) – manually generated sound data; requires “inRate” to be set, too.

  • inRate (integer) – sample rate; required if “inData” are entered.

Returns

No return value. Initializes the Sound-properties.

Return type

None

Notes

For non WAV-files, the file is first converted to WAV using FFMPEG, and then read in. A warning is generated, to avoid unintentional deletion of existing WAV-files.

SoundProperties:
  • source

  • data

  • rate

  • numChannels

  • totalSamples

  • duration

  • bitsPerSample

SoundMethods:
  • generate_sound

  • get_info

  • play

  • read_sound

  • summary

  • write_wav

Examples

>>> from sksound.sounds import Sound
>>> mySound1 = Sound()                  # here the user is prompted for an input file
>>> mySound2 = Sound('test.wav')     # here the input file is provided directly
>>>
>>> rate = 22050
>>> dt = 1./rate
>>> freq = 440
>>> t = np.arange(0,0.5,dt)
>>> x = np.sin(2*np.pi*freq * t)
>>> amp = 2**13
>>> sounddata = np.int16(x*amp)
>>> mySound3 = Sound(inData=sounddata, inRate=rate)
generate_sound(data, rate)[source]

Set the properties of a Sound-object.

get_info()[source]

Return information about the sound.

Parameters

None

Returns

  • source (name of inFile)

  • rate (sampleRate)

  • numChannels (number of channels)

  • totalSamples (number of total samples)

  • duration (duration [sec])

  • bitsPerSample (bits per sample)

Examples

>>> mySound = Sound('test.wav')
>>> info = mySound.get_info()
>>> (source, rate, numChannels, totalSamples, duration, bitsPerSample) = mySound.get_info()
play()[source]

Play the stored sound

Parameters

None

Returns

Return type

None

Notes

On “Windows” the module “winsound” is used; on “Linux” I use “pygame”; and on “OSX” the terminal command “afplay”.

Examples

>>> mySound = Sound('test.wav')
>>> mySound.play()
read_sound(inFile)[source]

Read data from a sound-file.

Parameters

inFile (string) – path- and file-name of infile

Returns

No return value. Sets the property “data” of the object.

Return type

None

Notes

  • For non WAV-files, the file is first converted to WAV using FFMPEG, and then read in.

  • If FFMPEG is not installed, non-WAV files produce a “sounds.NoFFMPEG_Error”

Examples

>>> mySound = Sound('test.wav')
>>> mySound.play()
>>> mySound.read_sound('test2.wav') # If you want to read in another(!) file
summary()[source]

Display information about the sound.

Parameters

None

Returns

Return type

None

Examples

>>> mySound = Sound()
>>> mySound.read_sound('test.wav')
>>> mySound.summary()
write_wav(full_out_file=None)[source]

Write sound data to a WAV-file.

Parameters

fullOutFile (string) – Path- and file-name of the outfile. If none is given, the user is asked interactively to choose a folder/name for the outfile.

Returns

Return type

None

Examples

>>> mySound = Sound('test.wav')
>>> mySound.write_wav()
sounds.main()[source]

Main function, to test the module