numpy 尝试从麦克风获取语音

68bkxrlz  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(173)

我在呼叫中心工作。我听电话并给他们打分。员工失分最常见的原因是沉默太久。我正在写一个应用程序。应用程序的逻辑如下:
我的代码正常工作,但它不重置计时器时,它听到声音。它需要在一个连续的循环中收听,并在听到声音时重置计时器。

import pyaudio
import numpy as np
import time
import threading
import tkinter as tk

def ses_alma():
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    sensitivity = 150  # Minimum algılanabilen ses şiddeti

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    print("Ses dinleme başladı...")

    last_sound_time = time.time()

    while True:
        data = stream.read(CHUNK)
        audio_data = np.frombuffer(data, dtype=np.int16)

        if np.max(np.abs(audio_data)) < sensitivity:
            if time.time() - last_sound_time >= 1:
                sessizlik_suresi.set(sessizlik_suresi.get() + 1)
        else:
            last_sound_time = time.time()
            sessizlik_suresi.set(0)

        if np.max(np.abs(audio_data)) > 100:  # Ses şiddeti 100'ün üzerinde ise süreyi sıfırla
            sessizlik_suresi.set(0)

        time.sleep(1)

    stream.stop_stream()
    stream.close()
    p.terminate()

def uyarı_ver():
    while True:
        if sessizlik_suresi.get() >= 25:  # 25 saniye sessizlik tespit edilirse
            show_popup()
            sessizlik_suresi.set(0)
        time.sleep(1)

def show_popup():
    popup = tk.Toplevel()
    popup.geometry("200x100")
    popup.title("UYARI")
    message = tk.Label(popup, text="25 saniyeden fazla sessizlik!")
    message.pack()
    popup.after(5000, popup.destroy) # 5 saniye sonra uyarı penceresini kapatır

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("300x100")
    root.title("Sessizlik Uyarısı")
    root.configure(bg="black")

    sessizlik_suresi = tk.IntVar()
    sessizlik_suresi.set(0)

    time_label = tk.Label(root, textvariable=sessizlik_suresi, font=("Arial Bold", 36), bg="black", fg="white")
    time_label.pack()

    ses_alma_thread = threading.Thread(target=ses_alma)
    uyarı_thread = threading.Thread(target=uyarı_ver)

    ses_alma_thread.start()
    uyarı_thread.start()

    root.mainloop()

    ses_alma_thread.join()
    uyarı_thread.join()

1-它将不断地收听来自麦克风2的声音。如果代表保持沉默太长时间,它将向代表给予弹出警告。3-当时间达到30秒时,它应该给予警告。
我的代码是工作,但它不重置计时器时,它听到一个声音。它需要在一个连续的循环中收听,并在听到声音时重置计时器。如何将此功能添加到代码中?

gupuwyp2

gupuwyp21#

看起来你想改变这一点:

if np.max(np.abs(audio_data)) > 100:  # Ses şiddeti 100'ün üzerinde ise süreyi sıfırla
        sessizlik_suresi.set(0)

对此:

if np.max(np.abs(audio_data)) > 100:  # Ses şiddeti 100'ün üzerinde ise süreyi sıfırla
        last_sound_time = time.time()
        sessizlik_suresi.set(0)

假设你的if条件是正确的,当检测到的音量超过100时,这将重置最后的声音时间。我翻译了你的评论,似乎这就是它的意思。

相关问题