LoginSignup
3
0

More than 3 years have passed since last update.

ウェイクワードを使ってみよう!

Last updated at Posted at 2020-12-09

ウェイクワードとは?

ウェイクワードとは、音声アシスタント機能を呼び出す(起動する)際に用いられる音声コマンドのことである。スマートフォンやスマートスピーカーに搭載されている音声アシスタントは、待機状態でもウェイクワードは常に拾える状態になっている。ウェイクワードを検知すると音声アシスタントが立ち上がり、自然文による命令を解釈して実行する準備を整える。ソース

環境

OS: Windows 10
プログラミング言語:python
環境構築ツール:conda

ライブラリーの紹介

Porcupine is a highly-accurate and lightweight wake word engine. It enables building always-listening voice-enabled applications

通訳:Porcupine は、高精度で軽量のウェイクワードエンジンです。 常に聞く音声対応アプリケーションの構築を可能にします

インストール方法


conda create -n porcupine_test python=3.7
conda activate porcupine_test
conda install pyaudio
pip install pvporcupine

コード

#!/usr/bin/env python3
import struct
import pyaudio
import pvporcupine
from datetime import datetime

porcupine = None
pa = None
audio_stream = None

try:
    keywords = ["computer"]
    porcupine = pvporcupine.create(keywords=keywords)

    pa = pyaudio.PyAudio()

    audio_stream = pa.open(
                    rate=porcupine.sample_rate,
                    channels=1,
                    format=pyaudio.paInt16,
                    input=True,
                    frames_per_buffer=porcupine.frame_length)

    print('Listening {')
    for keyword in keywords:
        print('  {}'.format(keyword))
    print('}')                

    while True:
        pcm = audio_stream.read(porcupine.frame_length)
        pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)

        result = porcupine.process(pcm)

        if result >= 0:
            print('[%s] Detected %s' % (str(datetime.now()), keywords[result]))

except KeyboardInterrupt:
    print('Stopping ...')

finally:
    if porcupine is not None:
        porcupine.delete()

    if audio_stream is not None:
        audio_stream.close()

    if pa is not None:
        pa.terminate()
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0