示例#1
0
def calculatePitchExtraction(name):
    song =  mainPath + name +".wav"
    # sr- sample rate - how many samples is recorded per second
    inputSignal, sr = librosa.load(song)
    time = np.arange(0, len(inputSignal)) / sr

    fmax = 1000 #Hz below C6 note
    fmin = 200 #Hz above G3 note
    elementWithMinPeriod = round(sr/fmax)
    elementWithMaxPeriod = round(sr/fmin)
    # print("timeMax", elementWithMinPeriod, elementWithMaxPeriod)

    A = []
    fundamentalFrequencies = []
    onsets = loff.loadOnsetFromFile(name)
    if onsets == '' :  
        onsets = enf.get_onsets_locations(inputSignal, window_size= 400, threshold = 5)
        onsets = enf.pick_best_onset_in_epsilon(onsets, 4000)

    onsetAmount = len(onsets)
    i = 0
    l = 0
    
    # print(onsetAmount)
    while (i<onsetAmount):
        if i == onsetAmount-1: end = len(inputSignal)
        else: end = onsets[i+1]-1
        y = inputSignal[onsets[i] : end]

        smallestIndex = 0
        smallest = 0.003
        j = elementWithMinPeriod
        while j < elementWithMaxPeriod: 
            AMDFvalue =  AMDF(j,len(y),y)
            if (j==elementWithMinPeriod): smallest = AMDFvalue
            if (AMDFvalue < smallest) :
                smallest = AMDFvalue
                smallestIndex = j
            j += 1
        pitchPeriod = smallestIndex/sr
        if pitchPeriod == 0: fundamentalFrequencies.append(0)
        else: fundamentalFrequencies.append(1/pitchPeriod)
        # print("A.append(smallestOffset)", smallestIndex, pitchPeriod)
        i += 1

    # print("fundamentalFrequencies", fundamentalFrequencies)

    noteSet = []
    for n in fundamentalFrequencies:
        noteSet.append(pitch2Note(n))
    
    # sd.saveInTxt('notes', name, noteSet, '\t')
    return (fundamentalFrequencies , noteSet)
示例#2
0
def calculatePitchExtraction(name):

    threshold = 5
    song = mainPath + name + ".wav"

    # sr- sample rate - how many samples is recorded per second
    input_signal, sr = librosa.load(song)
    window_size = 7800

    onsets = loff.loadOnsetFromFile(name)
    if onsets == '':
        # print("no onsets")
        onsets = enf.get_onsets_locations(input_signal, window_size, threshold)
        onsets = enf.pick_best_onset_in_epsilon(onsets, 4000)
    time = np.arange(0, len(input_signal)) / sr
    leftData = input_signal

    f1 = plt.figure(1)
    # fig, ax = plt.subplots()
    plt.plot(time, input_signal)

    onsetAmount = len(onsets)
    # onsets = onsets[np.argsort(onsets[:,0])]
    i = 0
    l = 0
    fundamentalFrequencies = []
    f2 = plt.figure(2)
    while (i < onsetAmount):
        if i == onsetAmount - 1: end = len(leftData)
        else: end = onsets[i + 1] - 1

        y = np.fft.rfft(leftData[onsets[i]:end])
        z = np.fft.rfftfreq(len(leftData[onsets[i]:end]), 1 / sr)

        k = findMinIndex(y)
        frequency = z[k]

        fundamentalFrequencies.append(frequency)
        plt.plot(list(range(len(y))), np.abs(y))

        i += 1

    noteSet = []
    for n in fundamentalFrequencies:
        noteSet.append(pitch2Note(n))

    # sd.saveInTxt('notes', name, noteSet, '\t')
    return (fundamentalFrequencies, noteSet)
def calculatePitchExtraction(name):
    song =  mainPath + name + ".wav"
    inputSignal, sr = librosa.load(song)

    onsets = loff.loadOnsetFromFile(name)
    fund_freqs = autocorrelation_function(onsets, inputSignal, sr)

    notes = []
    for f in fund_freqs:
        notes.append(pitch2Note(f))
    # print("Fundamental frequencies:")
    # print(fund_freqs)
    # print("Notes:")
    # print(notes)

    return (fund_freqs, notes);
示例#4
0
def showLinearScalingResults(analyzedSongName):
    dbSongNames = os.listdir(mainPathDatabase)
    # print(dbSongNames)
    song = mainPath + analyzedSongName + ".wav"

    timesInput = loff.loadOnsetFromFile(analyzedSongName)
    _, sr = librosa.load(song)
    timesInput = list(map(lambda x: x / sr, timesInput))
    freqsInput = leff.loadFreqsFromFile(analyzedSongName)

    space = ' & '
    fileName = 'MelodyCalculations'
    i = 0

    print(analyzedSongName)
    for dbSong in dbSongNames:
        (score, factor) = linearScaling.linearScaling(freqsInput, dbSong,
                                                      timesInput)
        print(dbSong, score, factor)
