Util functions

Data augmentation utils

compiam.utils.augment.attack_remix(in_path, out_dir, gain_factors=0.3, winDur=46.4, hopDur=5, sr=16000, n_jobs=4)[source]

Modifying the relative levels of attack and decay regions of an audio

Parameters:
  • in_path (str) – Path to input audio

  • out_dir (str) – Directory to output pitch shifted audios

  • gain_factors (float or list) – gain factor (or list of) to scale attack portion with

  • winDur (float) – Window size in milliseconds

  • hopDur (float) – Hop size in milliseconds

  • sr (float) – Sampling rate of input audio

  • n_jobs (int) – n jobs for parrelelization

compiam.utils.augment.spectral_shape(in_path, out_dir, gain_factors=(0.6, 2, 0.2), winDur=46.4, hopDur=0.005, sr=16000, n_jobs=4)[source]

Augmenting data by perturbin ‘nuisance attributes’ that are unimportant in the specific discrimination task.

Parameters:
  • in_path (str) – Path to input audio

  • out_dir (str) – Directory to output pitch shifted audios

  • gain_factors (float or list) – 3-tuple (or list of) with gain factors for remixing. Tuple entries correspond to each of bass, treble, & damped components.

  • winDur (float) – Window size in milliseconds

  • hopDur (float) – Hop size in milliseconds

  • sr (float) – Sampling rate of input audio

  • n_jobs (int) – n jobs for parrelelization

compiam.utils.augment.stroke_remix(in_path, out_dir, gain_factors=(0.6, 2, 0.2), templates='/home/runner/work/compIAM/compIAM/compiam/utils/augment/augmentation/templates.npy', winDur=46.4, hopDur=0.005, sr=16000, n_jobs=4)[source]
Simulate the expected variations of relative strengths of drums in a mix

using non-negative matrix factorization (NMF).

Parameters:
  • in_path (str) – Path to input audio

  • out_dir (str) – Directory to output pitch shifted audios

  • gain_factors (float or list) – 3-tuple (or list of) with gain factors for remixing. Tuple entries correspond to each of bass, treble, & damped components.

  • templates (str) – path to saved nmf templates

  • winDur (float) – Window size in milliseconds

  • hopDur (float) – Hop size in milliseconds

  • sr (float) – Sampling rate of input audio

  • n_jobs (int) – n jobs for parrelelization

Pitch-related utils

compiam.utils.pitch.cents_to_pitch(c, tonic)[source]

Convert cents value, <c> to pitch in Hz

Parameters:
  • c (float/int) – Pitch value in cents above <tonic>

  • tonic (float) – Tonic value in Hz

Returns:

Pitch value, <c> in Hz

Return type:

float

compiam.utils.pitch.extract_stability_mask(pitch, min_stab_sec, hop_sec, var, timestep)[source]

Extract boolean array corresponding to <pitch> - yes/no does point correspond to a region of “stable” pitch.

A window is passed along the pitch track, <pitch> and the minimum and maximum values compared to the average for that window. Regions corresponding to windows whose extremes deviate significantly from their means are marked as stable. Consecutive stable regions summing to more than <min_stab_sec> seconds in length are annotated with 1 indicating stable. Regions which are not stable or that are stable but do not make up at least <min_stab_sec> in length are annotated with 0 - not stable.

Parameters:
  • pitch (np.ndarray) – Pitch values in Hz or cents

  • min_stab_sec (float) – Stable regions of at least <min_stab_sec> seconds in length are annotated as stable. Shorter regions are not annotated.

  • hop_sec (float) – Hop length in seconds of window

  • var (float) – If the maximum/minimum pitch in a window deviates from its mean by more than this value, the window is considered unstable. Important to consider if the input is in cents or hertz!

  • timestep (float) – Time difference, in seconds, between each element in pitch

Returns:

Boolean array equal in length to <pitch>: is stable region or not?

Return type:

np.ndarray

compiam.utils.pitch.interpolate_below_length(arr, val, gap)[source]

Interpolate gaps of value, <val> of length equal to or shorter than <gap> in <arr>

