|
63 | 63 | HideCursor; |
64 | 64 |
|
65 | 65 |
|
66 | | -%% Audio |
67 | | -% Intialize PsychPortAudio |
68 | | -% in setParameters, one needs to define |
69 | | -% cfg.audio.channels |
70 | | -% cfg.fs |
71 | | -% cfg.PTBInitVolume |
72 | | - |
73 | | - |
74 | | -InitializePsychSound(1); |
75 | | - |
76 | | -if any(strcmp(cfg.stimComp,{'mac','linux'})) |
77 | | - |
78 | | - % pahandle = PsychPortAudio('Open' [, deviceid][, mode][, reqlatencyclass][, freq] ... |
79 | | - % [, channels][, buffersize][, suggestedLatency][, selectchannels][, specialFlags=0]); |
80 | | - % Try to get the lowest latency that is possible under the constraint of reliable playback |
81 | | - cfg.pahandle = PsychPortAudio('Open', [], [], 3, cfg.fs, cfg.audio.channels); |
82 | | - |
83 | | -else |
84 | | - |
85 | | - % get audio device list |
86 | | - audio_dev = PsychPortAudio('GetDevices'); |
87 | | - |
88 | | - % find output device using WASAPI deiver |
89 | | - idx = find([audio_dev.NrInputChannels] == 0 & ... |
90 | | - [audio_dev.NrOutputChannels] == 2 & ... |
91 | | - ~cellfun(@isempty, regexp({audio_dev.HostAudioAPIName},'WASAPI'))); |
92 | | - |
93 | | - % save device ID |
94 | | - cfg.audio.i = audio_dev(idx).DeviceIndex; |
95 | | - |
96 | | - % get device's sampling rate |
97 | | - cfg.fs = audio_dev(idx).DefaultSampleRate; |
98 | | - |
99 | | - % the latency is not important - but consistent latency is! Let's try with WASAPI driver. |
100 | | - cfg.pahandle = PsychPortAudio('Open', cfg.audio.i, 1, 3, cfg.fs, cfg.audio.channels); |
101 | | - % cfg.pahandle = PsychPortAudio('Open', [], [], 0, cfg.fs, cfg.audio.channels); |
| 66 | + %% Audio |
| 67 | + cfg = initAudio(cfg); |
102 | 68 |
|
103 | | -end |
104 | | - |
105 | | -% set initial PTB volume for safety (participants can adjust this manually |
106 | | -% at the begining of the experiment) |
107 | | -PsychPortAudio('Volume', cfg.pahandle, cfg.PTBInitVolume); |
108 | | - |
109 | | -cfg.audio.pushsize = cfg.fs*0.010; %! push N ms only |
110 | | -cfg.requestoffsettime = 1; % offset 1 sec |
111 | | -cfg.reqsampleoffset = cfg.requestoffsettime*cfg.fs; % |
112 | | - |
113 | | - |
114 | | -% playing parameters |
115 | | -% sound repetition |
116 | | -cfg.PTBrepet = 1; |
117 | | - |
118 | | -% Start immediately (0 = immediately) |
119 | | -cfg.PTBstartCue = 0; |
120 | | - |
121 | | -% Should we wait for the device to really start (1 = yes) |
122 | | -cfg.PTBwaitForDevice = 1; |
123 | 69 |
|
124 | 70 |
|
125 | 71 | %% Visual |
@@ -213,6 +159,55 @@ function initDebug(cfg) |
213 | 159 |
|
214 | 160 | end |
215 | 161 |
|
| 162 | +function cfg = initAudio(cfg) |
| 163 | + |
| 164 | + if cfg.initAudio |
| 165 | + |
| 166 | + requestedLatency = 3; |
| 167 | + |
| 168 | + InitializePsychSound(1); |
| 169 | + |
| 170 | + cfg.audio.devIdx= []; |
| 171 | + cfg.audio.playbackMode = 1; |
| 172 | + |
| 173 | + if IsWin |
| 174 | + |
| 175 | + % get audio device list |
| 176 | + audioDev = PsychPortAudio('GetDevices'); |
| 177 | + |
| 178 | + % find output device using WASAPI driver |
| 179 | + idx = find(... |
| 180 | + audioDev.NrInputChannels == 0 && ... |
| 181 | + audioDev.NrOutputChannels == 2 && ... |
| 182 | + ~cellfun(@isempty, regexp({audioDev.HostAudioAPIName}, 'WASAPI'))); |
| 183 | + |
| 184 | + % save device ID |
| 185 | + cfg.audio.devIdx = audioDev(idx).DeviceIndex; |
| 186 | + |
| 187 | + % get device's sampling rate |
| 188 | + cfg.audio.fs = audioDev(idx).DefaultSampleRate; |
| 189 | + |
| 190 | + end |
| 191 | + |
| 192 | + cfg.audio.pahandle = PsychPortAudio('Open', ... |
| 193 | + cfg.audio.devIdx, ... |
| 194 | + cfg.audio.playbackMode, ... |
| 195 | + requestedLatency, ... |
| 196 | + cfg.audio.fs, ... |
| 197 | + cfg.audio.channels); |
| 198 | + |
| 199 | + % set initial PTB volume for safety (participants can adjust this manually |
| 200 | + % at the begining of the experiment) |
| 201 | + PsychPortAudio('Volume', cfg.audio.pahandle, cfg.audio.initVolume); |
| 202 | + |
| 203 | + cfg.audio.pushSize = cfg.fs*0.010; %! push N ms only |
| 204 | + |
| 205 | + cfg.audio.requestOffsetTime = 1; % offset 1 sec |
| 206 | + cfg.audio.reqsSampleOffset = cfg.audio.requestOffsetTime*cfg.audio.fs; |
| 207 | + |
| 208 | + end |
| 209 | +end |
| 210 | + |
216 | 211 | function cfg = openWindow(cfg) |
217 | 212 |
|
218 | 213 | if cfg.testingSmallScreen |
|
0 commit comments