How to add pink noise to a video?

Hi! I’m coding an experiment in which we show short (3-5 s) videos containing audio, in which actors say some words. This works well. Now I need to add pink noise on top of the audio to make it hard to hear the actors’ words.

I can generate pink noise like that:

fs = 20e3;
duration = 100;
y = pinknoise(duration*fs);

I guess ‘PlayMovie’ + ‘Flip’ (see below) is what starts the audio, but I don’t know how to integrate the noise in the audio of the played video. Below is my min code. I’d appreciate any ideas!
A plus question is - how I could manipulate the loudness of the noise (to try out different signal-to-noise options)?

% open the window
[window, windowRect] = Screen('OpenWindow', max(Screen('Screens')))

% open the video
moviename = fullfile(path,'stim','stim_test.avi');
[movie, duration ,fps, None, None] = Screen('OpenMovie', window, moviename);

 % Start playback engine: loop = 0, soundvolume = 100
Screen('PlayMovie', movie, 1, 0, 100);
%get an absolute timestamp
vbl = Screen('Flip', window);

% play until q is pressed, then break

while ~strcmp(key, 'q') 

[pressed, tpress, keys] = KbCheck;
key = KbName(find(keys));

    tex = Screen('GetMovieImage', window, movie, 0);

    if tex<0  % End of the movie, break out of loop:
    if tex==0

% Draw the new texture to screen:
Screen('DrawTexture', window, tex);

% Update display:
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);

% Release texture:
Screen('Close', tex);

if pressed == 0
   [pressed, tpress, keys] = KbCheck;
   key = KbName(find(keys))

end % end the loop

    % Stop playback:
    Screen('PlayMovie', movie, 0);
    % Close movie:
    Screen('CloseMovie', movie);

I use:
PTB 3.0.18; Windows 10; Matlab 2021a

There’s an elegant method that usually works to do this live, allowing to change the volume of the dynamically generated and mixed in pink-noise for each played back movie, ie. change volume at Screen(‘OpenMovie’, …) time, trial by trial. The trick involves some special parameters to Screen(‘OpenMovie’) to take advantage of some GStreamer audio post-processing magic.

Figuring this out took me the better part of an hour, therefore if you want to know this solution, help PsychPaidSupportAndServices will tell you how to buy a paid support membership for PTB, so I can tell you the magic line of code.


Hi Mario,

Thank you for checking! I’m afraid I won’t be able to buy the paid support though…


Sorry to hear that. Best of luck with your problem.

Maybe you or your p.i. want to partake in our currently running user survey to add some feedback about what the obstacles are for your lab to support Psychtoolbox future and your own efficient work under the following link: Psychtoolbox user survey 2022!