Skip to content

Instantly share code, notes, and snippets.

@iandol
Last active August 21, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandol/22d54fc8beb3aa384362f66d7d78594e to your computer and use it in GitHub Desktop.
Save iandol/22d54fc8beb3aa384362f66d7d78594e to your computer and use it in GitHub Desktop.
Trying to get non-flickering, non-blocking movie playback on PTB, the trick is to use non-blocking double buffering, keep showing the old frame until a new frame is available. Otherwise if you block, you miss flips waiting for the correct time, and if you don't block, then you get blank frames if you flip at FPS.
function movietest()
bgColour = 0.5;
screen = max(Screen('Screens'));
screenSize = [];
ptb = mySetup(screen,bgColour,screenSize);
filename='C:\Code\opticka\stimuli\monkey.mp4';
preloadsecs = [];
flags1 = [];
pixelformat = [];
blocking = 0;
loop = 2;
tic
[movie, duration, fps, width, height] = Screen('OpenMovie', ptb.win, ...
filename, [], preloadsecs, flags1, pixelformat);
fprintf('\n\n--->>> MOVIETEST: %s\n\t%f seconds duration, %f fps, w x h = %i x %i, in %ims\n', ...
filename, duration, fps, width, height, round(toc*1e3));
fprintf('\t\t Blocking: %i | Loop: %i | Preloadsecs: %i | Pixelformat: %i | Flags: %i\n', blocking, loop, preloadsecs,...
pixelformat,flags1);
Priority(MaxPriority(ptb.win)); %bump our priority to maximum allowed
otex = -1;
Screen('PlayMovie', movie, 1, loop)
vbl(1)=Screen('Flip', ptb.win);
tic
while vbl(end) < vbl(1) + 2
texture = Screen('GetMovieImage', ptb.win, movie, blocking);
if texture > 0
if otex > 0; Screen('Close', otex); otex=-1;end
Screen('DrawTexture', ptb.win, texture);
otex = texture;
elseif otex > 0
Screen('DrawTexture', ptb.win, otex)
end
vbl(end+1) = Screen('Flip', ptb.win, vbl(end) + ptb.ifi/2);
end
if texture > 0; Screen('Close', texture); end
toc
figure;plot(diff(vbl)*1e3);title('VBL Times');ylabel('Time (ms)')
Screen('PlayMovie', movie, 0);
Screen('CloseMovie', movie);
Screen('Close', ptb.win);
%m = movieStimulus('fileName',filename,'blocking',blocking,'preloadSecs',preloadsecs,'loopStrategy',loop,'pixelFormat',pixelformat);
%m.run();
end
%----------------------
function ptb = mySetup(screen, bgColour, ws)
ptb.cleanup = onCleanup(@myCleanup);
PsychDefaultSetup(2); KbName('UnifyKeyNames');
Screen('Preference', 'SkipSyncTests', 0);
if isempty(screen); screen = max(Screen('Screens')); end
ptb.ScreenID = screen;
PsychImaging('PrepareConfiguration');
[ptb.win, ptb.winRect] = PsychImaging('OpenWindow', ptb.ScreenID, bgColour, ws, [], [], [], 1);
[ptb.w, ptb.h] = RectSize(ptb.winRect);
screenWidth = 405; % mm
viewDistance = 573; % mm
ptb.ppd = ptb.w/2/atand(screenWidth/2/viewDistance);
ptb.ifi = Screen('GetFlipInterval', ptb.win);
ptb.fps = 1 / ptb.ifi;
Screen('BlendFunction', ptb.win, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
end
%----------------------
function myCleanup()
disp('Clearing up...')
sca
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment