# Path: brain.py
# def cochlear(filename, stride, rate, db=-40, ears=1, a_1=-0.995, apply_filter=1, suffix='cochlear'):
#     original_rate, data = wavfile.read(filename)
#     assert data.dtype == np.int16
#     data = data / float(2**15)
#     if original_rate != rate:
#         data = resample(data, float(rate)/original_rate, 'sinc_best')
#     data = data*10**(db/20)
#     utils.array_to_csv('{}-audio.txt'.format(filename), data)
#     call(['./carfac-cmd', filename, str(len(data)), str(ears), str(rate), str(stride), str(a_1), str(apply_filter), suffix])
#     naps = utils.csv_to_array(filename+suffix)
#     return np.sqrt(np.maximum(0, naps)/np.max(naps))
# 
# Path: brain.py
# NUMBER_OF_BRAINS = 5


import os
import wave
import csv
import time
import re
import linecache
import sys
import cPickle as pickle
import zlib
import random
import multiprocessing as mp
import threading
import glob
import fcntl
import json
import sched
import datetime
import numpy as np
import zmq
import scipy.fftpack
import cv2
import IO
        import matplotlib.pyplot as plt
    import Oger
    import mdp
from subprocess import call
from scipy.io import wavfile
from sklearn.covariance import EmpiricalCovariance, MinCovDet
from scipy.stats import itemfreq
from brain import cochlear
from brain import NUMBER_OF_BRAINS
    from scipy.io import wavfile
def sentinel(host):
    context = zmq.Context()
    
    life_signal_Q = context.socket(zmq.PULL)
    life_signal_Q.bind('tcp://*:{}'.format(IO.SENTINEL))

    sender = context.socket(zmq.PUSH)
    sender.connect('tcp://{}:{}'.format(host, IO.EXTERNAL))

    poller = zmq.Poller()
    poller.register(life_signal_Q, zmq.POLLIN)

    book = {}
    save_name = False
    save_time = 0

    while True:
        events = dict(poller.poll(timeout=IO.PROCESS_TIME_OUT*2))

        if life_signal_Q in events:
            process = life_signal_Q.recv_pyobj()
            book[process] = time.time()

        for process in book.keys():
            if not save_name and time.time() - book[process] > IO.PROCESS_TIME_OUT*2:
                print '{} HAS DIED, SAVING'.format(process)
                save_name = brain_name()
                save_time = time.time()
                sender.send_json('save {}'.format(save_name))


# Path: brain.py
# def cochlear(filename, stride, rate, db=-40, ears=1, a_1=-0.995, apply_filter=1, suffix='cochlear'):
#     original_rate, data = wavfile.read(filename)
#     assert data.dtype == np.int16
#     data = data / float(2**15)
#     if original_rate != rate:
#         data = resample(data, float(rate)/original_rate, 'sinc_best')
#     data = data*10**(db/20)
#     utils.array_to_csv('{}-audio.txt'.format(filename), data)
#     call(['./carfac-cmd', filename, str(len(data)), str(ears), str(rate), str(stride), str(a_1), str(apply_filter), suffix])
#     naps = utils.csv_to_array(filename+suffix)
#     return np.sqrt(np.maximum(0, naps)/np.max(naps))
# 
# Path: brain.py
# NUMBER_OF_BRAINS = 5


# Path: utils.py
import os
import wave
import csv
import time
import re
import linecache
import sys
import cPickle as pickle
import zlib
import random
import multiprocessing as mp
import threading
import glob
import fcntl
import json
import sched
import datetime
import numpy as np
import zmq
import scipy.fftpack
import cv2
import IO
        import matplotlib.pyplot as plt
    import Oger
    import mdp
from subprocess import call
from scipy.io import wavfile
from sklearn.covariance import EmpiricalCovariance, MinCovDet
from scipy.stats import itemfreq
from brain import cochlear
from brain import NUMBER_OF_BRAINS
    from scipy.io import wavfile

def sentinel(host):
    context = zmq.Context()
    
    life_signal_Q = context.socket(zmq.PULL)
    life_signal_Q.bind('tcp://*:{}'.format(IO.SENTINEL))

    sender = context.socket(zmq.PUSH)
    sender.connect('tcp://{}:{}'.format(host, IO.EXTERNAL))

    poller = zmq.Poller()
    poller.register(life_signal_Q, zmq.POLLIN)

    book = {}
    save_name = False
    save_time = 0

    while True:
        events = dict(poller.poll(timeout=IO.PROCESS_TIME_OUT*2))

        if life_signal_Q in events:
            process = life_signal_Q.recv_pyobj()
            book[process] = time.time()

        for process in book.keys():
            if not save_name and time.time() - book[process] > IO.PROCESS_TIME_OUT*2:
                print '{} HAS DIED, SAVING'.format(process)
                save_name = brain_name()
                save_time = time.time()
                sender.send_json('save {}'.format(save_name))


        if save_name and (len(glob.glob('{}*'.format(save_name))) == NUMBER_OF_BRAINS or time.time() - save_time > IO.SYSTEM_TIME_OUT):