I want to pick some .wav files randomly from a certain directory without replacement or with no repititions .. how is that possible ? please help me in this regard

 採用された回答

Image Analyst
Image Analyst 2013 年 9 月 10 日

0 投票

You can get a directory and then use randperm to get a list of random, non-repeated indexes, or randi to get a list of possibly repeated indexes, or you can use this program I wrote that will play either a random wave file or one that you specify:
% Play a wav file. You can pass in 'random' and it will pick one at random from the folder to play.
% PlaySoundFile(handles.soundFolder, 'chime.wav');
% PlaySoundFile(handles.soundFolder, 'random');
function PlaySoundFile(soundFolder, baseWavFileName)
try % Read the sound file into MATLAB, and play the audio.
% soundFolder = fullfile(soundFolder, 'Sound Files');
if ~exist(soundFolder, 'dir')
warningMessage = sprintf('Warning: sound folder not found:\n%s', soundFolder);
WarnUser(warningMessage);
return;
end
if strcmpi(baseWavFileName, 'random')
itWorked = false;
tryCount = 1;
while itWorked == false
% Pick a file at random.
filePattern = fullfile(soundFolder, '*.wav');
waveFiles = dir(filePattern);
numberOfFiles = length(waveFiles);
% Get a random number
fileToPlay = randi(numberOfFiles, 1);
baseWavFileName = waveFiles(fileToPlay).name;
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
try
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
% It worked. It played because the audio format was OK.
itWorked = true;
catch
% Increment the try count and try again to find a file that plays.
tryCount = tryCount + 1;
if tryCount >= numberOfFiles
break;
end
end
end % of while()
else
% baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
end
catch ME
if strfind(ME.message, '#85')
% Unrecognized format. Play chime instead.
fprintf('Error in PlaySoundFile(): %s.\nUnrecognized sound format in file:\n\n%s\n', ME.message, fullWavFileName);
baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
end
end
errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from PlaySoundFile
By the way, WarnUser is simply
function WarnUser(message)
uiwait(warndlg(message));

11 件のコメント

sabrina lenglet
sabrina lenglet 2017 年 6 月 14 日
Hi , I try to use your script but I can't play sound file . it doesn't work . could you help me ? thanks
Walter Roberson
Walter Roberson 2017 年 6 月 14 日
What happens instead? Is there an error message? Does the sound come out wrong? Is there no sound?
sabrina lenglet
sabrina lenglet 2017 年 6 月 15 日
thanks Error in PlaySoundFile(). The error reported by MATLAB is:
Not enough input arguments.
sabrina lenglet
sabrina lenglet 2017 年 6 月 15 日
Hi ! I would like to play three .wav sound files randomly. I created a folder "soundfolder" in which I copied your script and three sound files can you help me ? thank you
Image Analyst
Image Analyst 2017 年 6 月 16 日
What did you pass in for soundFolder and baseWavFileName when you called PlaySoundFile()?
sabrina lenglet
sabrina lenglet 2017 年 6 月 19 日
I don't know how can I creat : baseWavFileName ? I want play sinus100Hz.wav, sinus800Hz.wav, sinus1500Hz.wav randomly. I am new in matlab ... thanks a lot
Image Analyst
Image Analyst 2017 年 6 月 19 日
It's a string, like
PlaySoundFile('c:/my sound files', 'sinus100Hz.wav')
If you need to create the wav file, see attached demo.
sabrina lenglet
sabrina lenglet 2017 年 6 月 21 日
編集済み: Walter Roberson 2017 年 6 月 21 日
HI, In th escrip I commented the end of your script) because I had always this message : Error in PlaySoundFile(). The error reported by MATLAB is: Not enough input arguments.
%errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
% fprintf('%s\n', errorMessage);
% WarnUser(errorMessage);
But now I have no message and no sound !
Thanks
This is the scrip that I an using :
% Play a wav file. You can pass in 'random' and it will pick one at random from the folder to play.
% PlaySoundFile(handles.soundFolder, 'chime.wav');
% PlaySoundFile(handles.soundFolder, 'random');
function PlaySoundFile(soundFolder, baseWavFileName)
try % Read the sound file into MATLAB, and play the audio.
%soundFolder = fullfile(soundFolder, 'Sound Files');
if ~exist(soundFolder, 'dir')
warningMessage = sprintf('Warning: sound folder not found:\n%s', soundFolder);
WarnUser(warningMessage);
return;
end
if strcmpi(baseWavFileName, 'random')
itWorked = false;
tryCount = 1;
while itWorked == false
% Pick a file at random.
filePattern = fullfile(soundFolder, '*.wav');
waveFiles = dir(filePattern);
numberOfFiles = length(waveFiles);
% Get a random number
fileToPlay = randi(numberOfFiles, 1);
baseWavFileName = waveFiles(fileToPlay).name;
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
try
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
% It worked. It played because the audio format was OK.
itWorked = true;
catch
% Increment the try count and try again to find a file that plays.
tryCount = tryCount + 1;
if tryCount >= numberOfFiles
break;
end
end
end % of while()
else
% baseWavFileName = 'Chime.wav';
% baseWavFileName = 'sinus1300.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
%soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
end
catch ME
if strfind(ME.message, '#85')
% Unrecognized format. Play chime instead.
fprintf('Error in PlaySoundFile(): %s.\nUnrecognized sound format in file:\n\n%s\n', ME.message, fullWavFileName);
baseWavFileName = 'sinus1300.wav';
% efface sab baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
end
end
% errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
% fprintf('%s\n', errorMessage);
% WarnUser(errorMessage);
end
return; % from PlaySoundFile
sabrina lenglet
sabrina lenglet 2017 年 6 月 21 日
This is the scrip that I an using and I execute it PlaySoundFile('D:\Sabrina\soundFolder','sinus1300.wav')
sabrina lenglet
sabrina lenglet 2017 年 6 月 21 日
編集済み: Walter Roberson 2017 年 6 月 21 日
% Play a wav file. You can pass in 'random' and it will pick one at random from the folder to play.
% PlaySoundFile(handles.soundFolder, 'chime.wav');
% PlaySoundFile(handles.soundFolder, 'random');
function PlaySoundFile(soundFolder, baseWavFileName)
try % Read the sound file into MATLAB, and play the audio.
%soundFolder = fullfile(soundFolder, 'Sound Files');
if ~exist(soundFolder, 'dir')
warningMessage = sprintf('Warning: sound folder not found:\n%s', soundFolder);
WarnUser(warningMessage);
return;
end
if strcmpi(baseWavFileName, 'random')
itWorked = false;
tryCount = 1;
while itWorked == false
% Pick a file at random.
filePattern = fullfile(soundFolder, '*.wav');
waveFiles = dir(filePattern);
numberOfFiles = length(waveFiles);
% Get a random number
fileToPlay = randi(numberOfFiles, 1);
baseWavFileName = waveFiles(fileToPlay).name;
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
try
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
% It worked. It played because the audio format was OK.
itWorked = true;
catch
% Increment the try count and try again to find a file that plays.
tryCount = tryCount + 1;
if tryCount >= numberOfFiles
break;
end
end
end % of while()
else
% baseWavFileName = 'Chime.wav';
% baseWavFileName = 'sinus1300.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
%soundsc(y,Fs,bits,range);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
end
catch ME
if strfind(ME.message, '#85')
% Unrecognized format. Play chime instead.
fprintf('Error in PlaySoundFile(): %s.\nUnrecognized sound format in file:\n\n%s\n', ME.message, fullWavFileName);
baseWavFileName = 'sinus1300.wav';
% efface sab baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, ~, ~] = audioread(fullWavFileName);
sound(waveFileData, Fs);
% soundsc(y,Fs,bits,range);
end
end
% errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
% fprintf('%s\n', errorMessage);
% WarnUser(errorMessage);
end
return; % from PlaySoundFile
sabrina lenglet
sabrina lenglet 2017 年 6 月 21 日
Hi it's work !!!! Thanks a lot

サインインしてコメントする。

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 10 日

1 投票

your_folder='E:\matlab';
d=dir([your_folder '\*.wav']);
f={d.name};
n=numel(f);
idx=randi(numel(f));
file=f{idx}
f(idx)=[];

2 件のコメント

jalal Ahmad
jalal Ahmad 2013 年 9 月 11 日
thank u it works
sabrina lenglet
sabrina lenglet 2017 年 6 月 14 日
How can I play this sound or file.wav ? thanks

サインインしてコメントする。

Walter Roberson
Walter Roberson 2013 年 9 月 10 日

0 投票

Yes. If your file names are in the cell array FileNames then
shuffleidx = randperm(length(fileNames));
for K = 1 : length(suffleidx)
thisfile = fileNames{shuffleidx(K)}
...
end

カテゴリ

ヘルプ センター および File ExchangeAudio I/O and Waveform Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by