Working with audio files

The files module contains functions related to handling audio data files, for example loading audio files, saving audio files, and examing and reformatting audio files.

soundpy.files.loadsound(filename, sr=None, mono=True, dur_sec=None, remove_dc=True, use_scipy=False)[source]

Loads sound file with scipy.io.wavfile.read or librosa.load (default librosa)

Parameters
  • filename (str) – The filename of the sound to be loaded

  • sr (int, optional) – The desired sample rate of the audio samples. If None, the sample rate of the audio file will be used.

  • mono (bool) – If True, the samples will be loaded in mono sound. If False, if the samples are in stereo, they will be loaded in stereo sound.

  • dur_sec (int, float, optional) – The length in seconds of the audio signal.

  • remove_dc_bias (bool) – If True, the mean is subtracted from the signal. This has shown to be very helpful when working with audio data. (default True)

  • use_scipy (bool) – If False, librosa will be used to load the audiofile. If True, scipy.io.wavfile and/or soundfile will be used. If the sound file is not compatible with scipy.io.wavfile.read, this functions converts the file to .wav format and/or changes the bit depth to be compatible. (default False)

Returns

  • data (nd.array [size=(num_samples,) or (num_samples, num_channels)]) – The normalized (between 1 and -1) sample data returned according to the specified settings.

  • sr (int) – The sample rate of the loaded samples.

See also

soundpy.files.prep4scipywavfile

Prepares audio file for scipy.io.wavfile.read.

soundpy.files.convert_audiofile

Converts audio file to .wav format.

soundpy.files.newbitdepth

Converts audio file to specified bitdepth.

soundpy.dsp.resample_audio

Resampe audio data to a specified sample rate.

soundpy.files.list_possibleformats

Lists the possible formats to load with soundpy.loadsound

librosa.load

The package used to load sound data by default. See librosa.

scipy.io.wavfile.read

The package used to load sound if use_scipy is set to True. See scipy.

soundpy.dsp.remove_dc_bias

Removes the ‘direct current’ bias from the signal.

soundpy.files.savesound(audiofile_name, signal_values, sr, remove_dc=True, overwrite=False, use_scipy=False, **kwargs)[source]

saves the wave at designated path

Parameters
  • audiofile_name (str or pathlib.PosixPath) – path and name the audio is to be saved under. (.wav format)

  • signal_values (ndarray) – values of real signal to be saved

  • sr (int) – sample rate of the audio samples.

  • remove_dc (bool) – If True, the mean is subtracted from the signal. (default True)

  • overwrite (bool) – If True, audio with the same naem will be overwritten. (default False)

  • use_scipy (bool) – If True, scipy.io.wavfile.write will be used. However, file conversion is limited. Can only save .wav files. Otherwise soundfile.write will be used, which can save audio under more audio fomats.

  • **kwargs (additional keyword arguments) – The keyword arguments for soundfile.write: https://pysoundfile.readthedocs.io/en/latest/index.html?highlight=write#soundfile.write

Returns

audiofile_name – The new audiofile name

Return type

pathlib.PosixPath

See also

scipy.io.wavfile.write

soundpy.files.conversion_formats

Lists the possible formats to save audio files if use_scipy is False.

soundpy.dsp.remove_dc_bias

Removes the ‘direct current’ bias from the signal.

soundpy.files.get_file_format(audiofile)[source]

Use soundfile to get file format.

soundpy.files.list_possibleformats(use_scipy=False)[source]
soundpy.files.list_audioformats()[source]
soundpy.files.audiofiles_present(directory, recursive=False)[source]

Checks to see if audio files are present.

Parameters
  • directory (str or pathlib.PosixPath) – The directory to look for audio.

  • recursive (bool) – If True, all nested directories will be checked as well. (default False)

Returns

True if audio is present; otherwise False.

Return type

bool

soundpy.files.collect_audiofiles(directory, hidden_files=False, wav_only=False, recursive=False)[source]

Collects all files within a given directory.

This includes the option to include hidden_files in the collection.

Parameters
  • directory (str or pathlib.PosixPath) – The path to where desired files are located.

  • hidden_files (bool) – If True, hidden files will be included. If False, they won’t. (default False)

  • wav_only (bool) – If True, only .wav files will be included. Otherwise, no limit on file type.

Returns

paths_list – Sorted list of file pathways.

Return type

list of pathlib.PosixPath objects

soundpy.files.collect_zipfiles(directory, hidden_files=False, ext='tgz', recursive=False)[source]

Collects all zipfiles within a given directory.

This includes the option to include hidden_files in the collection.

Parameters
  • directory (str or pathlib.PosixPath) – The path to where desired files are located.

  • hidden_files (bool) – If True, hidden files will be included. If False, they won’t. (default False)

  • wav_only (bool) – If True, only .wav files will be included. Otherwise, no limit on file type.

Returns

paths_list – Sorted list of file pathways.

Return type

list of pathlib.PosixPath objects

soundpy.files.ensure_only_audiofiles(audiolist)[source]
soundpy.files.prep4scipywavfile(filename, overwrite=False)[source]

Takes soundfile and saves it in a format compatible with scipy.io.wavfile

Parameters

filename (str) – Filename of the soundfile to load with scipy.io.wavfile

Returns

