Skip to content

Commit

Permalink
a solution to the sound playing problem, a first run of subjects on t…
Browse files Browse the repository at this point in the history
…he final experiment
  • Loading branch information
vincentadam87 committed Jan 5, 2014
1 parent 44bca7a commit 73fa11c
Show file tree
Hide file tree
Showing 5 changed files with 15,283 additions and 15,213 deletions.
33 changes: 33 additions & 0 deletions Code/Tests/test_players.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from psychopy import core, visual, gui, data, event
from psychopy.misc import fromFile, toFile
import time, random
from scipy.io.wavfile import write
import numpy as np
import pygame
from matplotlib import pyplot as plt
import os, sys, inspect
import time
# realpath() with make your script run, even if you symlink it :)
#cmd_subfolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]) + "/scripts")
cmd_subfolder = os.path.realpath('..')+'/scripts'
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
import sound_build, exp_param

#------------ load global experiment parameters
Exp = exp_param.exp_param()


for i in np.arange(0,np.shape(Exp.Sound_array)[0]):
s = sound_build.make_noisy_stim(i,Exp)
level = Exp.level
#scaled = np.int16(s/np.max(np.abs(s)) * (2**12-1))# 32767)
scaled = np.int16(s * (2**9-1))# 32767)
write('test.wav', 44100, scaled)
pygame.mixer.init(frequency=44100, size=-16, channels=1, buffer=4096)
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play()
time.sleep(Exp.duration_tot+0.5)
#plt.plot(Exp.time_stim,s)
#plt.show()

85 changes: 60 additions & 25 deletions Code/notebooks/Test_data_analysis.ipynb

Large diffs are not rendered by default.

30,226 changes: 15,113 additions & 15,113 deletions Code/notebooks/Test_experiment_parameters.ipynb

Large diffs are not rendered by default.

82 changes: 45 additions & 37 deletions Code/psychopy/run_experiment.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from psychopy import core, visual, gui, data, event
from psychopy.misc import fromFile, toFile
import time, random

from scipy.io.wavfile import write
import numpy as np
import pygame

import os, sys, inspect
import os, sys
# realpath() with make your script run, even if you symlink it :)
#cmd_subfolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]) + "/scripts")
cmd_subfolder = os.path.realpath('..')+'/scripts'
Expand Down Expand Up @@ -46,30 +46,35 @@ def set_msg(txt,type):
m = visual.TextStim(win,text=txt, pos=BOTTOM_POS,wrapWidth = 30)
m.draw()

def playsound(s,vol,it=1):
scaled = np.int16(s * (2**9-1))#
write('test.wav', 44100, scaled)
pygame.mixer.init(frequency=44100, size=-16, channels=1, buffer=4096)
pygame.mixer.music.load("test.wav")
if not (vol == None):
pygame.mixer.music.set_volume(vol)
pygame.mixer.music.play(it)

def run_calibration():
# display instructions
set_msg('SET SOUND LEVEL','TITLE')
set_msg('press up and down to increase/decrease sound level and press enter when level is confortable','MAIN')
set_msg('Press return to continue','KEY')

win.flip()
# prepare calibration sound
level = Exp.level
s = sound_build.make_lp_noise(100000,3000,Exp.rate)
scaled = np.int16(s*level)
write('test.wav', 44100, scaled)
# run calibration
pygame.mixer.init()
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play(200)
pygame.mixer.music.set_volume(0.5)
vol = 0.5
playsound(s,vol,200)
pressEnter = False
while pressEnter==False:
allKeys=event.waitKeys()
for thisKey in allKeys:
if thisKey=='up':
pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()+0.1)
pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()+0.03)
print(pygame.mixer.music.get_volume())
elif thisKey=='down':
pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()-0.1)
pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()-0.03)
print(pygame.mixer.music.get_volume())
elif thisKey in ['return']:
pressEnter = True
Expand All @@ -91,6 +96,7 @@ def display_instructions():

