Example #1
0
def main():
    global waveFrames
    global recordNumber
    dirName, recordNumber, recordDuration = interactions.getRecordParameters()

    print("First we record the background noise, so please don't make any noise.")
    print(
        "Please note that there is a delay of 0,08 seconds before the recording start to prevent to capture you hitting the Enter key")

    # open audio stream with callback function
    stream = p.open(format=conf.FORMAT,
                channels=conf.CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=conf.CHUNK,
                stream_callback=callback,
                input_device_index=conf.DEVICE_INDEX)
    raw_input("Press Enter key to start recording of background noise.")
    # wait to prevent to capture the hitting of enter key
    time.sleep(.08)
    waveFrames = []
    # start audio stream
    stream.start_stream()
    print("* recording")
    # wait for stream to finish
    while stream.is_active():
        time.sleep(recordDuration)
        stream.stop_stream()
    print("* done recording")
    print()
    
    # close stream
    stream.close()
    
    wf = wave.open("tmp/tmp.wav", "wb")
    wf.setnchannels(conf.CHANNELS)
    wf.setsampwidth(p.get_sample_size(conf.FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(waveFrames))
    wf.close
    userInput = interactions.wantToStoreRecord(waveFrames)
    f.clearTmpFolder()
    if userInput == '':
        # write audio data to wave file
        wf = wave.open(
            str(dirName) + "/backgroundNoise.wav", "wb")
        wf.setnchannels(conf.CHANNELS)
        wf.setsampwidth(p.get_sample_size(conf.FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(waveFrames))
        wf.close

    while userInput == '':  # TODO t':
        print("Ready to record %d seconds" % (recordDuration))
        print(
            "Please note that there is a delay of 0,08 seconds before the recording start to prevent to capture you hitting the Enter key")

        # open audio stream with callback function
        stream = p.open(format=conf.FORMAT,
                    channels=conf.CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=conf.CHUNK,
                    stream_callback=callback,
                    input_device_index=conf.DEVICE_INDEX)

        raw_input("Press Enter key to start " + str(recordNumber) + ". recording.")

        # wait to prevent to capture the hitting of enter key
        time.sleep(.08)
        waveFrames = []

        # start audio stream
        stream.start_stream()

        print("* recording")

        # wait for stream to finish
        while stream.is_active():
            time.sleep(recordDuration)
            stream.stop_stream()

        print("* done recording")
        print()
        
        # close stream
        stream.close()
        

        wf = wave.open("tmp/tmp.wav", "wb")
        wf.setnchannels(conf.CHANNELS)
        wf.setsampwidth(p.get_sample_size(conf.FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(waveFrames))
        wf.close

        userInput = interactions.wantToStoreRecord(waveFrames)
        f.clearTmpFolder()
        if userInput == '':
            # write audio data to wave file
            wf = wave.open(
                str(dirName) + "/" + str(recordNumber) + ".wav", "wb")
            recordNumber = recordNumber + 1
            wf.setnchannels(conf.CHANNELS)
            wf.setsampwidth(p.get_sample_size(conf.FORMAT))
            wf.setframerate(RATE)
            wf.writeframes(b''.join(waveFrames))
            wf.close

        userInput = raw_input(
            "Press Enter to record another record or input anything to quit.")

    
    p.terminate()

    print("Quit succefull")
    return("0")
Example #2
0
def main():
    global models
    global modelThreshold
    global modelScore

    print("")
    print("This script can only process conf.CHUNK (currently: " + str(conf.CHUNK) +
          ") frames per loop so if the file contains a number of frames which is not divisible by conf.CHUNK the last few frames are dropped")

    model = []
    wavenumber = 1
    fileName, modelName, optimalFrames, scriptpath = interactions.getTrainParameters()
    beginning = time.time()
    # do it while there are wave files
    wf = []
    while os.path.isfile(str(fileName) + "/" + str(wavenumber) + ".wav"):
        wf.append((wave.open(str(fileName) + "/" + str(wavenumber) + ".wav"), wavenumber))
        print("File " + str(fileName) + "/" + str(wavenumber) +
                ".wav found.")
        wavenumber += 1
    for i in wf:
        model.append(preprocess(i))
    for i in wf:
        i[0].close()
    wavenumber -= 1
    print("Processed " + str(wavenumber) + " files in " + str(time.time() - beginning) + " seconds, minimalize them.")

    if model != []:
        data = []
        for i in range(wavenumber):
            data.append(
                (model[i],
                 optimalFrames))
        beginning = time.time()
        f.clearTmpFolder()
        pool = multiprocessing.Pool(processes=4)
        result = pool.map(f.minimalizeAndCalcThreshold, data)
        minimalizedRecords = []
        calculatedThresholds = []
        for i in result:
            minimalizedRecords.append(i[0])
            calculatedThresholds.append(i[1])

        zeroFrame = np.zeros(conf.FEATURES_PER_FRAME, dtype=np.float64)
        models = []
        for i in range(len(minimalizedRecords)):
            features = copy.deepcopy(minimalizedRecords[i])
            tmpFeatures = [copy.deepcopy(zeroFrame) for number in range(optimalFrames)]
            tmpCounter = [0 for number in range(optimalFrames)]
            counter = 0.
            posCounter = [0 for number in range(len(minimalizedRecords))]
            # for every frame in this record try if we find mergable frames
            for h in range(optimalFrames):    
                # we try all recordings
                for j in range(len(minimalizedRecords)):
                    if f.compare(minimalizedRecords[j][h], features[h]) < calculatedThresholds[i][h]:
                        tmpFeatures[h] += minimalizedRecords[j][h]
                        tmpCounter[h] += 1
            for h in range(optimalFrames):
                tmpFeatures[h] = np.divide(tmpFeatures[h], tmpCounter[h])
                counter += tmpCounter[h]
            counter /= optimalFrames
            models.append(modelImport.Model(tmpFeatures, calculatedThresholds[i], modelName, tmpCounter, scriptpath))


        print()
        print("Computed the models in " + str(time.time() - beginning) + " seconds. Compute their score.")
        print()
        beginning = time.time()
        data = []
        for i in range(len(models)):
            data.append((models[i], fileName, i))
        pool = multiprocessing.Pool(processes=4)
        pool.map(q.qualityCheck, data)
        models = f.loadModels(tmp=True)
        print("Computed the scores in " + str(time.time() - beginning) + " seconds.")
        print()
        for i in range(len(models)):
            print("Model Nr:\t" +
                  str(i +
                      1) +
                  " | Frames:\t" +
                  str(len(models[i].features)) +
                  " | Matches:\t" +
                  str(models[i].matches) +
                  " | Influenced by:\t" +
                  str(models[i].influencedBy) +
                  " | Threshold:\t" +
                  str(models[i].threshold) +
                  " | Score:\t" +
                  str(models[i].score))

        # get the model number and substract 1 because list indexing starts
        # with 0
        modelNumber = interactions.getModelNumber(len(models)+1) - 1
        print("You selected Model " + str(modelNumber) + " with " + str(models[modelNumber].matches) + " Matches and a Score of: " + str(models[modelNumber].score))
        f.storeModel(models[modelNumber])