Reputation: 43
Is there any way to detect system sound instead of microphone sound? I want to be able to detect whenever my system makes a sound instead of when the microphone picks up the actual sound.
One way I found to do this use an "audio loop-back in either software or hardware (e.g. connect a lead from the speaker 'out' jack to the microphone 'in' jack)."
Capturing speaker output in Java
I am building a program that plays an mp3 file whenever a system sound happens but I don't want it to go off if the dog barks.
Thanks!
Upvotes: 4
Views: 6360
Reputation: 23
A simple way to do it
import time
from pycaw.pycaw import AudioUtilities
def is_audio_playing():
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
if session.State == 1: # State == 1 means the audio session is active
return True
return False
while True:
if is_audio_playing():
print("Audio is currently playing.")
else:
print("No audio is playing.")
time.sleep(1)
Upvotes: 0
Reputation: 24089
What about something with pyaudio (http://people.csail.mit.edu/hubert/pyaudio/)
Like this:
import pyaudio
chunk = 1024
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=chunk)
data = stream.read(chunk)
And then you could calculate the root-mean-square(RMS) of the audio sample and go from there.
Edited: You can see what kind of devices you can use by doing something like the following. (http://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.PyAudio.get_device_info_by_index)
import pyaudio
p = pyaudio.PyAudio()
for i in xrange(0,10):
try:
p.get_device_info_by_index(i)
except Exception,e:print e
Upvotes: 2