Skip to content

Commit a402b14

Browse files
Merge pull request #97 from marcobarilari/marco_improve-devSandbox
add more skill to devSandbox (audio and ppd calculation)
2 parents 9ae1304 + 4a996e8 commit a402b14

File tree

2 files changed

+105
-23
lines changed

2 files changed

+105
-23
lines changed

dev/devSandbox.m

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,62 @@
2323
% Init the structure that will contain PTB setup
2424
cfg = struct;
2525

26+
%% Visual
27+
28+
% set to false if no visual stimulation
29+
cfg.screen.do = true;
30+
2631
% Set the PTB window background manually
27-
cfg.color.background = [127 127 27];
32+
cfg.color.background = [127 127 127];
33+
34+
% Set monitor parameters if you care about visual angle
35+
cfg.visualAngle.do = true;
36+
cfg.screen.monitorWidth = 21.5; % cm
37+
cfg.screen.monitorDistance = 30; % cm
2838

2939
% Init PTB, see the Sub-Functions below
3040
cfg = devSandbox_initPTB(cfg);
3141

42+
% OUTPUT:
43+
%
44+
% cfg.screen.idx % Screen number where to draw, external screen if avaliable
45+
% cfg.screen.win % The onscreen window whose content should be shown at flip time
46+
% cfg.screen.winRect % The onscreen window size in pixels coordinates
47+
% cfg.screen.winWidth
48+
% cfg.screen.winHeight
49+
% cfg.screen.center % Center of the screen window
50+
% cfg.screen.ifi % Frame duration
51+
% cfg.screen.monitorRefresh % Monitor refresh rate
52+
53+
% Compute pixels per degrees
54+
cfg = devSandbox_computePpd(cfg);
55+
56+
% OUTPUT:
57+
%
58+
% cfg.screen.ppd % The number of pixels per degree given the distance to screen
59+
60+
%% Auditory
61+
% Set AUDIO
62+
63+
% set to false if no auditory stimulation
64+
cfg.audio.do = true;
65+
66+
% Set audio freq. and nb. of channels of your audio file input
67+
cfg.audio.fs = 44100;
68+
cfg.audio.channels = 2;
69+
70+
% Init Audio, see the Sub-Functions below
71+
cfg = devSandbox_initAudio(cfg);
72+
73+
% OUTPUT:
74+
%
75+
% cfg.audio.pahandle
76+
3277
%%
3378
% -------------------------------------------------------------------------
3479
% -------------------------- SET YOUR VARS HERE ---------------------------
3580
% -------------------------------------------------------------------------
3681

37-
% Define black and white
38-
white = WhiteIndex(cfg.screen.idx);
39-
grey = white / 2;
40-
inc = white - grey;
41-
4282
% Grating size in pixels
4383
gratingSizePix = 600;
4484

@@ -66,6 +106,12 @@
66106
% -------------------------------------------------------------------------
67107
% ------------------------------ PLAYGROUND -------------------------------
68108
% -------------------------------------------------------------------------
109+
110+
% Define black and white
111+
white = WhiteIndex(cfg.screen.idx);
112+
grey = white / 2;
113+
inc = white - grey;
114+
69115
% Define Half-Size of the grating image.
70116
textureSize = gratingSizePix / 2;
71117

@@ -167,29 +213,65 @@
167213

168214
% Shorter version of `initPTB.m`
169215

170-
% Skip the PTB sync test
171-
Screen('Preference', 'SkipSyncTests', 2);
216+
if cfg.screen.do
217+
218+
% Skip the PTB sync test
219+
Screen('Preference', 'SkipSyncTests', 2);
172220

173-
% Open a transparent window
174-
PsychDebugWindowConfiguration;
221+
% Open a transparent window
222+
PsychDebugWindowConfiguration;
175223

176-
% Here we call some default settings for setting up Psychtoolbox
177-
PsychDefaultSetup(2);
224+
% Here we call some default settings for setting up Psychtoolbox
225+
PsychDefaultSetup(2);
178226

179-
% Get the screen numbers and draw to the external screen if avaliable
180-
cfg.screen.idx = max(Screen('Screens'));
227+
% Get the screen numbers and draw to the external screen if avaliable
228+
cfg.screen.idx = max(Screen('Screens'));
181229

182-
% Open an on screen window
183-
[cfg.screen.win, cfg.screen.winRect] = Screen('OpenWindow', cfg.screen.idx, cfg.color.background);
230+
% Open an on screen window
231+
[cfg.screen.win, cfg.screen.winRect] = Screen('OpenWindow', cfg.screen.idx, cfg.color.background);
232+
233+
% Get the size of the on screen window
234+
[cfg.screen.winWidth, cfg.screen.winHeight] = WindowSize(cfg.screen.win);
235+
236+
% Get the Center of the Screen
237+
cfg.screen.center = [cfg.screen.winRect(3), cfg.screen.winRect(4)] / 2;
238+
239+
% Query the frame duration
240+
cfg.screen.ifi = Screen('GetFlipInterval', cfg.screen.win);
241+
242+
% Get monitor refresh rate
243+
cfg.screen.monitorRefresh = 1 / cfg.screen.ifi;
244+
245+
% Set up alpha-blending for smooth (anti-aliased) lines
246+
Screen('BlendFunction', cfg.screen.win, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
247+
248+
end
249+
250+
end
251+
252+
function cfg = devSandbox_initAudio(cfg)
253+
254+
if cfg.audio.do
255+
256+
InitializePsychSound(1);
257+
258+
cfg.audio.pahandle = PsychPortAudio('Open', ...
259+
[], ...
260+
[], ...
261+
[], ...
262+
cfg.audio.fs, ...
263+
cfg.audio.channels);
264+
265+
end
266+
end
184267

185-
% Get the size of the on screen window
186-
[cfg.screen.winWidth, cfg.screen.winHeight] = WindowSize(cfg.screen.win);
268+
function cfg = devSandbox_computePpd (cfg)
187269

188-
% Query the frame duration
189-
cfg.screen.ifi = Screen('GetFlipInterval', cfg.screen.win);
270+
% Computes the number of pixels per degree given the distance to screen and
271+
% monitor width. This assumes that the window fills the whole screen
190272

191-
% Set up alpha-blending for smooth (anti-aliased) lines
192-
Screen('BlendFunction', cfg.screen.win, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
273+
cfg.screen.FOV = 180 / pi * 2 * atan(cfg.screen.monitorWidth / (2 * cfg.screen.monitorDistance));
274+
cfg.screen.ppd = cfg.screen.winWidth / cfg.screen.FOV;
193275

194276
end
195277

tests/test_setDefaultsPTB.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function test_setDefaultsPtbAudio()
6565
'color', struct( ...
6666
'background', [0 0 0]), ...
6767
'text', struct('font', 'Courier New', 'size', 18, 'style', 1));
68-
68+
6969
expectedCFG.screen.monitorWidth = 42;
7070
expectedCFG.screen.monitorDistance = 134;
7171
expectedCFG.screen.resolution = {[], [], []};

0 commit comments

Comments
 (0)