For the complete documentation index, see llms.txt. This page is also available as Markdown.

Audio

Create Audio projects, upload recordings, label time segments, configure the project spectrogram and export spectrograms for training with the Python SDK.

Introduction

An Audio project holds recordings that are labeled with time segments and recording tags. A segment is a tag applied to a range of samples of one recording, optionally about one channel; a recording tag labels the whole recording. The labeling tool shows each recording as a waveform and a spectrogram, and the spectrogram is computed with settings that belong to the project: every recording in the project is analysed the same way, so every annotator is looking at the same picture and the picture can be reproduced later for training.

This tutorial covers:

  • creating an Audio project and configuring its spectrogram

  • uploading recordings

  • adding, reading, editing and removing segments and recording tags

  • rendering spectrograms of whole recordings and of labeled segments

  • exporting training crops for a whole project

  • downloading and uploading a project in Supervisely format

Audio projects need Supervisely instance 6.18.0 or newer and Supervisely Python SDK 6.74.41 or newer.

Prerequisites

Step 1. Prepare ~/supervisely.env file with credentials. Learn more here.

Step 2. Install the SDK.

pip install supervisely

Uncompressed WAV is decoded with the Python standard library. To decode FLAC, OGG, MP3 or M4A locally, install the optional decoders as well:

They are imported only when such a file is decoded. Uploading, importing and downloading never decode audio, so they work without them.

Step 3. Initialize the API client.

Create a project and configure the spectrogram

The spectrogram is a transform of the audio, and the settings decide what is visible: two tones 40 Hz apart are one line at fft_size=256 and two lines at fft_size=1024; an event 70 dB below full scale disappears at min_db=-60. Choose them once, before labeling starts.

A project that was never configured returns the platform defaults, which are what the labeling tool shows in that case. Values outside the ranges above raise ValueError before any request is sent.

Changing the settings is a project-wide change that requires permission to edit the project, not only to label in it. Existing labels are not changed, but they were drawn on a picture that no longer matches β€” which is why the settings are meant to be set once.

settings.fingerprint is a short hash over the fields that change the numbers (colormap and interpolation only change the colours). Two projects with the same fingerprint analyse audio identically, so their labels can be merged or used for training together.

Upload recordings

api.audio.upload_paths(dataset_id, names, paths) uploads several files at once. Files already on the server are recognised by hash and not sent again, so re-running an interrupted upload transfers only what is missing.

The platform does not store the sample rate, duration or number of channels of a recording. Read them from the file when you need them:

Label segments

A segment is a tag, so create the tags first:

The start and end of a segment are zero-based sample indices, both inclusive, into the original recording β€” not milliseconds and not frames. At 16 kHz, start=16000, end=23999 is the half-second from 1.0 s to 1.5 s. channel is the zero-based channel the label is about, or None for all channels.

A recording tag β€” "Entire recording" in the labeling tool β€” has no range and no channel. A tag can be on a recording only once:

Read, edit and remove labels

To edit or remove a label, pass it as it was read back β€” it carries the ids the platform needs. An edit replaces the label's meta whole, so change the object you read rather than building a new one:

api.audio.get_list(dataset_id) lists the recordings of a dataset, each with its tags.

Render a spectrogram

Download the recording, decode it, and render it under the project's settings:

Rows are mel bands for the mel scale and FFT bins otherwise, lowest frequency first; to_image puts the lowest frequency at the bottom, as in the labeling tool. Decibels are absolute (10*log10(power), a full-scale sine reads 0 dB) and clipped to [min_db, max_db]; pass as_db=False for raw power. Multichannel audio is averaged into a mixdown unless you pass channel=.

To reproduce the tool's on-screen grid rather than the natural resolution, pass the number of display rows:

Export training crops

render_segment renders just the labeled range, on the segment's channel:

The same for every segment in a project, saved as NumPy arrays named after the recording, the label and its tag:

Store the settings next to the crops (settings.to_json()): they are what makes a crop comparable with one rendered later.

Rendering in PyTorch or TensorFlow

The stored settings are enough to rebuild the same analysis in a training framework instead of the SDK. Six conventions are not implied by the field names, and getting any of them wrong is a visible difference rather than a rounding one:

What the settings mean

Window

Periodic (2*pi*i/N): torch.hann_window(n, periodic=True) and its Hamming and Blackman siblings

Padding

Frames centred on k*hop_length, padded with zeros (pad_mode="constant"), not reflected

Frames

(n - 1) // hop_length + 1. torch.stft(center=True) returns one more when n is a multiple of the hop: drop it

Scaling

Power divided by sum(window)^2, then multiplied by 4 on every bin except DC and Nyquist

Mel

HTK mel scale from 0 to Nyquist, and a weighted average: divide by the sum of each band's weights

dB

10*log10(power), floor 1e-20, clipped to [min_db, max_db]; no top_db, no normalisation to the loudest value

In PyTorch, from the decoded samples and the project's settings:

This matches sly.audio.render_spectrogram to within 0.002 dB for every scale and window. TensorFlow's tf.signal.stft has no centring, so pad fft_size // 2 zeros in front yourself; tf.signal.linear_to_mel_weight_matrix(lower_edge_hertz=0, upper_edge_hertz=rate / 2) is the same HTK filter bank.

Download and upload a project

sly.download works for Audio projects as for any other type:

meta.json carries the tags and the project's spectrogram settings; each annotation carries the recording's segments and recording tags, identified by tag name, plus its sample rate, sample count and channel count. See Audio Annotation for the format.

Upload it back as a new project, with its nested datasets, labels and spectrogram settings:

To add a project in this format, or a folder of plain recordings, to an existing project, use the Import app or sly.ImportManager inside an app. The imported spectrogram settings are applied only when the destination project has none yet and holds no recordings; otherwise it keeps its own, because changing them would change what its existing labels mean.

Last updated

Was this helpful?