Я пытаюсь написать сценарий, который активно прослушивает системный звук и, когда он слышит определенный звук, похожий на «baseline.wav», выполняет определенную команду.
Ниже — это фиктивный фрагмент, который я воображаю, но я не уверен, что он сработает для желаемого результата.
Изменить: я должен уточнить, что я относительно новичок в программировании на Python.< /p>
import pyaudio import wave
Define the audio file to listen for
audio_file = "baseline.wav"
Define the command to execute when audio is detected
command_to_execute = "your_specific_command_here"
Function to check if the audio is detected
def is_audio_detected(): # Code to detect if the audio is similar to "baseline.wav" # Return True if detected, False otherwise pass
Function to execute the command
def execute_command(): # Code to execute the specific command pass
Main function to listen to system audio
def listen_to_audio(): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* Listening for audio...")
while True:
data = stream.read(CHUNK)
if is_audio_detected():
print("Audio detected!")
execute_command()
stream.stop_stream()
stream.close()
p.terminate()
if name == "main": listen_to_audio()
Подробнее здесь: https://stackoverflow.com/questions/782 ... -in-python