madmom.processors

This module contains all processor related functionality.

Notes

All features should be implemented as classes which inherit from Processor (or provide a XYZProcessor(Processor) variant). This way, multiple Processor objects can be chained/combined to achieve the wanted functionality.

class madmom.processors.Processor[source]

Abstract base class for processing data.

classmethod load(infile)[source]

Instantiate a new Processor from a file.

This method un-pickles a saved Processor object. Subclasses should overwrite this method with a better performing solution if speed is an issue.

Parameters:
infile : str or file handle

Pickled processor.

Returns:
:class:`Processor` instance

Processor.

dump(outfile)[source]

Save the Processor to a file.

This method pickles a Processor object and saves it. Subclasses should overwrite this method with a better performing solution if speed is an issue.

Parameters:
outfile : str or file handle

Output file for pickling the processor.

process(data, **kwargs)[source]

Process the data.

This method must be implemented by the derived class and should process the given data and return the processed output.

Parameters:
data : depends on the implementation of subclass

Data to be processed.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the implementation of subclass

Processed data.

class madmom.processors.OnlineProcessor(online=False)[source]

Abstract base class for processing data in online mode.

Derived classes must implement the following methods:

  • process_online(): process the data in online mode,
  • process_offline(): process the data in offline mode.
process(data, **kwargs)[source]

Process the data either in online or offline mode.

Parameters:
data : depends on the implementation of subclass

Data to be processed.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the implementation of subclass

Processed data.

Notes

This method is used to pass the data to either process_online or process_offline, depending on the online setting of the processor.

process_online(data, reset=True, **kwargs)[source]

Process the data in online mode.

This method must be implemented by the derived class and should process the given data frame by frame and return the processed output.

Parameters:
data : depends on the implementation of subclass

Data to be processed.

reset : bool, optional

Reset the processor to its initial state before processing.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the implementation of subclass

Processed data.

process_offline(data, **kwargs)[source]

Process the data in offline mode.

This method must be implemented by the derived class and should process the given data and return the processed output.

Parameters:
data : depends on the implementation of subclass

Data to be processed.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the implementation of subclass

Processed data.

reset()[source]

Reset the OnlineProcessor.

This method must be implemented by the derived class and should reset the processor to its initial state.

class madmom.processors.OutputProcessor[source]

Class for processing data and/or feeding it into some sort of output.

process(data, output, **kwargs)[source]

Processes the data and feed it to the output.

This method must be implemented by the derived class and should process the given data and return the processed output.

Parameters:
data : depends on the implementation of subclass

Data to be processed (e.g. written to file).

output : str or file handle

Output file name or file handle.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the implementation of subclass

Processed data.

class madmom.processors.SequentialProcessor(processors)[source]

Processor class for sequential processing of data.

Parameters:
processors : list

Processor instances to be processed sequentially.

Notes

If the processors list contains lists or tuples, these get wrapped as a SequentialProcessor itself.

insert(index, processor)[source]

Insert a Processor at the given processing chain position.

Parameters:
index : int

Position inside the processing chain.

processor : Processor

Processor to insert.

append(other)[source]

Append another Processor to the processing chain.

Parameters:
other : Processor

Processor to append to the processing chain.

extend(other)[source]

Extend the processing chain with a list of Processors.

Parameters:
other : list

Processors to be appended to the processing chain.

process(data, **kwargs)[source]

Process the data sequentially with the defined processing chain.

Parameters:
data : depends on the first processor of the processing chain

Data to be processed.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the last processor of the processing chain

Processed data.

class madmom.processors.ParallelProcessor(processors, num_threads=None)[source]

Processor class for parallel processing of data.

Parameters:
processors : list

Processor instances to be processed in parallel.

num_threads : int, optional

Number of parallel working threads.

Notes

If the processors list contains lists or tuples, these get wrapped as a SequentialProcessor.

process(data, **kwargs)[source]

Process the data in parallel.

Parameters:
data : depends on the processors

Data to be processed.

kwargs : dict, optional

Keyword arguments for processing.

Returns:
list

Processed data.

class madmom.processors.IOProcessor(in_processor, out_processor=None)[source]

Input/Output Processor which processes the input data with the input processor and pipes everything into the given output processor.

All Processors defined in the input chain are sequentially called with the ‘data’ argument only. The output Processor is the only one ever called with two arguments (‘data’, ‘output’).

Parameters:
in_processor : Processor, function, tuple or list

