Mình đang tính viết một chương bật nhạc và tắt nhạc bằng giọng nói trên python3 về vấn đề bật nhạc thì dễ dàng,còn vấn đề tát nhạc thì thấy rất khó khăn, mình tìm trên google mãi không ra cách tắt nhạc khi nó đang phát.
Mong giúp đỡ
Cách stop một audio đang phát
mình đã làm đươc nhé các bạn
Nếu dk bác chỉ em với được k ạ , vấn đề là nhạc bật nó ồn mình ra lệnh tắt sao nó nghe được ạ
Đây là code pocketsphinx mình sử dụng cho nó luôn chạy khi nhạc được bật
# -*- encoding: utf-8 -*-
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
import pyaudio;import wave;import pydub
import os
import sys
import pygame as pg ;import time
class pocketSphinx():#Thư viện pocketsphin
def __init__(self,BUSY):
self.BUSY = BUSY
MODELDIR = "/home/l/Desktop/pocketsphinx/model/en-us"
# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(MODELDIR, '/home/l/Desktop/pocketsphinx/model/en-us/en-us/'))
config.set_string('-lm', os.path.join(MODELDIR, '/home/l/Desktop/pocketsphinx/model/en-us/en-us.lm.bin'))
config.set_string('-dict', os.path.join(MODELDIR, '/home/l/Desktop/pocketsphinx/model/en-us/cmudict-en-us.dict'))
config.set_string('-keyphrase', 'stop')
config.set_float('-kws_threshold', 1e+20)
decoder = Decoder(config)
audio = pyaudio.PyAudio()
stream = audio.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
stream.start_stream()
result = None
in_speech_bf = False
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
if decoder.get_in_speech() != in_speech_bf:
in_speech_bf = decoder.get_in_speech()
if not in_speech_bf:
decoder.end_utt()
print('RESULT:----------------------------------------------------------------------------------------------------------->', decoder.hyp().hypstr)
result = decoder.hyp().hypstr
#elif "no" in str(result) or "oh" in str(result) :
#return
if "stop" in str(result):
pg.mixer.music.stop()
print(type(pg.mixer.music.stop()))
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
time.sleep(1)
return
elif "no one" in str(result):
return
else:
decoder.start_utt()
else:
break;
decoder.end_utt()
# -*- encoding: utf-8 -*-
import pygame
import PocketSphinx1
def pmusic(file):
pygame.init()
pygame.mixer.init()
clock = pygame.time.Clock()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
PocketSphinx1.pocketSphinx(True)
while pygame.mixer.music.get_busy():
print("Playing...")
clock.tick(1000)
return
def stopmusic():
pygame.mixer.music.stop()
def getmixerargs():
pygame.mixer.init()
freq, size, chan = pygame.mixer.get_init()
return freq, size, chan
def initMixer():
BUFFER = 3072 # audio buffer size, number of samples since pygame 1.8.
FREQ, SIZE, CHAN = getmixerargs()
pygame.mixer.init(FREQ, SIZE, CHAN, BUFFER)
try:
initMixer()
file = 'C:\\data\\03.mp3'
pmusic(file)
except KeyboardInterrupt: # to stop playing, press "ctrl-c"
stopmusic()
print("\nPlay Stopped by user")
except Exception:
print("unknown error")
print("Done")
def pMUSIC(PATH):
A = pmusic(PATH);
code này mình sử dụng bằng python.
Vấn đề ở đây là bạn cần một thiết bị lọc tiếng ồn