filename – Filename of the soundfile compatible with scipy.io.wavfile

Return type

str

soundpy.files.conversion_formats()[source]

Lists the formats available for conversion.

soundpy uses soundfile to convert files; therefore, whatever available formats soundfile has will be listed here.

Examples

>>> conversion_formats
{'AIFF': 'AIFF (Apple/SGI)',
'AU': 'AU (Sun/NeXT)',
'AVR': 'AVR (Audio Visual Research)',
'CAF': 'CAF (Apple Core Audio File)',
'FLAC': 'FLAC (Free Lossless Audio Codec)',
'HTK': 'HTK (HMM Tool Kit)',
'SVX': 'IFF (Amiga IFF/SVX8/SV16)',
'MAT4': 'MAT4 (GNU Octave 2.0 / Matlab 4.2)',
'MAT5': 'MAT5 (GNU Octave 2.1 / Matlab 5.0)',
'MPC2K': 'MPC (Akai MPC 2k)',
'OGG': 'OGG (OGG Container format)',
'PAF': 'PAF (Ensoniq PARIS)',
'PVF': 'PVF (Portable Voice Format)',
'RAW': 'RAW (header-less)',
'RF64': 'RF64 (RIFF 64)',
'SD2': 'SD2 (Sound Designer II)',
'SDS': 'SDS (Midi Sample Dump Standard)',
'IRCAM': 'SF (Berkeley/IRCAM/CARL)',
'VOC': 'VOC (Creative Labs)',
'W64': 'W64 (SoundFoundry WAVE 64)',
'WAV': 'WAV (Microsoft)',
'NIST': 'WAV (NIST Sphere)',
'WAVEX': 'WAVEX (Microsoft)',
'WVE': 'WVE (Psion Series 3)',
'XI': 'XI (FastTracker 2)'}
soundpy.files.convert_audiofile(filename, format_type=None, sr=None, new_dir=False, overwrite=False, use_scipy=False, **kwargs)[source]

Converts and saves soundfile as .wav type in same or new directory.

Parameters
  • filename (str or pathlib.PosixPath) – The filename of the audiofile to be converted to .wav type

  • format_type (str) – The format to convert the audio file to. See soundpy.files.conversion_formats. (defaults to ‘wav’)

  • new_dir (str, pathlib.PosixPath, optional) – If False, the converted files will be saved in same directory as originals. If a path is provided, the converted files will be saved there. If no such directory exists, one will be created.

  • sr (int, optional) – The sample rate to be applied to the signal. If none supplied, the sample rate of the original file will be used.

  • **kwargs (additional keyword arguments) – The keyword arguments for soundfile.write: https://pysoundfile.readthedocs.io/en/latest/index.html?highlight=write#soundfile.write

Returns

f_wavfile – The filename / path where the audio file is saved.

Return type

pathlib.PosixPath

Examples

>>> audiofile = './example/audio.wav'
# in same directory
>>> audiofile_flac = sp.files.convert_audiofile(audiofile, format_type='flac')
>>> audiofile_flac
PosixPath('example/audio.flac')
# in new directory
>>> audiofile_flac = sp.files.convert_audiofile(audiofile, format_type='flac',
                                                 new_dir = './examples2/')
>>> audiofile_flac
PosixPath('examples2/audio.flac')
>>> # can establish desired conversion format in `new_dir`
>>> audiofile_ogg = sp.files.convert_audiofile(audiofile,
                                                 new_dir = './examples2/audio.ogg')
>>> audiofile_ogg
PosixPath('audiodata2/audio.ogg')

See also

soundpy.files.conversion_formats

Lists the possible formats to convert audio files.

soundpy.files.list_possibleformats

Lists the possible formats to load with soundpy.loadsound

soundpy.files.replace_ext(filename, extension)[source]

Adds or replaces an extension in the filename

Parameters
  • filename (str or pathlib.PosixPath) – Filename with the missing or incorrect extension

  • extension (str) – The correct extension for the given filename.

Returns

file_newext – The filename with the new extension

Return type

pathlib.PosixPath

soundpy.files.match_ext(filename1, filename2)[source]

Matches the file extensions.

If both have extensions, default set to that of filename1.

soundpy.files.newbitdepth(wave, bitdepth=16, newname=None, overwrite=False)[source]

Convert bitdepth to 16 or 32, to ensure compatibility with scipy.io.wavfile

Scipy.io.wavfile is easily used online, for example in Jupyter notebooks.

soundpy.files.adjustname(filename, adjustment=None)[source]

Adjusts filename.

Parameters
  • filename (str) – The filename to be adjusted

  • adjustment (str, optional) – The adjustment to add to the filename. If None, the string ‘_adj’ will be added.

Returns

fname – The adjusted filename with the original extension

Return type

str

Examples

>>> adjustname('happy.md')
'happy_adj.md'
>>> adjustname('happy.md', '_not_sad')
'happy_not_sad.md'
soundpy.files.extract(tar_url, extract_path='.')[source]
soundpy.files.delete_dir_contents(directory, remove_dir=False)[source]

https://stackoverflow.com/a/28834214

soundpy.files.matching_filenames(list1, list_of_lists)[source]
soundpy.files.remove_contaminated_files(list1, contaminated_files)[source]