forked from MarineBioAcousticsRC/Triton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimatewav_dir.m
272 lines (252 loc) · 10 KB
/
decimatewav_dir.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function decimatewav_dir(wavType)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% decimatexwav_dir.m
%
% Decimate a directory of wav, flac, or xwav files.
%
% Parameters:
% wavType - a string that is either 'wav' or 'xwav' depending on the
% type of file you are decimating.
% - works with flac files, treated similar to 'wav'
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global PARAMS
% get input directory with only x.wav files
%
ii = 1;
PARAMS.ddir = 'C:\'; % default directory
PARAMS.idir{ii} = uigetdir(PARAMS.ddir,['Select Directory ',num2str(ii),...
' with only ' wavType ' files']);
% if the cancel button is pushed, then no file is loaded so exit this script
if strcmp(num2str(PARAMS.idir{ii}),'0')
disp_msg('Canceled Button Pushed - no directory for PSD output file')
return
else
disp_msg('Input file directory : ')
disp_msg([PARAMS.idir{ii}])
% disp(' ')
end
% get info on xwav/wav files in dir
if strcmp(wavType,'wav')
d = dir(fullfile(PARAMS.idir{ii},['*.' wavType])); % directory info
if isempty(d)
d = dir(fullfile(PARAMS.idir{ii},'*.flac')); % maybe flac files?
if ~isempty(d)
wavType = 'flac'; % if it is flac files, update waveType
end
end
else
d = dir(fullfile(PARAMS.idir{ii},['*.' wavType])); % directory info
end
PARAMS.fname{ii} = char(d.name); % file names
fnsz = size(PARAMS.fname{ii});
PARAMS.nfiles{ii} = fnsz(1); % number of files in directory
disp_msg(['Number of ' wavType ' files in Input file directory is ',...
num2str(PARAMS.nfiles{ii})])
PARAMS.inpath = [PARAMS.idir{ii},'\'];
% first file's sample rate
PARAMS.infile = deblank(PARAMS.fname{ii}(1,:));
if strcmp(wavType,'wav')
PARAMS.ftype = 1; % files are xwavs
rdwavhd % get datafile info
elseif strcmp(wavType, 'flac')
PARAMS.ftype = 3;
% pull relevent info that would come vrom rdwavhd
info = audioinfo([PARAMS.inpath PARAMS.infile]);
PARAMS.nch = info.NumChannels; % Number of Channels
PARAMS.fs = info.SampleRate; % Sampling Rate(samples/second)
else
PARAMS.ftype = 2;
rdxwavhd
end
% get user input decimation factor
PARAMS.df = 100; % initial decimation factor
% user input dialog box
prompt={'Enter Decimation Factor (integer) : '};
def={num2str(PARAMS.df)};
dlgTitle=[num2str(PARAMS.fs),' = Original Sample Rate',];
lineNo=1;
AddOpts.Resize='on';
AddOpts.WindowStyle='normal';
AddOpts.Interpreter='tex';
in=inputdlg(prompt,dlgTitle,lineNo,def,AddOpts);
if length(in) == 0 % if cancel button pushed
return
end
% decimation factor
PARAMS.df = str2num(deal(in{1}));
% need to check that df is integer
if PARAMS.df - floor(PARAMS.df) ~= 0
disp_msg([num2str(PARAMS.df),' is not an integer - try again'])
return
end
% get output directory for decimated x.wav files
%
PARAMS.odir{ii} = uigetdir(PARAMS.ddir,['Select Directory ',num2str(ii),...
' for Output Decimated ' wavType ' files']);
% if the cancel button is pushed, then no file is loaded so exit this script
if strcmp(num2str(PARAMS.odir{ii}),'0')
disp_msg('Canceled Button Pushed - no directory for PSD output file')
return
else
disp_msg('Output decimated file directory : ')
disp_msg([PARAMS.odir{ii}])
% disp(' ')
end
PARAMS.outpath = [PARAMS.odir{ii},'\'];
disp_msg('This takes a while, please wait')
tic % start stopwatch timer
h = loadbar([' Decimating ' wavType ' Files ']);
total = PARAMS.nfiles{ii};
if strcmp(wavType,'wav')
extension_size = 4;
elseif strcmp(wavType,'flac')
extension_size = 5;
elseif strcmp(wavType, 'xwav')
extension_size = 6;
end
% loop over the files and
% get header info, start time, data byte loc and byte length
for jj = 1:PARAMS.nfiles{ii}
disp_msg(['File Number ', num2str(jj)])
% these needed for rdxwavhd
PARAMS.infile = deblank(PARAMS.fname{ii}(jj,:)); % get file names sequentally
PARAMS.outfile = [PARAMS.infile(1:length(PARAMS.infile)-extension_size),'.d',...
num2str(PARAMS.df),'.' wavType];
PARAMS.xhd.dSubchunkSize = [];
if strcmp(wavType, 'wav') || strcmp(wavType, 'flac')
% rdwavhd % this used to check df but now that is checked above
%Decimating wav files is easy
% [data,fs,nbits]=wavread([PARAMS.inpath,PARAMS.infile],'native');
[data,fs] = audioread([PARAMS.inpath,PARAMS.infile], 'native');
data = double(data);
info = audioinfo([PARAMS.inpath,PARAMS.infile]);
nbits = info.BitsPerSample;
nch = info.NumChannels;
% odata = decimate(double(data),PARAMS.df);
if nch == 1
odata = decimate(double(data),PARAMS.df);
else
for m = 1:nch
odata(:,m) = decimate(double(data(:,m)),PARAMS.df);
end
end
new_fs = fs/PARAMS.df;
if nbits == 16
% wavwrite(int16(odata), new_fs, nbits, [PARAMS.outpath,PARAMS.outfile]);
audiowrite([PARAMS.outpath,PARAMS.outfile],int16(odata),new_fs,'BitsPerSample',nbits);
elseif nbits == 24
% wavwrite(int32(odata), new_fs, nbits, [PARAMS.outpath,PARAMS.outfile]);
audiowrite([PARAMS.outpath,PARAMS.outfile],int32(odata),new_fs,'BitsPerSample',nbits);
elseif nbits == 32
% wavwrite(int32(odata), new_fs, nbits, [PARAMS.outpath,PARAMS.outfile]);
audiowrite([PARAMS.outpath,PARAMS.outfile],int32(odata),new_fs,'BitsPerSample',nbits);
else
disp_msg('Error: bit size not supported')
disp_msg(['Nbits = ', num2str(nbits)])
return
end
clear odata
pcntDone = (jj-1)/total;
loadbar(['Calculating, ', num2str(int8(pcntDone*100)),...
'% complete'], h, pcntDone)
continue
else
rdxwavhd
wrxwavhd(2)
end
% if PARAMS.nch == 1
% nsamp = 15e6; % number of samples to read for each decimation
% % also the number of samples in each hrp raw file
% elseif PARAMS.nch == 4
% nsamp = 3.596e6; % number of samples per channel to read for decimation
% else
% disp_msg('Error : number of channels not 1 or 4')
% disp_msg(['PARAMS.nch = ',num2str(PARAMS.nch)])
% end
%
% total_samples = PARAMS.xhd.dSubchunkSize / (PARAMS.samp.byte * PARAMS.nch);
%
% dnf = total_samples /nsamp; % number of decimations -- floating point
%
% dn = floor(dnf); % integer number of decimations
% drem = dnf - dn; % remainder (percentage) number of decimations
% if drem ~= 0
% disp_msg(['remainder of decimated samples ', num2str(drem)])
% end
% if (drem > 0) % most typical case
% dn = dn + 1;
% nsampLast = floor(nsamp * drem);
% elseif drem == 0
% disp_msg('all decimations same size')
% nsampLast = nsamp;
% elseif drem < 0
% disp_msg('error -- not possible')
% end
dn = PARAMS.xhd.NumOfRawFiles;
disp_msg(['Number of decimations : ',num2str(dn)])
% main loop to read in data from xwav, decimate, then write out to new xwav
% file
fid = fopen([PARAMS.inpath,PARAMS.infile],'r');
fod = fopen([PARAMS.outpath,PARAMS.outfile],'a'); % open as append, don't need to fseek
% total = total + dn;
count = 0;
switch wavType % maybe change this in the future to one for loop with one switch/case for di == 1
case 'wav'
for di = 1:dn
if di == dn
nsamp = nsampLast; % number of samples for last decimation
end
fseek(fid,PARAMS.xhd.dataIndex + (di-1)*nsamp,'bof');
% read the data
data = fread(fid,nsamp,precision);
%decimate and write
decimatedData = decimate(data,PARAMS.df); % decimated to a double which equals 32 bits
fwrite(fod,decimatedData,precision);
count = count + 1;
pcntDone = ((jj-1) + count/dn)/total;
loadbar(['Calculating, ',num2str(int8(pcntDone*100)),...
'% complete'],h, pcntDone)
end
case 'flac'
% this never gets called because of continue after simple
% decimation for wav and flac above.
case 'x.wav'
for di = 1:dn
% if di == dn
% nsamp = nsampLast; % number of samples for last decimation
% end
nsamp = PARAMS.xhd.byte_length(di) ./ (PARAMS.samp.byte * PARAMS.nch);
if rem(nsamp,PARAMS.df) ~= 0
nsamp = nsamp - rem(nsamp,PARAMS.df);
end
% jump over header and the number of decimations done so far...
% fseek(fid,PARAMS.xhd.byte_loc(1) +...
% (di-1)*nsamp*PARAMS.nch*PARAMS.samp.byte,'bof');
fseek(fid,PARAMS.xhd.byte_loc(di),'bof');
% read the data
data = fread(fid,[PARAMS.nch,nsamp],'int16');
%decimate and write
if PARAMS.nch == 1
fwrite(fod,decimate(data,PARAMS.df),'int16');
else
for m = 1:PARAMS.nch
ddata(m,:) = decimate(data(m,:),PARAMS.df);
end
fwrite(fod,ddata,'int16');
end
count = count + 1;
pcntDone = ((jj-1) + count/dn)/total;
loadbar(['Calculating, ',num2str(int8(pcntDone*100)),...
'% complete'],h, pcntDone)
end
end
fclose(fid);
fclose(fod);
% count = count+1;
% pcntDone = count/total;
% loadbar(['Calculating, ',num2str(int8(pcntDone*100)),'% complete'],h, pcntDone)
end
toc
close(h)