-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjp_maxvol.m
116 lines (80 loc) · 2.58 KB
/
jp_maxvol.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
function jp_maxvol(input_dirs, output_dirs, maxvol)
%JP_MAXVOL Increase maximum volume to a set amount.
%
% JP_MAXVOL(INPUTDIR, OUTPUTDIR, [MAXVOL]) where MAXVOL defaults to .97.
%
%
%
% From https://github.com/jpeelle/jp_matlab
if nargin < 2 || isempty(output_dirs)
output_dirs = input_dirs;
elseif ischar(output_dirs)
tmpout = output_dirs;
outputdirs = {};
for i=1:length(input_dirs)
output_dirs = {outputdirs{:} tmpout};
end
else
error('Not sure how to handle output_dirs');
end
if nargin < 3 || isempty(maxvol)
maxvol = .97;
end
verbose = 1;
% Make sure directories are cell arrays
if ischar(input_dirs) && size(input_dirs,1)==1
input_dirs = {input_dirs};
end
if ischar(output_dirs) && size(output_dirs,1)==1
output_dirs = {output_dirs};
end
% Make sure input directories exist
for i = 1:length(input_dirs)
if ~isdir(input_dirs{i})
error('Input directory %s not found.', input_dirs{i})
end
end
% Make sure the output directories exist
for i = 1:length(output_dirs)
if ~isdir(output_dirs{i})
mkdir(output_dirs{i});
end
end
% Get .wav files from each directory
% Go through D the first time to get the mean RMS
max_amplitude = 0;
if verbose > 0; fprintf('Looping through files to get info...'); end
num_wav = 0;
for i=1:length(input_dirs)
d = dir(input_dirs{i});
for j = 1:length(d)
fileName = d(j).name;
if length(fileName)>4 && strcmp(lower(fileName(end-3:end)),'.wav')
[y,fs] = audioread(fullfile(input_dirs{i},fileName));
num_wav = num_wav + 1;
if max(abs(y)) > max_amplitude
max_amplitude = max(abs(y));
end
end
end
end % going through input_dirs to get files
if verbose > 0; fprintf('done.\n Found %i files.\n', num_wav); end
% Loop through again to change the files
if verbose > 0; fprintf('Looping through files again to adjust info...'); end
g = maxvol./max_amplitude;
fprintf('Maximum absolute amplitude found was %.3f; multiplying each sound file by %.3f.\n', max_amplitude, g);
for i=1:length(input_dirs)
d = dir(input_dirs{i});
for j = 1:length(d)
fileName = d(j).name;
if length(fileName)>4 && strcmp(lower(fileName(end-3:end)),'.wav')
infile = fullfile(input_dirs{i}, fileName);
movefile = fullfile(input_dirs{i}, sprintf('OLD%s',fileName));
[y,fs] = audioread(infile);
y2 = y .* g;
outfile = fullfile(output_dirs{i}, fileName);
audiowrite(outfile, y2, fs)
end
end
end
fprintf('done.\n');