from defs import wavs
from defs import beats
from defs import features
from defs import paths as pt
import os

# nacte referencni nahravku a anotaci
# pomoci DTW vytvori nove anotace k nahravkam
print('*start*')
# vytvori cestu pro nove anotace
if not os.path.exists('data/_beats'):
    os.makedirs('data/_beats')
path = 'data/'
# nastaveni se kterym bylo dosazeno nejlepsich vysledku
stngs = wavs.Settings(sr=22050, frame_length=2048, hop_length=2048)
ref_wav = wavs.WavData(stngs, path+'_ref/ref.wav')
ref_wav.load_wav()
ref_beat = beats.BeatsData()
ref_beat.load_beats(path+'_ref/ref.csv', stngs)
ref_chroma = features.chroma_cens(ref_wav.data, stngs)
print('*reference nactena*')
names = pt.listdir('data/_wavs')
for name in names:
    print('*zpracovani '+name+'*')
    new_wav = wavs.WavData(stngs, path+pt.wav(name))
    new_wav.load_wav()
    new_chroma = features.chroma_cens(new_wav.data, stngs)
    new_beat = beats.BeatsData()
    new_beat.label = ref_beat.label
    new_beat.frame = beats.dtw_beats(ref_chroma, new_chroma, ref_beat.frame)
    new_beat.frame2time(stngs)
    new_beat.save_beats(path+pt.beat(name), stngs)
    print('*anotace ulozena*')
print('*hotovo*')

