Skip to content

Commit 70eb67e

Browse files
committed
Fix exception due to incorrect output data length
1 parent e82d053 commit 70eb67e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

nbswave/audio.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ def __init__(
5454
self.output = np.empty(self._get_array_size(length), dtype="int32")
5555

5656
def _get_array_size(self, length_in_ms: float) -> int:
57-
return int(length_in_ms * (self.frame_rate / 1000.0) * self.channels)
57+
frame_count = length_in_ms * (self.frame_rate / 1000.0)
58+
array_size = frame_count * self.channels
59+
array_size_aligned = self._get_aligned_array_size(array_size)
60+
return array_size_aligned
61+
62+
def _get_aligned_array_size(self, length: int):
63+
"""Pads an array length to the appropriate data format."""
64+
align = self.sample_width * self.channels
65+
length_aligned = math.ceil(length / align) * align
66+
return length_aligned
5867

5968
def overlay(self, sound, position=0):
6069
sound_sync = self._sync(sound)
@@ -68,7 +77,7 @@ def overlay(self, sound, position=0):
6877

6978
output_size = len(self.output)
7079
if end > output_size:
71-
pad_length = end - output_size
80+
pad_length = self._get_aligned_array_size(end - output_size)
7281
self.output = np.pad(
7382
self.output, pad_width=(0, pad_length), mode="constant"
7483
)

0 commit comments

Comments
 (0)