from defs import wavs
from defs import paths as pt
from defs import beats
import pandas as pd
import numpy as np
import os

# uklada casove znacky pro prvnich sest taktu
print('*start*')
# vytvori cestu pro ukladani casovych znacek
if not os.path.exists('data/six_bars'):
    os.makedirs('data/six_bars')
path = 'data/'
stngs = wavs.Settings(sr=22050, frame_length=2048, hop_length=128)
rng = list([1.1, 7.1])
names = pt.listdir('data/_wavs')
for n, name in enumerate(names):
    print('*zpracovani ' + name + '*')
    bts = beats.BeatsData()
    bts_n = beats.BeatsData()
    bts.load_beats(path+pt.beat(name), stngs)
    bts.trim(bts.index4label(rng[0]), bts.index4label(rng[1]))
    time = list()
    # vypocitava rozdil mezi sesti po sobe nasledujicimi dobami
    for i, beat in enumerate(bts.label[:len(bts.label)-6:6]):
        time.append(bts.time[(i+1)*6] - bts.time[i*6])
    if n == 0:
        tbl = time
    else:
        tbl = np.vstack((tbl, time))
# vypoctena data uklada do csv
df = pd.DataFrame(data=tbl, index=names)
df.to_csv('data/six_bars/six_bars.csv')

print('*hotovo*')