Beispiel #1
0
def record_and_trim(wakeword, nb_records=3):
    input(
        "You will record %s samples for your wakeword. Be sure to be in a quiet environment. Press enter once you are ready"
        % nb_records)
    directory = os.path.join('', wakeword)
    if os.path.exists(directory):
        os.system("rm -rf " + directory)
    os.makedirs(directory)

    is_validated = False
    while not is_validated:
        audios = []
        for i in range(nb_records):
            record_one(directory, i)
            dest_path = os.path.join(directory, "{0}.wav".format(i))
            audio = Audio.from_file(dest_path)
            audio.trim_silences(SNR_TRIM)
            while audio.duration() > MAX_RECORD_DURATION:
                print(
                    "WARNING: there seems to be too much noise in your environment."
                )
                record_one(directory, i)
                audio = Audio.from_file(dest_path)
                audio.trim_silences(SNR_TRIM)
            audios.append(audio)

        if any([
                abs(audio_1.duration() - audio_2.duration()) >
                MAX_DIFFERENCE_DURATION for i, audio_1 in enumerate(audios)
                for j, audio_2 in enumerate(audios) if i < j
        ]):
            print(
                "WARNING: there seems to be too much difference between your records."
            )
        else:
            is_validated = True

    for i, audio in enumerate(audios):
        dest_path = os.path.join(directory, "{0}.wav".format(i))
        audio.write(dest_path)

    #remove silences:
    for i, audio in enumerate(audios):
        dest_path = os.path.join(directory, "{0}.wav".format(i))
        audio = Audio.from_file(dest_path)
        audio.write(dest_path.replace('.wav', '_uncut.wav'))
        audio.trim_silences(SNR_TRIM_CUT)
        audio.write(dest_path)

    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), directory)
    print("Your samples have been saved in %s" % path)
def record_and_trim(hotword_key, nb_records=3):
    raw_input("Your will have to record your personal hotword." \
              " Please be sure to be in a quiet environment." \
              " Press enter once you are ready.\n".format(
        nb_records))

    directory = os.path.join(FOLDER_BASE, "personal_{0}".format(str(datetime.datetime.now().strftime(DATE_FORMAT))))
    os.makedirs(directory)

    is_validated = False
    while not is_validated:
        audios = []
        for i in range(nb_records):
            record_one(directory, i)
            dest_path = os.path.join(directory, "{0}.wav".format(i))
            audio = Audio.from_file(dest_path)
            audio.trim_silences(SNR_TRIM)
            while audio.duration() > MAX_RECORD_DURATION:
                print "WARNING: there seems to be too much noise in your" \
                      " environment please retry to record this sample by " \
                      "following the instructions."
                record_one(directory, i)
                audio = Audio.from_file(dest_path)
                audio.trim_silences(SNR_TRIM)
            audios.append(audio)

        if any([abs(
                        audio_1.duration() - audio_2.duration()) > MAX_DIFFERENCE_DURATION
                for i, audio_1 in enumerate(audios) for j, audio_2 in
                enumerate(audios) if i < j]):
            print "WARNING: there seems to be too much difference between " \
                  "your records please retry to record all of them by following " \
                  "the instructions."
        else:
            is_validated = True

    for i, audio in enumerate(audios):
        dest_path = os.path.join(directory, "{0}.wav".format(i))
        audio.write(dest_path)

    config = {
        "hotword_key": hotword_key,
        "kind": "personal",
        "dtw_ref": 0.22,
        "from_mfcc": 1,
        "to_mfcc": 13,
        "band_radius": 10,
        "shift": 10,
        "window_size": 10,
        "sample_rate": RATE,
        "frame_length_ms": 25.0,
        "frame_shift_ms": 10.0,
        "num_mfcc": 13,
        "num_mel_bins": 13,
        "mel_low_freq": 20,
        "cepstral_lifter": 22.0,
        "dither": 0.0,
        "window_type": "povey",
        "use_energy": False,
        "energy_floor": 0.0,
        "raw_energy": True,
        "preemphasis_coefficient": 0.97,
        "model_version": 1
    }

    with open(os.path.join(directory, "config.json"), "wb") as f:
        json.dump(config, f, indent=4)

    print "Your model has been saved in {0}".format(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), directory))