Psychtoolbox: How to play selected audio files?

조회 수: 56 (최근 30일)
Takara Sumoto
Takara Sumoto 2020년 10월 20일
댓글: Takara Sumoto 2020년 10월 24일
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
Takara Sumoto
Takara Sumoto 2020년 10월 24일
Thank you for your suggestion!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by