Sound Processing Utilities¶
This module contains two classes:
Classes¶
|
Class for working with sound in Python. |
Class for storing the config-info for FFMPEG. |
Methods Sound¶
|
Set the properties of a Sound-object. |
Return information about the sound. |
|
Play the stored sound |
|
|
Read data from a sound-file. |
Display information about the sound. |
|
|
Write sound data to a WAV-file. |
Methods FFMPEG_info¶
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
Mac users using Anaconda should follow the instructions on
Otherwise, the tips under
seemed to work. Binaries are also available from
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”
- class sounds.Sound(inFile: str | PathLike = '', inData: ndarray | None = None, inRate: float | None = 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 or pathlib-path) – 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)
- 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 –
- 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 –
- Return type:
None
Examples
>>> mySound = Sound() >>> mySound.read_sound('test.wav') >>> mySound.summary()
- write_wav(out_file: PathLike | None = None) PathLike | None [source]¶
Write sound data to a WAV-file.
- Parameters:
out_file (path of the outfile. If none is given,) – the user is asked interactively to choose a folder/name for the outfile.
- Returns:
out_file
- Return type:
path of the (selected) outfile
Examples
>>> mySound = Sound('test.wav') >>> mySound.write_wav()