Skip to content

Commit

Permalink
Refactor wave creations in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed May 24, 2021
1 parent c20e86f commit 27a7766
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 49 deletions.
27 changes: 10 additions & 17 deletions tests/test_AudioClips.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
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"))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
21 changes: 4 additions & 17 deletions tests/test_ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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():
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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``
Expand Down
25 changes: 10 additions & 15 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
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():
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 27a7766

Please sign in to comment.