Skip to content

Video editing with Python

Add 'audio_delay' FX (#1481)

Álvaro Mondéjar6e996533c75c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb35abaa..6f8389ed 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `video.io.ffmpeg_reader.ffmpeg_parse_infos` returns data from all streams by FFmpeg inputs in attribute `inputs` [\#1466](https://github.com/Zulko/moviepy/pull/1466)
- `video.io.ffmpeg_reader.ffmpeg_parse_infos` returns metadata of the container in attribute `metadata` [\#1466](https://github.com/Zulko/moviepy/pull/1466)
- `center`, `translate` and `bg_color` arguments to `video.fx.rotate` [\#1474](https://github.com/Zulko/moviepy/pull/1474)
+- `audio.fx.audio_delay` FX [\#1481](https://github.com/Zulko/moviepy/pull/1481)
### Changed <!-- for changes in existing functionality -->
- Lots of method and parameter names have been changed. This will be explained better in the documentation soon. See https://github.com/Zulko/moviepy/pull/1170 for more information. [\#1170](https://github.com/Zulko/moviepy/pull/1170)
diff --git a/docs/ref/audiofx.rst b/docs/ref/audiofx.rst
index 52bed443..4edb6245 100644
--- a/docs/ref/audiofx.rst
+++ b/docs/ref/audiofx.rst
@@ -39,6 +39,7 @@ the module ``audio.fx`` is loaded as ``afx`` and you can use ``afx.multiply_volu
:toctree: audiofx
:nosignatures:
+ audio_delay
audio_fadein
audio_fadeout
audio_normalize
diff --git a/docs/ref/audiofx/moviepy.audio.fx.all.audio_delay.rst b/docs/ref/audiofx/moviepy.audio.fx.all.audio_delay.rst
new file mode 100644
index 00000000..043714bb
--- /dev/null
+++ b/docs/ref/audiofx/moviepy.audio.fx.all.audio_delay.rst
@@ -0,0 +1,6 @@
+moviepy.audio.fx.all.audio_delay
+================================
+
+.. currentmodule:: moviepy.audio.fx.all
+
+.. autofunction:: audio_delay
\ No newline at end of file
diff --git a/moviepy/audio/fx/__init__.py b/moviepy/audio/fx/__init__.py
index 1d16ddb9..38951a1c 100644
--- a/moviepy/audio/fx/__init__.py
+++ b/moviepy/audio/fx/__init__.py
@@ -1,5 +1,6 @@
# import every video fx function
+from moviepy.audio.fx.audio_delay import audio_delay
from moviepy.audio.fx.audio_fadein import audio_fadein
from moviepy.audio.fx.audio_fadeout import audio_fadeout
from moviepy.audio.fx.audio_loop import audio_loop
@@ -9,6 +10,7 @@ from moviepy.audio.fx.multiply_volume import multiply_volume
__all__ = (
+ "audio_delay",
"audio_fadein",
"audio_fadeout",
"audio_left_right",
diff --git a/moviepy/audio/fx/audio_delay.py b/moviepy/audio/fx/audio_delay.py
new file mode 100644
index 00000000..845ce21c
--- /dev/null
+++ b/moviepy/audio/fx/audio_delay.py
@@ -0,0 +1,56 @@
+import numpy as np
+
+from moviepy.audio.AudioClip import CompositeAudioClip
+from moviepy.audio.fx.multiply_volume import multiply_volume
+from moviepy.decorators import audio_video_fx
+
+
+@audio_video_fx
+def audio_delay(clip, offset=0.2, n_repeats=8, decay=1):
+ """Repeats audio certain number of times at constant intervals multiplying
+ their volume levels using a linear space in the range 1 to ``decay`` argument
+ value.
+
+ Parameters
+ ----------
+
+ offset : float, optional
+ Gap between repetitions start times, in seconds.
+
+ n_repeats : int, optional
+ Number of repetitions (without including the clip itself).
+
+ decay : float, optional
+ Multiplication factor for the volume level of the last repetition. Each
+ repetition will have a value in the linear function between 1 and this value,
+ increasing or decreasing constantly. Keep in mind that the last repetition
+ will be muted if this is 0, and if is greater than 1, the volume will increase
+ for each repetition.
+
+ Examples
+ --------
+
+ >>> from moviepy import *
+ >>> videoclip = AudioFileClip('myaudio.wav').fx(
+ ... audio_delay, offset=.2, n_repeats=10, decayment=.2
+ ... )
+
+ >>> # stereo A note
+ >>> make_frame = lambda t: np.array(
+ ... [np.sin(440 * 2 * np.pi * t), np.sin(880 * 2 * np.pi * t)]
+ ... ).T
+ ... clip = AudioClip(make_frame=make_frame, duration=0.1, fps=44100)
+ ... clip = audio_delay(clip, offset=.2, n_repeats=11, decay=0)
+ """
+ decayments = np.linspace(1, max(0, decay), n_repeats + 1)
+ return CompositeAudioClip(
+ [
+ clip.copy(),
+ *[
+ multiply_volume(
+ clip.with_start((rep + 1) * offset), decayments[rep + 1]
+ )
+ for rep in range(n_repeats)
+ ],
+ ]
+ )
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 42dad6d8..1aed0d25 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -13,7 +13,12 @@ from moviepy import (
VideoClip,
VideoFileClip,
)
-from moviepy.audio.fx import audio_normalize, multiply_stereo_volume, multiply_volume
+from moviepy.audio.fx import (
+ audio_delay,
+ audio_normalize,
+ multiply_stereo_volume,
+ multiply_volume,
+)
from moviepy.utils import close_all_clips
from moviepy.video.fx import (
blackwhite,
@@ -928,5 +933,89 @@ def test_multiply_stereo_volume():
close_all_clips(locals())
+ ("duration", "offset", "n_repeats", "decay"),
+ (
+ (0.1, 0.2, 11, 0),
+ (0.4, 2, 5, 2),
+ (0.5, 0.6, 3, -1),
+ (0.3, 1, 7, 4),
+ ),
+)
+def test_audio_delay(duration, offset, n_repeats, decay):
+ """Check that creating a short pulse of audio, the delay converts to a sound
+ with the volume level in the form `-_-_-_-_-`, being `-` pulses expressed by
+ `duration` argument and `_` being chunks of muted audio. Keep in mind that this
+ way of test the FX only works if `duration <= offset`, but as does not make sense
+ create a delay with `duration > offset`, this is enough for our purposes.
+
+ Note that decayment values are not tested here, but are created using
+ `multiply_volume`, should be OK.
+ """
+ # limits of this test
+ assert n_repeats > 0 # some repetition, if not does not make sense
+ assert duration <= offset # avoid wave distorsion
+ assert not offset * 1000000 % 2 # odd offset -> no accurate muted chunk size
+
+ # stereo A note
+ make_frame = lambda t: np.array(
+ [np.sin(440 * 2 * np.pi * t), np.sin(880 * 2 * np.pi * t)]
+ ).T.copy(order="C")
+
+ # stereo audio clip
+ clip = AudioClip(make_frame=make_frame, duration=duration, fps=44100)
+ clip_array = clip.to_soundarray()
+
+ # stereo delayed clip
+ delayed_clip = audio_delay(clip, offset=offset, n_repeats=n_repeats, decay=decay)
+ delayed_clip_array = delayed_clip.to_soundarray()
+
+ # size of chunks with audios
+ sound_chunk_size = clip_array.shape[0]
+ # muted chunks size
+ muted_chunk_size = int(sound_chunk_size * offset / duration) - sound_chunk_size
+
+ zeros_expected_chunk_as_muted = np.zeros((muted_chunk_size, 2))
+
+ decayments = np.linspace(1, max(0, decay), n_repeats)
+
+ for i in range(n_repeats + 1): # first clip, is not part of the repeated ones
+
+ if i == n_repeats:
+ # the delay ends in sound, so last muted chunk does not exists
+ break
+
+ # sound chunk
+ sound_start_at = i * sound_chunk_size + i * muted_chunk_size
+ sound_ends_at = sound_start_at + sound_chunk_size
+
+ # first sound chunk
+ if i == 0:
+ assert np.array_equal(
+ delayed_clip_array[:, :][sound_start_at:sound_ends_at],
+ multiply_volume(clip, decayments[i]).to_soundarray(),
+ )
+
+ # muted chunk
+ mute_starts_at = sound_ends_at + 1
+ mute_ends_at = mute_starts_at + muted_chunk_size
+
+ assert np.array_equal(
+ delayed_clip_array[:, :][mute_starts_at:mute_ends_at],
+ zeros_expected_chunk_as_muted,
+ )
+
+ # check muted bounds
+ assert not np.array_equal(
+ delayed_clip_array[:, :][mute_starts_at - 1 : mute_ends_at],
+ zeros_expected_chunk_as_muted,
+ )
+
+ assert not np.array_equal(
+ delayed_clip_array[:, :][mute_starts_at : mute_ends_at + 1],
+ zeros_expected_chunk_as_muted,
+ )
+
+
if __name__ == "__main__":
pytest.main()