我正在Squeak的一个小项目中工作,遇到一个问题:我无法获取WAV文件以正确解码。
这是我目前用于解码的两种方法:
convert4bitUnsignedTo16Bit: anArray
"Convert the given array of samples--assumed to be 4-bit unsigned, linear data--into 16-bit signed samples. Return an array containing the resulting samples. I only thinking it is unsigned. I don't really know."
| n samples s |
n _ anArray size.
samples _ SoundBuffer newStereoSampleCount: (n * 2).
1 to: n do: [:i |
s _ anArray at: i.
samples at: (i * 2) put: (self imaDecode: s).
samples at: ((i * 2) - 1) put: (self imaDecode: s)].
^ samples
。
imaDecode: number
| n |
n _ number.
n >= 128 ifTrue: [n _ n - 256].
^ (n) * 16
它以正确的速度给我发出声音,如果我仔细听,就能听到原始声音。但这是非常静态的。
我想知道是否有人可以发现我的代码出了什么问题,并帮助我弄清楚为什么声音如此静态。 (顺便说一句:我将从SampledSound中的
convert4bitUnsignedFrom16Bit:
方法调用readFrom:
方法,并使用data
变量作为参数)。-TheCompModder
最佳答案
解码器方法的输入是ByteArray
。每个8位字节以4位编码存储两个样本。假设这是立体声轨道,则左/右声道将存储在每个字节的高/低4位中。您的imaDecode:
方法不会提取这些位。我认为它应该看起来像这样(显然未经测试):
1 to: n do: [:i |
byte := anArray at: i.
left := byte bitAnd: 15. "lower 4 bits"
right := (byte >> 4) bitAnd: 15. "upper 4 bits"
samples at: (i * 2) put: (left - 8) << 12.
samples at: ((i * 2) - 1) put: (right - 8) << 12].
这会将4位值放入
left
和right
中,通过-8
对其进行偏置(假设它们实际上是有符号的),然后扩展12位以成为完整的16位带符号的样本。顺便说一句,我认为您的缓冲区大小太大,它的
stereoSampleCount
应该是n
而不是n * 2
。另外,如果需要进一步的帮助,您可能希望将示例文件发布到Squeak开发人员邮件列表中。
关于audio - 在Squeak Smalltalk中解码WAV文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35963648/