~ A Python wrapper for Olaf - Acoustic fingerprinting in Python» By Joren on Friday 22 September 2023
Fig: Python wrapping C.
The python wrapper should make it more accessible for developers to get started with it and makes it compatible with other Python libraries. A few notable libraries are the librosa python package for music and audio analysis, nnAudio, A fast GPU audio processing toolbox and other more general plotting, data processing and machine learning libraries. Despite Python’s many flaws, its rich library ecosystem is unmatched.
The associated GitHub repository contains documentation on how to use the Olaf python wrapper and also contains examples. The first shows how to index a song into the database and subsequently query the database. The second visualises the event points extracted by Olaf. The figure below shows shows the resulting event points, extracted with Olaf, plotted on a magnitude spectrogram, calculated with Olaf. The spectrogram on top is calculated using librosa and is meant to be very similar to Olaf.
Fig: A power spectrum from librosa and one from Olaf, with event points marked.
The wrapper was made with Python CFFI which works reasonably well. The automatically generated wrapper library support a large part of the C language but it needs a compilation step for each platform. Currently, the instructions assume a POSIX-like system, but technically, the wrapper can also function on Windows, albeit with the potential need for Windows-equivalent instructions in place of certain POSIX ones. The wrapper is wrapped in an easy to use python class called
from olaf import Olaf, OlafCommand import librosa # Store the first ten seconds of an audio file audio_file = librosa.ex('choice') Olaf(OlafCommand.STORE,audio_file).do(duration=10.0) # Query for a part of the same file (with an offset of 7 seconds), but change volume y, sr = librosa.load(audio_file,mono=True, sr=16000,duration=10,offset=7.0) y = y * 0.8 #change the volume results = Olaf(OlafCommand.QUERY,audio_file).do(y=y) # We expect a match between the stored and partially overlapping query print(results)