set_msg('INSTRUCTIONS','TITLE')
set_msg('press up or down to charaterize the melodic contour of the sound you hear : Up-Down or Down-Up (you will hear some examples during training sessions)','MAIN')
set_msg('Press any key to continue','KEY')
win.flip()
core.wait(0.5)
event.waitKeys()
Expand Down Expand Up @@ -142,49 +148,51 @@ def run_training(session,duration):
time_start = core.getTime()
time_play = time_start
time_stop = time_start + duration
N_stims = len(Exp.Training_sounds[session-1])
while (core.getTime()<time_stop):

i = random.randint(1,N_stims)
s = sound_build.make_random_training_sound(session,Exp)
scaled = np.int16(s/np.max(np.abs(s)) * 32767)
write('test.wav', 44100, scaled)
pygame.mixer.init(frequency=44100, size=-16, channels=1, buffer=4096)
pygame.mixer.music.set_volume(vol)
pygame.mixer.music.load("test.wav")
s = sound_build.make_random_training_sound(session,Exp)
core.wait(time_play - core.getTime())
set_msg('Up or down?','MAIN')
win.flip()
pygame.mixer.music.play()

thisResp = get_response()
playsound(s,vol)
get_response()
print(core.getTime() -time_start)
time_play = core.getTime() + iti


def run_main_experiment():
time_start = core.getTime()
time_play = time_start
order = Exp.make_random_stim_order()
for i in order[1:10]:
Nonethird = np.floor(len(order)/3)
Ntwothird = np.floor(2*len(order)/3)

t = 0
for i in order:
t = t+1
print(core.getTime() -time_start)
if t in [Nonethird,Ntwothird]:
set_msg('Short Break!','MAIN')
set_msg('Press return to continue','KEY')
win.flip()
event.waitKeys(keyList=['return','space'])
core.wait(1)



s = sound_build.make_noisy_stim(i,Exp)
scaled = np.int16(s/np.max(np.abs(s)) * 32767)
write('test.wav', 44100, scaled)
core.wait(0.2)
pygame.mixer.init(frequency=44100, size=-16, channels=1, buffer=4096)
pygame.mixer.music.set_volume(vol)
pygame.mixer.music.load("test.wav")
core.wait(time_play - core.getTime())

core.wait(time_play - core.getTime())
set_msg('Up or down?','MAIN')
win.flip()
pygame.mixer.music.play()
core.wait(1)
playsound(s,vol)
core.wait(0.1)
#core.wait(0.5) #wait 500ms; but use a loop of x frames for more accurate timing in fullscreen
thisResp = get_response()
iscorrect = int(Exp.isRespCorrect(i,thisResp)) # bool to int to write in file
iscorrect = Exp.isRespCorrect(i,thisResp) # 1=correct, O=incorrect, -1=missed
time_play = core.getTime() + iti
dataFile.write('%i,%i,%i\n' %(i, thisResp,iscorrect))
core.wait(1)
dataFile.close()


Expand Down Expand Up @@ -213,7 +221,7 @@ def run_main_experiment():
display_instructions()

######### TRAINING Sessions #######################
training_duration = 5
training_duration = Exp.Training_duration


set_msg('TRAINING SESSIONS','TITLE')
Expand All @@ -236,7 +244,7 @@ def run_main_experiment():
win.flip()
event.waitKeys(keyList=['return'])

set_msg('SESSION 1','TITLE')
set_msg('SESSION 2','TITLE')
set_msg('Press enter to begin','KEY')
win.flip()
event.waitKeys(keyList=['return'])
Expand All @@ -250,7 +258,7 @@ def run_main_experiment():

############## Main experiment ##################
set_msg('MAIN EXPERIMENT','TITLE')
set_msg('You will now start the main experiment','MAIN')
set_msg('You will now start the main experiment. You will hear a greater variety of sounds than in the training sessions','MAIN')
set_msg('Press return to start','KEY')
win.flip()
event.waitKeys(keyList=['return'])
Expand Down
70 changes: 32 additions & 38 deletions Code/scripts/exp_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,38 @@
import random
class exp_param:

# Global parameters
# Target/flanker sound parameters
rate = 44100 #44.1 khz, the sampling rate
f0 =250
fi = f0*np.sqrt(2)
# Experiment structure
N_stim = 23 # total number of stim
N_rep = 1 # number of repetition per stim
# Stimulus parameters
interval = 0.1
se_blank_duration = 0.01

