This section contains the details of the underlying code used to create the concatenator script. This is included so that developer may use the library for further work in this area and aims to provide a complete description of functions and class structures in an easy to use format.
audiofile.
AudioFile
(filepath, mode, format=None, channels=None, samplerate=None, name=None, *args, **kwargs)[source]¶Object for storing and accessing basic information for an audio file.
This object is a wrapper for the pysndfile audio object. It provides additional functionality alongside the ability to open and close audiofiles without deleting their containing object.
Arguments:
check_mono
()¶Check that the audio file is a mono audio file
check_not_empty
()¶Check that the file contains audio
check_valid
(force_mono=False)¶Test to make sure that the audio file is valid for use. ie mono, not empty
close
()¶Use for closing the associated audio file outside of a with statement
construct_format
(*args, **kwargs)¶construct a format specification for libsndfile from major format string and encoding string
convert_to_mono
(overwrite_original=False)¶Converts stereo audiofiles to mono.
Arguments:
encoding_str
(*args, **kwargs)¶Return string representation of encoding (e.g. pcm16).
See get_sndfile_encodings() for a list of available encoding strings that are supported by a given sndfile format
error
(*args, **kwargs)¶Report error numbers related to the current sound file.
fade_audio
(audio, position, fade_time, mode)¶Fade the audio in or out linearly from the position specified over the time specified.
Arguments:
gen_default_wav
(path, overwrite_existing=False, mode='w', channels=1)¶Convenience method that creates a wav file with the following spec at the path given:
gen_white_noise
(length, gain)¶Generate mono white noise of the number of samples specified.
Arguments:
gen_window
(window_type, window_size, sym=True)¶Generates a window function of given size and type Returns a 1D numpy array
sym: Used in the triangle window generation. When True (default), generates a symmetric window, for use in filter design. When False, generates a periodic window, for use in spectral analysis
Available window types:
generate_grain_times
(grain_length, overlap, save_times=False)¶Generates an array of start and finish pairs based on overlapping frames at the grain length specified.
Note that only full grains within the size of the sample are returned. incomplete grains found at the end of files are ignored.
Arguments:
get_channels
(*args, **kwargs)¶Return number of channels of sndfile.
get_format
(*args, **kwargs)¶Return raw format specification from sndfile.
get_frames
(*args, **kwargs)¶Return number of frames in file (number of samples per channel).
get_pysndfile_version
(*args, **kwargs)¶return tuple describing the version of pysndfile
get_samplerate
(*args, **kwargs)¶Return the samplerate of the file.
get_seek_position
()¶Returns the current seeker position in the file
get_sndfile_encodings
(*args, **kwargs)¶Return lists of available encoding for the given sndfile format.
Arguments:
get_sndfile_formats
(*args, **kwargs)¶Return lists of available file formats supported by libsndfile and pysndfile.
get_sndfile_version
(*args, **kwargs)¶return a tuple of ints representing the version of the libsdnfile that is used
get_strings
(*args, **kwargs)¶get all stringtypes from the sound file.
see stringtype_name_top_id.keys() for the list of strings that are supported by the libsndfile version you use.
major_format_str
(*args, **kwargs)¶return short string representation of major format.
(e.g. aiff) see get_sndfile_formats() for a complete list of file formats
mono_arrays_to_stereo
(array1, array2)¶Converts two horizontal numpy arrays to one concatenated verticaly stacked array that can be written to a stereo file.
ms_to_samps
(ms)¶Converts milliseconds to samples based on the sample rate of the audio file
normalize_audio
(audio, maximum=1.0)¶Normalize array of audio so that the maximum sample value is equal to the maximum provided
normalize_file
(overwrite_original=False)¶Normalizes the entire file
open
()¶Use for opening the associated audio file outside of a with statement
read_frames
(*args, **kwargs)¶Read the given number of frames and fill numpy array.
Arguments
Notes: One column per channel.
read_grain
(*args, **kwargs)¶Read a grain of audio from the file. if grain ends after the end of the file, the grain can be padded with zeros using the padding argument.
Audio object seeker is not changed
Arguments:
rename_file
(*args, **kwargs)¶Renames the audio file associated with the object to the name specified as an argument
Arguments:
replace_audiofile
(replacement_filename)¶Replace the current audiofile and audiofile object with the file specified.
rewind
(*args, **kwargs)¶Rewind read/write/read and write position given by mode to start of file.
samps_to_ms
(samps)¶Convert samples to milliseconds based on the sample rate of the audio file
samps_to_secs
(samps)¶Converts samples to seconds based on the sample rate of the audio file
secs_to_samps
(seconds)¶Converts seconds to samples based on the sample rate of the audio file
seek
(*args, **kwargs)¶Seek into audio file: similar to python seek function, but taking only audio data into account.
Arguments
Returns offset: int the number of frames from the beginning of the file
Notes: Offset relative to audio data: meta-data are ignored. if an invalid seek is given (beyond or before the file), an IOError is raised; note that this is different from the seek method of a File object.
seekable
(*args, **kwargs)¶Return true for soundfiles that support seeking.
set_auto_clipping
(*args, **kwargs)¶Enable auto clipping when reading/writing samples from/to sndfile.
auto clipping is enabled by default. auto clipping is required by libsndfile to properly handle scaling between sndfiles with pcm encoding and float representation of the samples in numpy. When auto clipping is set to on reading pcm data into a float vector and writing it back with libsndfile will reproduce the original samples. If auto clipping is off, samples will be changed slightly as soon as the amplitude is close to the sample range because libsndfile applies slightly different scaling factors during read and write.
set_string
(*args, **kwargs)¶Set one of the stringtypes to the string given as argument.
If you try to write a stringtype that is not supported by the library a RuntimeError will be raised
strError
(*args, **kwargs)¶Report error strings related to the current sound file.
switch_mode
(mode)¶Switch audiofile to mode specified.
This allows for convenient reading and writing of audiofiles without direct closing and opening of the underlying pysndfile object.
writeSync
(*args, **kwargs)¶Call the operating system’s function to force the writing of all file cache buffers to disk. No effect if file is open as read
write_frames
(*args, **kwargs)¶write 1 or 2 dimensional array into sndfile.
Arguments:
containing data to write.
Notes:
One column per channel.
updates the write pointer.
if the input type is float, and the file encoding is an integer type, you should make sure the input data are normalized normalized data (that is in the range [-1..1] - which will corresponds to the maximum range allowed by the integer bitwidth).
audiofile.
AnalysedAudioFile
(*args, **kwargs)[source]¶Generates and stores analysis information for an audio file.
Arguments:
analysis_data_grains
(times, analysis, *args, **kwargs)¶retrieve data for analysis within start and end time pairs in the format specified.
Arguments:
create_analysis
()¶Generate all analyses that have been set in the self.available_analyses member.
create_analysis_group
(analysis_file)¶Create HDF5 group for object to store analyses for this audio file.
Audio file analyses are organized in groups per audio file. This function creates a group in the analysis HDF5 file with the name of the audio file. Analyses of this file are stored in analysis sub-groups.
database.
AudioDatabase
(audio_dir=None, db_dir=None, analysis_list=[], *args, **kwargs)[source]¶A class for encapsulating a database of AnalysedAudioFile objects.
Arguments:
add_file
(file_object)¶Add an AnalysedAudioFile object to the database
analyse_database
(subdir_paths, reanalyse)¶create selected analyses for audio files in the database.
Parameters:
create_subdirs
()¶Generate database folder structure at the self.db_dir location
If the folder structure already exists this will be used, else a new structure will be generated.
load_database
(reanalyse=False)¶Create/Read from a pre-existing database
organize_audio
(subdir_paths, symlink=True)¶Moves/symlinks any audio from the audio directory specified to the database.
Parameters:
database.
Matcher
(database1, database2, *args, **kwargs)[source]¶Database comparison object.
Used to compare and match entries in two AnalysedAudioFile databases.
brute_force_matcher
(grain_size, overlap)¶Searches for matches to each grain by brute force comparison
calculate_db_inds
(match_indexes, source_sample_indexes)¶Generate the database sample index and grain index for each match based on their indexes generated from the concatenated matching
Output array will be a 3 dimensional array with an axis for each target grain, a dimension for each match of said grain and a dimension containing database sample index and the sample’s grain index.
count_grains
(database, grain_length, overlap)¶Calculate the number of grains in the database
distance_calc
(data1, data2)¶Calculates the euclidean distance between two arrays of data.
Distance is calculated with special handeling of Nan values, if they exist in the data. A Nan value matched to another Nan value is classed as a perfect match. a Nan matched to any other value is calculated as the furthest match + 10%
match
(match_function, grain_size=None, overlap=None)¶Find the closest match to each object in database 1 in database 2 using the matching function specified.
swap_databases
()¶Convenience method to swap databases, changing the source database into the target and vice-versa
database.
Synthesizer
(database1, database2, *args, **kwargs)[source]¶An object used for synthesizing output based on grain matching.
enforce_intensity
(grain, source_sample, source_grain_ind, target_sample, target_grain_ind)¶Scales the amplitude of the grain by the difference between it’s intensity and the intensity of the grain specified.
This method will fail if either AnalysedAudioFile object does not have any intensity analyses.
enforce_pitch
(grain, source_sample, source_grain_ind, target_sample, target_grain_ind)¶Shifts the pitch of the grain by the difference between it’s f0 and the f0 of the grain specified.
This method will fail if either AnalysedAudioFile object does not have an f0 analysis.
swap_databases
()¶Convenience method to swap databases, changing the source database into the target and vice-versa
synthesize
(grain_size=None, overlap=None)¶Synthesized output from the match data in the output database to create audio in the output database.
analysis.
Analysis
(AnalysedAudioFile, frames, analysis_group, name, config=None)[source]¶Basic descriptor class to build analyses on.
The Analysis base class works as an interface between child descriptor objects and the HDF5 storage file. This is designed to seperate descriptor generation from data IO, allowing for quick development of new descriptor classes. The base Analysis class has methods for retreiving analyses from file and saving data created by analysis objects to file. It also has basic formatting methods used to return data in the required format for processed such as descriptor comparisons.
In order to create a new descriptor, the hdf5_dataset_formatter method will need to be overwritten by the child class to generate and store the descriptor’s output in the appropriate manner. Examples of this can be seen through the currently implemented descriptors.
analysis_formatter
(frames, selection, format)¶Calculate the average analysis value of the grain using the match format specified.
create_analysis
(*args, **kwargs)¶Create the analysis and save to the HDF5 file.
analysis_function: The function used to create the analysis. returned data will be stored in the HDF5 file.
get_analysis_grains
(start, end)¶Retrieve analysis frames for period specified in start and end times. arrays of start and end time pairs will produce an array of equivelant size containing frames for these times.
hdf5_dataset_formatter
(analysis_method, *args, **kwargs)¶Note: This is a generic formatter designed as a template to be overwritten by a descriptor sub-class.
Formats the output from the analysis method to save to the HDF5 file.
Places data and attributes in 2 dictionaries to be stored in the HDF5 file.
analysis.
CentroidAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Temporal centroid descriptor class for generation of temporal centroid audio analysis.
This descriptor calculates the temporal centroid for overlapping grains of an AnalysedAudioFile object. A full definition of temporal centroid analysis can be found in the documentation.
Arguments:
calc_centroid_frame_times
(centroidframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_centroid_analysis
(frames, window_size=512, window=<function triang>, overlapFac=0.5)¶Calculate the Centroid values of windowed segments of the audio file and save to disk.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
F0Analysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶F0 analysis descriptor class for generation of fundamental frequency estimation.
This descriptor calculates the fundamental frequency for overlapping grains of an AnalysedAudioFile object. A full definition of F0 analysis can be found in the documentation.
Arguments:
analysis_formatter
(data, selection, format)¶Calculate the average analysis value of the grain using the match format specified.
calc_f0_frame_times
(f0frames, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_f0_analysis
(frames, samplerate, window_size=512, overlapFac=0.5, threshold=0.0, m0=None, M=None)¶Generate F0 contour analysis.
Calculate the frequency and harmonic ratio values of windowed segments of the audio file and save to disk.
get_analysis_grains
(start, end)¶Retrieve analysis frames for period specified in start and end times. arrays of start and end time pairs will produce an array of equivelant size containing frames for these times.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
FFTAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶FFT analysis descriptor class for generation of FFT spectral analysis.
This descriptor calculates the spectral content for overlapping grains of an AnalysedAudioFile object. A full definition of FFT analysis can be found in the documentation.
Arguments:
calc_fft_frame_times
(fftframes, sample_frames, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
create_fft_analysis
(frames, window_size=512, window_overlap=2, window_type='hanning')[source]¶Create a spectral analysis for overlapping frames of audio.
get_analysis_grains
(start, end)[source]¶Retrieve analysis frames for period specified in start and end times. arrays of start and end time pairs will produce an array of equivelant size containing frames for these times.
analysis.
F0HarmRatioAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶The F0 HarmRatio analysis object is a placeholder class to allow access to the harmonic ratio generated in the f0 analysis. As a result it does not have it’s own “create analysis method as other analyses do. it is designed to be used for the retreival of the f0 harmonic ratio analysis for matching.
F0 analysis must be generated for the AnalysedAudioFile in order to use this object.
analysis_formatter
(data, selection, format)¶Calculate the average analysis value of the grain using the match format specified.
calc_F0HarmRatio_frame_times
(F0HarmRatioframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
get_analysis_grains
(start, end)¶Retrieve analysis frames for period specified in start and end times. arrays of start and end time pairs will produce an array of equivelant size containing frames for these times.
analysis.
KurtosisAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Kurtosis descriptor class for generation of Kurtosis audio analysis.
This descriptor calculates the temporal kurtosis for overlapping grains of an AnalysedAudioFile object. A full definition of kurtosis analysis can be found in the documentation.
Arguments:
calc_kurtosis_frame_times
(kurtosisframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_kurtosis_analysis
(frames, variance, window_size=512, window=<function hann>, overlapFac=0.5)¶Calculate the Kurtosis values of windowed segments of the audio file and save to disk.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
PeakAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Peak descriptor class for generation of per-grain maximum peak audio analysis.
This descriptor calculates the maximum peak for overlapping grains of an AnalysedAudioFile object. A full definition of peak analysis can be found in the documentation.
Arguments:
calc_peak_frame_times
(peakframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_peak_analysis
(frames, window_size=512, window=<function triang>, overlapFac=0.5)¶Calculate the Peak values of windowed segments of the audio file and save to disk.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
RMSAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶RMS descriptor class for generation of RMS audio analysis.
This descriptor calculates the Root Mean Square analysis for overlapping grains of an AnalysedAudioFile object. A full definition of RMS analysis can be found in the documentation.
Arguments:
calc_rms_frame_times
(rmsframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_rms_analysis
(frames, samplerate, window_size=512, window=<function hann>, overlapFac=0.5)¶Generate RMS contour analysis.
Calculate the RMS values of windowed segments of the audio file and save to disk.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
SpectralCentroidAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Spectral centroid descriptor class for generation of spectral centroid audio analysis.
This descriptor calculates the spectral centroid for overlapping grains of an AnalysedAudioFile object. A full definition of spectral centroid analysis can be found in the documentation.
Arguments:
calc_spccntr_frame_times
(spccntr_frames, sample_frame_count, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
analysis.
SpectralCrestFactorAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Spectral crest factor descriptor class for generation of spectral crest factor audio analysis.
This descriptor calculates the spectral crest factor for overlapping grains of an AnalysedAudioFile object. A full definition can be found in the documentation.
Arguments:
calc_spccf_frame_times
(spccf_frames, sample_frame_count, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
analysis.
SpectralFlatnessAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Spectral flatness descriptor class for generation of spectral flatness audio analysis.
This descriptor calculates the spectral flatness for overlapping grains of an AnalysedAudioFile object. A full definition can be found in the documentation.
Arguments:
calc_spcflatness_frame_times
(spcflatness_frames, sample_frame_count, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
analysis.
SpectralFluxAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Spectral flux descriptor class for generation of spectral flux audio analysis.
This descriptor calculates the spectral flux for overlapping grains of an AnalysedAudioFile object. A full definition of spectral flux analysis can be found in the documentation.
Arguments:
calc_spcflux_frame_times
(spcflux_frames, sample_frame_count, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
create_spcflux_analysis
(fft)[source]¶Calculate the spectral flux of the fft frames.
length: the length of the window used to calculate the FFT. output_format = Choose either “freq” for output in Hz or “ind” for bin index output
analysis.
SpectralSpreadAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Spectral spread descriptor class for generation of spectral spread audio analysis.
This descriptor calculates the spectral spread for overlapping grains of an AnalysedAudioFile object. A full definition of spectral spread analysis can be found in the documentation.
Arguments:
calc_spcsprd_frame_times
(spcsprd_frames, sample_frame_count, samplerate)[source]¶Calculate times for frames using sample size and samplerate.
create_spcsprd_analysis
(fft, spectral_centroid, samplerate, output_format='ind')[source]¶Calculate the spectral spread of the fft frames.
fft: Real fft frames. spectral_centroid: spectral centroid frames (in index format). length: the length of the window used to calculate the FFT. samplerate: the samplerate of the audio analysed.
analysis.
VarianceAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Variance descriptor class for generation of variance audio analysis.
This descriptor calculates the Root Mean Square analysis for overlapping grains of an AnalysedAudioFile object. A full definition of variance analysis can be found in the documentation.
Arguments:
calc_variance_frame_times
(varianceframes, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_variance_analysis
(frames, window_size=512, overlapFac=0.5)¶Generate an energy contour analysis.
Calculate the Variance values of windowed segments of the audio file and save to disk.
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.
analysis.
ZeroXAnalysis
(AnalysedAudioFile, frames, analysis_group, config=None)[source]¶Zero-corssing descriptor class for generation of zero-crossing rate analysis.
This descriptor calculates the zero-crossing rate for overlapping grains of an AnalysedAudioFile object. A full definition of zero-crossing analysis can be found in the documentation.
Arguments:
calc_zerox_frame_times
(zerox_frames, sample_frames, samplerate)¶Calculate times for frames using sample size and samplerate.
create_zerox_analysis
(frames, window_size=512, overlapFac=0.5, *args, **kwargs)¶Generate zero crossing value for window of the signal
hdf5_dataset_formatter
(*args, **kwargs)¶Formats the output from the analysis method to save to the HDF5 file.