Parameters:
  • arr (np.array) – Array to interpolate

  • val (number) – Value expected in gaps to interpolate

  • gap (number) – Maximum gap length to interpolate, gaps of <val> longer than <g> will not be interpolated

Returns:

interpolated array

Return type:

np.array

compiam.utils.pitch.is_stable(seq, max_var)[source]

Compute is sequence of value has stability given an input tolerance

Parameters:
  • seq – sequence of values to study

  • max_var – Maximum tolerance to consider stable/not stable

Returns:

1 (stable) or 0 (not stable)

compiam.utils.pitch.normalisation(pitch, tonic, bins_per_octave=120, max_value=4)[source]

Normalize pitch given a tonic.

Parameters:
  • pitch – a 2-D list with time-stamps and pitch values per timestamp.

  • tonic – recording tonic to normalize the pitch to.

  • bins_per_octave – number of frequency bins per octave.

  • max_value – maximum value to clip the normalized pitch to.

Returns:

a 2-D list with time-stamps and normalized to a given tonic pitch values per timestamp.

compiam.utils.pitch.pitch_seq_to_cents(pseq, tonic)[source]

Convert sequence of pitch values to sequence of cents above <tonic> values

Parameters:
  • pseq (np.array) – Array of pitch values in Hz

  • tonic (float) – Tonic value in Hz

Returns:

Sequence of original pitch value in cents above <tonic>

Return type:

np.array

compiam.utils.pitch.pitch_to_cents(p, tonic)[source]

Convert pitch value, <p> to cents above <tonic>.

Parameters:
  • p (float) – Pitch value in Hz

  • tonic (float) – Tonic value in Hz

Returns:

Pitch value, <p> in cents above <tonic>

Return type:

float

compiam.utils.pitch.resample_melody_series(times, frequencies, voicing, times_new, kind='linear')[source]

NOTE: This function is DIRECTLY PORTED FROM mir_eval.melody

Resamples frequency and voicing time series to a new timescale. Maintains any zero (“unvoiced”) values in frequencies.

If times and times_new are equivalent, no resampling will be performed.

Parameters:
  • times – Times of each frequency value

  • frequencies – Array of frequency values, >= 0

  • voicing – Array which indicates voiced or unvoiced. This array may be binary or have continuous values between 0 and 1.

  • times_new – Times to resample frequency and voicing sequences to

:param kind:kind parameter to pass to scipy.interpolate.interp1d.

(Default value = ‘linear’)

Returns frequencies_resampled:

Frequency array resampled to new timebase

Returns voicing_resampled:

Voicing array resampled to new timebase

compiam.utils.pitch.resampling(pitch, new_len)[source]

Resample pitch to a given new length in samples

Parameters:
  • pitch – a 2-D list with time-stamps and pitch values per timestamp.

  • new_len – new length of the output pitch

Beat and downbeat tracking utils

Dynamic Bayesian Networks (DBN) for beat, downbeat and bar tracking.

Self-contained numpy/scipy implementation of the DBN post-processing described in

Böck, S., Krebs, F., & Widmer, G. (2016). Joint beat and downbeat tracking with recurrent neural networks. In Proceedings of the 17th International Society for Music Information Retrieval Conference (ISMIR 2016).

Krebs, F., Böck, S., & Widmer, G. (2015). An efficient state-space model for joint tempo and meter tracking. In Proceedings of the 16th International Society for Music Information Retrieval Conference (ISMIR 2015).

Beat, downbeat and bar tracking are exposed via dbn_beat_tracking(), dbn_downbeat_tracking() and dbn_bar_tracking().

class compiam.utils.dbn.BarStateSpace(num_beats, min_interval, max_interval, num_intervals=None)[source]

State space of a bar, i.e. num_beats consecutive beat state spaces.

class compiam.utils.dbn.BeatStateSpace(min_interval, max_interval, num_intervals=None)[source]

State space of a single beat period, for a range of tempi (beat intervals in frames).

class compiam.utils.dbn.HiddenMarkovModel(transitions, observation_pointers)[source]

