def power(strip, speaker, sound_name, duration, reverse, color): if reverse: prev = NUM_PIXELS else: prev = 0 gc.collect() # Tidy up RAM now so animation's smoother start_time = time.monotonic() # Save audio start time sound.play_wav(sound_name, speaker) while True: elapsed = time.monotonic() - start_time # Time spent playing sound if elapsed > duration: # Past sound duration? break # Stop animating fraction = elapsed / duration # Animation time, 0.0 to 1.0 if reverse: fraction = 1.0 - fraction # 1.0 to 0.0 if reverse fraction = math.pow(fraction, 0.5) # Apply nonlinear curve threshold = int(NUM_PIXELS * fraction + 0.5) num = threshold - prev # Number of pixels to light on this pass if num != 0: if reverse: off(strip, lower=threshold, upper=prev) else: on(strip, lower=prev, upper=threshold, color=color) # NeoPixel writes throw off time.monotonic() ever so slightly # because interrupts are disabled during the transfer. # We can compensate somewhat by adjusting the start time # back by 30 microseconds per pixel. start_time -= NUM_PIXELS * 0.00003 prev = threshold if reverse: off(strip) else: on(strip, lower=0, upper=NUM_PIXELS, color=color)
def swing(hardware: Hardware, state: State): sound.play_wav(state.sounds.swing(), hardware.speaker) saber.swell(hardware.strip, state.idle_color, state.color) sound.play_wav(state.sounds.idle(), hardware.speaker, override_current_sound=False, loop=True)
def power_on(hardware: Hardware, state: State): hardware.powerButton.power_on() saber.power(hardware.strip, hardware.speaker, state.sounds.on(), 1.0, False, state.idle_color) sound.play_wav(state.sounds.idle(), hardware.speaker, override_current_sound=False, loop=True) state.time_since_wav_was_looped = time.monotonic()
def clash(hardware: Hardware, state: State): sound.play_wav(state.sounds.clash(), hardware.speaker) saber.flash(hardware.strip, colors.WHITE, state.idle_color, num_of_flashes=1) sound.play_wav(state.sounds.idle(), hardware.speaker, override_current_sound=False, loop=True)
def main(): warnings.simplefilter('ignore') clear_tmp() print('0. Simplified') print('1. Full') option = input('Select input: ') if option == 0: dir = 'simplified_dict/' else: dir = 'dictionary/' print('Loading recognizer, please wait...') speech_recognizer = SpeechRecognizer(dir) print('Loading completed.') print('0. Record new') print('1. Use existing wav file (from wav/)') print('2. Just record wav for later use') print('3. Split existing file (from wav/)') option = input('Select input: ') if option == 0: speech_file = record_wav() elif option == 1: speech_file = raw_input('File name (without .wav): ') elif option == 2: _ = record_wav() return elif option == 3: speech_file = raw_input('File name (without .wav): ') hint = input( 'Hint maybe (enter expected number of words, 0 for no hint)? ') rate, samples = wavfile.read('wav/{0}.wav'.format(speech_file)) rate, speech_words = extract_words(rate, samples, hint) for speech in speech_words: _ = save_tmp_wav(rate, speech) print('Extracted {0} words'.format(len(speech_words))) return else: print('Invalid option') return hint = input('Hint maybe (enter expected number of words, 0 for no hint)? ') rate, samples = wavfile.read('wav/{0}.wav'.format(speech_file)) rate, speech_words = extract_words(rate, samples, hint) print('Extracted {0}'.format(len(speech_words))) correct_count = 0 count = 0 for speech in speech_words: speech_copy = speech[:] _ = raw_input('Press enter to continue...') wav_name = save_tmp_wav(rate, speech) play_wav('tmp/{0}'.format(wav_name)) word, confidence = try_recognition(rate, speech_copy, speech_recognizer) print('Recognized [{0}] with confidence {1}'.format(word, confidence)) count += 1 correct = input('Is it correct (0/1)? ') if correct == 0: add = input('Do you want to add this word in dictionary (0/1)? ') if add == 0: print(':(') else: text_word = raw_input('Write string representation: ') text_word.rstrip('\n').replace(' ', '_') save_wav(text_word, wav_name, rate, speech) print('Added new word [{0}]'.format(text_word)) else: correct_count += 1 os.rename('tmp/{0}'.format(wav_name), 'tmp/{0}.wav'.format(word)) print(':)') print('Recognized {0}% correctly.'.format( 100.0 * float(correct_count) / float(count)))
def cycle_idle_loop(hardware, state): hardware.speaker.audio.stop() sound.play_wav(state.sounds.idle(), hardware.speaker, loop=True)
def mode_select(hardware: Hardware, state: State): if state.mode != mode.MODE_SELECT: sound.play_wav('wow1', hardware.speaker) while hardware.powerButton.pressed(): # Wait for button to be released pass saber.display_mode_select(hardware.strip, state.mode_selector)