Esempio n. 1
0
 def __init__(self, channel=0, supress_error=True):
     with ignore_stderr(enable=supress_error):
         self.pyaudio = pyaudio.PyAudio()
     self.channels = None
     self.channel = channel
     self.device_index = None
     self.rate = 16000
     self.bitwidth = 2
     self.bitdepth = 16
     ####find device####
     count = self.pyaudio.get_device_count()
     for i in range(count):
         info = self.pyaudio.get_device_info_by_index(i)
         name = info["name"].encode("utf-8")
         chan = info["maxInputChannels"]
         if name.lower().find("respeaker") >= 0:
             self.channels = chan
             self.device_index = i
             break
     if self.device_index is None:
         info = self.pyaudio.get_default_input_device_info()
         self.channels = info["maxInputChannels"]
         self.device_index = info["index"]
     self.channel = min(self.channels - 1, max(0, self.channel))
     self.stream = self.pyaudio.open(
         rate=self.rate,
         format=self.pyaudio.get_format_from_width(self.bitwidth),
         channels=1,
         input=True,
         input_device_index=self.device_index,
     )
     self.aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
     self.baidu = BaiduVoiceApi(appkey=API_KEY, secretkey=SECRET_KEY)
Esempio n. 2
0
p = pyaudio.PyAudio()
stream = p.open(
    rate=RESPEAKER_RATE,
    format=p.get_format_from_width(RESPEAKER_WIDTH),
    channels=RESPEAKER_CHANNELS,
    input=True,
    start=False,
)

APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

baidu = BaiduVoiceApi(appkey=API_KEY, secretkey=SECRET_KEY)


def generator_list(list):
    for l in list:
        yield l


def record():
    stream.start_stream()
    print("* recording")
    frames = []
    for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
    print("* done recording")
Esempio n. 3
0
def reg():

    RESPEAKER_RATE = 16000
    RESPEAKER_CHANNELS = 1
    RESPEAKER_WIDTH = 2
    CHUNK = 1024
    RECORD_SECONDS = 2
    #WAVE_OUTPUT_FILENAME = "output.wav"

    p = pyaudio.PyAudio()
    stream = p.open(
        rate=RESPEAKER_RATE,
        format=p.get_format_from_width(RESPEAKER_WIDTH),
        channels=RESPEAKER_CHANNELS,
        input=True,
        start=False,
    )

    APP_ID = '10783244'
    API_KEY = 'GlQae1odOq3Y1w2wCpGBOI27'
    SECRET_KEY = 'aeb8dc5f91cc18990a873edb294e6641'

    aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    baidu = BaiduVoiceApi(appkey=API_KEY, secretkey=SECRET_KEY)

    def generator_list(list):
        for l in list:
            yield l

    def record():
        stream.start_stream()
        print("* recording")
        frames = []
        for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("* done recording")
        stream.stop_stream()
        print("start to send to baidu")
        # audio_data should be raw_data
        text = baidu.server_api(generator_list(frames))
        if text:
            try:
                text = json.loads(text)
                for t in text['result']:
                    print(t)
                    return (t)
            except KeyError:
                return ("get nothing")
        else:
            print("get nothing")
            return ("get nothing")

    word_0 = "开"
    word_1 = "快"
    os.system("play dong.wav")
    outputtext = record()
    if word_0 in outputtext:
        os.system("play ./turnon.mp3")
    print outputtext
    stream.close()
    p.terminate()
Esempio n. 4
0
class RespeakerAudio(object):
    def __init__(self, channel=0, supress_error=True):
        with ignore_stderr(enable=supress_error):
            self.pyaudio = pyaudio.PyAudio()
        self.channels = None
        self.channel = channel
        self.device_index = None
        self.rate = 16000
        self.bitwidth = 2
        self.bitdepth = 16
        ####find device####
        count = self.pyaudio.get_device_count()
        for i in range(count):
            info = self.pyaudio.get_device_info_by_index(i)
            name = info["name"].encode("utf-8")
            chan = info["maxInputChannels"]
            if name.lower().find("respeaker") >= 0:
                self.channels = chan
                self.device_index = i
                break
        if self.device_index is None:
            info = self.pyaudio.get_default_input_device_info()
            self.channels = info["maxInputChannels"]
            self.device_index = info["index"]
        self.channel = min(self.channels - 1, max(0, self.channel))
        self.stream = self.pyaudio.open(
            rate=self.rate,
            format=self.pyaudio.get_format_from_width(self.bitwidth),
            channels=1,
            input=True,
            input_device_index=self.device_index,
        )
        self.aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
        self.baidu = BaiduVoiceApi(appkey=API_KEY, secretkey=SECRET_KEY)

    def stop(self):
        self.stop()
        try:
            self.stream.stop_stream()
            self.stream.close()
        except:
            pass
        finally:
            self.stream = None
        try:
            self.pyaudio.terminate()
        except:
            pass

    def generator_list(self, list):
        for l in list:
            yield l

    def record(self):
        self.stream.start_stream()
        print("* recording")
        frames = []
        for i in range(0, int(self.rate / CHUNK * RECORD_SECONDS)):
            data = self.stream.read(CHUNK)
            frames.append(data)
        print("done recording")
        self.stream.stop_stream()
        print("start to send to baidu")
        text = self.baidu.server_api(self.generator_list(frames))
        # print (text)
        if text:
            try:
                text = json.loads(text)
                for t in text['result']:
                    print(t)
                    # self.voice_publisher.publish(str(text))
                    return (str(t))
            except KeyError:
                return ("get nothing")
        else:
            print("get nothing")
            return ("get nothing")