-
Notifications
You must be signed in to change notification settings - Fork 4
/
concat_audio_files.m
92 lines (80 loc) · 2.95 KB
/
concat_audio_files.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
function [concat, fs, original_time_series, clipped_time_series] = concat_audio_files(audio_files, varargin)
%% DESCRIPTION:
%
% This function will load and concatenate all audio files. Parameters
% below apply some control over stimulus processing (e.g., trimming
% silence from beginning and end of sound, etc.).
%
% INPUT:
%
% audio_files: cell array of file names. All files should have the
% same sampling rate and the same number of channels
%
% Alternatively, this can be a cell array of time series.
% If this is the case, then each time series will be
% assumed to have the same sampling rate and number of
% channels.
%
% Parameters:
%
% 'fs': sampling rate. Only required if audio_files is a cell array of
% time series.
%
% 'remove_silence': bool, if set, silence at beginning and end of
% sounds will be removed before concatenating files.
% If this is set to TRUE, then amp_thresh must be
% defined below as well.
%
% 'amplitude_threshold': double, this is the amplitude threshold used to
% determine what is "silence" and what is not.
% Anything smaller than this value will be
% considered silence.
%
% 'mixer': Dx1 mixing matrix. Some of the helper functions require
% single channel inputs, so the mixer may need to be put in
% line to collapse the data into a single channel.
%
% OUTPUT:
%
% concat: concatenated time series.
%
% fs: sampling rate of concatenated time series.
%
% Development:
%
% None (yet)
%
% Christopher W Bishop
% University of Washington
% 10/14
%% GATHER PARAMETERS
d=varargin2struct(varargin{:});
%% DEFINE OUTPUT PARAMETERS
concat = [];
fs = [];
all_time_series = {};
% Load each file, remove silence if necessary
for i=1:numel(audio_files)
% We pass in d as a paramter list so SIN_loaddata can have access to
% the sampling rate information if it exists.
[time_series, tfs] = SIN_loaddata(audio_files{i}, d);
% Apply mixer to time_series
time_series = time_series*d.mixer;
% Sampling rate check
if isempty(fs)
fs = tfs;
elseif tfs ~= fs
error('Sampling rates do not match');
end % if isempty(fs)
% Remove silence if user tells us to
if d.remove_silence
time_series_clipped = threshclipaudio(time_series, d.amplitude_threshold, 'begin&end');
end % if d.remove_silence
% Concatenate time series
concat = [concat; time_series_clipped]; %#ok<*AGROW>
% Error check for out of range values
% if max(max(abs(time_series))) > 1, error('Value out of range'); end
% Append to all time series
clipped_time_series{i} = time_series_clipped;
original_time_series{i} = time_series;
end % for i=1:numel(audio_files)