Пример #1
0
def analyze(file):
    # Feature extraction example
    import numpy as np
    import librosa

    # Load the example clip
    y, sr = librosa.load(file)

    # Set the hop length; at 22050 Hz, 512 samples ~= 23ms
    hop_length = 512

    # Separate harmonics and percussives into two waveforms
    y_harmonic, y_percussive = librosa.effects.hpss(y)

    y_harmonic = abs(y_harmonic)
    y_percussive = abs(y_percussive)

    # FIXME: this should be sampling rate dependant
    max_filter_win_size = 1000
    y_harmonic = maximum_filter(y_harmonic, max_filter_win_size)
    y_harmonic = gaussian_filter1d(y_harmonic, 1000)

    y_percussive = maximum_filter(y_percussive, max_filter_win_size)
    y_percussive = gaussian_filter1d(y_percussive, 1000)

    # normalize
    amax = np.amax(y_harmonic)
    y_harmonic = y_harmonic / amax

    amax = np.amax(y_percussive)
    y_percussive = y_percussive / amax

    y_total = y_percussive + y_harmonic

    #max_positions = argrelextrema(y_total, np.greater)[0]
    max_positions, _ = find_peaks(y_total, distance=150, prominence=0.1)

    # open file with wavfile

    previous_result = [0] * 84

    model = tf.keras.models.load_model("model")
    tablature = Tablature()

    limits = np.zeros(y_total.shape[0])

    for pulse in range(0, len(max_positions) - 1):
        time_from = max_positions[pulse]
        time_to = max_positions[pulse + 1]

        diff = time_to - time_from
        DIFF_PERCENTAGE = 0.8
        C = np.abs(
            librosa.cqt(y[time_from:time_from + int(diff * DIFF_PERCENTAGE)] /
                        float(65535),
                        sr=sr,
                        norm=0,
                        filter_scale=3))

        from_print = (1 / sr) * time_from
        to_print = (1 / sr) * (time_from + diff * DIFF_PERCENTAGE)

        limits[int(from_print * sr)] = 1
        limits[int(to_print * sr)] = 1

        # get info
        print("note " + str(pulse) + " from: " + str(from_print) + "s to: " +
              str(to_print) + "s")
        wavfile.write("temp/" + str(pulse) + ".wav", sr,
                      y[time_from:int(time_from + diff * DIFF_PERCENTAGE)])

        # add all samples
        result = np.sum(C, axis=1)

        amax = np.amax(result)
        result = result / amax

        new_result = result - previous_result
        previous_result = result

        # normalize
        amax = np.amax(new_result)

        # ignore silence pulses
        if amax < 0.01:
            continue

        new_result = new_result / amax

        # predict values
        prediction = model.predict(np.array([new_result]))

        new_result = np.where(prediction[0] == np.amax(prediction[0]))
        note_name = new_result[0]

        tablature.addNotes(note_name + noteutils.NoteUtils.offsetGuitar)
        """if cur_time != previous_time:
            previous_time = cur_time//3
            tablature.addTime(str(cur_time))"""

    # plot
    time = (1 / sr) * len(y_total)
    xf = np.linspace(0.0, time, len(y_total))
    plt.plot(xf, y_total)
    plt.plot(xf, limits)
    plt.plot(xf, y)
    plt.plot(max_positions / sr, y_total[max_positions], "x")
    plt.show()

    tablature.print()