Main Yaafe module, providing classes to extract features with Yaafe.
FeaturePlan is a collection of features to extract, configured for a specific sample rate.
Parameters: |
|
---|
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')
True
>>> fp.addFeature('mfcc_d1: MFCC blockSize=512 stepSize=256 > Derivate DOrder=1')
True
>>> fp.addFeature('mfcc_d2: MFCC blockSize=512 stepSize=256 > Derivate DOrder=2')
True
>>> 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. |
---|
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 |
---|
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')
True
>>> fp.addFeature('sr: SpectralRolloff blockSize=512 stepSize=256')
True
>>> fp.addFeature('sf: SpectralFlux blockSize=512 stepSize=256')
True
>>> engine = Engine()
>>> engine.load(fp.getDataFlow())
True
>>> # 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']
array([[...]])
>>> feats['sf']
array([[...]])
>>> feats['sr']
array([[...]])
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.
Write data on an input.
Parameters: |
|
---|
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)
True
>>> # create your AudioFileProcessor
>>> afp = AudioFileProcessor()
>>> # leave output format to None
>>> afp.processFile(engine,audiofile)
0
>>> # retrieve features from engine
>>> feats = engine.readAllOutputs()
>>> # do what you want with your feature data
>>> feats['mfcc']
array([[...]])
To write features directly to output files, just set an output format with the setOutputFormat() method.
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.
Parameters: |
|
---|---|
Returns: | 0 on success, negative value on fail |
Set output format.
Parameters: |
|
---|---|
Returns: | True if ok, False if format does not exists. |