Skip to content

Latest commit

 

History

History
55 lines (51 loc) · 1.23 KB

README.md

File metadata and controls

55 lines (51 loc) · 1.23 KB

Libs Documentation

audiothread.py

Examples

Simplest way

from audiothread import *

th = AudioCommandThread()
th.start()

Initialize with the specified work queue

from audiothread import *
import queue

cmd_q = queue.Queue()
th = AudioCommandThread(cmd_q=cmd_q)
th.start()

Playing pure tone with specific frequency

cmd = TonePlayCommand(config=AudioConfig(fs=16000, ch=1), out_freq=440)
th.push(cmd)

Detecting the frequency with its corresponding amplitude

def result_cb(detected_tones):
    if len(detected_tones) == 0:
        print("no detected tone.")
        return
    freq, amp = detected_tones[0]
    if amp > 0:
        print("detected: ", int(freq), " Hz", end="\r", flush=True)

cmd = ToneDetectCommand(config=AudioConfig(fs=16000, cb=result_cb), framemillis=100, nfft=4096)
th.push(cmd)

Stoping the action

cmd.stop()

Stoping the thread

th.join()

Note that the cmd will be "dirty" after calling cmd.stop() and hence it will not be executed when it is pushed again, except for calling cmd.reset()

th.push(cmd)
# blablabla
cmd.stop()
# If you need to execute the same command again
cmd.reset()
th.push(cmd)