cpp-lln-lab / CPP_PTB

a set of function to make it easier to create behavioral, EEG, fMRI experiment with psychtoolbox
https://cpp-ptb.readthedocs.io/en/latest/index.html#
MIT License
12 stars 13 forks source link

Add safeties to help find the right audio device. #194

Closed Remi-Gau closed 1 year ago

Remi-Gau commented 1 year ago

When failing to open the device at the requested frequency, list all available devices and their default frequency.

Add a function to loop through a list of audio device and play a noise on them.

Remi-Gau commented 1 year ago
​
sca
clear
clc
PsychPortAudio('Close');
​
cfg.audio.playbackMode = 1;
cfg.audio.channels = 2;
cfg.audio.requestedLatency = 3;
​
InitializePsychSound(1);
audioDev = PsychPortAudio('GetDevices');
​
% tmp = [2 3 9];
​
for idx = 1:audioDev(end).DeviceIndex

    fprintf(1, '\n%i: %s\n', idx, audioDev(idx).DeviceName);

    cfg.audio.devIdx = audioDev(idx).DeviceIndex;

    % get device's sampling rate
    cfg.audio.fs = audioDev(idx).DefaultSampleRate;

    fprintf(num2str(cfg.audio.fs));

    try
        cfg.audio.pahandle = PsychPortAudio('Open', ...
            cfg.audio.devIdx, ...
            cfg.audio.playbackMode, ...
            cfg.audio.requestedLatency, ...
            cfg.audio.fs, ...
            cfg.audio.channels);

        clear sound
        sound = rand(cfg.audio.channels, cfg.audio.fs);

        PsychPortAudio('FillBuffer', cfg.audio.pahandle, sound);
        PsychPortAudio('Start', cfg.audio.pahandle);

        WaitSecs(1.5);

        PsychPortAudio('Close');

        WaitSecs(1);
    catch
    end

%     pressSpaceForMe 

end