Hidden Markov Model with sparse transitions and shared observation densities.

Only the Viterbi decoding is implemented. The prior is a uniform distribution over the states, that is transitioned once before the first observation.

viterbi(log_densities)[source]

Most likely state sequence given the observation log-densities.

Parameters:

log_densities – 2-D array (frames x densities) with the observation log-densities.

Returns:

tuple with the state path and its log-probability.

compiam.utils.dbn.beat_transitions(state_space, transition_lambda)[source]

Transition matrix of a beat or bar state space.

States advance deterministically within a beat, and the tempo can change when entering a new beat.

Parameters:
  • state_space – a BeatStateSpace or BarStateSpace.

  • transition_lambda – rate of the exponential tempo transition distribution.

Returns:

sparse matrix with destination states in rows and origin states in columns.

compiam.utils.dbn.dbn_bar_tracking(beats, downbeat_activations, beats_per_bar=(3, 4), observation_weight=100, meter_change_prob=1e-07)[source]

Track the position of given beats within the bar with a DBN.

Parameters:
  • beats – 1-D array with the beat times (in seconds).

  • downbeat_activations – 1-D array with the downbeat probability at each beat, strictly between 0 and 1.

  • beats_per_bar – list of candidate numbers of beats per bar.

  • observation_weight – weight of the downbeat activations.

  • meter_change_prob – probability of a change of bar length at the end of a bar.

Returns:

2-D array with the beat times (in seconds) and their position in the bar (1 for downbeats).

compiam.utils.dbn.dbn_beat_tracking(activations, fps, min_bpm=55.0, max_bpm=215.0, num_tempi=None, transition_lambda=100, observation_lambda=16, threshold=0, correct=True)[source]

Track beats in a beat activation function with a DBN.

Parameters:
  • activations – 1-D array of beat probabilities, one per frame. They should be strictly between 0 and 1.

  • fps – frames per second of the activations.

  • min_bpm – minimum tempo (in beats per minute).

  • max_bpm – maximum tempo (in beats per minute).

  • num_tempi – number of tempi to model, if None all integer beat intervals are used.

  • transition_lambda – higher values favour a constant tempo over a tempo change between beats.

  • observation_lambda – split one beat period into N parts, the first representing beat states.

  • threshold – threshold the activations before decoding.

  • correct – align the beats to the maximum of the activations.

Returns:

1-D array with the beat times (in seconds).

compiam.utils.dbn.dbn_downbeat_tracking(activations, fps, beats_per_bar, min_bpm=55.0, max_bpm=215.0, num_tempi=60, transition_lambda=100, observation_lambda=16, threshold=0.05, correct=True)[source]

Jointly track beats and downbeats with a DBN.

One DBN is decoded per candidate bar length, and the most likely one is kept.

Parameters:
  • activations – 2-D array (frames x 2) with the probabilities of a non-downbeat beat and a downbeat. They should be strictly between 0 and 1, and add up to less than 1.

  • fps – frames per second of the activations.

  • beats_per_bar – list of candidate numbers of beats per bar.

  • min_bpm – minimum tempo (in beats per minute).

  • max_bpm – maximum tempo (in beats per minute).

  • num_tempi – number of tempi to model, if None all integer beat intervals are used.

  • transition_lambda – higher values favour a constant tempo over a tempo change between beats.

  • observation_lambda – split one beat period into N parts, the first representing beat states.

  • threshold – threshold the activations before decoding.

  • correct – align the beats to the maximum of the activations.

Returns:

2-D array with the beat times (in seconds) and their position in the bar (1 for downbeats).

compiam.utils.dbn.exponential_transition(from_intervals, to_intervals, transition_lambda)[source]

Tempo transition probabilities between two sets of beat intervals.

Parameters:
  • from_intervals – beat intervals of the origin states.

  • to_intervals – beat intervals of the destination states.

  • transition_lambda – rate of the exponential distribution, higher values favour a constant tempo. If None, only transitions between equal intervals are allowed.

Returns:

2-D array (origin x destination) with the row-normalised transition probabilities.