Example #1
0
    def __init__(self):
        # initialize the game parameters
        screen_width = 320
        screen_height = 480
        screen_size = (screen_width, screen_height)
        pg_screen = pygame.display.set_mode((screen_width, screen_height))

        # initialize the screens
        self.screens = {
            "game_screen": GameScreen(pg_screen, screen_size),
            "help_screen": HelpScreen(pg_screen, screen_size),
            "highscores_screen": HighscoresScreen(pg_screen, screen_size),
            "main_menu": MainMenu(pg_screen, screen_size),
            "login_screen": LoginScreen(pg_screen, screen_size),
            "register_screen": RegisterScreen(pg_screen, screen_size),
        }
        self.screen = "main_menu"
        self.audio = Audio(has_audio)
        self.audio.set_music(self.screens["main_menu"].music_on)
        self.audio.set_sound(self.screens["main_menu"].sound_on)

        self.game_time = time.time()

        # run the game loop forever
        while True:
            self.game_loop()
Example #2
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)
Example #3
0
def listen_signal(signal: np.ndarray, Fs: int=44100) -> None:
    display(Audio(signal, rate=Fs))
Example #4
0
def listen_signal(signal: np.ndarray,
                  Fs: int = 44100,
                  normalize: bool = True) -> None:
    if normalize:
        signal = signal.copy() / np.amax(np.absolute(signal))
    display(Audio(signal, rate=Fs))
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))
Example #6
0
class MrNom(object):
    def __init__(self):
        # initialize the game parameters
        screen_width = 320
        screen_height = 480
        screen_size = (screen_width, screen_height)
        pg_screen = pygame.display.set_mode((screen_width, screen_height))

        # initialize the screens
        self.screens = {
            "game_screen": GameScreen(pg_screen, screen_size),
            "help_screen": HelpScreen(pg_screen, screen_size),
            "highscores_screen": HighscoresScreen(pg_screen, screen_size),
            "main_menu": MainMenu(pg_screen, screen_size),
            "login_screen": LoginScreen(pg_screen, screen_size),
            "register_screen": RegisterScreen(pg_screen, screen_size),
        }
        self.screen = "main_menu"
        self.audio = Audio(has_audio)
        self.audio.set_music(self.screens["main_menu"].music_on)
        self.audio.set_sound(self.screens["main_menu"].sound_on)

        self.game_time = time.time()

        # run the game loop forever
        while True:
            self.game_loop()

    def game_loop(self):
        results = {}

        # process timing stuff
        current_time = time.time()
        time_delta = current_time - self.game_time
        self.game_time = current_time

        # process the event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (
                event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
            ):
                print("shutting down...")
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                print(pos)
                mouse_results = self.screens[self.screen].mouse_down(pos)
                if mouse_results:
                    results = {**results, **mouse_results}
            elif event.type == pygame.KEYDOWN:
                self.screens[self.screen].key_press(event)

        # call update on the screen, and add results if present
        update_results = self.screens[self.screen].update(time_delta)
        if update_results:
            results = {**results, **update_results}

        if results.get("screen") is not None:
            self.screen = results.get("screen")

        if results.get("music") is not None:
            self.audio.set_music(results["music"])
        if results.get("sound") is not None:
            self.audio.set_sound(results["sound"])
        if results.get("play_sound") is not None:
            self.audio.play_sound(results["play_sound"])

        self.screens[self.screen].draw()

        pygame.display.update()
        pygame.time.delay(30)  # ~30fps