Skip to content

stephanie: upgrade: python3: Some quick fixes (code in description) #1

Description

@Abhi011999
import scipy.io.wavfile as wavfile
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.signal import argrelextrema
from scipy.interpolate import interp1d
import abjad


#WAV = 'scale.wav'
#WAV = 'longscale.wav'
#WAV = 'longscaleMOD.wav'
#WAV = 'clarinet.wav'
# WAV = 'clarinetMOD.wav'
#WAV = 'GuitarMod.wav'
#WAV = 'VocalMOD.wav'
WAV = 'input_audio.wav'


rate, data = wavfile.read(WAV)
# ? time = np.arange(len(data[:,0]))*1.0/rate
time = np.arange(len(data))*1.0/rate
# time = np.arange(len(data[:,0]))*1.0/rate

#plt.plot(time,data[:,0])
#plt.show()

nfft = 1024*6
#? pxx, freq, bins, plot = plt.specgram(data[:,0],NFFT=nfft)
pxx, freq, bins, plot = plt.specgram(data,NFFT=nfft)
# pxx, freq, bins, plot = plt.specgram(data[:,0],NFFT=nfft)

plt.show()

a = np.mean(pxx,axis=0)
aa = np.arange(len(a))
#? a = a/np.max(a)*np.max(data[:,0])
a = a/np.max(a)*np.max(data)
# a = a/np.max(a)*np.max(data[:,0])
aa = aa/np.max(aa) * time[-1]

f = interp1d(aa,a)
newSmooth = f(time)

indMax = argrelextrema(newSmooth, np.greater)[0]
indMin = argrelextrema(newSmooth, np.less)[0]

lastValue = np.where(newSmooth==newSmooth[-1])[0]
indMin = np.hstack((indMin,lastValue))

print(indMin, indMax)
#?
indMax = np.append(indMax, indMax[-1])
#?
print(indMin, indMax)

#? plt.plot(time,data[:,0])
plt.plot(time,data)
# plt.plot(time,data[:,0])
plt.plot(aa,a)
plt.plot(time[indMax],newSmooth[indMax])
plt.plot(time[indMin],newSmooth[indMin])
plt.show()

NoteFile = pd.read_excel('NoteFreq.xlsx',0)
notes = np.array([indMax,indMin]).T


def getHarmonics(p,f,maxPower,maxFrequency,harmonics,harm):
    x = maxFrequency/harm
    #problem is that its not exactly in f
    ind1 = np.where(f<=x+1)
    ind2 = np.where(f>=x-1)
    mask = np.in1d(ind1,ind2)
    index = np.where(mask == True)[0]

    print('frequency')
    print(f[index])
    condition = p[index]/maxPower
    try:
        condition = condition[0]
    except IndexError:
        pass

    #if p[index]/maxPower>=0.90:
    if condition>=0.90:
        harmonics.append(f[index][0])

    return harmonics


def getFreq(notes):
    individualNotes = []
    freqs = []
    letterNotes = []

    for v in notes:
        if v[1] <= v[0]:
            continue

        #? individualNotes.append(data[v[0]:v[1],0])
        individualNotes.append(data[v[0]:v[1]])
        # individualNotes.append(data[v[0]:v[1],0])

        #? p = 20*np.log10(np.abs(np.fft.rfft(data[v[0]:v[1], 0])))
        p = 20*np.log10(np.abs(np.fft.rfft(data[v[0]:v[1]])))
        # p = 20*np.log10(np.abs(np.fft.rfft(data[v[0]:v[1], 0])))
        f = np.linspace(0, rate/2.0, len(p))
        # plt.plot(f,p)
        # plt.show()

        harmonics = []
        maxPower = np.max(p)
        maxFrequency = f[np.where(p==max(p))][0]

        for j in range(2,8):
            harmonics = getHarmonics(p,f,maxPower,maxFrequency,harmonics,j)

        if harmonics==[]:
            harmonics = [maxFrequency]

        print('HARMONICS')
        maxFreq = harmonics
        print(maxFreq)

        note = NoteFile.query(f'Lower<@maxFreq[0]<Upper').iat[0,0]
        print(f'note: {note}')

        letterNotes.append(str(note).strip().lower())

    return letterNotes, freqs, individualNotes


letterNotes, freqs, individualNotes = getFreq(notes)
print('letternotes', letterNotes)

staff = abjad.Staff()

def fixNotes(letters):
    note = ''
    m = letters

    if m[-1]=='0':
        #note = m[0].upper()+ m[0].upper()+ m[0].upper()
        note = m[0]+3*','

    if m[-1]=='1':
        #note = m[0].upper()+ m[0].upper()
        note = m[0]+2*','

    if m[-1]=='2':
        note = m[0]+','

    if m[-1]=='3':
        note = m[0]

    if m[-1]=='4':
        note = m[0]+"'"

    if m[-1]=='5':
        note = m[0]+2*"'"

    if m[-1]=='6':
        note = m[0]+3*"'"

    if m[-1]=='7':
        note = m[0]+4*"'"

    if m[-1]=='8':
        note = m[0]+5*"'"

    if m[-1]=='9':
        note = m[0]+6*"'"

    if m[-1]=='10':
        note = m[0]+7*"'"

    if m[1]=='s':
        fixed = note
        fixed = fixed[:1]+'s'+fixed[1:]
    else:
        fixed = note

    return fixed


for note in letterNotes:
    print(f'letters: {note}')
    fixed = fixNotes(note)
    print(f'fixed: {fixed}')
    staff.extend(fixed)

abjad.show(staff)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions