[Cuis-dev] Writing a SoundBuffer to a WAV or AIFF file is broken
David Gouge
davidgouge at pm.me
Mon Mar 2 06:30:26 PST 2026
It seems that I just found a small bug when playing around with the Sound package.
From a Workspace do:
Feature require: 'Sound'.
(PluckedSound default) storeWAVOnFileNamed: '/tmp/foo.wav'.
Exception raised.. Here's the offending stack:
SoundBuffer(Object)>>doesNotUnderstand: #asUtfBytes
UniFileStream>>nextPutAll:: aStringOrBytes
"Argument can be String, UnicodeString or ByteArray.
Write to this file. Both kinds of Strings are UTF-8 encoded."
| bytes |
aStringOrBytes isEmpty ifTrue: [ ^self ].
bytes := aStringOrBytes.
(aStringOrBytes is: #ByteArray) ifFalse: [
bytes := aStringOrBytes asUtf8Bytes]. <--- Exception raised here.
My naive newbie fix is as follows:
AbstractSound>>storeSampleCount: samplesToStore bigEndian: bigEndianFlag on: aBinaryStream
.. Clipped ..
'Storing audio...' displayProgressAt: Sensor mousePoint
from: 0 to: samplesToStore during: [:barBlock | | remaining out |
remaining := samplesToStore.
[remaining > 0] whileTrue: [
barBlock value: samplesToStore - remaining.
stereoBuffer primFill: 0. "clear the buffer"
self playSampleCount: (bufSize min: remaining) into: stereoBuffer startingAt: 1.
self isStereo
ifTrue: [out := stereoBuffer asByteArray] "<-- answer ByteArray instead of SoundBuffer"
ifFalse: [out := stereoBuffer extractLeftChannel asByteArray]. "<--- and here"
... Clipped ..
This has let me work around the bug but I'm a complete Cuis noob so take it FWIW.
Best Regards,
-Dave
Sent with [Proton Mail](https://proton.me/mail/home) secure email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20260302/55b44455/attachment.htm>
More information about the Cuis-dev
mailing list