# Path: brain.py
# def _project(audio_id, sound_to_face, NAP, video_producer):
#     stride = IO.VIDEO_SAMPLE_TIME / (IO.NAP_RATE/IO.NAP_STRIDE)
#     length = np.floor(NAP.shape[0]*.8).astype('int')
#     NAP = NAP[:length:stride]
#     try: 
#         face_id = np.random.choice(sound_to_face[audio_id])
#         tarantino = utils.load_esn(video_producer[(audio_id, face_id)])
#         return tarantino(NAP)
#     except:
#         matches = filter(lambda key: key[0] == audio_id, video_producer.keys())
#         if len(matches):
#             tarantino = utils.load_esn(video_producer[random.choice(matches)])
#         else:
#             tarantino = utils.load_esn(video_producer[random.choice(video_producer.keys())])
#         return tarantino(NAP)
# 
# def _extract_NAP(segstart, segend, soundfile, suffix='cochlear'):
#     new_sentence = utils.csv_to_array(soundfile + suffix)
#     audio_segments = utils.get_segments(soundfile)
#     segstart_norm = int(np.rint(new_sentence.shape[0]*segstart/audio_segments[-1]))
#     segend_norm = int(np.rint(new_sentence.shape[0]*segend/audio_segments[-1]))
#     return utils.trim_right(new_sentence[segstart_norm:segend_norm])
# 
# def _three_amigos(context, host):
#     stateQ = context.socket(zmq.SUB)
#     stateQ.connect('tcp://{}:{}'.format(host, IO.STATE))
#     stateQ.setsockopt(zmq.SUBSCRIBE, b'') 
# 
#     eventQ = context.socket(zmq.SUB)
#     eventQ.connect('tcp://{}:{}'.format(host, IO.EVENT))
#     eventQ.setsockopt(zmq.SUBSCRIBE, b'') 
# 
#     brainQ = context.socket(zmq.PUSH)
#     brainQ.connect('tcp://{}:{}'.format(host, IO.BRAIN))
# 
#     return stateQ, eventQ, brainQ


import multiprocessing as mp
import sys
import glob
import cPickle as pickle
import time
import os
import itertools
import random
import numpy as np
import zmq
import utils
import IO
import association
import my_sai_test as mysai
import myCsoundAudioOptions
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt
from uuid import uuid4
from collections import deque
from subprocess import call
from collections import namedtuple
from scipy.stats import itemfreq
from scipy.cluster.vq import kmeans, vq
from brain import _project, _extract_NAP, _three_amigos

                    best_match,_,_,_,_ = audio_memory.find(NAP)
                    audio_id = best_match.audio_id
                    soundfile = best_match.wav_file
        
                    numWords = len(audio_segments)-1
                    print numWords
                    association.send_pyobj(['setParam', 'numWords', numWords ])
                    association.recv_pyobj()
                    
                    association.send_pyobj(['makeSentence', audio_id])
                    print 'respond_sentence waiting for association output...', 
                    sentence, secondaryStream = association.recv_pyobj()

                    print '*** (respond) Play sentence', sentence, secondaryStream
                    start = 0 
                    nextTime1 = 0
                    nextTime2 = 0
                    enableVoice2 = 1

                    play_events = []

                    for i in range(len(sentence)):
                        word_id = sentence[i]
                        soundfile = np.random.choice(wavs[word_id])
                        voiceChannel = 1
                        speed = 1
                        
                        # segment start and end within sound file, if zero, play whole file
                        segstart, segend = wav_audio_ids[(soundfile, word_id)]

# Path: brain.py
# def _project(audio_id, sound_to_face, NAP, video_producer):
#     stride = IO.VIDEO_SAMPLE_TIME / (IO.NAP_RATE/IO.NAP_STRIDE)
#     length = np.floor(NAP.shape[0]*.8).astype('int')
#     NAP = NAP[:length:stride]
#     try: 
#         face_id = np.random.choice(sound_to_face[audio_id])
#         tarantino = utils.load_esn(video_producer[(audio_id, face_id)])
#         return tarantino(NAP)
#     except:
#         matches = filter(lambda key: key[0] == audio_id, video_producer.keys())
#         if len(matches):
#             tarantino = utils.load_esn(video_producer[random.choice(matches)])
#         else:
#             tarantino = utils.load_esn(video_producer[random.choice(video_producer.keys())])
#         return tarantino(NAP)
# 
# def _extract_NAP(segstart, segend, soundfile, suffix='cochlear'):
#     new_sentence = utils.csv_to_array(soundfile + suffix)
#     audio_segments = utils.get_segments(soundfile)
#     segstart_norm = int(np.rint(new_sentence.shape[0]*segstart/audio_segments[-1]))
#     segend_norm = int(np.rint(new_sentence.shape[0]*segend/audio_segments[-1]))
#     return utils.trim_right(new_sentence[segstart_norm:segend_norm])
# 
# def _three_amigos(context, host):
#     stateQ = context.socket(zmq.SUB)
#     stateQ.connect('tcp://{}:{}'.format(host, IO.STATE))
#     stateQ.setsockopt(zmq.SUBSCRIBE, b'') 
# 
#     eventQ = context.socket(zmq.SUB)
#     eventQ.connect('tcp://{}:{}'.format(host, IO.EVENT))
#     eventQ.setsockopt(zmq.SUBSCRIBE, b'') 
# 
#     brainQ = context.socket(zmq.PUSH)
#     brainQ.connect('tcp://{}:{}'.format(host, IO.BRAIN))
# 
#     return stateQ, eventQ, brainQ


# Path: altbrain.py
import multiprocessing as mp
import sys
import glob
import cPickle as pickle
import time
import os
import itertools
import random
import numpy as np
import zmq
import utils
import IO
import association
import my_sai_test as mysai
import myCsoundAudioOptions
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt
from uuid import uuid4
from collections import deque
from subprocess import call
from collections import namedtuple
from scipy.stats import itemfreq
from scipy.cluster.vq import kmeans, vq
from brain import _project, _extract_NAP, _three_amigos


                    best_match,_,_,_,_ = audio_memory.find(NAP)
                    audio_id = best_match.audio_id
                    soundfile = best_match.wav_file
        
                    numWords = len(audio_segments)-1
                    print numWords
                    association.send_pyobj(['setParam', 'numWords', numWords ])
                    association.recv_pyobj()
                    
                    association.send_pyobj(['makeSentence', audio_id])
                    print 'respond_sentence waiting for association output...', 
                    sentence, secondaryStream = association.recv_pyobj()

                    print '*** (respond) Play sentence', sentence, secondaryStream
                    start = 0 
                    nextTime1 = 0
                    nextTime2 = 0
                    enableVoice2 = 1

                    play_events = []

                    for i in range(len(sentence)):
                        word_id = sentence[i]
                        soundfile = np.random.choice(wavs[word_id])
                        voiceChannel = 1
                        speed = 1
                        
                        # segment start and end within sound file, if zero, play whole file
                        segstart, segend = wav_audio_ids[(soundfile, word_id)]

                        NAP = _extract_NAP(segstart, segend, soundfile)