Skip to content

Commit 1ec1199

Browse files
committed
refactor ptbSoundDeviceTest and add cpp_ptb init and uninit function
1 parent 46e068f commit 1ec1199

File tree

6 files changed

+241
-85
lines changed

6 files changed

+241
-85
lines changed

cpp_ptb.m

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
function cpp_ptb(varargin)
2+
%
3+
% General intro function for CPP SPM
4+
%
5+
% USAGE::
6+
%
7+
% cpp_ptb
8+
% cpp_ptb('init')
9+
% cpp_ptb('uninit')
10+
% cpp_ptb('dev')
11+
%
12+
% :param action:
13+
% :type action: string
14+
%
15+
% :returns: - :action: (type) (dimension)
16+
%
17+
% Example::
18+
%
19+
% (C) Copyright 2022 CPP_PTB developers
20+
21+
p = inputParser;
22+
23+
defaultAction = 'init';
24+
25+
addOptional(p, 'action', defaultAction, @ischar);
26+
27+
parse(p, varargin{:});
28+
29+
action = p.Results.action;
30+
31+
switch lower(action)
32+
33+
case 'init'
34+
35+
initCppPtb();
36+
37+
case 'uninit'
38+
39+
uninitCppPtb();
40+
41+
case 'run_tests'
42+
43+
runTests();
44+
45+
end
46+
47+
end
48+
49+
function initCppPtb()
50+
%
51+
% Adds the relevant folders to the path for a given session.
52+
% Has to be run to be able to use CPP_PTB.
53+
%
54+
% USAGE::
55+
%
56+
% initCppPtb()
57+
%
58+
% (C) Copyright 2021 CPP_PTB developers
59+
60+
thisDirectory = fileparts(mfilename('fullpath'));
61+
62+
global CPP_PTB_INITIALIZED
63+
global CPP_PTB_PATHS
64+
65+
if isempty(CPP_PTB_INITIALIZED)
66+
67+
pathSep = ':';
68+
if ~isunix
69+
pathSep = ';';
70+
end
71+
72+
CPP_PTB_PATHS = fullfile(thisDirectory);
73+
CPP_PTB_PATHS = cat(2, CPP_PTB_PATHS, ...
74+
pathSep, ...
75+
genpath(fullfile(thisDirectory, 'src')));
76+
CPP_PTB_PATHS = cat(2, CPP_PTB_PATHS, pathSep, ...
77+
genpath(fullfile(thisDirectory, 'lib')));
78+
79+
addpath(CPP_PTB_PATHS, '-begin');
80+
81+
checkPtbVersion();
82+
83+
CPP_PTB_INITIALIZED = true();
84+
85+
detectCppPtb();
86+
87+
else
88+
fprintf('\n\nCPP_PTB already initialized\n\n');
89+
90+
end
91+
92+
end
93+
94+
function detectCppPtb()
95+
96+
workflowsDir = cellstr(which('initPTB.m', '-ALL'));
97+
98+
if isempty(workflowsDir)
99+
error('CPP_PTB is not in your MATLAB / Octave path.\n');
100+
101+
elseif numel(workflowsDir) > 1
102+
fprintf('CPP_PTB seems to appear in several different folders:\n');
103+
for i = 1:numel(workflowsDir)
104+
fprintf(' * %s\n', fullfile(workflowsDir{i}, '..', '..'));
105+
end
106+
error('Remove all but one with ''pathtool''' .\ n'); % or ''spm_rmpath
107+
108+
end
109+
end
110+
111+
function uninitCppPtb()
112+
%
113+
% Removes the added folders from the path for a given session.
114+
%
115+
% USAGE::
116+
%
117+
% uninitCppPtb()
118+
%
119+
% (C) Copyright 2021 CPP_PTB developers
120+
121+
thisDirectory = fileparts(mfilename('fullpath'));
122+
123+
global CPP_PTB_INITIALIZED
124+
global CPP_PTB_PATHS
125+
126+
if isempty(CPP_PTB_INITIALIZED) || ~CPP_PTB_INITIALIZED
127+
fprintf('\n\nCPP_PTB not initialized\n\n');
128+
return
129+
130+
else
131+
rmpath(CPP_PTB_PATHS);
132+
133+
if IsOctave
134+
clear -g;
135+
else
136+
clearvars -GLOBAL;
137+
end
138+
139+
end
140+
141+
end
142+
143+
function runTests()
144+
%
145+
% (C) Copyright 2019 CPP_PTB developers
146+
147+
tic;
148+
149+
cpp_ptb();
150+
151+
cd(fileparts(mfilename('fullpath')));
152+
153+
fprintf(sprintf('\nHome is %s\n', getenv('HOME')));
154+
155+
warning('OFF');
156+
157+
folderToCover = fullfile(pwd, 'src');
158+
testFolder = fullfile(pwd, 'tests');
159+
160+
success = moxunit_runtests(testFolder, ...
161+
'-verbose', '-recursive', '-with_coverage', ...
162+
'-cover', folderToCover, ...
163+
'-cover_xml_file', 'coverage.xml', ...
164+
'-cover_html_dir', fullfile(pwd, 'coverage_html'));
165+
166+
if success
167+
system('echo 0 > test_report.log');
168+
else
169+
system('echo 1 > test_report.log');
170+
end
171+
172+
toc;
173+
174+
end

miss_hit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
project_root
33

44
line_length: 100
5-
regex_function_name: "[a-z]+(([A-Z]){1}[A-Za-z]+)*"
5+
regex_function_name: "[a-z]+(_*[a-zA-Z0-9]+[a-z]*)*"
66
regex_script_name: "[a-z]+(([A-Z]){1}[A-Za-z]+)*"
77
regex_parameter_name: "[A-Z]{2,}|[a-z]+([A-Z]+[a-z]*)*"
88
copyright_entity: "Sam Schwarzkopf"

runTests.m

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/miss_hit.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# camelCase
2+
regex_function_name: "[a-z]+([A-Z0-9]+[a-z]*)*"

src/utils/ptbSoundDeviceTest.m

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%
2+
% Small script to loop through all the devices
3+
% and try to play some white noise on it to see which one actually works.
4+
%
5+
% (C) Copyright 2022 CPP_PTB developers
6+
7+
sca;
8+
clear;
9+
clc;
10+
11+
% init CPP PTB
12+
rootDir = cd(fullfile(fileparts(mfilename('fullpath')), '..', '..'));
13+
cpp_ptb;
14+
15+
% PsychPortAudio('Close');
16+
17+
cfg.audio.playbackMode = 1;
18+
cfg.audio.channels = 2;
19+
cfg.audio.requestedLatency = 3;
20+
21+
InitializePsychSound(1);
22+
23+
% Get a list of all the audio devices connected
24+
audioDev = PsychPortAudio('GetDevices');
25+
26+
% Here we can decide to test just a few (value must be between 1 and numel(audioDev))
27+
tmp = 10;
28+
29+
% Here we loop through all the devices and try to play some white noise on it
30+
% to see which one actually works.
31+
for idx = 10
32+
33+
fprintf(1, '\n%i: %s\n', idx, audioDev(idx).DeviceName);
34+
35+
cfg.audio.devIdx = audioDev(idx).DeviceIndex;
36+
37+
% get device's sampling rate
38+
cfg.audio.fs = audioDev(idx).DefaultSampleRate;
39+
40+
try
41+
cfg.audio.pahandle = PsychPortAudio('Open', ...
42+
cfg.audio.devIdx, ...
43+
cfg.audio.playbackMode, ...
44+
cfg.audio.requestedLatency, ...
45+
cfg.audio.fs, ...
46+
cfg.audio.channels);
47+
48+
clear sound;
49+
sound = rand(cfg.audio.channels, cfg.audio.fs);
50+
51+
PsychPortAudio('FillBuffer', cfg.audio.pahandle, sound);
52+
PsychPortAudio('Start', cfg.audio.pahandle);
53+
54+
WaitSecs(1.5);
55+
56+
PsychPortAudio('Close');
57+
58+
WaitSecs(1);
59+
catch
60+
end
61+
62+
pressSpaceForMe;
63+
64+
end

src/utils/ptb_soundDevice_test.m

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)