import sys
sys.version = '3.6.7 (default, Nov 6 2019, 16:19:42) \n[GCC 7.3.0]'
sys.path.append('thinkdsp/')
import thinkdsp
#segment: ebben a hangmintaban keressuk az eredeti hangmintaval "original_sample" leginkabb korrelalo reszt original_sample: az eredeti madarhang minta
# A fuggveny a bemeneti hangmintat mintavetelenkent osszehasonlitja az altalunk keresett hangmintaval es vissza adja annak a maximalis korrelaciot.
from flask import request, session, app
from flask import current_app
from datetime import datetime
def main():
	current_app.logger.info("Received request")
	now = datetime.now()
	current_time = now.strftime("%H:%M:%S")
	output = ""
	output = output + "Elinditva:" + current_time + "\n"
	maxcorr = 0
	time = 0
	filename_raw = str(request.get_data())
	filename = filename_raw[2:-1]
	sturnusVulgaris = thinkdsp.read_wave("wavs/%s.wav" % filename)
	train_sample = sturnusVulgaris.segment(start=4,duration=1)
	test_sample = sturnusVulgaris.segment(start=0,duration=5)
	segment = test_sample
	original_sample = train_sample
#sturnusVulgaris = thinkdsp.read_wave(filename) 1minta 1 masodperc(framerate szama = utolso elem)
	lastsample = segment.framerate
	for timestamp in segment.ts[:-lastsample]:
		#mindig az adott mintaveteltol szamitott 1mp-es mintat vesszuk
		segment_chunk = segment.segment(start=timestamp, duration=1)
		#Correlation coefficient two waves.
		correlation = round(original_sample.corr(segment_chunk),8)
		if correlation > maxcorr :
			maxcorr = correlation
			time = timestamp
			output = str(output) + str(round(correlation,8)) + "\n"
		if maxcorr > 0.9:
			output = output + str(maxcorr)
			end = datetime.now()
			end_time = end.strftime("%H:%M:%S")
			return output + "\nVegeredmeny:\n" + str(maxcorr) + "\nMasodperc:\n" + str(time) + "\nBefejezve:"+ end_time