0 Members and 2 Guests are viewing this topic.
"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""import pyaudioimport waveCHUNK = 1024FORMAT = pyaudio.paInt16CHANNELS = 2RATE = 44100RECORD_SECONDS = 5WAVE_OUTPUT_FILENAME = "output.wav"p = pyaudio.PyAudio()stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)print("* recording")frames = []for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data)print("* done recording")stream.stop_stream()stream.close()p.terminate()wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')wf.setnchannels(CHANNELS)wf.setsampwidth(p.get_sample_size(FORMAT))wf.setframerate(RATE)wf.writeframes(b''.join(frames))wf.close()[\code]Again, I've never worked with this or audio in general so I'm making some guesses based on the documentation and interwebs.
Expression 'r' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2018Expression 'PaAlsaStreamComponent_FinishConfigure( &self->capture, hwParamsCapture, inParams, self->primeBuffers, realSr, inputLatency )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2655Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2767Traceback (most recent call last): File "speechcontrol.py", line 33, in <module> inputStream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,output=False,frames_per_buffer=CHUNK) File "/usr/lib/pymodules/python2.7/pyaudio.py", line 714, in open stream = Stream(self, *args, **kwargs) File "/usr/lib/pymodules/python2.7/pyaudio.py", line 396, in __init__ self._stream = pa.open(**arguments)IOError: [Errno Unanticipated host error] -9999
###!/usr/bin/python2try: import pocketsphinx as psexcept: import pocketsphinx as psimport sphinxbase,pyaudio,wavehmmd = '/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k'lmdir = '/usr/local/share/pocketsphinx/model/lm/en_US/wsj0vp.5000.DMP'dictp = '/usr/local/share/pocketsphinx/model/lm/en_US/cmu07a.dic'hmmd = '/usr/local/share/pocketsphinx/model/hmm/en/tidigits'lmdir = '/usr/local/share/pocketsphinx/model/lm/en/tidigits.DMP'dictp = '/usr/local/share/pocketsphinx/model/lm/en/tidigits.dic'#lmdir = '/home/sorunome/languagemodel_persona.lm'#dictp = '/home/sorunome/dictionary_persona.dic'p = pyaudio.PyAudio()device = p.get_device_info_by_index(0)CHUNK = 5750FORMAT = pyaudio.paInt16CHANNELS = 1RATE = int(device['defaultSampleRate'])inputStream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,output=False,frames_per_buffer=CHUNK)frames = []for i in range(RATE/CHUNK * 5): frames.append(inputStream.read(CHUNK))inputStream.stop_stream()inputStream.close()p.terminate()write_frames = wave.open('tmp.wav','wb')write_frames.setnchannels(CHANNELS)write_frames.setsampwidth(p.get_sample_size(FORMAT))write_frames.setframerate(RATE)write_frames.writeframes(''.join(frames))write_frames.close()wavFile = file('tmp.wav','rb')wavFile.seek(44)#speechRec = ps.Decoder(lm='/usr/local/share/pocketsphinx/model/lm/en_US/hub4.5000.DMP',dict='/usr/local/share/pocketsphinx/model/lm/en_US/hub4.5000.dic',hmm='/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k')#speechRec = ps.Decoder(lm='/home/sorunome/languagemodel_persona.lm',dict='/home/sorunome/dictionary_persona.dic',hmm='/usr/local/share/pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k')speechRec = ps.Decoder(hmm = hmmd,lm = lmdir,dict = dictp)speechRec.decode_raw(wavFile)print 'EPIC output',speechRec.get_hyp()