PsychPortAudio full duplex mode working with limited success

Hello Mario,

I'm trying to use PsychPortAudio's full duplex mode, unfortunately with limited success.

 

I'm able to playback and record simultaneously but not exactly as I expect.

The code below should playback an audio file, wait for a vocal response and record it. The vocal response should occur during or after the playback.

 

However, in practice, the vocal response is recorded only if it takes place during the playback but not if it's already finished, and the output recording contains everything from the playback start time and not from the vocal response start time.

 

I attach my code, based on the toolbox demos.

I use Roland Cakewalk UA-4FX external USB sound card on Win7 64bit/Matlab R2017a/PTB 3.0.14.

 

I would be much grateful for any advice.

Thanks,

Dan

 

 

function myAudioTask

 

 

InitializePsychSound(1);

freq = 44100;

myfile = '../sounds/myfile.wav';

 

try

    pahandle = PsychPortAudio('Open', [], 3, 1, freq, [2, 1]);

   

    for t = 1 : 3

       

        disp('start trial');

       

        % % preallocate recording buffer

        PsychPortAudio('GetAudioData', pahandle, 10);               

        wavdata = audioread(myfile);

        wavdata = [wavdata(:,1) wavdata(:,1)];% 2 channels

        PsychPortAudio('FillBuffer', pahandle, wavdata'); % loads data into buffer

        audio_onset = PsychPortAudio('Start', pahandle,1,0,1);

        disp('start playback');

               

        level = 0; % init

        triggerlevel = 0.01; % init

   

        % Repeat as long as below trigger-threshold (within max time)

        while level < triggerlevel

            % Fetch current audiodata:

            audiodata = PsychPortAudio('GetAudioData', pahandle);

            predata = audiodata; % save the last buffer

            % Compute maximum signal amplitude in this chunk of data:

            if ~isempty(audiodata)

                level = max(abs(audiodata(1,:)));

            else

                level = 0;

            end

                     

            % if exceeded trial time then go to next trial

            if GetSecs - audio_onset > 6

                StopAudioRecord(pahandle); % stop recording and empty buffer

                return;

            end

           

            % Below trigger-threshold?

            if level < triggerlevel

                WaitSecs(0.005);

            end

        end % while

       

        % Ok, last fetched chunk was above threshold!

       

        rt = GetSecs - audio_onset;

        disp(num2str(rt));

        recordedaudio = predata;

        % record a few more seconds

        starttime = GetSecs;

        while GetSecs - starttime < 5

            audiodata = PsychPortAudio('GetAudioData', pahandle);

            recordedaudio = [recordedaudio audiodata]; % add to audio vector

        end

       

        StopAudioRecord(pahandle); % stop recording and empty buffer       

        wavfile = sprintf('../soundfile%.3d.wav',t);

        audiowrite(wavfile, transpose(recordedaudio), freq);

    end

 

catch ME

    fclose('all');

    % Close the audio device:

    PsychPortAudio('Close');

    rethrow(ME);

end

fclose('all');

% Close the audio device:

PsychPortAudio('Close');

end

 

%==========================================================================

 

function StopAudioRecord(phand)

% Stop sound capture:

PsychPortAudio('Stop', phand);

% empty before next trial:

PsychPortAudio('GetAudioData', phand);

end

 

%==========================================================================