Exemplo n.º 1
0
    def get_audio_stats(self, data, sample_size):
        wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(self.CHANNELS)
        wf.setsampwidth(sample_size)
        wf.setframerate(self.RATE)
        wf.writeframes(data)
        wf.close()

        recording_data_dict = sox.file_info.stat(self.WAVE_OUTPUT_FILENAME)
        recording_data = list(recording_data_dict.values())
        recording_date = datetime.now()

        doa = DOA(rate=self.RATE)
        doa.put(data)

        return {
            "MinimumAmplitude": str(recording_data[0]),
            "RoughFrequency": str(recording_data[1]),
            "ScaledBy": str(recording_data[2]),
            "MeanNorm": str(recording_data[3]),
            "MaximumDelta": str(recording_data[4]),
            "RmsAmplitude": str(recording_data[5]),
            "Length": str(recording_data[6]),
            "SamplesRead": str(recording_data[7]),
            "VolumeAdjustment": str(recording_data[8]),
            "MinimumDelta": str(recording_data[9]),
            "MidlineAmplitude": str(recording_data[10]),
            "RmsDelta": str(recording_data[11]),
            "MeanAmplitude": str(recording_data[12]),
            "MeanDelta": str(recording_data[13]),
            "MaximumAmplitude": str(recording_data[14]),
            "DeviceId": self.config.device,
            "Direction": doa.get_direction(),
            "RecordingDate": recording_date.strftime("%Y%m%d%H%M%S")
        }
Exemplo n.º 2
0
def main():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch0 = ChannelPicker(channels=4, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.6, verbose=True)
    doa = DOA(rate=16000)

    src.link(ch0)
    ch0.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Exemplo n.º 3
0
def main():
    src = Source(rate=16000, channels=4)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    kws = KWS(model='snowboy', sensitivity=0.6, verbose=True)
    doa = DOA(rate=16000)

    src.link(ch0)
    ch0.link(kws)
    src.link(doa)

    def on_detected(keyword):
        print('detected {} at direction {}'.format(keyword,
                                                   doa.get_direction()))

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()

    # wait a second to allow other threads to exit
    time.sleep(1)
Exemplo n.º 4
0
def main():
    src = Source(rate=16000, frames_size=320, channels=4)
    ds = DelaySum(channels=4,
                  frames_size=src.frames_size,
                  max_offset=max_offset)
    kws = KWS()
    doa = DOA(rate=16000, chunks=20)

    src.link(ds)
    ds.link(kws)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        pixel_ring.wakeup(direction)
        print('detected {} at direction {}'.format(keyword, direction))

    kws.on_detected = on_detected

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            print('quit')
            break

    src.recursive_stop()
Exemplo n.º 5
0
def main():
    src = Source(rate=16000, channels=4)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)
    src.link(ch1)
    ch1.link(kws)
    src.link(doa)
    outf = "./log/test.wav"
    fsall = FileSink(outf, rate=16000, channels=4)
    src.link(fsall)
    #ch1.link(fs)
    for i in range(4):
        ch = ChannelPicker(channels=4, pick=i)
        src.link(ch)
        outf = "./log/test"+str(i)+".wav"
        fs = FileSink(outf, rate=16000, channels=1)
        ch.link(fs)


    def on_detected(keyword):
         print('detected {} at direction {}'.format(keyword, doa.get_direction()))

    kws.set_callback(on_detected)

    src.recursive_start()
    sec = 0
    while sec < 5:
        try:
            time.sleep(1)
            sec = sec + 1
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Exemplo n.º 6
0
def main():
    doa = None
    # DoA
    #    src = Source(rate=16000, frames_size=320, channels=4, device_index=0)
    src = Source(rate=16000, frames_size=320, channels=4)
    doa = DOA(rate=16000, chunks=20)
    src.link(doa)
    src.recursive_start()

    # GPIO
    GPIO.output(led_gnd_pin, False)
    GPIO.output(led1_pin, True)
    GPIO.output(led2_pin, True)

    # Google Assistant
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--credentials',
                        type=existing_file,
                        metavar='OAUTH2_CREDENTIALS_FILE',
                        default=os.path.join(
                            os.path.expanduser('/home/pi/.config'),
                            'google-oauthlib-tool', 'credentials.json'),
                        help='Path to store and read OAuth2 credentials')
    args = parser.parse_args()
    with open(args.credentials, 'r') as f:
        credentials = google.oauth2.credentials.Credentials(token=None,
                                                            **json.load(f))

    with Assistant(credentials, "RPi3_Assistant") as assistant:
        for event in assistant.start():
            process_event(event, doa)
