[open] Audio threshold
Hi!
I am trying for long time now to detect an audio threshold from the microphone through the inline code.
The problem is that I am trying to do that in a loop, I am opening the stream each loop which is time consuming and sometimes doesn't work…
Is there any way to open the microphone stream once in the start of the experiment (exp.) and using the data when needed???
The michrophone code:
TAP_THRESHOLD = self.experiment.get("mic")
FORMAT = pyaudio.paInt16
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 2
RATE = 44100
INPUT_BLOCK_TIME = 0.01
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
def get_rms( block ):
"""Get root mean square as a measure of loudness"""
import struct
import math
global SHORT_NORMALIZE
count = len(block)/2
format = "%dh" % (count)
shorts = struct.unpack( format, block )
sum_squares = 0.0
for sample in shorts:
n = sample * SHORT_NORMALIZE
sum_squares += n*n
return math.sqrt( sum_squares / count )
# Open the mic
try:
stream = pyaudio.PyAudio().open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
input_device_index = 0,
frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
except IOError, e:
print e
print "mic stream error1"
Comments
Hi,
In principle, creating and closing a stream for every trial should be fine. From you code snippet it's not clear how you are trying to do this exactly. Are you sure that you are closing the stream? Working examples can be found in this discussion:
If the issue persists, could you remove the
try ... except ...
from your code and post the stacktrace from the debug window? The error message should give some insight into what goes wrong.Alternatively, I suppose you could create a
stream
object at the start of the experiment (in the same way as you're doing now), and callstream.start_stream()
at the start of the trial, andstream.stop_stream()
at the end. For documentation, see:But I would think that simply creating a new stream on every trial should work just as well.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
hi sebastiaan
Thanks for the quik answer
It seems that sometimes the pyaudio gets an garbage as an input, very high values from the mic.
Here is the complete code
Hmm, ok, but what exactly is the problem here? I took your previous post to mean that you occasionally get an error when you open a new stream. But now it sounds like the stream is opening fine, but you're getting funny-sounding input from the mic.
Could you be a bit more specific about what the problem is exactly? Please also include full error messages (from the debug window) if there are any.
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi
Thanks for the help, I really need it…
the problem is that some times when i open the stream it gets a value of 8000 from the stream,
and some times it is ok and stops just when I talk.
This code runs in a loop many times.
the problem shows up just on an opening of the stream, and never appeared on the first time that I open the stream.
this led me to the conclusion that if I'll open the stream just once outside of the loop it will be ok…
I'm afraid that you're still not being specific enough!
What exactly has a value of 8000, and where did you get this value from? Is 8000 the loudness returned by
get_rms()
that you printed to the debug window or something like that? If so, is 8000 even an unusual value, and, if it is, what are usual values? Also, if error messages are not the problem, why do you have so manytry ... except ...
statements in your code? These generally just hide problems and make scripts more difficult to debug, so unless there is a good reason I would remove them.You really need to provide a detailed explanation of the problem for anyone to shed some light on it.
As an aside, if you haven't already done so, I would also record some microphone input with a tool like Audacity. If this looks normal, then you know at least that the microphone input itself is fine.
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!