Skip to content

Commit 0570c89

Browse files
committed
refactor audio init
1 parent 2842d2a commit 0570c89

File tree

2 files changed

+69
-56
lines changed

2 files changed

+69
-56
lines changed

initPTB.m

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -63,63 +63,9 @@
6363
HideCursor;
6464

6565

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);
10268

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;
12369

12470

12571
%% Visual
@@ -213,6 +159,55 @@ function initDebug(cfg)
213159

214160
end
215161

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+
216211
function cfg = openWindow(cfg)
217212

218213
if cfg.testingSmallScreen

setDefaultsPTB.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@
1717
fieldsToSet.monitorWidth = 42;
1818
fieldsToSet.screenDistance = 134;
1919

20+
if isfield(cfg, 'initAudio') && cfg.initAudio
21+
22+
fieldsToSet.audio.fs = 44800;
23+
fieldsToSet.audio.channels = 2;
24+
fieldsToSet.audio.initVolume = 1;
25+
26+
% playing parameters
27+
% sound repetition
28+
fieldsToSet.audio.repeat = 1;
29+
30+
% Start immediately (0 = immediately)
31+
fieldsToSet.audio.startCue = 0;
32+
33+
% Should we wait for the device to really start?
34+
fieldsToSet.audio.waitForDevice = 1;
35+
36+
end
37+
2038
% loop through the defaults and set them in cfg if they don't exist
2139
names = fieldnames(fieldsToSet);
2240

0 commit comments

Comments
 (0)