27.2.17 Joining audio clips together
The splice
command joins two audio clips together with an overlap and optionally a crossfade.
-
splice takes two mandatory arguments and one or two optional arguments:
-
A, an audio clip.
- B, an audio clip with the same number of channels,
bit depth and sample rate as A.
- Optionally, len, a positive integer defining the overlap size
(by default, len is the minimum of 0.5· s, LA/2, and LB/2, where
LA and LB are the lengths of A and B in samples and s is the sample rate).
- Optionally, p, a real number defining the crossfade curve
(by default, p is computed automatically, see below).
- splice(A,B ⟨,len,p ⟩)
returns the audio clip obtained by splicing A and B with an overlap
of len samples and a crossfade which is computed using the function
f(x)=xp, x∈[0,1], for p>0.
- For positively correlated audio data on the overlap, it is best to set p=1.0
(constant gain crossfade), while for zero-correlated data p=0.5 (constant power crossfade)
works better. If p is not specified, it is computed automatically in [0.5,1.0]
such that p−0.5 is proportional to the absolute value of the cosine distance between
the overlapping data. Setting p=0 disables crossfading and thus the overlapped data is
simply added together.
Example
With the audio file gettysburg.wav downloaded from
here:
clip:=normalize(readwav("/home/luka/Downloads/gettysburg.wav"),-1) |
|
a sound clip with 387574 samples at 22050 Hz (16 bit, mono)
| | | | | | | | | | |
|
To remove the portion of the speech between 5.6 and 10.5 seconds,
splice the portions on the left and right by using an overlap of
size 0.25 seconds.
To visualize the audio and the selected portion, you can enter:
t1:=5.6:; t2:=10.5:; d:=duration(clip):; cfl:=0.25:;
rectangle(t1-i,t2-i,2/(t2-t1),display=pink+filled); plotwav(clip); |
Extract the portions from 0 to t1 and from t2 to the end
and splice them with an automatic crossfade:
snd:=splice(clip(0.0..t1),clip(t2..d),cfl) |
|
a sound clip with 274016 samples at 22050 Hz (16 bit, mono)
| | | | | | | | | | |
|
Use playsnd (see Section 27.2.14) to hear the result.