Psychtoolbox: How to play selected audio files?

18 次查看(过去 30 天)
I would like to play Beep and selected audion files alternately.
How should I write a code to play selected audio files?
I have a code to play all audio files randomly but I want to play only selected files and not randomely.
% Code for Audio multiple files read
audio_files=dir('C:\toolbox\aud_file2\*.wav');
% initializes sound driver...the 1 pushes for low latency
InitializePsychSound(1);
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
%randomize
a=randperm(80);
for i=1:length(audio_files)
% load sound file (make sure that it is in the same folder as this script)
[soundData freq ] = audioread('Beep1000convert.mp3');
% loads data into buffer
PsychPortAudio('FillBuffer', pahandle, soundData');
% how many repititions of the sound
repetitions=1;
%starts sound immediatley
PsychPortAudio('Start', pahandle, repetitions,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
% load sound file (make sure that it is in the same folder as this script)
aud_file=strcat('C:\toolbox\aud_file2\',audio_files(a(i)).name);
[soundData freq]=audioread(aud_file);
% wait
rs = randperm(3);
WaitSecs(2.0);
% fill buffer
PsychPortAudio('FillBuffer', pahandle, soundData');
% starts sound immediatley
PsychPortAudio('Start', pahandle, 1,0);
% stop
PsychPortAudio('Stop', pahandle, 1,0);
  4 个评论
Mathieu NOE
Mathieu NOE 2020-10-22
hello
see example below
if you want to loop automatically through the directory (all files) , you can do like this :
% method for looping through all files in directory
file = dir ('*.wav');
M= length (file)
%%%%%%%%%%%
NFFT = 512;
NOVERLAP = round(0.75*NFFT);
w = hamming(NFFT);
%%%%%%%%%%%
for ck = 1:M
[data,Fs]= audioread(file(ck).name);
[sensor_spectrum, freq] = pwelch(data,w,NOVERLAP,NFFT,Fs);
figure(ck),plot(freq,20*log10(sensor_spectrum));
title(['Spectra / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
xlabel('Time (s)');ylabel('Frequency (Hz)');
end
if you want to creat a short list manually you can do (old fashioned way but still works ) :
% method for looping through manual files list
% file names = strings
% make sure to have all strings same length (add trailling blank for shorter names)
file(1,:) = 'Approach_Gear_Drop_Aft Ctr.wav';
file(2,:) = 'Approach_Gear_Drop_Aft LH.wav ';
file(3,:) = 'Approach_Gear_Drop_Aft RH.wav ';
M = 3;
%%%%%%%%%%%
NFFT = 512;
NOVERLAP = round(0.75*NFFT);
w = hamming(NFFT);
%%%%%%%%%%%
for ck = 1:M
[data,Fs]= audioread(file(ck,:));
[sensor_spectrum, freq] = pwelch(data,w,NOVERLAP,NFFT,Fs);
figure(ck),plot(freq,20*log10(sensor_spectrum));
title(['Spectra / Fs = ' num2str(Fs) ' Hz / Delta f = ' num2str(freq(2)-freq(1)) ' Hz ']);
xlabel('Time (s)');ylabel('Frequency (Hz)');
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Using audio files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by