# task parameters
iti = 1

f0 =250 # lowest fundamental in octave pair
fi = f0*np.sqrt(2) # half way between f0 and 2*f0
duration_stim =0.06 # total duration of stimulus in sec
RATIO = [1,1.69,2.25,2.89,4] # ratio of alternating click-trains
harmonics_flanker = [3,20] # harmonics chosen to best match timbre of target alternating click trains
fc_ACT_DARK = 3000 # for butterworth filter
fc_HCT_DARK = 3500 # for hard cut of harmonics higher in HCT
fc_HCT_BROAD = 20000 # for hard cut of harmonics higher in HCT

# Stimulus structure
interval = 0.1 # blank interval length between flankers and target
se_blank_duration = 0.01 # extra milliseconds at sound onset and offset
duration_tot = 3*duration_stim + 2*interval + 2*se_blank_duration #0.38

### ACT & HCT
f_c_noise = 1000 # cut-off frequency for noise
level = 1000

# Experiment structure
N_stim = 23 # total number of stim
N_rep = 10 # number of repetition per stim (Hehrmann 30)
iti = 1 # time between response and begining of next trial
Training_duration = 60 #120 # 300 =5 minutes

# Constants
ACT = 0
HCT = 1
CONTROL = 2

RATIO = [1,1.69,2.25,2.89,4]

BROAD = 1
DARK = 0
fc_ACT_DARK = 3000 #
fc_HCT_DARK = 3500 #
fc_HCT_BROAD = 20000 #
### Flanker
harmonics_flanker = [3,20] # harmonics chosen to best match timbre of target alternating click trains
# noise characteristics
f_c_noise = 1000

level = 1000
CORRECT_UP = 1
CORRECT_DOWN = 0

time_stim = np.arange(0,np.floor(duration_tot*rate))/rate
time_target = np.arange(0,np.floor(duration_stim*rate))/rate
Expand All @@ -62,26 +58,24 @@ class exp_param:
[ACT, 0,f0, RATIO[4] , BROAD], #13
[ACT, -6,f0, RATIO[4] , BROAD], #14
# HCT Stimuli
[HCT, 0,f0, DARK, 0], #15 % second is freq ratio (1 = f0, 0.5 = f0/2)
[HCT, 0,f0, BROAD,0], #16
[HCT, 0,f0*2, DARK,1], #17
[HCT, 0,f0*2, BROAD,1], #18
[HCT, 0,f0, DARK, CORRECT_DOWN], #15 % second is freq ratio (1 = f0, 0.5 = f0/2)
[HCT, 0,f0, BROAD,CORRECT_DOWN], #16
[HCT, 0,f0*2, DARK,CORRECT_UP], #17
[HCT, 0,f0*2, BROAD,CORRECT_UP], #18
# ACT Control
[CONTROL, 0,f0, 4.0, DARK,0], #19
[CONTROL, 0,f0, 4.0, BROAD,0], #20
[CONTROL, -6,2*f0, 4.0, DARK,1], #21
[CONTROL, -6,2*f0, 4.0, BROAD,1], #22
[CONTROL, 0,f0, RATIO[4] , DARK,CORRECT_DOWN], #19
[CONTROL, 0,f0, RATIO[4] , BROAD,CORRECT_DOWN], #20
[CONTROL, -6,2*f0, RATIO[4] , DARK,CORRECT_UP], #21
[CONTROL, -6,2*f0, RATIO[4] , BROAD,CORRECT_UP], #22
# Additional training
[ACT, 0,f0, 9 , BROAD], #23
[ACT, 0,f0, 9 , BROAD], #23
[ACT, 0,f0, 9 , DARK]]) #24

CorrectResp = np.array([[15,16,19,20],[17,18,21,22]]) # down / up
Calibration_sounds = [0,1,23,24]
Training_sounds = np.array([[0,1,23,24],[0,1,6,7,12,13]])
Training_duration = 300 # 5 minutes

def __init__(self):

pass

def make_random_stim_order(self):
Expand Down

0 comments on commit 73fa11c

Please sign in to comment.