# import sounddevice as sd
# from scipy.io.wavfile import write
# import scipy
# import time

# fs = 44200  # Sample rate

# snippets = []
# for i in range (5):
#   seconds = 1  # Duration of recording
#   myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
#   snippets.append(myrecording)
#   sd.wait()

# for i in range (5):
#   write('output{}.wav'.format(i), fs, snippets[i])
#   sd.play(c, fs)
#   sd.wait()

from scipy.io.wavfile import read
import numpy as np
from numpy import*
import matplotlib.pyplot as plt
a=read("/home/jreinking/Projekte/doorbell/raspberry-pi-projects/src/output2.wav")
print(a)
# plt.plot(list(map(lambda x: x[0], a[1])))
# plt.show()

def dtw(s, t):
    n, m = len(s), len(t)
    dtw_matrix = np.zeros((n+1, m+1))
    for i in range(n+1):
        for j in range(m+1):
            dtw_matrix[i, j] = np.inf
    dtw_matrix[0, 0] = 0
    
    for i in range(1, n+1):
        for j in range(1, m+1):
            cost = abs(s[i-1] - t[j-1])
            # take last min from a square box
            last_min = np.min([dtw_matrix[i-1, j], dtw_matrix[i, j-1], dtw_matrix[i-1, j-1]])
            dtw_matrix[i, j] = cost + last_min
    return dtw_matrix

m = dtw(list(map(lambda x: x[0], a[1])), list(map(lambda x: x[0], a[1])))
print(m)



# print('======')

# c = scipy.vstack((snippets[0], snippets[1], snippets[2], snippets[3], snippets[4]))