def calculatePitchExtraction(name, version=1):

    threshold = 5
    song = mainPath + name + ".wav"

    # sr- sample rate - how many samples is recorded per second
    input_signal, sr = librosa.load(song)
    window_size = 7800

    onsets = loff.loadOnsetFromFile(name)
    if onsets == '':
        # print("no onsets")
        onsets = enf.get_onsets_locations(input_signal, window_size, threshold)
        onsets = enf.pick_best_onset_in_epsilon(onsets, 4000)
    time = np.arange(0, len(input_signal)) / sr
    leftData = input_signal

    onsetAmount = len(onsets)
    # onsets = onsets[np.argsort(onsets[:,0])]
    i = 0
    l = 0
    fundamentalFrequencies = []
    fundamentalFrequencies2 = []

    fmax = 600  #Hz
    fmin = 200  #Hz

    while (i < onsetAmount):
        if i == onsetAmount - 1: end = len(leftData)
        else: end = onsets[i + 1] - 1
        partialData = leftData[onsets[i]:end]

        logFft = np.log(np.abs(np.fft.rfft(partialData)))
        cepstrum = np.fft.rfft(logFft)

        frequencyVector = np.fft.rfftfreq(len(partialData), d=1 / sr)
        df = frequencyVector[1] - frequencyVector[0]
        # df is a sample spacing that appeared in the frequencyVector
        quefrencyVector = np.fft.rfftfreq(logFft.size, df)

        ceps = cepstrum

        nceps = len(ceps)
        peaks = np.zeros(nceps)
        k = 3
        while (k < nceps - 2):
            # print("k",k)
            y1 = ceps[k - 1]
            y2 = ceps[k]
            y3 = ceps[k + 1]
            if (y2 > y1 and y2 >= y3):
                peaks[k] = ceps[k]
            k = k + 1

        valid = (quefrencyVector > 1 / fmax) & (quefrencyVector <= 1 / fmin)
        # Maximal value among all cepstrum data
        maxQuefrencyIndex = np.argmax(np.abs(cepstrum)[valid])
        f0 = 1 / quefrencyVector[valid][maxQuefrencyIndex]
        fundamentalFrequencies.append(f0)

        # Maximal value among peaks in cepstrum data
        (maxIndx, maxVal) = findMaxIndex(peaks[valid])
        f02 = 1 / quefrencyVector[valid][maxIndx]
        fundamentalFrequencies2.append(f02)

        # printCepstralAnalysis(partialData, sr,frequencyVector, quefrencyVector, ceps)

        i += 1

    # print("Fundamental Frequencies", fundamentalFrequencies)
    # print("Fundamental Frequencies2", fundamentalFrequencies2)
    noteSet = []
    for n in fundamentalFrequencies:
        noteSet.append(pitch2Note(n))
    noteSet2 = []
    for n in fundamentalFrequencies2:
        noteSet2.append(pitch2Note(n))

    if version == 1: return (fundamentalFrequencies, noteSet)
    if version == 2: return (fundamentalFrequencies2, noteSet2)
                    c = copy.deepcopy(noteList)
                    buckets[i] = [c, oldTime]
                    i += 1
                    oldTime = round(
                        mido.tick2second(absoluteTime, tpb, lastTempo), 3)
                noteList.append(msg.note)
            elif msg.type == 'note_off':
                if msg.time != 0:
                    c = copy.deepcopy(noteList)
                    buckets[i] = [c, oldTime]
                    i += 1
                    oldTime = round(
                        mido.tick2second(absoluteTime, tpb, lastTempo), 3)
                noteList.remove(msg.note)
    buckets[i] = [noteList, oldTime]
    factor, score = calculate(queryMIDI, buckets[:i + 1], timesInput)
    return (score, factor)


if __name__ == "__main__":
    name = "GdySlicznaPannaA"
    song = mainPath + name + ".wav"
    # (freqsInput, _) = cpem.chooseNotes(name)
    timesInput = loff.loadOnsetFromFile(name)
    freqsInput = leff.loadFreqsFromFile(name)

    _, sr = librosa.load(song)
    timesInput = list(map(lambda x: x / sr, timesInput))
    linearScaling(freqsInput, "GdySlicznaPanna1.mid", timesInput)
    # linearScaling(freqs, "GdySlicznaPannaA", timesInput)
    # linearScaling(freqs, "KawalekPodlogiK", timesInput)