import numpy as np
import matplotlib.pyplot as plt
import time
from copy import copy
import os

from single_pitch import single_pitch
from channel import channel 
from pseudo_speech import Pseudospeech_Synthetizer_class
from encryption import Encryption_class
from speech_analyzer import Speech_Analyzer_class
from speech_synthesizer import Speech_Synthesizer_class

################################################################

my_analyzer    = Speech_Analyzer_class("speech_model.npz","spherical_code.npz") # model parameters generated by speech_model.py and spherical_code.py
my_encryptor   = Encryption_class("spherical_code.npz") # model parameters generated by spherical_code.py
my_ps_sp_synthetizer = Pseudospeech_Synthetizer_class("pseudospeech_model.npz","spherical_code.npz") # model parameters generated by pseudo_speech_model.py and spherical_code.py
my_sp_synthesizer    = Speech_Synthesizer_class("speech_model.npz") # model parameters generated by speech_model.py

# pseudo random data used for enciphering/deciphering
keybits        = np.random.randint(2, size = (160, 10000))

print("step 1")
speech_samples = np.fromfile("temp/hts1a.raw", dtype='int16')
# print(speech_samples.shape)

##### SPEECH ENCODING ######
print("step 2")
pitch_indices, energy_indices, timbre_indices = my_analyzer.analyze_speech(speech_samples)

###### ENCRYPTION ######
print("step 3")
pitch_indices_enc, energy_indices_enc, timbre_indices_enc = my_encryptor.speech_encryption(pitch_indices, energy_indices, timbre_indices, keybits)

###### PSEUDOSPEECH SYNTHESIS ######
print("step 4")
signal = my_synthetizer.synthesize_pseudospeech(pitch_indices_enc, energy_indices_enc, timbre_indices_enc)

###### CHANNEL DISTORTION ######
print("step 5")
signal_rec = channel(signal, "SILK", 16000, 48000) # data samples, codec type, sampling frequency (Hz), compression rate (b/s)

###### PSEUDOSPEECH ANALYSIS ######
print("step 6")
pitch_indices_rec, energy_indices_rec, timbre_indices_rec = my_synthetizer.analyze_pseudospeech(signal_rec)

# ###### DECRYPTION ######
print("step 7")
pitch_indices_dec, energy_indices_dec, timbre_indices_dec = my_encryptor.speech_decryption(pitch_indices_rec, energy_indices_rec, timbre_indices_rec, keybits)

# ###### SPEECH SYNTHESIS ######
print("step 8")
my_speech_synthesizer.synthesize_speech(pitch_indices_dec, energy_indices_dec, timbre_indices_dec) # save to file / input of the narrowband (8kHz) LPCNet

print("Finished")


################

# plt.figure()
# plt.plot(energy_indices)

# plt.figure()
# plt.plot(pitch_indices)

# plt.figure()
# plt.plot(np.transpose(timbre_indices))

################

# plt.figure()
# plt.plot(energy_indices_enc)

# plt.figure()
# plt.plot(pitch_indices_enc)

# plt.figure()
# plt.plot(np.transpose(timbre_indices_enc))

################

# plt.figure()
# plt.plot(energy_indices_rec)

# plt.figure()
# plt.plot(pitch_indices_rec)

# plt.figure()
# plt.plot(np.transpose(timbre_indices_rec))

################

# plt.figure()
# plt.plot(energy_indices_dec)

# plt.figure()
# plt.plot(pitch_indices_dec)

# plt.figure()
# plt.plot(np.transpose(timbre_indices_dec))

################

plt.show()

