Skip to content

Commit

Permalink
Add 'multiply_volume' FX tests (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jan 22, 2021
1 parent 8cc3eeb commit b37c35c
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from moviepy import AudioClip, AudioFileClip, BitmapClip, ColorClip, VideoFileClip
from moviepy.audio.fx import audio_normalize, multiply_stereo_volume
from moviepy.audio.fx import audio_normalize, multiply_stereo_volume, multiply_volume
from moviepy.utils import close_all_clips
from moviepy.video.fx import (
blackwhite,
Expand Down Expand Up @@ -637,10 +637,54 @@ def test_audio_normalize_muted():
close_all_clips(locals())


def test_multiply_volume():
clip = AudioFileClip("media/crunching.mp3")
clip_array = clip.to_soundarray()

# stereo mute
clip_muted = multiply_volume(clip, 0)

left_channel_muted = clip_muted.to_soundarray()[:, 0]
right_channel_muted = clip_muted.to_soundarray()[:, 1]

z_channel = np.zeros(len(left_channel_muted))

assert np.array_equal(left_channel_muted, z_channel)
assert np.array_equal(right_channel_muted, z_channel)

# stereo level doubled
clip_doubled = multiply_volume(clip, 2)
clip_doubled_array = clip_doubled.to_soundarray()
left_channel_doubled = clip_doubled_array[:, 0]
right_channel_doubled = clip_doubled_array[:, 1]
expected_left_channel_doubled = clip_array[:, 0] * 2
expected_right_channel_doubled = clip_array[:, 1] * 2

assert np.array_equal(left_channel_doubled, expected_left_channel_doubled)
assert np.array_equal(right_channel_doubled, expected_right_channel_doubled)

# mono muted
sinus_wave = lambda t: [np.sin(440 * 2 * np.pi * t)]
mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
muted_mono_clip = multiply_volume(mono_clip, 0)
mono_channel_muted = muted_mono_clip.to_soundarray()

z_channel = np.zeros(len(mono_channel_muted))
assert np.array_equal(mono_channel_muted, z_channel)

mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
doubled_mono_clip = multiply_volume(mono_clip, 2)
mono_channel_doubled = doubled_mono_clip.to_soundarray()
d_channel = mono_clip.to_soundarray() * 2
assert np.array_equal(mono_channel_doubled, d_channel)

close_all_clips(locals())


def test_multiply_stereo_volume():
clip = AudioFileClip("media/crunching.mp3")

# mute
# stereo mute
clip_left_channel_muted = multiply_stereo_volume(clip, left=0)
clip_right_channel_muted = multiply_stereo_volume(clip, right=0, left=2)

Expand All @@ -652,22 +696,22 @@ def test_multiply_stereo_volume():
assert np.array_equal(left_channel_muted, z_channel)
assert np.array_equal(right_channel_muted, z_channel)

# double level
# stereo level doubled
left_channel_doubled = clip_right_channel_muted.to_soundarray()[:, 0]
d_channel = clip.to_soundarray()[:, 0] * 2
assert np.array_equal(left_channel_doubled, d_channel)
expected_left_channel_doubled = clip.to_soundarray()[:, 0] * 2
assert np.array_equal(left_channel_doubled, expected_left_channel_doubled)

# mono muted
sinus_wave = lambda t: [np.sin(440 * 2 * np.pi * t)]
mono_clip = AudioClip(sinus_wave, duration=2, fps=22050)
mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
muted_mono_clip = multiply_stereo_volume(mono_clip, left=0)
mono_channel_muted = muted_mono_clip.to_soundarray()

z_channel = np.zeros(len(mono_channel_muted))
assert np.array_equal(mono_channel_muted, z_channel)

# mono doubled
mono_clip = AudioClip(sinus_wave, duration=2, fps=22050)
mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
doubled_mono_clip = multiply_stereo_volume(
mono_clip, left=None, right=2
) # using right
Expand Down

0 comments on commit b37c35c

Please sign in to comment.