Exemplo n.º 7
0
def main():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            print('STOP')
            break

    src.recursive_stop()
Exemplo n.º 8
0
def main():
##    RESPEAKER_RATE = 16000
##    RESPEAKER_CHANNELS = 1
##    RESPEAKER_WIDTH = 2
##    CHUNK = 1024
##    RECORD_SECONDS = 2
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)
    



    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        src.stream.start()
        print("* recording")
        frames = []
        for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("* done recording")
        src.stream.stop()
        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)
            except KeyError: 
                print("get nothing")
        else:
            print("get nothing")
            
        
        

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Exemplo n.º 9
0
    def run(self):
        global moving, last_movement_timestamp, doa_valid
        src = Source(rate=16000, channels=4, frames_size=320)
        #ch1 = ChannelPicker(channels=4, pick=1)
        doa = DOA(rate=16000)
        #src.link(ch1)
        src.link(doa)
        src.recursive_start()

        self.running = True
        while self.running:
            try:
                time.sleep(1)
                current_timestamp = datetime.datetime.now()
                if doa_valid == True and (
                    (current_timestamp - last_movement_timestamp).seconds > 2):
                    position, amplitute = doa.get_direction()
                    if amplitute > 2000:
                        pixels.wakeup(position)
                        print amplitute, position
                        if position > 0 and position < 180:
                            pivot_right()
                            time.sleep(position / 200)
                            stop()
                        elif position >= 180 and position < 360:
                            pivot_left()
                            position = 360 - position
                            time.sleep(position / 200)
                            stop()
                        time.sleep(2)
                    else:
                        pixels.speak()
                else:
                    pixels.think()
            except:
                print sys.exc_info()

        src.recursive_stop()
Exemplo n.º 10
0
def t3_bot():
    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS()
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)
    pixels.listen()
    pwm.setPWM(0, 0, 370)
    pwm.setPWM(1, 0, 640)

    counter = 0

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        if position >= 30 and position <= 180:
            pwm.setPWM(0, 0, 175)
            pwm.setPWM(1, 0, 500)
        elif position > 180 and position <= 330:
            pwm.setPWM(0, 0, 560)
            pwm.setPWM(1, 0, 500)
        elif position > 330 or position < 30:
            pwm.setPWM(0, 0, 370)
            pwm.setPWM(1, 0, 6200)
        else:
            pwm.setPWM(0, 0, 370)
            pwm.setPWM(1, 0, 640)

        #talkassist.os.system("espeak 'may i help you'")
        print("How may I help you?")
        print("call google assistant here, delete this line.")

    kws.set_callback(on_detected)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
            counter += 1
            print("counter is at " + str(counter))
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Exemplo n.º 11
0
def main():
    src = Source(rate=16000, frames_size=160, channels=4)
    ch0 = ChannelPicker(channels=src.channels, pick=0)
    ns = NS(rate=src.rate, channels=1)
    kws = KWS()
    doa = DOA(rate=16000, chunks=50)
    alexa = Alexa()

    alexa.state_listener.on_listening = pixel_ring.listen
    alexa.state_listener.on_thinking = pixel_ring.think
    alexa.state_listener.on_speaking = pixel_ring.speak
    alexa.state_listener.on_finished = pixel_ring.off

    # data flow between elements
    # ---------------------------
    # src -> ns -> kws -> alexa
    #    \
    #    doa
    src.pipeline(ch0, ns, kws, alexa)

    src.link(doa)

    def on_detected(keyword):
        direction = doa.get_direction()
        print('detected {} at direction {}'.format(keyword, direction))
        alexa.listen()
        pixel_ring.wakeup(direction)

    kws.on_detected = on_detected

    is_quit = []

    def signal_handler(sig, frame):
        is_quit.append(True)
        print('quit')

    signal.signal(signal.SIGINT, signal_handler)

    src.pipeline_start()
    while not is_quit:
        time.sleep(1)

    src.pipeline_stop()
    pixel_ring.off()

    # wait a second to allow other threads to exit
    time.sleep(1)
Exemplo n.º 12
0
def main():
    ser = serial.Serial("/dev/ttyACM0", 9600)

    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    kws = KWS(model='dad.pmdl')
    doa = DOA(rate=16000)

    src.link(ch1)
    ch1.link(kws)
    src.link(doa)

    def on_detected(keyword):
        position = doa.get_direction()
        pixels.wakeup(position)
        print('detected {} at direction {}'.format(keyword, position))
        if position <= 90:
            ser.write(b"1000\n")
        elif position > 90 and position <= 180:
            ser.write(b"0100\n")
        elif position > 180 and position <= 270:
            ser.write(b"0010\n")
        else:
            ser.write(b"0001\n")

        vibrate_time = 0.5
        time.sleep(vibrate_time)
        ser.write(b"0000\n")

    kws.set_callback(on_detected)

    src.recursive_start()
    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
    ser.close()
def main():
    logging.basicConfig(level=logging.DEBUG)

    src = Source(rate=16000, channels=4)
    ch1 = ChannelPicker(channels=4, pick=1)
    ns = NS(rate=16000, channels=1)
    kws = KWS(model='snowboy')
    doa = DOA(rate=16000)
    alexa = Alexa()

    alexa.state_listener.on_listening = pixels.listen
    alexa.state_listener.on_thinking = pixels.think
    alexa.state_listener.on_speaking = pixels.speak
    alexa.state_listener.on_finished = pixels.off

    def on_detected(keyword):
        direction = doa.get_direction()
        logging.info('detected {} at direction {}'.format(keyword, direction))
        pixels.wakeup(direction)
        alexa.listen()

    kws.on_detected = on_detected

    src.link(ch1)
    ch1.link(ns)
    ns.link(kws)
    kws.link(alexa)

    src.link(doa)

    src.recursive_start()

    while True:
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            break

    src.recursive_stop()
Exemplo n.º 14
0
class XYRobot(object):

    jike = config.get('jike', False)

    src = Source(rate=16000, channels=4, frames_size=320)
    ch1 = ChannelPicker(channels=4, pick=1)
    #这里需要填写pmdl的绝对路径
    #kws = KWS()
    #kws = KWS(os.path.join(constants.DATA_PATH ,config.get('kws_file_name','')))
    kws = KWS(
        os.path.join(constants.DATA_PATH, config.get('kws_file_name', '')))
    doa = DOA(rate=16000)

    def __init__(self):
        print('极客模式:' + str(self.jike))
        self.src.link(self.ch1)
        self.ch1.link(self.kws)
        self.src.link(self.doa)

        def on_detected(keyword):
            position = self.doa.get_direction()
            pixels.wakeup(position)
            voice = os.path.join(
                constants.DATA_PATH, 'sysvoices',
                'sysvoice' + str(random.randint(1, 8)) + '.mp3')
            direction = pixels.positionToDirection()
            print('detected {} at direction {} is {}'.format(
                keyword, position, direction))
            #这里是唤醒后进行打招呼的语音播放
            pixels.speak(voice)
            print(str(keyword) + voice)

            record.speech('你在我' + direction)

            if self.jike:
                print('进入极客模式')
                # 创建线程01,不指定参数
                thread_01 = Thread(target=instructions.jikeThread,
                                   args=(
                                       "jikeThread01",
                                       record,
                                       pixels,
                                   ))
                # 启动线程01
                thread_01.start()
            else:
                #开始录音
                outputtext = record.record()
                #这里是指令,语音识别后返回文字,将文字传入instructionsFunc,进行处理和播放
                instructions.instructionsFunc(outputtext, record, pixels)

        self.kws.set_callback(on_detected)
        self.src.recursive_start()

        config.init()

    def sigint_handler(self, signum, frame):
        record.stream.stop_stream()
        record.stream.close()
        record.p.terminate()

        self.src.recursive_stop()

        print 'catched interrupt signal!'
        sys.exit(0)

    def train(self, w1, w2, w3, m):
        '''
            传入三个wav文件,生成snowboy的.pmdl模型
            '''
        def get_wave(fname):
            with open(fname, 'rb') as infile:
                return base64.b64encode(infile.read()).decode('utf-8')

        url = 'https://snowboy.kitt.ai/api/v1/train/'
        data = {
            "name":
            "wukong-robot",
            "language":
            "zh",
            "token":
            config.get('snowboy_token', ''),
            "voice_samples": [{
                "wave": get_wave(w1)
            }, {
                "wave": get_wave(w2)
            }, {
                "wave": get_wave(w3)
            }]
        }

        response = requests.post(url, json=data)
        print(response.ok)
        if response.ok:
            with open(m, "wb") as outfile:
                outfile.write(response.content)
            return 'Snowboy模型已保存至{}'.format(m)
        else:
            return "Snowboy模型生成失败,原因:{}".format(response.text)