From 493d429e3febd274ceb73ff47aa9474553dfcf9b Mon Sep 17 00:00:00 2001 From: mcc Date: Mon, 2 Oct 2023 15:47:29 -0400 Subject: [PATCH] Clearer error messages when write() is called with incorrectly formatted channels --- soundfile.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/soundfile.py b/soundfile.py index cc13192..dde887e 100644 --- a/soundfile.py +++ b/soundfile.py @@ -1332,10 +1332,11 @@ def _check_dtype(self, dtype): def _array_io(self, action, array, frames): """Check array and call low-level IO function.""" - if (array.ndim not in (1, 2) or - array.ndim == 1 and self.channels != 1 or - array.ndim == 2 and array.shape[1] != self.channels): - raise ValueError("Invalid shape: {0!r}".format(array.shape)) + if self.channels not in (1,2): + raise ValueError("Cannot convert: file has {0} channels, but only 1 or 2 is supported".format(self.channels)) + array_channels = 1 if array.ndim == 1 else array.shape[1] + if array_channels != self.channels: + raise ValueError("Invalid shape: {0!r} (Expected {1} channels, got {2})".format(array.shape, self.channels, array_channels)) if not array.flags.c_contiguous: raise ValueError("Data must be C-contiguous") ctype = self._check_dtype(array.dtype.name)