Skip to content

Video editing with Python

Add tests for 'find_audio_period' (#1585)

Álvaro Mondéjar66a7584e4020
diff --git a/moviepy/audio/tools/cuts.py b/moviepy/audio/tools/cuts.py
index d7de2bcd..24ec220d 100644
--- a/moviepy/audio/tools/cuts.py
+++ b/moviepy/audio/tools/cuts.py
@@ -3,10 +3,18 @@ import numpy as np
def find_audio_period(clip, min_time=0.1, max_time=2, time_resolution=0.01):
"""Finds the period, in seconds of an audioclip.
- The beat is then given by bpm = 60/T
- min_time and max_time are bounds for the returned value, time_resolution
- is the numerical precision
+ Parameters
+ ----------
+
+ min_time : float, optional
+ Minimum bound for the returned value.
+
+ max_time : float, optional
+ Maximum bound for the returned value.
+
+ time_resolution : float, optional
+ Numerical precision.
"""
chunksize = int(time_resolution * clip.fps)
chunk_duration = 1.0 * chunksize / clip.fps
diff --git a/tests/test_AudioClips.py b/tests/test_AudioClips.py
index fe5dd4e7..40e0ce2d 100644
--- a/tests/test_AudioClips.py
+++ b/tests/test_AudioClips.py
@@ -14,12 +14,11 @@ from moviepy.audio.AudioClip import (
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.utils import close_all_clips
-from tests.test_helper import TMP_DIR
+from tests.test_helper import TMP_DIR, get_mono_wave, get_stereo_wave
def test_audioclip():
- make_frame = lambda t: [np.sin(440 * 2 * np.pi * t)]
- audio = AudioClip(make_frame, duration=2, fps=22050)
+ audio = AudioClip(get_mono_wave(440), duration=2, fps=22050)
audio.write_audiofile(os.path.join(TMP_DIR, "audioclip.mp3"), bitrate="16")
assert os.path.exists(os.path.join(TMP_DIR, "audioclip.mp3"))
@@ -58,11 +57,8 @@ def test_concatenate_audioclips_render():
"""Concatenated AudioClips through ``concatenate_audioclips`` should return
a clip that can be rendered to a file.
"""
- make_frame_440 = lambda t: [np.sin(440 * 2 * np.pi * t)]
- make_frame_880 = lambda t: [np.sin(880 * 2 * np.pi * t)]
-
- clip_440 = AudioClip(make_frame_440, duration=0.01, fps=44100)
- clip_880 = AudioClip(make_frame_880, duration=0.000001, fps=22050)
+ clip_440 = AudioClip(get_mono_wave(440), duration=0.01, fps=44100)
+ clip_880 = AudioClip(get_mono_wave(880), duration=0.000001, fps=22050)
concat_clip = concatenate_audioclips((clip_440, clip_880))
concat_clip.write_audiofile(os.path.join(TMP_DIR, "concatenate_audioclips.mp3"))
@@ -154,12 +150,11 @@ def test_CompositeAudioClip_by__init__():
def test_concatenate_audioclip_with_audiofileclip():
- # stereo A note
- make_frame = lambda t: np.array(
- [np.sin(440 * 2 * np.pi * t), np.sin(880 * 2 * np.pi * t)]
- ).T
-
- clip1 = AudioClip(make_frame, duration=1, fps=44100)
+ clip1 = AudioClip(
+ get_stereo_wave(left_freq=440, right_freq=880),
+ duration=1,
+ fps=44100,
+ )
clip2 = AudioFileClip("media/crunching.mp3")
concat_clip = concatenate_audioclips((clip1, clip2))
@@ -185,9 +180,7 @@ def test_concatenate_audiofileclips():
def test_audioclip_mono_max_volume():
- # mono
- make_frame_440 = lambda t: np.sin(440 * 2 * np.pi * t)
- clip = AudioClip(make_frame_440, duration=1, fps=44100)
+ clip = AudioClip(get_mono_wave(440), duration=1, fps=44100)
max_volume = clip.max_volume()
assert isinstance(max_volume, float)
assert max_volume > 0
diff --git a/tests/test_audiotools.py b/tests/test_audiotools.py
new file mode 100644
index 00000000..4bbf21fc
--- /dev/null
+++ b/tests/test_audiotools.py
@@ -0,0 +1,36 @@
+"""Audio tools tests."""
+
+import pytest
+
+from moviepy.audio.AudioClip import AudioClip, CompositeAudioClip
+from moviepy.audio.fx.multiply_volume import multiply_volume
+from moviepy.audio.tools.cuts import find_audio_period
+from moviepy.video.fx.loop import loop
+
+from tests.test_helper import get_mono_wave, get_stereo_wave
+
+
[email protected]("wave", ("mono", "stereo"))
+def test_find_audio_period(wave):
+ if wave == "mono":
+ wave1 = get_mono_wave(freq=400)
+ wave2 = get_mono_wave(freq=100)
+ else:
+ wave1 = get_stereo_wave(left_freq=400, right_freq=220)
+ wave2 = get_stereo_wave(left_freq=100, right_freq=200)
+ clip = CompositeAudioClip(
+ [
+ AudioClip(make_frame=wave1, duration=0.3, fps=22050),
+ multiply_volume(
+ AudioClip(make_frame=wave2, duration=0.3, fps=22050),
+ 0,
+ end_time=0.1,
+ ),
+ ]
+ )
+ loop_clip = loop(clip, 4)
+ assert round(find_audio_period(loop_clip), 6) == pytest.approx(0.29932, 0.1)
+
+
+if __name__ == "__main__":
+ pytest.main()
diff --git a/tests/test_ffmpeg_reader.py b/tests/test_ffmpeg_reader.py
index 9c866f3e..a5e7116a 100644
--- a/tests/test_ffmpeg_reader.py
+++ b/tests/test_ffmpeg_reader.py
@@ -17,7 +17,7 @@ from moviepy.video.io.ffmpeg_reader import (
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.VideoClip import BitmapClip
-from tests.test_helper import TMP_DIR
+from tests.test_helper import TMP_DIR, get_mono_wave
def test_ffmpeg_parse_infos():
@@ -107,19 +107,8 @@ def test_ffmpeg_parse_infos_multiple_audio_streams():
TMP_DIR, "ffmpeg_parse_infos_multiple_streams.mp4"
)
- make_frame_440 = lambda t: np.array(
- [
- np.sin(440 * 2 * np.pi * t),
- ]
- )
- make_frame_880 = lambda t: np.array(
- [
- np.sin(880 * 2 * np.pi * t),
- ]
- )
-
- clip_440 = AudioClip(make_frame_440, fps=22050, duration=0.01)
- clip_880 = AudioClip(make_frame_880, fps=22050, duration=0.01)
+ clip_440 = AudioClip(get_mono_wave(440), fps=22050, duration=0.01)
+ clip_880 = AudioClip(get_mono_wave(880), fps=22050, duration=0.01)
clip_440.write_audiofile(clip_440_filepath)
clip_880.write_audiofile(clip_880_filepath)
@@ -177,9 +166,7 @@ def test_ffmpeg_parse_infos_metadata():
os.remove(filepath)
# create video with 2 streams, video and audio
- audioclip = AudioClip(
- lambda t: np.sin(440 * 2 * np.pi * t), fps=22050
- ).with_duration(1)
+ audioclip = AudioClip(get_mono_wave(440), fps=22050).with_duration(1)
videoclip = BitmapClip([["RGB"]], fps=1).with_duration(1).with_audio(audioclip)
# build metadata key-value pairs which will be passed to ``ffmpeg_params``
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 66c642c1..cf35a919 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -52,7 +52,7 @@ from moviepy.video.fx import (
time_symmetrize,
)
-from tests.test_helper import TMP_DIR, get_test_video
+from tests.test_helper import TMP_DIR, get_mono_wave, get_stereo_wave, get_test_video
def test_accel_decel():
@@ -1473,13 +1473,12 @@ def test_audio_delay(duration, offset, n_repeats, decay):
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 = AudioClip(
+ make_frame=get_stereo_wave(left_freq=440, right_freq=880),
+ duration=duration,
+ fps=44100,
+ )
clip_array = clip.to_soundarray()
# stereo delayed clip
@@ -1547,11 +1546,9 @@ def test_audio_delay(duration, offset, n_repeats, decay):
)
def test_audio_fadein(sound_type, fps, clip_duration, fadein_duration):
if sound_type == "stereo":
- make_frame = lambda t: np.array(
- [np.sin(440 * 2 * np.pi * t), np.sin(160 * 2 * np.pi * t)]
- ).T.copy(order="C")
+ make_frame = get_stereo_wave(left_freq=440, right_freq=160)
else:
- make_frame = lambda t: np.sin(440 * 2 * np.pi * t)
+ make_frame = get_mono_wave(440)
clip = AudioClip(make_frame, duration=clip_duration, fps=fps)
new_clip = audio_fadein(clip, fadein_duration)
@@ -1608,11 +1605,9 @@ def test_audio_fadein(sound_type, fps, clip_duration, fadein_duration):
)
def test_audio_fadeout(sound_type, fps, clip_duration, fadeout_duration):
if sound_type == "stereo":
- make_frame = lambda t: np.array(
- [np.sin(440 * 2 * np.pi * t), np.sin(160 * 2 * np.pi * t)]
- ).T.copy(order="C")
+ make_frame = get_stereo_wave(left_freq=440, right_freq=160)
else:
- make_frame = lambda t: np.sin(440 * 2 * np.pi * t)
+ make_frame = get_mono_wave(440)
clip = AudioClip(make_frame, duration=clip_duration, fps=fps)
new_clip = audio_fadeout(clip, fadeout_duration)
diff --git a/tests/test_helper.py b/tests/test_helper.py
index 6fa76a82..deec503b 100644
--- a/tests/test_helper.py
+++ b/tests/test_helper.py
@@ -4,6 +4,8 @@ import functools
import sys
import tempfile
+import numpy as np
+
from moviepy.video.io.VideoFileClip import VideoFileClip
@@ -24,3 +26,21 @@ else:
@functools.lru_cache(maxsize=None)
def get_test_video():
return VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
+
+
[email protected]_cache(maxsize=None)
+def get_stereo_wave(left_freq=440, right_freq=220):
+ def make_stereo_frame(t):
+ return np.array(
+ [np.sin(left_freq * 2 * np.pi * t), np.sin(right_freq * 2 * np.pi * t)]
+ ).T.copy(order="C")
+
+ return make_stereo_frame
+
+
[email protected]_cache(maxsize=None)
+def get_mono_wave(freq=440):
+ def make_mono_frame(t):
+ return np.sin(freq * 2 * np.pi * t)
+
+ return make_mono_frame