Filter löschen
Filter löschen

How to generate all possible combinations of audio files?

2 Ansichten (letzte 30 Tage)
Hello all,
Sorry for such a basic question, but would you be able to help me with putting together a code which would combine together multiple audio files from one folder? I want to create new audio files (2 channels) by combining audio samples I already have (all 1 channel, same bit rates, same fs, but different length) in a way that each sample is combined with each sample (i.e., generate all possible combinations of samples).
For example: sample1 + sample2, sample1 + sample3, sample2 + sample3...
This is how I read them into my workspace:
myFiles = dir(fullfile('MATLAB\SAMPLEsounds\*.wav'));
for i = 1:size(myFiles, 1)
[audioIn,fs] = audioread(['MATLAB\SAMPLEsounds\' myFiles(i).name]);
I'm struggling with creating a code which would generate all possible combinations of sounds in that folder, add them together (with zero-padding the difference in length) and save them as new wav files.
I tried these to generate all possible combinations:
combinations = nchoosek(SoundNames, 2);
but this gives me only the list of sound names, not actual sounds; and
combinations = nchoosek(audioIn, 2);
bu this doesn't accept audio file as an input argument and gives the following error:
Error using zeros. Requested 1514508166x2 (22.6GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
Since they are not always the same length, I wanted to use catpad to add silence to the shorter ones, but again, I don't know how to feed 'each with each sample' into the function.
soundAdded(number) = catpad(2, sound1, sound2);
Could you please give some suggestions how to combine all these things together? I would appreciate your advice.

Akzeptierte Antwort

Spencer Chen
Spencer Chen am 13 Feb. 2020
If you have to use nchoose(), then something like this would work:
combinations = nchoosek(SoundNames, 2);
for i = 1:size(combinations,1)
% Load and add sound from combinations{i,1} and combinations{i,2}
I would personally do the nested for-loop:
for i = 1:numel(myFiles)
for j = (i+1):numel(myFiles)
% Load and add sound from myFiles(i).name and myFiles(j).name
But to be most efficient, I'll do a first pass to load all sound files (given we've got enought RAM), then do the nested for-loop for combining the sound.
I don't have catpad installed, so can't test it, but I can guess what it does. To hone in on my Matlab skills, I would personally just write the code to do this operation. It's not too hard, and it's a good exercise.
  1 Kommentar
Magdalena Kachlicka
Magdalena Kachlicka am 28 Mär. 2020
Hi Spencer,
Thanks for your suggestions and sorry for such a late response! Still, It might be useful for someone.
Yes, catpad virtually adds up together the two audio files by zero-padding the difference in their length.
Nested for-loop did the job, but with more files, my computer couldn't handle it. In the end, I loaded the audio files with one loop and then combined the sounds with the separate loop.
pair_index = permn([1:length(audiofiles)], 2);
for i = 1:min(length(pair_index))
 soundAdded(number) = catpad(2, sound1, sound2);  

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Audio I/O and Waveform Generation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by