def __init__(self, CONF): "Initialize microphone" path = os.path.join("output", "recordings", CONF["participant"] + "_" + CONF["session"]) if not os.path.exists(path): os.makedirs(path) self.path = path self.CONF = CONF microphone.switchOn() self.mic = microphone.AdvAudioCapture()
def test_AudioCapture_basics(self): microphone.haveMic = False with pytest.raises(microphone.MicrophoneError): microphone.AdvAudioCapture(autoLog=False) microphone.haveMic = True microphone.switchOn(16000, 1, 2048) microphone.switchOn(48000) mic = microphone.AdvAudioCapture(saveDir=self.tmp, autoLog=False) mic = microphone.AdvAudioCapture(saveDir=self.tmp + '_test', autoLog=False) mic.record(.10, block=False) # returns immediately core.wait(.02) mic.stop() mic.reset() mic.record(0.2, block=True) assert os.path.isfile(mic.savedFile)
def NART_task(myClock, datafn): wavDirName = datafn + '_wav' if not os.path.isdir(wavDirName): os.makedirs(wavDirName) # to hold .wav files microphone.switchOn() mic = microphone.AdvAudioCapture(name='mic', saveDir=wavDirName, stereo=True) import codecs stimuli_list = codecs.open('Stimuli\\nart_wordlist.txt', 'r', 'utf-8') .read().split('\n') for this in stimuli_list: fixation_screen(myClock, waittime=1) NART_word.setText(this) NART_word.draw() win.flip() wavfile = mic.record(1200) event.waitKeys(keyList=['space']) mic.stop() if event.getKeys(keyList=['escape']): mic.stop() quitEXP(True)
def NART_task(myClock, datafn): wavDirName = datafn + '_wav' if not os.path.isdir(wavDirName): os.makedirs(wavDirName) # to hold .wav files microphone.switchOn() mic = microphone.AdvAudioCapture(name='mic', saveDir=wavDirName, stereo=True) import codecs stimuli_list = codecs.open('Stimuli\\nart_wordlist.txt', 'r', 'utf-8').read().split('\n') for this in stimuli_list: fixation_screen(myClock, waittime=1) NART_word.setText(this) NART_word.draw() win.flip() wavfile = mic.record(1200) event.waitKeys(keyList=['space']) mic.stop() if event.getKeys(keyList=['escape']): mic.stop() quitEXP(True)
def setup_class(self): global sound from psychopy import sound microphone.switchOn(48000) self.tmp = mkdtemp(prefix='psychopy-tests-microphone')
# Setup the Window win = visual.Window(size=[1680, 1050], fullscr=True, screen=0, winType='pyglet', allowGUI=False, allowStencil=False, monitor='MBP_Monitor', color=[0, 0, 0], colorSpace='rgb', blendMode='avg', useFBO=True, units='height') # Enable sound input/output: microphone.switchOn() # store frame rate of monitor if we can measure it expInfo['frameRate'] = win.getActualFrameRate() if expInfo['frameRate'] != None: frameDur = 1.0 / round(expInfo['frameRate']) else: frameDur = 1.0 / 60.0 # could not measure, so guess # create a default keyboard (e.g. to check for escape) defaultKeyboard = keyboard.Keyboard() # Initialize components for Routine "instr" instrClock = core.Clock() Stroop_Instr = visual.TextStim(win=win, name='Stroop_Instr', text='default text',
# Start Code - component code to be run before the window creation wavDirName = filename + '_wav' if not os.path.isdir(wavDirName): os.makedirs(wavDirName) # to hold .wav files # Setup the Window win = visual.Window( size=(1024, 768), fullscr=True, screen=0, winType='pyglet', allowGUI=False, allowStencil=False, monitor='testMonitor', color=[0,0,0], colorSpace='rgb', blendMode='avg', useFBO=True, units='height') # Enable sound input/output: microphone.switchOn() # store frame rate of monitor if we can measure it expInfo['frameRate'] = win.getActualFrameRate() if expInfo['frameRate'] != None: frameDur = 1.0 / round(expInfo['frameRate']) else: frameDur = 1.0 / 60.0 # could not measure, so guess # Initialize components for Routine "trial" trialClock = core.Clock() # Create some handy timers globalClock = core.Clock() # to track the time since experiment started routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine # ------Prepare to start Routine "trial"-------
from psychopy import data from psychopy import microphone import random import time # class ExperimentRuntime(ioHubExperimentRuntime): # # def run(self, *args): exp = data.ExperimentHandler(name='exp', version='1.0', extraInfo={'participant':0}, runtimeInfo=None, originPath=None, savePickle=True, saveWideText=True, dataFileName='datafile') microphone.switchOn(sampleRate=16000) name = "speech_exp_0.wav" mic = microphone.AdvAudioCapture(filename=name) mic.setMarker(tone=5000, secs=0.015, volume=0.03) mic.record(5.5, block=False) beg_time = time.time() while time.time() < beg_time+5: mic.playMarker() exp.addData("time", time.time()) time.sleep(1) mic.stop()
if not practice == 1: if not test == 1: if len(speechList) != numTrials: print speechList print 'Exiting...: Unexpected number of qualified *.mp4 files' core.quit() tmpSoundFile = normjoin(rootPath, 'temp.wav') babblePath = normjoin(rootPath, 'Babble') babbleList = ['babble' + str(f) for f in table['BabbleFile']] bab1File = normjoin(babblePath, 'babble1.wav') info, bab1 = scipy.io.wavfile.read(bab1File) babbleRMS = rms(bab1) # Set up microphone, must be 16000 or 8000 Hz for speech recognition microphone.switchOn(sampleRate=16000) mic = microphone.AdvAudioCapture(name='mic', saveDir=dataOutPath, stereo=False) #Initiate the PsychPy window win = visual.Window([1920, 1080]) #sound.init(48000,buffer=500) if not test: #Present an example of the talker without noise. No response taken. keystext = "Please listen to the example sentence.Press spacebar when ready. " text = visual.TextStim(win, keystext, pos=(0, 0), units='pix') text.draw() win.flip() core.wait(0.5) k = event.waitKeys()
# PsychoPy only knows English color names; 'en-UK' might work better here for some speakers: options = {'lang': 'en-US'} # Set up visual things: win = visual.Window(color=-0.05) instr = visual.TextStim(win, text='say a color name when you see the microphone icon\n green, hot pink, gold, fire brick, dark red, ...\n\npress q or say exit to quit; expects "%s" input' % options['lang'], height=0.08, pos=(0,-0.4), wrapWidth=1.4) word = visual.TextStim(win, text='', height=0.2, opacity=0.8, pos=(0,.25)) icon = visual.ImageStim(win, image='mic.png', opacity=0.6) anykey = visual.TextStim(win, text='Press any key to start', pos=instr.pos-[0,.3], color='darkblue') display(instr, anykey) if 'escape' in event.waitKeys(): core.quit() win.flip() # Set up microphone, must be 16000 or 8000 Hz for speech recognition switchOn(sampleRate=16000) mic = AudioCapture() while not (event.getKeys(['escape', 'q']) or word.text in ['exit']): display(instr, word) core.wait(1.5) display(instr, word, icon) # Record for 2.5 seconds, get the "best word" guess: wavfile = mic.record(2.5) display(instr, word) guess = Speech2Text(wavfile, **options).getResponse() os.remove(wavfile) if guess.word: word.setText(guess.word)
def trial(self, clock, window, shapes, mouse, keys, text_color, wait_time, warning_time, exp, count, ser): """ Main speech type function :param clock: clock used for standardized timing; initialized in the main experimental loop :param window: display window :param shapes: array of shape objects to be used (not already randomized) :param mouse: mouse device :param text_color: color for text :param wait_time: max seconds for trial to wait before continuing if trial is not completed :param warning_time: num of seconds left to begin countdown :param exp: experiment object for adding trial data :param count: number of speech trials during this experiment for file naming :param ser: serial port that links to XBee for syncing :return: status of trial where 0 = completed but incorrect; 1 = completed and correct; 2 = incomplete """ # Default Value Set Up for Timing # global stimulus_beg_time stimulus_beg_time = -1 global in_between_time in_between_time = -1 global total_stimuli_time total_stimuli_time = -1 # Text values count_label = visual.TextStim(window, units='norm', text=u'', pos = [0, -0.6], height=0.2, color=text_color, colorSpace='rgb255',alignHoriz='center', alignVert='center') second_label = visual.TextStim(window, units='norm', text=u'Speak color of blocks', pos=[0,0.3], height=0.1, color=text_color, colorSpace='rgb255',alignHoriz='center', alignVert='center') done_label = visual.TextStim(window, units='norm', text=u'Done', pos=[0,-0.25], height=0.1, color=text_color, colorSpace='rgb255',alignHoriz='center', alignVert='center') done_button = visual.Rect(window, width=0.5, height=0.25, lineColor=(0, 0, 0), lineWidth=2, lineColorSpace='rgb', pos=(0, -0.25)) next_label = visual.TextStim(window, units='norm', text=u'Speech Round', pos=[0,0], height=0.1, color=text_color, colorSpace='rgb',alignHoriz='center', alignVert='center') BLOCK_LIST = [second_label, done_button, done_label] # Display round name helper.displayNewRound(window, next_label) # Microphone Set Up # microphone.switchOn(sampleRate=16000) name = "speech_exp_%d.wav" %count mic = microphone.AdvAudioCapture(filename=name) # todo: can edit marker to output as sync signal; played when recording starts # marker currently set to not output any sound on onset of recording mic.setMarker(tone=5000, secs=0.015, volume=0.0) # Block Sequence Display # print "%f BEGIN BLOCK SEQUENCE" %(clock.getTime()) ser.write("Begin Sequence") global in_between_time in_between_time = helper.drawSequence(window, shapes, clock) ser.write("End Sequence") print "%f END BLOCK SEQUENCE" %(clock.getTime()) # for block interaction # self.hub.clearEvents() start_time = clock.getTime() timeout_counter = 0 self.hub.clearEvents() # store time right when clicking stimuli is presented for reference window.callOnFlip(track_speech_time, clock, mouse) window.flip() # records for length of specified wait time mic.record(wait_time, block=False) ser.write("Start") while mic.recorder.running: [s.draw() for s in BLOCK_LIST] count_label.draw() window.flip() timeout_counter += 1 ## FOR MOUSE-CLICK END: ## # buttons, times = mouse.getPressed(getTime=True) # if mouse.isPressedIn(done_button, buttons=[0]): # break ## FOR KEYBOARD END: ## events = keys.getKeys() if len(events) != 0: break # adjust countdown value, to be displayed with the next flip if timeout_counter >= ((wait_time - warning_time)*60) and timeout_counter % 60 == 0: count_label.setText(((wait_time*60)-timeout_counter)/60) # turn off microphone and saves the audio file automatically ser.write("Finish") microphone.switchOff() finish_time = clock.getTime() # once the round is finished, use previous counters to calculate total time spent and individual click times total_stimuli_time = finish_time - speech_beg_time print "\n%f" %(finish_time) print "%f TOTAL TIME TO FINISH ROUND" %(total_stimuli_time) # save data in the experiment file exp.addData("stimulus_begin_time", speech_beg_time) exp.addData("in_between_time", in_between_time) exp.addData("total_stimuli_time", total_stimuli_time) exp.addData("time1", start_time) exp.addData("time2", finish_time) # return status code based on correctness of sequence if timeout_counter == wait_time*60: return 2 if timeout_counter < wait_time*60: # assume finished normally by clicking button return 0 return -1
from psychopy import microphone, core, prefs prefs.general['audioLib'] = ['pyo'] microphone.switchOn(sampleRate=44100) mic = microphone.AdvAudioCapture(stereo=False) mic.record(sec=2, filename='../recordings/test.wav') core.quit()
def trial(self, clock, window, shapes, mouse, keys, text_color, wait_time, warning_time, exp, count, ser): """ Main speech type function :param clock: clock used for standardized timing; initialized in the main experimental loop :param window: display window :param shapes: array of shape objects to be used (not already randomized) :param mouse: mouse device :param text_color: color for text :param wait_time: max seconds for trial to wait before continuing if trial is not completed :param warning_time: num of seconds left to begin countdown :param exp: experiment object for adding trial data :param count: number of speech trials during this experiment for file naming :param ser: serial port that links to XBee for syncing :return: status of trial where 0 = completed but incorrect; 1 = completed and correct; 2 = incomplete """ # Default Value Set Up for Timing # global stimulus_beg_time stimulus_beg_time = -1 global in_between_time in_between_time = -1 global total_stimuli_time total_stimuli_time = -1 # Text values count_label = visual.TextStim(window, units='norm', text=u'', pos=[0, -0.6], height=0.2, color=text_color, colorSpace='rgb255', alignHoriz='center', alignVert='center') second_label = visual.TextStim(window, units='norm', text=u'Speak color of blocks', pos=[0, 0.3], height=0.1, color=text_color, colorSpace='rgb255', alignHoriz='center', alignVert='center') done_label = visual.TextStim(window, units='norm', text=u'Done', pos=[0, -0.25], height=0.1, color=text_color, colorSpace='rgb255', alignHoriz='center', alignVert='center') done_button = visual.Rect(window, width=0.5, height=0.25, lineColor=(0, 0, 0), lineWidth=2, lineColorSpace='rgb', pos=(0, -0.25)) next_label = visual.TextStim(window, units='norm', text=u'Speech Round', pos=[0, 0], height=0.1, color=text_color, colorSpace='rgb', alignHoriz='center', alignVert='center') BLOCK_LIST = [second_label, done_button, done_label] # Display round name helper.displayNewRound(window, next_label) # Microphone Set Up # microphone.switchOn(sampleRate=16000) name = "speech_exp_%d.wav" % count mic = microphone.AdvAudioCapture(filename=name) # todo: can edit marker to output as sync signal; played when recording starts # marker currently set to not output any sound on onset of recording mic.setMarker(tone=5000, secs=0.015, volume=0.0) # Block Sequence Display # print "%f BEGIN BLOCK SEQUENCE" % (clock.getTime()) ser.write("Begin Sequence") global in_between_time in_between_time = helper.drawSequence(window, shapes, clock) ser.write("End Sequence") print "%f END BLOCK SEQUENCE" % (clock.getTime()) # for block interaction # self.hub.clearEvents() start_time = clock.getTime() timeout_counter = 0 self.hub.clearEvents() # store time right when clicking stimuli is presented for reference window.callOnFlip(track_speech_time, clock, mouse) window.flip() # records for length of specified wait time mic.record(wait_time, block=False) ser.write("Start") while mic.recorder.running: [s.draw() for s in BLOCK_LIST] count_label.draw() window.flip() timeout_counter += 1 ## FOR MOUSE-CLICK END: ## # buttons, times = mouse.getPressed(getTime=True) # if mouse.isPressedIn(done_button, buttons=[0]): # break ## FOR KEYBOARD END: ## events = keys.getKeys() if len(events) != 0: break # adjust countdown value, to be displayed with the next flip if timeout_counter >= ( (wait_time - warning_time) * 60) and timeout_counter % 60 == 0: count_label.setText(((wait_time * 60) - timeout_counter) / 60) # turn off microphone and saves the audio file automatically ser.write("Finish") microphone.switchOff() finish_time = clock.getTime() # once the round is finished, use previous counters to calculate total time spent and individual click times total_stimuli_time = finish_time - speech_beg_time print "\n%f" % (finish_time) print "%f TOTAL TIME TO FINISH ROUND" % (total_stimuli_time) # save data in the experiment file exp.addData("stimulus_begin_time", speech_beg_time) exp.addData("in_between_time", in_between_time) exp.addData("total_stimuli_time", total_stimuli_time) exp.addData("time1", start_time) exp.addData("time2", finish_time) # return status code based on correctness of sequence if timeout_counter == wait_time * 60: return 2 if timeout_counter < wait_time * 60: # assume finished normally by clicking button return 0 return -1