Skip to content

Commit 5e09e39

Browse files
committed
audio adds into initPTB
1 parent 8db1a32 commit 5e09e39

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

initPTB.m

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,63 @@
4040

4141

4242
%% Audio
43+
% Intialize PsychPortAudio
44+
% in setParameters, one needs to define
45+
% cfg.audio.channels
46+
% cfg.fs
47+
% cfg.PTBInitVolume
48+
49+
4350
InitializePsychSound(1);
4451

52+
if any(strcmp(cfg.stimComp,{'mac','linux'}))
53+
54+
% pahandle = PsychPortAudio('Open' [, deviceid][, mode][, reqlatencyclass][, freq] ...
55+
% [, channels][, buffersize][, suggestedLatency][, selectchannels][, specialFlags=0]);
56+
% Try to get the lowest latency that is possible under the constraint of reliable playback
57+
cfg.pahandle = PsychPortAudio('Open', [], [], 3, cfg.fs, cfg.audio.channels);
58+
59+
else
60+
61+
% get audio device list
62+
audio_dev = PsychPortAudio('GetDevices');
63+
64+
% find output device using WASAPI deiver
65+
idx = find([audio_dev.NrInputChannels] == 0 & ...
66+
[audio_dev.NrOutputChannels] == 2 & ...
67+
~cellfun(@isempty, regexp({audio_dev.HostAudioAPIName},'WASAPI')));
68+
69+
% save device ID
70+
cfg.audio.i = audio_dev(idx).DeviceIndex;
71+
72+
% get device's sampling rate
73+
cfg.fs = audio_dev(idx).DefaultSampleRate;
74+
75+
% the latency is not important - but consistent latency is! Let's try with WASAPI driver.
76+
cfg.pahandle = PsychPortAudio('Open', cfg.audio.i, 1, 3, cfg.fs, cfg.audio.channels);
77+
% cfg.pahandle = PsychPortAudio('Open', [], [], 0, cfg.fs, cfg.audio.channels);
78+
79+
end
80+
81+
% set initial PTB volume for safety (participants can adjust this manually
82+
% at the begining of the experiment)
83+
PsychPortAudio('Volume', cfg.pahandle, cfg.PTBInitVolume);
84+
85+
cfg.audio.pushsize = cfg.fs*0.010; %! push N ms only
86+
cfg.requestoffsettime = 1; % offset 1 sec
87+
cfg.reqsampleoffset = cfg.requestoffsettime*cfg.fs; %
88+
89+
90+
% playing parameters
91+
% sound repetition
92+
cfg.PTBrepet = 1;
93+
94+
% Start immediately (0 = immediately)
95+
cfg.PTBstartCue = 0;
96+
97+
% Should we wait for the device to really start (1 = yes)
98+
cfg.PTBwaitForDevice = 1;
99+
45100

46101
%% Visual
47102

0 commit comments

Comments
 (0)