Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Audio threshold

edited March 2014 in OpenSesame

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

  • edited 5:23AM

    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 call stream.start_stream() at the start of the trial, and stream.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

  • edited 5:23AM

    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

    import pyaudio 
    import serial
    import time
    from time import sleep 
    from openexp.keyboard import keyboard
    time1=self.time()
    
    flagn=0 # this flag is the 'r' option flag that will stop the rotation 
    my_keyboard = keyboard(exp, keylist=['escape'], timeout=1)
    my_keyboard2 = keyboard(exp, keylist=['1','2','3','4','5','6','7','8','9','0',None], timeout=1600) #1/3this should be changed according to how much time we need after each vibration in miliseconds
    
    # fixation point
    from openexp.canvas import canvas
    my_canvas = canvas(exp)
    my_canvas.set_penwidth(8) #penwidth
    my_canvas.line(410, 390, 610, 390)  #here you can change the fixation
    my_canvas.line(510, 290, 510, 490)
    my_canvas.show()
    #self.sleep(1000)
    #end fixation point
    
    
    
    #ser = serial.Serial("com30", 9600)
    time.sleep(2)
    exp.ser.bytesize = serial.EIGHTBITS
    
    
    
    
    
    in1 = self.experiment.get("f1")
    in2 = self.experiment.get("f2")
    in3 = self.experiment.get("f3")
    in4 = self.experiment.get("f4")
    in5 = self.experiment.get("f5")
    in6 = self.experiment.get("f6")
    in7 = self.experiment.get("f7")
    in8 = self.experiment.get("f8")
    in9 = self.experiment.get("f9")
    in10 = self.experiment.get("f10")
    
    
    vol = self.experiment.get("volume")
    
    modevt = self.experiment.get("mode")
    
    timeop = int(self.experiment.get("timemili")/10)
    
    
    regb = 0
    regd = 0
    regb = in6 + in7*2 + in8*4 + in9*8 + in10*16 + 32
    
    regd = in1*1 + in2*4 + in3*8 + in4*16 + in5*32
    
    
    
    if modevt == 'a':
        exp.ser.write('a')
        time.sleep(0.025)
        #exp.ser.close()
    
    
    #the beep code
    from openexp.sampler import sampler
    #src = exp.get_file('C:\Program Files (x86)\OpenSesame\sounds\pi.wav')
    src = exp.get_file('pi.wav')
    my_sampler = sampler(exp, src)
    my_sampler.play()
    #bip = self.experiment.get("beeptime")
    bip = 200
    self.sleep(bip)
    #end of beep code
    
    
    #microphone def
    
    if modevt == 't':
        exp.ser.write('t')
        exp.ser.write(chr(regb))
        exp.ser.write(chr(regd))
        exp.ser.write(chr(timeop))
        exp.ser.write(chr(vol))
    
        time.sleep(0.01)
        #exp.ser.close()
    
    
    if modevt == 'r':
        flagn=1
        exp.ser.write('r')
        exp.ser.write(chr(regb))
        exp.ser.write(chr(regd))
        exp.ser.write(chr(vol))
    
        start_response_interval = self.time()
    
        # Listen until a sound has been detected
    
    #TAP_THRESHOLD = 0.1
    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"
    
    normal_exit = 0
    esc_exit = 0
    
    start_response_interval = self.time()
    # Listen until a sound has been detected
    maxtime=self.time()
    while True:
            try:        
                my_keyboard.get_key()
                #if response == None:           
                block = stream.read(INPUT_FRAMES_PER_BLOCK)
                loudness = get_rms( block )
                if loudness > TAP_THRESHOLD:
                    print "mic exit"
                    normal_exit = 1             
                    self.experiment.set("response_time", self.time() - start_response_interval)
                    if flagn == 1 :
                        exp.ser.write('n')
                    # Close the mic
                    stream.stop_stream()
                    stream.close()
                    break   
                if self.time()-maxtime > 3000:
                    normal_exit = 1             
                    self.experiment.set("response_time", self.time() - start_response_interval)
                    if flagn == 1 :
                        exp.ser.write('n')
                    print "max time"
                    stream.stop_stream()
                    stream.close()
                    break
            except IOError, e:
                print e
                print "mic stream error"
                if flagn == 1 :
                        exp.ser.write('n')
                self.experiment.set("response_time", "check if mic is connected")
                # Close the mic
                stream.stop_stream()
                stream.close()
                break
            except Exception:
                print "esc exit"
                if flagn == 1 :
                        exp.ser.write('n')
                self.experiment.set("response_time", "problem, escape was pressed during test")
                # Close the mic
                stream.stop_stream()
                stream.close()
                esc_exit = 1
                raise
                break
    
    
    
    
    if normal_exit == 1: #normal exit and not exception
        end_time2 = self.time()
        my_keyboard2.flush()
        responsek, timeout2 = my_keyboard2.get_key()
        self.experiment.set("responsek", responsek)
        #print timeout2
        #print end_time2
        end_time22= self.time()
        if end_time22- end_time2 < 1599:
            #print timeout2
            print "working"
            eittime = float(float(0) - float(end_time2 - end_time22)/1000) #3/3this should be changed according to how much time we need after each vibration in seconds
            print "eittime = %.4f" %(eittime)
            time.sleep(1.6-eittime)
    
  • edited 5:23AM

    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.

  • edited 5:23AM

    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…

  • edited April 2014

    the problem is that some times when i open the stream it gets a value of 8000 from the stream,

    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 many try ... 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

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games