<commit_before>#import unicodedata

class Listener:

    def __init__(self):
        import log
        self.myLog = log.Log('listener.log')

        # files and paths
        self.fileName = 'rectest.wav' # default file name
        self.teachPath = 'recordings/'

        # audio data        
        self.max_freq = 11025 
        self.fs = 0
        self.numSamples = 0
        self.bitsPerSample = 16 # number of bits per sample in the file...assumed to be 16
        self.rawData = [] # raw data from wave file
        self.normalizedData = [] # wave file data normalized on -1 to 1
        self.normalizedDataDB = [] # normalized data coverted to decibels

        # FFT data
        self.fftData = []
        self.fftDataABS = []
        self.fftNumUsefulBins = 11026 # set when FFT performed or when FFT data read from file
        self.fftBinSize = 0.0

        # A-weight data
        self.fftDataAweight = []
        self.normalized_aWeight = []
        self.statsRMSdB_A = 0.0



        #stats
        self.statsMaxAmplitudeDB = 0.0
        self.statsRMSdB = 0.0
        self.statsCorrelation = 0.0

    def audioCapture(self):
        # captures audio into a file
        # from http://www.g7smy.co.uk/?p=283

        #SAVEFILE='rectest.wav'
        #DURATION='1'
        #RATE='22050'
        #FORMAT='S16_LE' # see manual for arecord
        #CHANNELS='1'

        from subprocess import call
        # this function won't work in python 3.5
        call('/usr/bin/arecord -D plughw:1 -f S16_LE -c1 -r22050 --duration=1 ' + self.fileName + ' > /dev/null 2>&1', shell=True)

    def saveFFT(self, newFile, myFFT):
        # save FFT data to a file
        file = open(newFile,'w') # open the file in write mode
        
        # only save useful data
        for x in range(self.fftNumUsefulBins):
            file.write(str(abs(myFFT[x])) + '\n')
        
        file.close() # be nice and close out the file

    def getAudioData(self, audioFile=""):
        # get audio data out of the file
        if audioFile=="":
            audioFile = self.fileName
            
        from scipy.io import wavfile 

        # TODO: error checking on file operations...does it exist...can I access it...etc
            
        # https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.io.wavfile.read.html
        self.fs, self.rawData = wavfile.read(audioFile) # load the data

        # from http://samcarcagno.altervista.org/blog/basic-sound-processing-python/
        if self.rawData.dtype == "int16":
            self.bitsPerSample = 16
        elif self.rawData.dtype == "int32":
            self.bitsPerSample = 32
        else: # unknown....we asked for 16 so assume it...but log an error
            self.bitsPerSample = 16
            self.myLog.add("WARNING in getAudioData(): unknown number of bits per sample: " + self.rawData.dtype + " continuing with 16 bits")

        self.numSamples=len(self.rawData)
                        
        self.normalizeData()
        
    def normalizeData(self): # normalize audio data 
        # normalize wave data between -1 and 1
        
        normalizeFactor = 2**(self.bitsPerSample-1)<commit_msg>Check scipy module exists before using it<commit_after>#import unicodedata

class Listener:

    def __init__(self):
        import log
        self.myLog = log.Log('listener.log')

        # files and paths
        self.fileName = 'rectest.wav' # default file name
        self.teachPath = 'recordings/'

        # audio data        
        self.max_freq = 11025 
        self.fs = 0
        self.numSamples = 0
        self.bitsPerSample = 16 # number of bits per sample in the file...assumed to be 16
        self.rawData = [] # raw data from wave file
        self.normalizedData = [] # wave file data normalized on -1 to 1
        self.normalizedDataDB = [] # normalized data coverted to decibels

        # FFT data
        self.fftData = []
        self.fftDataABS = []
        self.fftNumUsefulBins = 11026 # set when FFT performed or when FFT data read from file
        self.fftBinSize = 0.0

        # A-weight data
        self.fftDataAweight = []
        self.normalized_aWeight = []
        self.statsRMSdB_A = 0.0



        #stats
        self.statsMaxAmplitudeDB = 0.0
        self.statsRMSdB = 0.0
        self.statsCorrelation = 0.0

    def audioCapture(self):
        # captures audio into a file
        # from http://www.g7smy.co.uk/?p=283

        #SAVEFILE='rectest.wav'
        #DURATION='1'
        #RATE='22050'
        #FORMAT='S16_LE' # see manual for arecord
        #CHANNELS='1'

        from subprocess import call
        # this function won't work in python 3.5
        call('/usr/bin/arecord -D plughw:1 -f S16_LE -c1 -r22050 --duration=1 ' + self.fileName + ' > /dev/null 2>&1', shell=True)

    def saveFFT(self, newFile, myFFT):
        # save FFT data to a file
        file = open(newFile,'w') # open the file in write mode
        
        # only save useful data
        for x in range(self.fftNumUsefulBins):
            file.write(str(abs(myFFT[x])) + '\n')
        
        file.close() # be nice and close out the file

    def getAudioData(self, audioFile=""):
        # get audio data out of the file
        if audioFile=="":
            audioFile = self.fileName
        
        import sys
        if not 'scipy' in sys.modules:
            self.myLog.add("ERROR: scipy module not installed")
            print("ERROR: scipy module not installed")
            exit()
        
            
        from scipy.io import wavfile 

        # TODO: error checking on file operations...does it exist...can I access it...etc
            
        # https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.io.wavfile.read.html
        self.fs, self.rawData = wavfile.read(audioFile) # load the data

        # from http://samcarcagno.altervista.org/blog/basic-sound-processing-python/
        if self.rawData.dtype == "int16":
            self.bitsPerSample = 16
        elif self.rawData.dtype == "int32":
            self.bitsPerSample = 32
        else: # unknown....we asked for 16 so assume it...but log an error
            self.bitsPerSample = 16
            self.myLog.add("WARNING in getAudioData(): unknown number of bits per sample: " + self.rawData.dtype + " continuing with 16 bits")

        self.numSamples=len(self.rawData)
                        
        self.normalizeData()
        
    def normalizeData(self): # normalize audio data 
        # normalize wave data between -1 and 1
        
        normalizeFactor = 2**(self.bitsPerSample-1)
{'fraction_non_alphanumeric': 0.10191082802547771, 'fraction_numerical': 0.02335456475583864, 'mean_word_length': 1.864900086880973, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1814396', 'n_tokens_mistral': 2136, 'n_tokens_neox': 1988, 'n_words': 547}