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)?

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

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

Thanks very much for the quick response! However, I'm struggling to implement your suggestion.
Here's my code so far:
%Script to batch process mono wav files into stereo wav files
input_directory = uigetdir
output_directory = [uigetdir filesep]
extension = '*.wav'
files = dir(fullfile(input_directory,'*.wav'));
for i=1:length(files)
Name{i,1} = files(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)
%Try to get oringinal filename before _# and extension for naming stereo
%track
% [p, baseFileName] = fileparts(files(i).name); %Remove file extension
% leftChannel = audioread(fullFileName); %Need to add in concatenated _0
% rightChannel = audioread(fullFileName); %Need to add in concatenated _1
% Stereo = [leftChannel, rightChannel];
% audiowrite([output_directory baseFileName '_stereo.wav'], Stereo, 44100); %write stereo track
Running the above gives me two errors so far:
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Error in MonoToStereo (line 11)
uScore = cellfun(@(x) strfind(x,'_'),Name);
I am very new to Matlab scripting so some advice on how to go about concatenating the _#.wav portions would also be really helpful.
well playing around with it try
uScore = cellfun(@(x) strfind(x,'_'),Name,'UniformOutput',false)
% uScore = num2cell(uScore);
Fname = cellfun(@(x,y) x(1:y-1),Name,uScore,'UniformOutput',false)
Fname = unique(Fname)
for fileNum = 1:length(Fname)
leftchannel = audioread(fullfile(inputdir,[cell2mat(Fname(1)) '_1.wav']));
%etc.
end
perhaps my test case allowed the initial one to work out. however i made some dummy empty files with file names so i wasn't able to recreate your errors.
Thanks again! This is almost working!
With the following code I am able to get correct stereo files - as long as there are no "_" in the filename. Rather than looking for "_" to designate the uniquename, is it possible to strip the file extension and last character (0 or 1) in order to create the unique names? I often have filenames like:
Cond1_SN0_Fl_0
Cond1_SN0_Fl_1
Cond2_SN3_Fl_0
Cond2_SN3_Fl_1
So I need to strip the last 5 characters from each file in order to eventually create:
Cond1_SN0_Fl_stereo
and
Cond2_SN3_Fl_stereo
Current code:
%Script to batch process mono wav files into stereo wav files
input_directory = uigetdir
output_directory = [uigetdir filesep]
extension = '*.wav'
files = dir(fullfile(input_directory,'*.wav'));
for i=1:length(files)
Name{i,1} = files(i).name;
end
uScore = cellfun(@(x) strfind(x,'_'),Name,'UniformOutput',false)
Fname = cellfun(@(x,y) x(1:y-1),Name,uScore,'UniformOutput',false)
Fname = unique(Fname)
for j = 1:length(Fname)
leftChannel = audioread(fullfile(input_directory,[cell2mat(Fname(j)) '_0.wav']));
rightChannel = audioread(fullfile(input_directory,[cell2mat(Fname(j)) '_1.wav']));
Stereo = [leftChannel, rightChannel];
audiowrite([output_directory cell2mat(Fname(j)) '_stereo.wav'], Stereo, 44100); %write stereo track
end
well... i was using the _ as a marker but if all your files is #.wav we could have simply done something like this:
% uScore = cellfun(@(x) strfind(x,'_'),Name,'UniformOutput',false)
% Fname = cellfun(@(x,y) x(1:y-1),Name,uScore,'UniformOutput',false)
Fname = cellfun(@(x) x(1:end-5),Name,'UniformOutput',false)
Where we just lop off the last 5 characters in the file name. probably could have done this first too.
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
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)

Asked:

JLC
on 9 Jul 2014

Commented:

on 10 Jul 2014

Community Treasure Hunt

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

Start Hunting!