Input processor. Can be a Processor (or subclass thereof like SequentialProcessor or ParallelProcessor), a function accepting a single argument (‘data’). If a tuple or list is given, it is wrapped as a SequentialProcessor.

out_processor : OutputProcessor, function, tuple or list

OutputProcessor or function accepting two arguments (‘data’, ‘output’). If a tuple or list is given, it is wrapped in an IOProcessor itself with the last element regarded as the out_processor and all others as in_processor.

process(data, output=None, **kwargs)[source]

Processes the data with the input processor and pipe everything into the output processor, which also pipes it to output.

Parameters:
data : depends on the input processors

Data to be processed.

output: str or file handle

Output file (handle).

kwargs : dict, optional

Keyword arguments for processing.

Returns:
depends on the output processors

Processed data.

madmom.processors.process_single(processor, infile, outfile, **kwargs)[source]

Process a single file with the given Processor.

Parameters:
processor : Processor instance

Processor to be processed.

infile : str or file handle

Input file (handle).

outfile : str or file handle

Output file (handle).

madmom.processors.process_batch(processor, files, output_dir=None, output_suffix=None, strip_ext=True, num_workers=4, shuffle=False, **kwargs)[source]

Process a list of files with the given Processor in batch mode.

Parameters:
processor : Processor instance

Processor to be processed.

files : list

Input file(s) (handles).

output_dir : str, optional

Output directory.

output_suffix : str, optional

Output suffix (e.g. ‘.txt’ including the dot).

strip_ext : bool, optional

Strip off the extension from the input files.

num_workers : int, optional

Number of parallel working threads.

shuffle : bool, optional

Shuffle the files before distributing them to the working threads

Notes

Either output_dir and/or output_suffix must be set. If strip_ext is True, the extension of the input file names is stripped off before the output_suffix is appended to the input file names.

Use shuffle if you experience out of memory errors (can occur for certain methods with high memory consumptions if consecutive files are rather long).

class madmom.processors.BufferProcessor(buffer_size=None, init=None, init_value=0)[source]

Buffer for processors which need context to do their processing.

Parameters:
buffer_size : int or tuple

Size of the buffer (time steps, [additional dimensions]).

init : numpy array, optional

Init the buffer with this array.

init_value : float, optional

If only buffer_size is given but no init, use this value to initialise the buffer.

Notes

If buffer_size (or the first item thereof in case of tuple) is 1, only the un-buffered current value is returned.

If context is needed, buffer_size must be set to >1. E.g. SpectrogramDifference needs a context of two frames to be able to compute the difference between two consecutive frames.

reset(init=None)[source]

Reset BufferProcessor to its initial state.

Parameters:
init : numpy array, shape (num_hiddens,), optional

Reset BufferProcessor to this initial state.

process(data, **kwargs)[source]

Buffer the data.

Parameters:
data : numpy array or subclass thereof

Data to be buffered.

Returns:
numpy array or subclass thereof

Data with buffered context.

buffer(data, **kwargs)

Buffer the data.

Parameters:
data : numpy array or subclass thereof

Data to be buffered.

Returns:
numpy array or subclass thereof

Data with buffered context.

madmom.processors.process_online(processor, infile, outfile, **kwargs)[source]

Process a file or audio stream with the given Processor.

Parameters:
processor : Processor instance

Processor to be processed.

infile : str or file handle, optional

Input file (handle). If none is given, the stream present at the system’s audio inpup is used. Additional keyword arguments can be used to influence the frame size and hop size.

outfile : str or file handle

Output file (handle).

kwargs : dict, optional

Keyword arguments passed to audio.signal.Stream if in_stream is ‘None’.

Notes

Right now there is no way to determine if a processor is online-capable or not. Thus, calling any processor with this function may not produce the results expected.

madmom.processors.pickle_processor(processor, outfile, **kwargs)[source]

Pickle the Processor to a file.

Parameters:
processor : Processor instance

Processor to be pickled.

outfile : str or file handle

Output file (handle) where to pickle it.

madmom.processors.io_arguments(parser, output_suffix='.txt', pickle=True, online=False)[source]

Add input / output related arguments to an existing parser.

Parameters:
parser : argparse parser instance

Existing argparse parser object.

output_suffix : str, optional

Suffix appended to the output files.

pickle : bool, optional

Add a ‘pickle’ sub-parser to the parser.

online : bool, optional

Add a ‘online’ sub-parser to the parser.