Sorry, for the bug in the minimal working example - it didn't updated buffer length counter. Here is the fixed version.
Incorrect work of sox_read function with In-memory output buffers