Python: How to Create an Automatic Recording Program

automatic recording program connects the radio station, if someone talking channel automatically recorded
all the recording stored in an audio file of a day, will record the start time, stop time, duration, the starting position in the recording, convenient and rapid positioning, no one spoke automatically stop recording, saving disk,
Multithreading technology, even if the program crashes the recording file will not be damaged
= = = = = = = = = =
Warning:
Please abide by the Radio Management Regulations of the People’s Republic of China when using this procedure. > Regulations of the People’s Republic of China on Radio Administration
This procedure is only used for learning and communication, and shall not be used for illegal purposes

import threading
import pyaudio
import copy
import math
import time
import numpy
import wave

localtime = time.localtime()
localtimestr = time.strftime("%Y-%m-%d-%H-%M-%S",localtime)
#ltime = time.time()
line = 0
class RecordThread(threading.Thread):
    def __init__(self, audiofile="C:/Users/Public/RE/"+localtimestr+".wav"):
        threading.Thread.__init__(self)
        self.bRecord = True
        self.rr = True
        self.audiofile = audiofile
        self.chunk = 1024
        self.format = pyaudio.paInt16
        self.channels = 1
        self.rate = 16000

    def run(self):
        #print("RUN....")
        audio = pyaudio.PyAudio()
        wavfile = wave.open(self.audiofile, 'wb')
        wavfile.setnchannels(self.channels)
        wavfile.setsampwidth(audio.get_sample_size(self.format))
        wavfile.setframerate(self.rate)
        wavstream = audio.open(format=self.format,
                               channels=self.channels,
                               rate=self.rate,
                               input=True,
                               frames_per_buffer=self.chunk)
        global xx
        global yy
        xx = 0
        yy = 0
        global line
        alltime = 0
        ntime1 = 0
        ntime2 = 0
        starttime = 0
        stoptime = 0
        timediff = 0
        while self.bRecord:
            data = wavstream.read(self.chunk)
            wavdata = numpy.fromstring(data,dtype=numpy.short)
            M = []
            for i in range(0,len(wavdata),16000):
                M.append(wavdata[i:i+16000]/10)
                M=map(abslist,M)
                sound = list(map(mean,M))
            if sound[0] > 50:
                #print("over")
                #Write
                xx = 1
                wavfile.writeframes(data)
            else:
                xx = 0
            if xx > yy:
                yy = 1
                #START
                starttime = time.time()
                alltime = round(alltime + timediff,3)
                
                log("StartTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(starttime))+"    开始时间: "+timestr(alltime))
                print("StartTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(starttime))+"    开始时间: "+timestr(alltime))
            if xx < yy:
                yy = 0
                #STOP
                stoptime = time.time()
                timediff = round(stoptime - starttime,3)
                
                srt(str(line)+"\n"+timestr(alltime)+" --> "+timestr(alltime+timediff)+"\n"+str(line)+"\n"+"<font color=#5F9F9F>"+time.strftime("%H:%M:%S",time.localtime(starttime))+"  ->  "+time.strftime("%H:%M:%S",time.localtime(stoptime))+"</font> "+"<font color=#4D4DFF>"+timestr(timediff)+"</font>"+"\n")
                line = line + 1
                log("StopTime:  "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(stoptime))+"    结束时间: "+timestr(alltime+timediff)+"\n")
                print("StopTime:  "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(stoptime))+"    结束时间: "+timestr(alltime+timediff))
                print("Time: "+timestr(timediff)+"\n")
                 
        wavstream.stop_stream()
        wavstream.close()
        audio.terminate()
        
    def stoprecord(self):
        print("stop")
        self.bRecord = False
        
    def pause(self):
        print("pause")
        self.rr = False
       
    def next(self):
        print("next")
        self.rr = True
        
def abslist(a):
    return list(map(abs,a))
def mean(a):
    return numpy.longlong(sum(a))/len(a)

def log(msg):
    with open('C:/Users/Public/RE/'+localtimestr+ '.txt','a+') as file:
        file.write(msg+"\n")
        file.close()

def srt(msg):
    with open('C:/Users/Public/RE/'+localtimestr+ '.srt','a+') as file:
        file.write(msg+"\n")
        file.close()

        
def timestr(sec):
    m,s = divmod(sec,60)
    h,m = divmod(m,60)
    return str("%d:%02d:%.2f"%(h,m,s))



    
rt = RecordThread()
line = line + 1
#print(timestr(2.65))

log("RUN ...... Start At "+localtimestr+"    SYS OK!"+"  Frequency:91.1Mhz")
srt(str(line)+"\n"+"0:00:00.0 --> 0:00:30.0\n"+"{\\an8}"+"<font color=#FFFF00>"+ str(time.strftime("%Y/%m/%d %H:%M:%S",localtime))+"</font>"+" <font color=#00FFFF>(20:00-21:00)</font>"+"\n"+"<font color=#00FF00>438.025 -5 88.5 <i>QTH Suzhou Jiangsu China</i></font>\n<font color=#3299CC>苏州市业余无线电 472752158</font>\n\n1\n0:00:00.0 --> 0:00:30.0\n{\\an5}请遵守<font color=#FF0000><u><b>《中华人民共和国无线电管理条例》</b></u></font>\n")
print("RUN ...... Start At "+localtimestr+"    SYS OK!"+"  Frequency:91.1Mhz")
rt.start()

Read More: