How can i pair files with almost the same filename in order to create stereo wav files from mono wav files (ideally as a batch)?

1 view (last 30 days)
I have a somewhat complicated task I would like to perform that is well beyond my novice level of experience with Matlab. I am using Matlab R2014a.
I use a recording system that creates 2 mono wav files from an acoustic manikin. This is usually done in a batch process across several conditions. The mono wav files are saved in a single directory and are named by the condition. What differentiates the files is the last character in the filename, before the extension - 0 designates the file as left ear, 1 designates right ear.
For example FirstCondition_0.wav FirstCondition_1.wav SecondCondition_0.wav SecondCondition_1.wav ClassicalMusic_0.wav ClassicalMusic_1.wav
What I'd like to do is create a script that can create stereo wav files from the pairs of recordings, as a batch process.
I can do it manually, but after some experiments there are a lot of files.
What I envision is using uigetdir to set the input directory and the output directory, then somehow use a loop to combine the pairs with filename_0.wav becoming (:,1) and filename_1.wav becoming (:,2) for each matched pair.
Does anyone have any suggestions, examples, or ideas?
Thanks!

Accepted Answer

Joseph Cheng
Joseph Cheng on 9 Jul 2014
since you have already the script to combine you can get the list of items by using something like this
list = dir('*.txt');
for i=1:length(list)
Name{i,1} = list(i).name;
end
uScore = cellfun(@(x) strfind(x,'_'),Name);
uScore = num2cell(uScore);
Fname = cellfun(@(x,y) x(1:y-1),Name,uScore,'UniformOutput',false)
unique(Fname)
This will get you the specific names where you can concatenate the '_0.wav' and the _1.wav to it before processing.
so get the list of files, do a for loop for the number of unique filename (before _) and concatenate the _#.wav for your script.
  7 Comments
JLC
JLC on 10 Jul 2014
Brilliant, that worked perfectly!
Here is my final script for anyone who comes across this:
%Script to batch process mono wav files into stereo wav files
input_directory = uigetdir; %select directory containing stereo files
output_directory = [uigetdir filesep]; %select directory to store mono files
%Look for audio files that are stored in the uncompressed *.WAV format
extension = '*.wav';
%Get the files
files = dir(fullfile(input_directory,'*.wav'));
%Loop through one file at a time to read in file names
for i=1:length(files)
Name{i,1} = files(i).name;
end
%Create unique name pairs by removing final 5 characters "#.wav"
Fname = cellfun(@(x) x(1:end-5),Name,'UniformOutput',false);
Fname = unique(Fname);
for j = 1:length(Fname)
leftChannel = audioread(fullfile(input_directory,[cell2mat(Fname(j)) '0.wav'])); %Read _0 to left channel
rightChannel = audioread(fullfile(input_directory,[cell2mat(Fname(j)) '1.wav'])); %Read _1 to right channel
Stereo = [leftChannel, rightChannel]; %Create stereo data
audiowrite([output_directory cell2mat(Fname(j)) 'stereo.wav'], Stereo, 44100); %write stereo track
%Report finishing stereo wav file writing
disp(['Finished writing ' cell2mat(Fname(j)) 'stereo.wav']);
end
Joseph Cheng
Joseph Cheng on 10 Jul 2014
Yeah, i guess i was too preoccupied to just see that we needed to just lop off the last 5. however what we initially tried was something that would be interesting to use if you need this with more complicated file names.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!