New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wave.py does not always write proper length in header #55335
Comments
wave.py does not always honor the sampwidth setting, especially on little endian machines. If sampwidth is not one and big_endian is not set, then datawritten will not be muliplied by sampwidth, causing the header to be incorrect, and the file to appear to contain less data than it chould. |
I agree that the _sampwidth multiplier is needed regardless of endianness. The simplest option would be to pull the _datawritten statement out of the alternation, making the code read if self._sampwidth > 1 and big_endian:
import array
data = array.array(_array_fmts[self._sampwidth], data)
data.byteswap()
data.tofile(self._file)
else:
self._file.write(data)
self._datawritten = self._datawritten + len(data) * self._sampwidth Note: while _sampwidth is initialized to 0, _ensure_header_written() checks that it is not 0, and it is used elsewhere as a divisor. The above adds a usually unneeded multiply by 1, but the alternative requires duplication of _file.write and two _datawritten statements if self._sampwidth > 1:
if big_endian:
import array
data = array.array(_array_fmts[self._sampwidth], data)
data.byteswap()
data.tofile(self._file)
else: # little_endian
self._file.write(data)
self._datawritten = self._datawritten + len(data) * self._sampwidth
else: # _sampwidth == 1
self._file.write(data)
self._datawritten = self._datawritten + len(data) This module is new to me. Can you think of any way to test this issue, perhaps by writing to StringIO file? This is not a heavily tested module ;-) In 3.3, the openfp synonym for open could perhaps be deprecated. |
Yep, your solution is better. I can provide some text files (lists of numbers) and two programs, wave2text.py and text2wav.py. These are the programs I wrote that found this issue in the first place. Add a few checks and you could run text2wave.py and then wave2text.py and verify that the results of these two steps match the original text files. I look forward to working on this module with you. Unfortunately the time frame would have to be sometime in 2011, as I am currently very busy. |
I don't see anything wrong in current code. In first alternation data is an array of sampwidth-sized items and the number of written bytes is len(data) * self._sampwidth. In second alternation data is raw bytes object and the number of written bytes is just len(data). Could you please provide a sample script which exposes the wrong behavior? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: