def main(): parser = argparse.ArgumentParser() parser.add_argument("--filename", "-f", default="~/recording.wav") args = parser.parse_args() with Board() as board: print("Press button to start recording.") board.led.state = Led.BEACON board.button.wait_for_press() done = threading.Event() board.led.state = Led.BLINK board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print("Recording: %.02f seconds [Press button to stop]" % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype="wav") print("Press button to play recorded sound.") board.led.state = Led.BEACON board.button.wait_for_press() print("Playing...") board.led.state = Led.ON play_wav(args.filename) print("Done.")
def record(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: #print('Press button to start recording.') #board.button.wait_for_press() done = threading.Event() done.set #board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) #time.sleep(0.5) #cloudspeech_demo.speech() i = cloudspeech_demo.startOrder() if i == 'yes': print('happy') findWords = cloudspeech_demo.speech() print(findWords) mcs_api.post_mcs_word(findWords) #elif i=='over': #break; elif mcs_api.get_mcs_start() == 'off': break d = datetime.datetime.now() if (d.month < 10): if (d.day < 10): d = str(d.year) + "0" + str(d.month) + "0" + str(d.day) else: d = str(d.year) + "0" + str(d.month) + str(d.day) else: if (d.day < 10): d = str(d.year) + str(d.month) + "0" + str(d.day) else: d = str(d.year) + str(d.month) + str(d.day) pid = mcs_api.get_mcs_id() pid = pid.split('*') print(pid) d = "UploadFiles/" + d + "-" + pid[1] record_file(AudioFormat.CD, filename=d, wait=wait, filetype='wav') if (mcs_api.get_mcs_start() == "off"): upload_file.main(is_update_file_function=bool(True), update_drive_service_folder_name='Uploaded Files', update_drive_service_name=None, update_file_path=os.getcwd() + '/UploadFiles/') #print('Press button to play recorded sound.') #board.button.wait_for_press() #print('Playing...') #play_wav(args.filename) print('Done.')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: print('Press button to start recording.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') rate, data = wave.read( "recoding.wav" ) #Lit le son. Conserve les amplitudes dans la variable data n = data.size duree = 1.0 * n / rate a = chain(*data) data2 = list(a) Tracer_spectre(0, .5, data2, rate)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='enregistrement.wav') args = parser.parse_args() with Board() as board: print('Press button to start recording.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print('Press button to play recorded sound.') board.button.wait_for_press() print('Playing...') play_wav(args.filename) print('Done.')
def main(): print('Start recording...') audio.record_file(audio.AudioFormat.CD, filename='sample.wav', wait=wait, filetype='wav') print('Done.')
def record_candy(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() # with Board() as board: # print('Press button to start recording.') # board.button.wait_for_press() # done = threading.Event() #board.button.when_pressed = done.set #def wait(): # start = time.monotonic() # while not done.is_set(): # duration = time.monotonic() - start # print('Recording: %.02f seconds [Press button to stop]' % duration) # time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=300, filetype='wav') #print('Press button to play recorded sound.') #board.button.wait_for_press() print('Playing...') play_wav(args.filename) print('Done.')
def record(filepath, duration): try: start = time.time() record_file(AudioFormat.CD, filename=filepath, wait=lambda: time.sleep(duration), filetype='wav') except Exception as e: logging.error("Error while recording to file {}: {}".format( filepath, e))
def record_candy(self): #record scene: refusing to eat durian parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() def wait(): time.sleep(7) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav')
def record_birthday(self): #start to record singing of birthday-song parser = argparse.ArgumentParser() parser.add_argument('--filename_2', '-f', default='birthday.wav') args = parser.parse_args() def wait(): time.sleep(23) record_file(AudioFormat.CD, filename=args.filename_2, wait=wait, filetype='wav')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: print('Press button to start recording.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') def Tracer_spectre(debut, duree, data, rate): start = int(0 * rate) stop = int((0 + 1) * rate) spectre = math.abs(fft(data[start:stop])) #Réalise le spectre spectre = spectre / spectre.max( ) #Normalise le spectre par rapport à la fondamentale spectre2 = list(chain(spectre)) #Transforme l'array en liste maxi = spectre2.index(max( spectre)) #Trouve l'indice de la fréquence max (fondamentale) n = spectre.size freq = [] for k in range(n): freq.append(1.0 / n * rate * k) vlines(freq, [0], spectre, 'r') print("La fréquence fondamentale de ce son est : {} Hz".format( freq[maxi])) rate, data = wave.read( "recoding.wav" ) #Lit le son. Conserve les amplitudes dans la variable data n = data.size duree = 1.0 * n / rate a = chain(*data) data2 = list(a) Tracer_spectre(0, .5, data2, rate)
def check_microphone_works(): with tempfile.NamedTemporaryFile() as f: input('When you are ready, press Enter and say "Testing, 1 2 3"...') print('Recording for %d seconds...' % RECORD_DURATION_SECONDS) record_file(AudioFormat.CD, filename=f.name, filetype='wav', wait=lambda: time.sleep(RECORD_DURATION_SECONDS)) print('Playing back recorded audio...') play_wav(f.name) if not ask('Did you hear your own voice?'): error(ERROR_NO_RECORDED_SOUND) return False return True
def record(): with Board() as board: print('press to record.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('recording: %.02f s[press to stop recording]' % duration) time.sleep(0.5) record_file(Lab, filename='r.wav', wait=wait, filetype='wav')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: done = threading.Event() form = AudioFormat(sample_rate_hz=16000, num_channels=1, bytes_per_sample=2) record_file(form, filename=args.filename, wait=interval_predicate, filetype='wav') print('[EQUIBOX] Recorded Interval')
def recognize(language_code='en_US', wait=None, duration=5): global seconds global play_file status, host, client_id = check_file_exist() if status == False: print("ERROR: Can't find the credential file") sys.exit(1) timestamp = math.ceil(time.time()) filename = '/tmp/file-' + str(timestamp) #print(filename) if wait != None: record_file(AudioFormat.CD, filename=filename, wait=wait, filetype='wav') else: seconds = duration record_file(AudioFormat.CD, filename=filename, wait=_wait_for_duration, filetype='wav') try: #url = "http://192.168.101.153:8081/client/ABCDE" url = host + "/voice/" + client_id files = {'file': open(filename, 'rb')} values = {'languageCode': language_code} req = requests.post(url, files=files, data=values) #play_wav(filename) if play_file == True: play_wav(filename) os.remove(filename) #print(req.text) reply = json.loads(req.text) if reply["status"] == True: return reply["transcript"] else: if reply["error"] != None: print("ERROR:", reply["error"]) return "" except Exception as err: return ""
def voice_to_text(filename='recording.wav'): client = speech.SpeechClient() with Board() as board: # logging.debug('Press button to start recording.') # board.button.wait_for_press() # done = threading.Event() # board.button.when_pressed = done.set def wait(): start = time.monotonic() duration = 0 while duration < 7: #not done.is_set(): duration = time.monotonic() - start logging.debug('Recording: %.02f seconds' % duration) time.sleep(0.5) format = AudioFormat(sample_rate_hz=44100, num_channels=1, bytes_per_sample=2) record_file(format, filename=filename, wait=wait, filetype='wav') #logging.debug('Press button to play recorded sound.') #board.button.wait_for_press() # logging.debug('Playing...') # play_wav(filename) # logging.debug('Done.') # use speech to with io.open(filename, 'rb') as audio_file: content = audio_file.read() audio = types.RecognitionAudio(content=content) config = types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, language_code='en-US') response = client.recognize(config, audio) msg = "" for result in response.results: msg += result.alternatives[0].transcript logging.debug('Transcript: {}'.format( result.alternatives[0].transcript)) return msg
def _work(self): tts.say("Time to work! The LED lights will turn off as time elapses.") self._board.led.state = Led.PULSE_SLOW record_file( AudioFormat.CD, filename=self._file, wait=self._wait, filetype="wav", ) self._done.clear() session_score = pyloudness.get_loudness(self._file)["Loudness Range"]["LRA"] tts.say("Session score: {:.2}".format(session_score)) if session_score > 5: tts.say("Try to be a little more quiet next round") self._score += session_score
def record(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='clinical.wav') args = parser.parse_args() with Board() as board: print('請按下按鈕開始錄音.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('錄音中: %.02f 秒 [按下按鈕停止錄音]' % duration) time.sleep(0.5) record_file(Lab, filename=args.filename, wait=wait, filetype='wav')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: print('Press button to start recording.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') '''fromaddr = "*****@*****.**"
def _work(self): tts.say("Time to work!") self._board.led.state = Led.PULSE_SLOW record_file( AudioFormat.CD, filename=self._file, wait=self._wait, filetype="wav", ) tts.say("Session ended") self._done.clear() session_score = pyloudness.get_loudness( self._file)["Loudness Range"]["LRA"] if session_score > 40: tts.say("Try to be a little more quiet next round") self._score += session_score
def determine_note_fondamentale(): parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() with Board() as board: print('Press button to start recording.') board.button.wait_for_press() done = threading.Event() board.button.when_pressed = done.set def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') def readwave(filename): wav = wave.open(filename) nchannels = wav.getnchannels() nframes = wav.getnframes() sampwidth = wav.getsampwidth() framerate = wav.getframerate() frames = wav.readframes(nframes) if sampwidth == 1: # 8 bit : unsigned char data = struct.unpack('%sB' % (nframes * nchannels), frames) elif sampwidth == 2: # 16 bits : signed short data = struct.unpack('%sh' % (nframes * nchannels), frames) return (data, framerate) recording_name = '../AIY-projects-python/src/examples/voice/recording.wav' data = list(readwave(recording_name)[0]) rate = readwave(recording_name)[1] start = int(0 * rate) stop = int((0 + 1) * rate) spectre = abs(fft(data[start:stop])) #Réalise le spectre spectre = spectre / spectre.max( ) #Normalise le spectre par rapport à la fondamentale spectre2 = list(chain(spectre)) #Transforme l'array en liste maxi = spectre2.index( max(spectre)) #Trouve l'indice de la fréquence max (fondamentale) n = spectre.size freq = [] for k in range(n): freq.append(1.0 / n * rate * k) print(freq[maxi]) os.remove('../AIY-projects-python/src/examples/voice/recording.wav')
def listen_me(): global text, duration parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() # 라이브러리 준비 Vokaturi.load("/home/pi/lib/piZero.so") # 클라우드 스피치랑 텍스트 자연어처리 클라이언트 각각 초기화 client = CloudSpeechClient() nlp_client = language.LanguageServiceClient() logging.basicConfig(level=logging.INFO) with Board() as board: while True: print('말해보자.') text = None duration = 0. emotion = None def wait(): global text, duration start = time.monotonic() while text is None: # 텍스트로 인식 text = client.recognize(language_code='ko-KR') duration = time.monotonic() - start # 녹음하면서 record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print(text) print('Recorded: %.02f seconds' % duration) if text in ['들어줘서 고마워', '내 얘기 들어줘서 고마워', '어시스턴트', '잘가', '잘 가']: return # 텍스트 감정 분석 document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) sentiment = nlp_client.analyze_sentiment( document=document).document_sentiment print('텍스트 감정 분석*********************************') print('Text: {}'.format(text)) print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude)) ##################### 실험후 바꿔도 됨 #################### pos_standard = 0.6 neg_standard = 0.1 # magnitude_standard = 0.1 # text sentiment analysis is enough if (sentiment.score < neg_standard or sentiment.score > pos_standard): if sentiment.score < neg_standard: emotion = False print("@@@negative") else: emotion = True print("@@@positive") else: # 녹음 파일 감정 분석 print('오디오 감정 분석*********************************') (sample_rate, samples) = scipy.io.wavfile.read(args.filename) # print (" sample rate %.3f Hz" % sample_rate) # print ("Allocating Vokaturi sample array...") buffer_length = len(samples) print(" %d samples, %d channels" % (buffer_length, samples.ndim)) c_buffer = Vokaturi.SampleArrayC(buffer_length) if samples.ndim == 1: # mono c_buffer[:] = samples[:] / 32768.0 else: # stereo c_buffer[:] = 0.5 * (samples[:, 0] + 0.0 + samples[:, 1]) / 32768.0 # print ("Creating VokaturiVoice...") voice = Vokaturi.Voice(sample_rate, buffer_length) # print ("Filling VokaturiVoice with samples...") voice.fill(buffer_length, c_buffer) # print ("Extracting emotions from VokaturiVoice...") quality = Vokaturi.Quality() emotionProbabilities = Vokaturi.EmotionProbabilities() voice.extract(quality, emotionProbabilities) if quality.valid: # print ("Neutral: %.3f" % emotionProbabilities.neutrality) # print ("Happy: %.3f" % emotionProbabilities.happiness) # print ("Sad: %.3f" % emotionProbabilities.sadness) # print ("Angry: %.3f" % emotionProbabilities.anger) # print ("Fear: %.3f" % emotionProbabilities.fear) # fear 는 무시하도록 하자. wave_score = emotionProbabilities.happiness - ( emotionProbabilities.sadness + emotionProbabilities.anger) if wave_score > 0: print('@@@긍정') emotion = True else: print('@@@부정') emotion = False # text 분석 모호하고 wave 분석 실패했을때 (주로 목소리 짧아서) if emotion is None: print('please say again') # 아님 중립적 반응 넣어도 됨. continue # 여기서 부터 반응. with Leds() as leds: if emotion is True: # tts.say('I am glad to hear that.') # tts.say('진짜? 대박.') leds.pattern = Pattern.blink(100) color = (255, 255, 0) leds.update(Leds.rgb_pattern(color)) time.sleep(1) # play_wav('laugh.wav') else: # tts.say('I am sorry to hear that.') # tts.say('저런. 힘내.') leds.pattern = Pattern.breathe(1000) color = (102, 140, 255) leds.update(Leds.rgb_on(color)) time.sleep(1)
def listen_me(): global text, duration parser = argparse.ArgumentParser() parser.add_argument('--filename', '-f', default='recording.wav') args = parser.parse_args() # 라이브러리 준비 Vokaturi.load("/home/pi/lib/piZero.so") # 클라우드 스피치, 텍스트 자연어처리, tts 클라이언트 각각 초기화 client = CloudSpeechClient() nlp_client = language.LanguageServiceClient() tts_client = texttospeech.TextToSpeechClient() pos_wavs = [] neut_wavs = [] neg_wavs = [] intro_wavs = [] pos_wavs.append(text_to_audio(tts_client, '진짜?', '0.wav')) pos_wavs.append(text_to_audio(tts_client, '대박', '1.wav')) pos_wavs.append(text_to_audio(tts_client, '우와', '2.wav')) pos_wavs.append(text_to_audio(tts_client, '하하', '3.wav')) neut_wavs.append(text_to_audio(tts_client, '응', '10.wav')) neut_wavs.append(text_to_audio(tts_client, '그렇구나', '11.wav')) neut_wavs.append(text_to_audio(tts_client, '그래서?', '12.wav')) neut_wavs.append(text_to_audio(tts_client, '응응', '13.wav')) neg_wavs.append(text_to_audio(tts_client, '저런', '4.wav')) neg_wavs.append(text_to_audio(tts_client, '힘내', '5.wav')) neg_wavs.append(text_to_audio(tts_client, '에휴', '6.wav')) intro_wavs.append(text_to_audio(tts_client, '들어줄게. 얘기해봐', 'intro0.wav')) intro_wavs.append(text_to_audio(tts_client, '무슨 일 이야?', 'intro1.wav')) play_wav(random.choice(intro_wavs)) logging.basicConfig(level=logging.INFO) with Board() as board: while True: print('말해보자.') text = None duration = 0. emotion = None def wait(): global text, duration start = time.monotonic() while text is None: # 텍스트로 인식 text = client.recognize(language_code='ko-KR') duration = time.monotonic() - start # 녹음하면서 record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print(text) print('Recorded: %.02f seconds' % duration) if text in ['들어줘서 고마워', '내 얘기 들어줘서 고마워', '어시스턴트', '잘가', '잘 가']: return # 텍스트 감정 분석 document = types.Document(content=text, type=enums.Document.Type.PLAIN_TEXT) sentiment = nlp_client.analyze_sentiment( document=document).document_sentiment print('텍스트 감정 분석*********************************') print('Text: {}'.format(text)) print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude)) ##################### 실험후 바꿔도 됨 #################### pos_standard = 0.6 neg_standard = 0.1 # magnitude_standard = 0.1 # text sentiment analysis is enough if (sentiment.score < neg_standard or sentiment.score > pos_standard): if sentiment.score < neg_standard: emotion = False print("@@@negative") else: emotion = True print("@@@positive") else: # 녹음 파일 감정 분석 print('오디오 감정 분석*********************************') (sample_rate, samples) = scipy.io.wavfile.read(args.filename) # print (" sample rate %.3f Hz" % sample_rate) # print ("Allocating Vokaturi sample array...") buffer_length = len(samples) print(" %d samples, %d channels" % (buffer_length, samples.ndim)) c_buffer = Vokaturi.SampleArrayC(buffer_length) if samples.ndim == 1: # mono c_buffer[:] = samples[:] / 32768.0 else: # stereo c_buffer[:] = 0.5 * (samples[:, 0] + 0.0 + samples[:, 1]) / 32768.0 # print ("Creating VokaturiVoice...") voice = Vokaturi.Voice(sample_rate, buffer_length) # print ("Filling VokaturiVoice with samples...") voice.fill(buffer_length, c_buffer) # print ("Extracting emotions from VokaturiVoice...") quality = Vokaturi.Quality() emotionProbabilities = Vokaturi.EmotionProbabilities() voice.extract(quality, emotionProbabilities) if quality.valid: # print ("Neutral: %.3f" % emotionProbabilities.neutrality) # print ("Happy: %.3f" % emotionProbabilities.happiness) # print ("Sad: %.3f" % emotionProbabilities.sadness) # print ("Angry: %.3f" % emotionProbabilities.anger) # print ("Fear: %.3f" % emotionProbabilities.fear) # fear 는 무시하도록 하자. wave_score = emotionProbabilities.happiness - ( emotionProbabilities.sadness + emotionProbabilities.anger) if wave_score > 0 and sentiment.score > 0.4: print('@@@긍정') emotion = True elif wave_score < 0 and sentiment.score < 0.4: print('@@@부정') emotion = False # text 스코어와 wave 스코어가 불일치 할때는 중립반응 (emotion = None) # 여기서 부터 반응. with Leds() as leds: if emotion is True: play_wav(random.choice(pos_wavs)) leds.pattern = Pattern.blink(100) color = (255, 255, 0) leds.update(Leds.rgb_pattern(color)) time.sleep(1) # play_wav('laugh.wav') elif emotion is False: play_wav(random.choice(neg_wavs)) leds.pattern = Pattern.breathe(1000) color = (102, 140, 255) leds.update(Leds.rgb_on(color)) time.sleep(1) # play_wav('people-cheering.wav') # 중립 리액션 else: play_wav(random.choice(neut_wavs)) leds.pattern = Pattern.blink(5) color = (230, 0, 115) leds.update(Leds.rgb_on(color)) time.sleep(1)
duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print('Press button to play recorded sound.') board.button.wait_for_press() print('Playing...') play_wav(args.filename) print('Done.') if __name__ == '__main__': main()def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print('Press button to play recorded sound.') board.button.wait_for_press() print('Playing...') play_wav(args.filename) print('Done.') if __name__ == '__main__': main()
def wait(): start = time.monotonic() while not done.is_set(): duration = time.monotonic() - start print('Recording: %.02f seconds [Press button to stop]' % duration) time.sleep(0.5) record_file(AudioFormat.CD, filename=args.filename, wait=wait, filetype='wav') print('Press button to play recorded sound.') board.button.wait_for_press() print('Playing...') play_wav(args.filename) print('Done.') if __name__ == '__main__': main()def wait():