#!/usr/bin/env broken

# Broken!

import sys
import os
import signal
from math import sqrt
try:
    estbase = os.environ["ESTDIR"]
except KeyError:
    print "\n** environment variable ESTDIR is unset **\n"

estmoduledir = "%s/wrappers/python" % estbase
sys.path.append( estmoduledir )

import EST_Wave

espscol = 23
window = 0.1
silence_threshold = 500
silence_count_threshold = 20
speech_count_threshold = 2
end_skip = 0.5     # ignore this part of the file

files = 0

labdir = sys.argv[1]
wavdir = sys.argv[2]

if not os.path.isdir(labdir) or not os.path.isdir(wavdir):
    print "Usage: argv[0] classify_lab_dir final_wav_dir *.d"
    exit(1)


for file in sys.argv[3:]:
    wave = EST_Wave.EST_Wave()

    base = os.path.splitext(os.path.basename(file))[0]
    labelfile = "%s/%s.lab" % (labdir,base)

    subwavfile = "%s/%s" % (wavdir,base)
    #print "Loading %s\n" % file
    wave.load(file)

    current_sil = 0
    silence_started = 0
    silence_count = silence_count_threshold
    speech_count = 0

    window_size = window * wave.sample_rate()
    frames = wave.num_samples()
    start_frame = 0.5 * window_size + 1
    end_frame =  frames - start_frame


    c = end_frame
    last_sv = 1000
    line = ""

    while c > start_frame:

        sv = 0
        for i in range( -0.5 * window_size,
                         0.5*  window_size):
            sv = sv + abs(wave.a(c+i))
        sv = sv / window_size

        if sv < silence_threshold:   # potential silence
            if last_sv < silence_threshold:
                silence_count = silence_count + 1
            else: 
                silence_count = 1
            if silence_count > silence_count_threshold:
                line = "%f %d sil ;  file %s ;\n" % (c/wave.sample_rate(), espscol, ".dummy")
                #print "%f sil %d\n" % (c/wave.sample_rate(),sv)
                c = start_frame

            
        last_sv = sv
        c = c - window*wave.sample_rate()

    # write file
    lfd = open(labelfile,"w")
    lfd.write("separator ;\n")
    lfd.write("#\n")
    lfd.write(line)
    lfd.write("%f %d speech ;  file %s ;\n" % (float(frames)/wave.sample_rate(), espscol, subwavfile))
    lfd.close()           

   