MATLAB Answers

How to randomize audio files

조회 수: 21(최근 30일)
Takara Sumoto
Takara Sumoto 10 Sep 2019
댓글: Takara Sumoto 11 Sep 2019
I have 80 audio files and want to play them randomely (not in the file number's orders).
My code so far is;
for i=1:length(audio_files)
aud_file=strcat('C:\toolbox\aud_file\',audio_files(i).name);
% load sound file (make sure that it is in the same folder as this script)
[soundData freq]=audioread(aud_file);
% opens sound buffer at a different frequency
pahandle = PsychPortAudio('Open', [], [], 2, []);
% 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);
%wait
WaitSecs(1);
%close
PsychPortAudio('Close', pahandle);
end
I searched the code to rondomize which is;
a=zeros(1,80);
for i=1:80
a=randperm(80);
end
But I'm not sure where I should add this randomize code.
Should the code be somewhere in the audio loop or before the loop?

  댓글 수: 0

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

채택된 답변

David K.
David K. 10 Sep 2019
It looks to me that the line
aud_file=strcat('C:\toolbox\aud_file\',audio_files(i).name);
is where you get a song. Since the loop variable i is how you are currently getting the songs you can instead use it to index your random sequence. So, you can put it before the loop and index it to get the random values you want.
a=randperm(80);
for i=1:length(audio_files)
aud_file=strcat('C:\toolbox\aud_file\',audio_files(a(i)).name);
I removed everything else from the randomizer code you posted since I am pretty sure it is unneccessary.

  댓글 수: 5

표시 이전 댓글 수: 2
Takara Sumoto
Takara Sumoto 10 Sep 2019
Yes, they are playing just in the order.
Walter Roberson
Walter Roberson 10 Sep 2019
Are you sure you used
aud_file=strcat('C:\toolbox\aud_file\',audio_files(a(i)).name);
and not
aud_file=strcat('C:\toolbox\aud_file\',audio_files(i).name);
Takara Sumoto
Takara Sumoto 11 Sep 2019
That's what I was doing wrong!
It works now.
Thank you very much for your help!

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

추가 답변(1개)

Johannes Fischer
Johannes Fischer 10 Sep 2019
편집: Johannes Fischer 10 Sep 2019
What kind of randomization do you want?
Play all sounds once, before playing a sound twice?
randomizedOrder = randperm(80);
for i = randomizedOrder
% ...load and play audio
end
Or every time you load a file, each sound has the same probability of being loaded.
% number of consecutive audio playbacks
N = 10;
randomizedOrder = ceil(rand(1, N)*80);
for i = randomizedOrder
% ...load and play audio
end

  댓글 수: 1

Takara Sumoto
Takara Sumoto 10 Sep 2019
Thank you for the repley.
In the experiment, I'm going to play 80 audio files in a row (including "wait", "beep cue", "key press", etc. for each file), but I want to randomize files for each perticipant.
I tried both codes above but I coud only play in the order.

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by