from defs import wavs
from defs import paths as pt
from defs import beats
from defs import features as ft
import winsound
import soundfile as sf
import os

# extrahuje parametry z nahravek
print('*start*')
# vytvori cestu pro ukladani parametru
if not os.path.exists('data/_feats'):
    os.makedirs('data/_feats')
if not os.path.exists('data/clip'):
    os.makedirs('data/clip')
path = 'data/'
stngs = wavs.Settings(sr=44100, frame_length=2048, hop_length=512)
# extrahujeme mparametry z prvnich deseti taktu
rng = list([1.1, 11.1])
names = pt.listdir('data/_wavs')
for name in names:
    print('*zpracovani ' + name + '*')
    bts = beats.BeatsData()
    bts.load_beats(path+pt.beat(name), stngs)
    bts.trim(bts.index4label(rng[0]), bts.index4label(rng[1]))
    wav = wavs.WavData(stngs, path+pt.wav(name))
    y = wav.load_wav([bts.time[0], bts.time[-1]])
    # zpracovavany usek se muze ulozit pro poslechovou kontorlu
    sf.write(path+pt.clip(name), y, stngs.sr, 'PCM_24')
    # winsound.PlaySound(path+pt.clip(name), winsound.SND_FILENAME)
    print('*extrakce*')
    fts = ft.FeatsData()
    fts.get_feats(y, stngs)
    fts.save_feats(path+pt.feat(name))
    print('*parametry ulozeny*')
print('*hotovo*')
