[Cuis-dev] Writing a SoundBuffer to a WAV or AIFF file is broken
Juan Vuletich
juan at cuis.st
Fri Mar 27 11:13:43 PDT 2026
Hi David,
While your code runs without error, the file produced has wrong byte
ordering and sounds wrong, at least on my Mac. I preferred simplifying
the method a bit. Just pushed the updated package to GitHub.
Thanks,
On 2026-03-02 11:30 AM, David Gouge via Cuis-dev wrote:
>
> 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.
>
--
Juan Vuletich
www.cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20260327/a63059cc/attachment.htm>
More information about the Cuis-dev
mailing list