import ctypes
import sys
from ctypes import *
import os


class delaystruct(Structure):
 _fields_ = [

        ("Crude_DelayEst", c_long),  # c_byte

 ]

def cal_delay(reffile, testfile, samplerate):
        delays = delaystruct()
        mydll = ctypes.windll.LoadLibrary(sys.prefix + '/time_align.dll')
        inputFile = c_char_p(
               bytes(reffile.encode('utf-8')))  # create_unicode_buffer(inFile.encode('utf-8'), len(inFile))
        outputFile = c_char_p(
               bytes(testfile.encode('utf-8')))  # create_unicode_buffer(outFile.encode('utf-8'), len(outFile))
        if samplerate == 8000:
               mode = 'nb'
        if samplerate == 16000:
               mode = 'wb'
        cmode = c_char_p(bytes(mode.encode('utf-8')))
        mydll.cal_delay(inputFile, outputFile, samplerate, cmode, byref(delays))
        # print(delays.Nutterances)
        # print(delays.Largest_uttsize)
        # print(delays.Nsurf_samples)
        # print(delays.Crude_DelayEst)
        # print(delays.Crude_DelayConf)
        print(delays.Crude_DelayEst)
        return delays.Crude_DelayEst

if __name__ == '__main__':
       ref = r'E:\files\cle_malePolqaWB_8000.pcm'
       test = r'E:\files\cle_malePolqaWB_8000_d.pcm'
       cal_delay(ref,test,16000)
       pass