在 matlab / octave 中将多个不同长度的音频文件交叉淡入淡出
Cross fading multiple audio files of different lengths together in matlab / octave
你好我正在尝试将几个音频文件交叉淡入淡出(大约 40 个文件,长度在 30 到 54 秒或更长时间之间变化)。我正在使用 Octave 3.8.1(类似于 matlab)。这些文件都有不同的文件长度。我发现一些代码仅在文件长度相同且仅交叉淡入淡出两个文件时才有效 cross fade code link。如何交叉淡入淡出具有不同文件长度的多个文件? 所以 S2 将是 S2=rand(911,1) + 1;或 S3=rand(932,1) + 1;或 S4=rand(654,1);等...
S1 = rand(1000,1);
S2 = rand(1000,1) + 1; % so S2 would be S2=rand(911,1)
%\ cross-fade over last 200 elements
n = 200;
W = linspace(1,0,n)'; %'
S1(end-n+1:end) = S1(end-n+1:end).*W;
S2(1:n) = S2(1:n).*(1-W);
S12 = zeros(size(S1,1) + size(S2,1) - n, 1);
S12(1:size(S1,1)) = S1;
S12(end-size(S1,1)+1:end) = S12(end-size(S1,1)+1:end) + S2;
在这里我将交叉淡入淡出两个窦。第一个为 1kHz,第二个为 2.2kHz。第一个正弦长 10 秒,第二个 15 秒。
我将在 6 秒后开始交叉渐变,渐变需要 4 秒,所以最终长度为 6+4+15-4 = 21 秒长
## use FS=44800 to hear it
FS = 44800;
t1 = linspace (0, 10, FS * 10);
t2 = linspace (0, 15, FS * 15);
## crossfade in 4s
cl = 4 * FS;
c = linspace (0, 1, cl);
a1 = 0.5 * sin (2 * pi * 1e3 * t1); # 1kHz
a2 = 0.9 * sin (2 * pi * 2.2e3 * t2); # 2.2kHz
## 100% a1 from 0..6s
s1 = a1(1:end-cl);
## crossfade a1 to a2 in 4s
s2 = a1(end-cl+1:end) .* (1-c) + a2(1:cl) .* c;
## 100% a2 for 11s
s3 = a2(cl+1:end);
## concatenate them
s = [s1 s2 s3];
t = linspace (0, numel(s)/FS, numel (s));
subplot (2, 1, 1);
plot (t , s)
xlabel ("t [s]")
grid on
wavwrite (s, FS, "out.wav");
## plot the FFT
f = linspace (0, FS/2, numel (s)/2);
subplot (2, 1, 2);
plot (f, abs (fft (s))(1:numel(s)/2));
grid on
xlabel ("f [Hz]")
这里是频谱图。你可以清楚地看到交叉淡入淡出
你好我正在尝试将几个音频文件交叉淡入淡出(大约 40 个文件,长度在 30 到 54 秒或更长时间之间变化)。我正在使用 Octave 3.8.1(类似于 matlab)。这些文件都有不同的文件长度。我发现一些代码仅在文件长度相同且仅交叉淡入淡出两个文件时才有效 cross fade code link。如何交叉淡入淡出具有不同文件长度的多个文件? 所以 S2 将是 S2=rand(911,1) + 1;或 S3=rand(932,1) + 1;或 S4=rand(654,1);等...
S1 = rand(1000,1);
S2 = rand(1000,1) + 1; % so S2 would be S2=rand(911,1)
%\ cross-fade over last 200 elements
n = 200;
W = linspace(1,0,n)'; %'
S1(end-n+1:end) = S1(end-n+1:end).*W;
S2(1:n) = S2(1:n).*(1-W);
S12 = zeros(size(S1,1) + size(S2,1) - n, 1);
S12(1:size(S1,1)) = S1;
S12(end-size(S1,1)+1:end) = S12(end-size(S1,1)+1:end) + S2;
在这里我将交叉淡入淡出两个窦。第一个为 1kHz,第二个为 2.2kHz。第一个正弦长 10 秒,第二个 15 秒。
我将在 6 秒后开始交叉渐变,渐变需要 4 秒,所以最终长度为 6+4+15-4 = 21 秒长
## use FS=44800 to hear it
FS = 44800;
t1 = linspace (0, 10, FS * 10);
t2 = linspace (0, 15, FS * 15);
## crossfade in 4s
cl = 4 * FS;
c = linspace (0, 1, cl);
a1 = 0.5 * sin (2 * pi * 1e3 * t1); # 1kHz
a2 = 0.9 * sin (2 * pi * 2.2e3 * t2); # 2.2kHz
## 100% a1 from 0..6s
s1 = a1(1:end-cl);
## crossfade a1 to a2 in 4s
s2 = a1(end-cl+1:end) .* (1-c) + a2(1:cl) .* c;
## 100% a2 for 11s
s3 = a2(cl+1:end);
## concatenate them
s = [s1 s2 s3];
t = linspace (0, numel(s)/FS, numel (s));
subplot (2, 1, 1);
plot (t , s)
xlabel ("t [s]")
grid on
wavwrite (s, FS, "out.wav");
## plot the FFT
f = linspace (0, FS/2, numel (s)/2);
subplot (2, 1, 2);
plot (f, abs (fft (s))(1:numel(s)/2));
grid on
xlabel ("f [Hz]")
这里是频谱图。你可以清楚地看到交叉淡入淡出