5. The yaafelib Python module

Main Yaafe module, providing classes to extract features with Yaafe.

5.1. FeaturePlan

class yaafelib.FeaturePlan(sample_rate=44100, normalize=None, resample=False)

FeaturePlan is a collection of features to extract, configured for a specific sample rate.

  • sample_rate – analysis samplerate
  • normalize – signal maximum normalization, in ]0,1], or None to skip normalization.

This collection can be load from a file using the loadFeaturePlan() method, or built by adding features with the addFeature() method.

Then, the getDataFlow() method retrieve the corresponding DataFlow object.

>>> fp = FeaturePlan(sample_rate=16000)
>>> fp.addFeature('mfcc: MFCC blockSize=512 stepSize=256')
>>> fp.addFeature('mfcc_d1: MFCC blockSize=512 stepSize=256 > Derivate DOrder=1')
>>> fp.addFeature('mfcc_d2: MFCC blockSize=512 stepSize=256 > Derivate DOrder=2')
>>> df = fp.getDataFlow()
>>> df.display()

Add a feature defined according the feature definition syntax.

Parameters:definition (string) – feature definition
Return type:True on success, False on fail.

Get the DataFlow object representing how to extract defined features.

Return type:DataFlow

Loads feature extraction plan from a file. The file must be a text file, where each line defines a feature (see feature definition syntax).

Return type:True on success, False on fail.

5.2. DataFlow

class yaafelib.DataFlow

A DataFlow object hold a directed acyclic graph of computational steps describing how to compute some audio features.

DataFlow can be loaded directly from a dataflow file using the load() method, or created with a FeaturePlan object. The advanced user may also build a dataflow graph from scratch.


Print the DataFlow to the standard output


write a got graph corresponding to the DataFlow

Parameters:filename (string) – file to write

Build DataFlow from a dataflow file.

Parameters:filename (string) – dataflow file name.
Returns:True on success, False on fail.

write DataFlow into a dataflow file.

Parameters:filename (string) – file to write

5.3. Engine

class yaafelib.Engine

A Engine object is in charge of processing computations defined in a DataFlow object on given inputs.

>>> # Initialization
>>> fp = FeaturePlan(sample_rate=16000)
>>> fp.addFeature('mfcc: MFCC blockSize=512 stepSize=256')
>>> fp.addFeature('sr: SpectralRolloff blockSize=512 stepSize=256')
>>> fp.addFeature('sf: SpectralFlux blockSize=512 stepSize=256')
>>> engine = Engine()
>>> engine.load(fp.getDataFlow())
>>> # get input metadata
>>> engine.getInputs()
{'audio': {'sampleRate': 16000.0,
           'frameLength': 1,
           'sampleStep': 1,
           'parameters': {'SampleRate': '16000'},
           'size': 1}}
>>> # get output metadata
>>> engine.getOutputs()
{'sr': {'sampleRate': 16000.0,
        'frameLength': 512,
        'sampleStep': 256,
        'parameters': {'normalize': '-1',
                       'yaafedefinition': 'SpectralRolloff blockSize=512 stepSize=256',
                       'version': '...'},
        'size': 1},
 'sf': ...,
 'mfcc': ...}
>>> # extract features from a numpy array
>>> import numpy # needs numpy
>>> audio = numpy.random.randn(1,1000000)
>>> feats = engine.processAudio(audio)
>>> feats['mfcc']
>>> feats['sf']
>>> feats['sr']

It is also possible to extract features block per block:

engine.reset() # first reset the engine
for i in range(1,10): # iterate over your audio block
   audio = numpy.random.rand(1,32000) # get your audio data
   engine.writeInput('audio',audio) # write audio array on 'audio' input
   engine.process() # process available data
   feats = engine.readAllOutputs() # read available feature data
   # do what you want with your feature data
engine.flush() # do not forget to flush
feats = engine.readAllOutputs() # read last data
# do what you want your feature data

When extracting features block per block, you should be aware of Yaafe’s engine internals.


Process available data and flush all buffers so that all output data is available. Analysis is ended, the reset() method must be called before any further calls to writeInput() and process().


Get input metadata. Result format is the same as for getOutputs() method, but the general case is that there is only one input named ‘audio’ and the sole relevant metadata are:

Samplerate :expected audio sampleRate
Parameters :attached parameters

Others fields should be set to 1.


Get output metadata. For each output feature, you get the following metadata:

Samplerate :audio analysis samplerate
Samplestep :Number of audio samples between consecutive feature values
Framelength :Analysis frame size in number of audio samples
Size :size the feature (or number of coefficients)
Parameters :attached parameters.

Configure engine according to the given dataflow.

Parameters:dataflow (DataFlow or string) – dataflow object or filename of a dataflow file.
Returns:True on success, False on fail.

Process available data.


Convenient method to extract features from data. It successively calls reset(), writeInput(), process(), flush(), and returns output of readAllOutputs()


Read all outputs.

Returns:dictionary with output name as key and numpy.array as value.

Read a specific output, and returns values as a numpy.array

Parameters:name (string) – output name to read
Return type:numpy.array

Reset engine. All buffers are cleared, and a new analysis can start.

writeInput(name, data)

Write data on an input.

  • name (string) – input on which to write
  • data (numpy array) – data to write.

5.4. AudioFileProcessor

class yaafelib.AudioFileProcessor

A AudioFileProcessor object allow to extract features from audio files, and possibly write output features into files.

It must be provided with a configured Engine.

Here is how to extract features from audio files and get it as numpy arrays:

>>> # configure your engine
>>> engine = Engine()
>>> engine.load(dataflow_file) 
>>> # create your AudioFileProcessor 
>>> afp = AudioFileProcessor()
>>> # leave output format to None
>>> afp.processFile(engine,audiofile)
>>> # retrieve features from engine
>>> feats = engine.readAllOutputs()
>>> # do what you want with your feature data
>>> feats['mfcc']

To write features directly to output files, just set an output format with the setOutputFormat() method.

processFile(engine, filename)

Extract features from the given file using the given engine.

If an output format has been set, then output files will be written, else output feature data can be read using engine’s Engine.readOutput() or Engine.readAllOutputs() methods.

  • engine (Engine) – engine to use for feature extraction. It must already have been configured.
  • filename (string) – audio file to process

0 on success, negative value on fail

setOutputFormat(format, outDir, params)

Set output format.

  • format (string) – format to set
  • outDir (string) – base output directory for output files
  • params (dict) – format parameters

True if ok, False if format does not exists.

Table Of Contents

Previous topic

4. Other Yaafe tools

Next topic

6. Extending Yaafe