Пример #1
0
def onset_detection(input_file, output_file, threshold, freqs, log_flag):
    '''Onset detection

    :parameters:
      - input_file : str
          path to input audio file (wav, mp3, m4a, flac, etc.)
      - annotations_file : str
          path to the annotations file (txt, csv, etc)
      - delimiter: str
          delimiter string to process the annotations file
    '''

    # 1. load the wav file
    # use an example audio file provided
    print('Loading audio file ...', input_file)
    y, sr = audio.load(input_file, sr=None)

    # 2. values for compute accentuation feature
    hop = 5e-3  # hop size
    nfilts = 80  # Number of MEL filters
    alpha = 10e4  # compression parameter for dB conversion - log10(alpha*abs(S)+1)

    # 3. values for peak detection on the accentuation feature function
    pre_avg = 14  # number of past frames for moving average
    pos_avg = 10  # number of future frames for moving average
    pre_max = 14  # number of past frames for moving maximum
    pos_max = 10  # number of future frames for moving maximum

    # 4. onsets detection
    print('Detecting onsets')
    onset_times, feature_values = onsets.detection(y,
                                                   fs=sr,
                                                   hop=hop,
                                                   nfilts=nfilts,
                                                   log_flag=log_flag,
                                                   alpha=alpha,
                                                   minfreq=freqs[0],
                                                   maxfreq=freqs[1],
                                                   threshold=threshold,
                                                   pre_avg=pre_avg,
                                                   pos_avg=pos_avg,
                                                   pre_max=pre_max,
                                                   pos_max=pos_max)

    # 5. plot everything
    plt.plot()
    display.wave_plot(y, sr, onsets=onset_times)
    plt.title('detected onsets')
    plt.show()

    # 6. save onsets to csv
    print('Saving onset annotations ...', output_file)
    annotations.save_onsets(output_file, onset_times)
Пример #2
0
def onset_detection(input_file, output_file, preset_name, json_file, verbose):
    '''Onset detection

    :parameters:
      - input_file : str
          path to input audio file (wav, mp3, m4a, flac, etc.)
      - output_file : str
          path to the output csv file
      - preset_name : str
            name of the preset to load (e.g. "chico")
      - json_file : str
           path to the json file of presets. Default: carat/presets/onsets.json
      - verbose : bool
           if True print values of parameters in chosen preset. Default: True

    '''

    # 1. load the wav file
    # use an example audio file provided
    print('Loading audio file ...', input_file)
    y, sr = audio.load(input_file, sr=None)

    # 2. load preset values
    print('Loading preset parameter values from', json_file, 'json file...')
    preset = util.load_preset(preset_name, json_file)
    if verbose:
        print('Values in', preset_name, 'preset are:', preset)

    # 3. onsets detection
    print('Detecting onsets')
    onset_times, feature_values = onsets.detection(y, fs=sr, **preset)

    # 4. plot everything
    plt.plot()
    display.wave_plot(y, sr, onsets=onset_times)
    plt.title('detected onsets')
    plt.show()

    # 5. save onsets to csv
    print('Saving onset annotations ...', output_file)
    annotations.save_onsets(output_file, onset_times)
def align_beats_to_onsets(audio_file, beat_annotations_file,
                          onset_annotations_file, delimiter):
    '''Align the location of beats to the location of onsets closest to the beats

    :parameters:
      - audio_file : str
          path to input audio file (wav, mp3, m4a, flac, etc.)
      - beat_annotations_file : str
          path to the beat annotations file (txt, csv, etc)
      - onset_annotations_file : str
          path to the annotations file (txt, csv, etc)
      - delimiter: str
          delimiter string to process the annotations file
    '''

    # 1. load the wav file
    print('Loading audio file ...', audio_file)
    y, sr = audio.load(audio_file, sr=None, duration=30.0)

    # 2. load beat annotations
    print('Loading beat annotations ...', beat_annotations_file)
    beats, _ = annotations.load_beats(beat_annotations_file,
                                      delimiter=delimiter)

    # 3. load onset annotations
    print('Loading onset annotations ...', onset_annotations_file)
    onsets, _ = annotations.load_onsets(onset_annotations_file,
                                        delimiter=delimiter)

    # 4. compute beats from onsets
    print('Computing beats from onsets ...')
    beat_ons = microtiming.beats_from_onsets(beats, onsets)

    # 5. plot everything
    ax1 = plt.subplot(2, 1, 1)
    display.wave_plot(y, sr, ax=ax1, beats=beats)
    # plot aligned beats
    ax2 = plt.subplot(2, 1, 2, sharex=ax1)
    display.wave_plot(y, sr, ax=ax2, beats=beat_ons)

    plt.show()
Пример #4
0
# within the rhythm cycle (e.g. 1.1, 1.2, or 1, 2).
#
# **Note 3:** The same annotations file is used for both beats and downbeats.
# This is based on annotation labels that provide a particular string to identify the downbeats.
# In this case, this string is .1, and is the one used by default. You can specify the string to
# look for in the labels data to select downbeats by setting the `downbeat_label` parameter value.
# For instance, `downbeat_label='1'` is used for loading annotations of the samba files included.
#
# **Note 4:** By default the columns are assumed to be separated by a comma, but you can specify
# another separating string by setting the `delimiter` parameter value. For instance, a blank space
# `delimiter=' '` is used for loading annotations of the samba files included.
#
# Let's print the first 10 beat and the first 3 downbeats, with their corresponding labels.
print(beats[:10])
print(beat_labs[:10])

print(downbeats[:3])
print(downbeat_labs[:3])

##############################################
# Finally we plot the audio waveform and the beat annotations

plt.figure(figsize=(12, 6))
ax1 = plt.subplot(2, 1, 1)
display.wave_plot(y, sr, ax=ax1)
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
display.wave_plot(y, sr, ax=ax2, beats=downbeats, beat_labs=downbeat_labs)
plt.tight_layout()

plt.show()
Пример #5
0
# Next, we'll load the annotations provided for the example audio file.
# We get the path to the annotations file corresponding to example number 1,
# and then we load beats and downbeats, along with their labels.
annotations_path = util.example("ansina_beats")

beats, beat_labs = annotations.load_beats(annotations_path)
downbeats, downbeat_labs = annotations.load_downbeats(annotations_path)

##############################################
# Then, we'll compute the accentuation feature.
#
# **Note:** This example is tailored towards the rhythmic patterns of the lowest
# sounding of the three drum types taking part in the recording, so the analysis
# focuses on the low frequencies (20 to 200 Hz).
acce, times, _ = features.accentuation_feature(y, sr, minfreq=20, maxfreq=200)

##############################################
# Finally we plot the audio waveform, the beat annotations and the accentuation feature values.

# plot waveform and accentuation feature
plt.figure(figsize=(12, 6))
# plot waveform
ax1 = plt.subplot(2, 1, 1)
display.wave_plot(y, sr, ax=ax1, beats=beats, beat_labs=beat_labs)
# plot accentuation feature
ax2 = plt.subplot(2, 1, 2, sharex=ax1)
display.feature_plot(acce, times, ax=ax2, beats=beats, beat_labs=beat_labs)
plt.tight_layout()

plt.show()