Skip to content

fix: AudioArrayClip() cannot process mono audio correctly - #2598

Open
AdairLi2504 wants to merge 1 commit into
Zulko:masterfrom
AdairLi2504:audioarrayclip-mono-support
Open

AdairLi2504 wants to merge 1 commit into
Zulko:masterfrom
AdairLi2504:audioarrayclip-mono-support

Conversation

@AdairLi2504

Copy link
Copy Markdown

It fixes the #2597 by the first way, and I've tested it by pytest and the original code in #2597

  • I have provided code that clearly demonstrates the bug and that only works correctly when applying this fix
  • I have added suitable tests demonstrating a fixed bug or new/changed feature to the test suite in tests/
  • I have properly documented new or changed features in the documentation or in the docstrings
  • I have properly explained unusual or unexpected code in the comments around it

@MohammediYunus

MohammediYunus commented Oct 7, 2026 •

Copy link
Copy Markdown

I checked this with a synthetic one-second, 440 Hz mono tone at 8 kHz. On master it exports as two seconds at 220 Hz; at 3ff5f1c the timing and pitch are correct. One detail I noticed: the exported WAV now has two channels, while the input is Nx1 and documented as mono.

Would you be open to preserving mono output by allocating the vectorized frame result with the input channel width instead of duplicating the input array?

result = np.zeros((len(t), self.array.shape[1]))

For that approach, the existing chunk assembly in to_soundarray also needs to concatenate along the time axis. Its mono hstack branch turns nine samples with buffersize=4 into shape (3, 3), while eight or ten samples raise a dimension mismatch. Using np.concatenate(chunks, axis=0) preserves Nx1 arrays and also handles function-generated one-dimensional mono and stereo.

A small regression worth keeping:

samples = np.arange(9, dtype=float).reshape(-1, 1) / 10
clip = AudioArrayClip(samples, fps=10)
assert clip.nchannels == 1
np.testing.assert_array_equal(clip.to_soundarray(buffersize=4), samples)

I tested the channel-preserving variant locally with mono/stereo frame reads, equal and unequal chunk sizes, and PCM export sample/channel counts. All 13 focused checks and the 16 existing AudioClips tests pass. I have not run the full MoviePy suite for this review.

Checked on macOS, Python 3.11 and NumPy 2.4.6.

@AdairLi2504
AdairLi2504 force-pushed the audioarrayclip-mono-support branch from 3ff5f1c to 537801b Compare October 8, 2026 12:18
@AdairLi2504

Copy link
Copy Markdown
Author

I checked this with a synthetic one-second, 440 Hz mono tone at 8 kHz. On master it exports as two seconds at 220 Hz; at 3ff5f1c the timing and pitch are correct. One detail I noticed: the exported WAV now has two channels, while the input is Nx1 and documented as mono.

Would you be open to preserving mono output by allocating the vectorized frame result with the input channel width instead of duplicating the input array?

result = np.zeros((len(t), self.array.shape[1]))

For that approach, the existing chunk assembly in to_soundarray also needs to concatenate along the time axis. Its mono hstack branch turns nine samples with buffersize=4 into shape (3, 3), while eight or ten samples raise a dimension mismatch. Using np.concatenate(chunks, axis=0) preserves Nx1 arrays and also handles function-generated one-dimensional mono and stereo.

A small regression worth keeping:

samples = np.arange(9, dtype=float).reshape(-1, 1) / 10
clip = AudioArrayClip(samples, fps=10)
assert clip.nchannels == 1
np.testing.assert_array_equal(clip.to_soundarray(buffersize=4), samples)

I tested the channel-preserving variant locally with mono/stereo frame reads, equal and unequal chunk sizes, and PCM export sample/channel counts. All 13 focused checks and the 16 existing AudioClips tests pass. I have not run the full MoviePy suite for this review.

Checked on macOS, Python 3.11 and NumPy 2.4.6.

Thank you. I have pushed a new commit to fix that by this way

@MohammediYunus

Copy link
Copy Markdown

Thanks for updating it! I reran the checks at 537801b. Mono frame reads and PCM export now preserve the single channel.

There’s one remaining case in to_soundarray: its mono branch still uses horizontal stacking when joining chunks. This returns (3, 3) rather than (9, 1):

import numpy as np
from moviepy import AudioArrayClip

samples = np.arange(9, dtype=float).reshape(-1, 1) / 10
clip = AudioArrayClip(samples, fps=10)
np.testing.assert_array_equal(clip.to_soundarray(buffersize=4), samples)

With 8 or 10 samples, the same call raises a dimension mismatch. Could you also concatenate those chunks along axis 0 and keep these three lengths as regressions? That preserves the time axis for both Nx1 arrays and one-dimensional function-generated mono.

The unchanged focused checks give 10 passes and those 3 failures on this revision. Tested with Python 3.11.15 and NumPy 2.4.6 on macOS; I haven’t run the full suite.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants