Play a sound with a specific timing

17 visualizzazioni (ultimi 30 giorni)
Fabio
Fabio il 3 Gen 2024
Commentato: MarKf il 3 Gen 2024
Hello,
I would like to play a sound, and 100 ms after the sound onset to deliver a transcranial magnetic stimulation (TMS). To do so I am using MAGIC toolbox I wrote the code below, but I am having some issues regarding the sound onset; I have loaded the sound track, and I use the play command to play the sound. However, when I run the code, the onset of the sound takes considerably later than I thought, even later than the TMS pulse (I can hear the maching going off). The myMV command is instantaneous, so I assume this is a buffer issue? Is there a better way to play a sound?
I hope everything is clear
Thank you in advance
Fabio
%setup the parameter for the sound
[y, Fs] = audioread('Typing.wav');
player = audioplayer(y, Fs);
for t=1:10
% Play sound
play(player)
% Pause for 100ms
pause(0.1)
%Give pulse to TMS machine
myMV.fire()
rem=10-t
p(t,1)=randsample(isi,1)
pause(p)
end
  1 Commento
MarKf
MarKf il 3 Gen 2024
Almost-millisecond precision with Matlab can be archived with low level programming, and also very much depends on your system as well (I assume you're using Windows, in this specific case Mac or Linux would have been easier) so neurosceintists usually go with dedicated software. Like your magstimical toolbox which however might not be of use for delivering triggers to and from your sound card. Another one is the beloved Psychtoolbox. PTB3 may be a hassle to learn, but it also provides timing checks when triggers do not get delivered accurately (again, depending on the system, may happen for a few trials). You don't have to use the whole toolbox for your whole experiment, you can just use the sound subroutines. There are also other software solutions of course. You could also program it yourself (maybe taking inspiration from the open source code of the aforementioned solutions) but seing that you're just starting I'd suggest that for later on.

Accedi per commentare.

Risposte (1)

Hassaan
Hassaan il 3 Gen 2024
Modificato: Hassaan il 3 Gen 2024
To minimize the delay, you can use more precise timing functions available in MATLAB, like Psychtoolbox, which is often used in psychological experiments for its more accurate timing capabilities. Here's how you might approach this using Psychtoolbox:
  1. Install Psychtoolbox: If you don't already have it, you'll need to install the Psychtoolbox, which is specifically designed for experiments in neuroscience and psychology and offers precise control over timing.
  2. Rewrite Your Code: Use the Psychtoolbox functions for playing sound. The sound function is known for having lower latencies compared to audioplayer.
Here is an example using Psychtoolbox:
% Setup the parameter for the sound
[y, Fs] = audioread('Typing.wav');
% Initialize Psychtoolbox sound driver
InitializePsychSound(1); % The '1' flag initializes the sound driver with low latency
% Open audio device for low-latency output
pahandle = PsychPortAudio('Open', [], [], 1, Fs, size(y,2));
% Fill the audio playback buffer with the sound data
PsychPortAudio('FillBuffer', pahandle, y');
for t = 1:10
% Start audio playback for a single repetition (1), immediately (0)
PsychPortAudio('Start', pahandle, 1, 0, 1);
% Wait for 100ms
WaitSecs(0.1);
% Give pulse to TMS machine
myMV.fire();
% Get the remaining number of trials
rem = 10 - t;
% Random interstimulus interval
p(t,1) = randsample(isi,1);
% Wait for the interstimulus interval before the next trial
WaitSecs(p(t,1));
% Stop the audio playback
PsychPortAudio('Stop', pahandle);
end
% Close the audio device
PsychPortAudio('Close', pahandle);
Please note that the PsychPortAudio function is part of Psychtoolbox and is designed for low-latency audio operations. The WaitSecs function provides high precision timing which is better than pause.
Remember, when using Psychtoolbox, you should also ensure that the rest of your experimental setup (like the TMS machine) can be synchronized with the same precision. If myMV.fire() cannot be precisely timed, you will still face synchronization issues. You may need to explore hardware triggers or other synchronization methods provided by your TMS machine for precise control.
Lastly, make sure you've set the correct audio driver and that your hardware is capable of low-latency operation. Some operating systems or sound cards may not support low-latency audio out of the box and could require additional configuration.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems
  • Electrical and Electronics Engineering

Prodotti


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by