/
audio_align.m
53 lines (39 loc) · 1.72 KB
/
audio_align.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
function audio_align(audio_ref_path, audio_path, image_path, audio_write_path, video_write_path, TRtoTrim, tempRes)
% audio_align(audio_path, image_path, video_write_path)
% Takes one audio file and one image file and then does the alignment
% between the two and outputs to video file in video_write_path.
%
% audio_path: audio filepath (.wav)
% image_path: image filepath (.avi)
% video_write_path: video filepath (.avi)
%% Data loading
[audio_ref_data, sampling_f] = audioread(audio_ref_path);
[audio_data, sampling_f] = audioread(audio_path);
alignment_thresh = max(audio_ref_data) / 4; % Alignment Threshold to determine when scanner is "on"
%% Find begin and end for alignment.
% Super Naive Implementation Attempt
% Find the first time above thresh, call it "begin" and same for "end".
threshold_idxs = find(abs(audio_ref_data) > alignment_thresh);
if isempty(threshold_idxs)
warning('audio file is invalid!')
warning(['audio_path: ' audio_path])
warning(['image_path: ' image_path])
return;
end
begin_idx = threshold_idxs(1);
end_idx = threshold_idxs(end) + 1;
if begin_idx == 1 || abs(size(audio_ref_data, 1)) < 1
%Not worth aligning. throw out.
warning('audio data invalid. remember to turn on audio recording before scan!');
return;
end
end_idx = min(end_idx, size(audio_ref_data,1));
truncated_audio = audio_data(begin_idx:end_idx);
%% Trim the amount based on the TR.
trim_amt_s = (TRtoTrim) * tempRes
trim_audio_num = floor(trim_amt_s / (1/sampling_f))
truncated_audio = truncated_audio(trim_audio_num+1:end);
audiowrite(audio_write_path, truncated_audio, sampling_f);
system(strjoin(['ffmpeg -i ', image_path, ' -i ', audio_write_path, ...
' -c:a copy ', video_write_path]));
end