简体   繁体   English

使用 wavio Python 3.5 读取 24 位 WAV

[英]Reading 24-Bit WAV with wavio Python 3.5

I am using wavio by WarrenWeckesser as I need to read 24-bit wav files in python.我正在使用WarrenWeckesser的 wavio,因为我需要在 python 中读取 24 位 wav 文件。 The wav files I have are produced by some instrumentation and I am trying to get the raw values without any normalisation or scaling.我拥有的 wav 文件是由一些仪器生成的,我试图在没有任何标准化或缩放的情况下获取原始值。

In the wavio module the code that does the work is this:在 wavio 模块中,完成工作的代码是这样的:

    if sampwidth == 3:
        a = _np.empty((num_samples, nchannels, 4), dtype=_np.uint8)
        raw_bytes = _np.fromstring(data, dtype=_np.uint8)
        a[:, :, :sampwidth] = raw_bytes.reshape(-1, nchannels, sampwidth)
        a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255
        result = a.view('<i4').reshape(a.shape[:-1]

Can someone explain what it is actually doing (I am a relatively new to numpy and array slicing).有人能解释一下它实际上在做什么吗(我对 numpy 和数组切片比较陌生)。 I understand most of it but I don't understand what is going on here:我明白其中的大部分,但我不明白这里发生了什么:

    a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255

In my case it does the transform from 24 to 32 bit but I can't work out whether it is scaling the data, or simply padding it out without changing any raw values.在我的例子中,它进行了从 24 位到 32 位的转换,但我无法确定它是在缩放数据,还是只是在不更改任何原始值的情况下填充它。

The shape of a is (num_samples, nchannels, 4) and sampwidth == 3 , so that line is the same as的形状a(num_samples, nchannels, 4)sampwidth == 3 ,从而使线相同

a[:, :, 3:] = (a[:, :, 2:3] >> 7) * 255

which is the same as这与

a[:, :, 3] = (a[:, :, 2] >> 7) * 255

we could devectorize the outer two loops:我们可以对外部的两个循环进行去向量化:

for i in range(num_samples):
    for j in range(nchannels):
        a[i, j, 3] = (a[i, j, 2] >> 7) * 255

The dtype of a is _np.uint8 , so a[...] >> 7 can only give out 0 when the value is <128, or 1 when it is ≥128, so the above becomes: a_np.uint8_np.uint8 ,所以a[...] >> 7只能在值 <128 时给出 0,或者在 ≥128 时给出 1,所以上面变成:

for i in range(num_samples):
    for j in range(nchannels):
        v = a[i, j, 2]
        a[i, j, 3] = 255 if v >= 128 else 0

If the data are 24-bit little-endian integers, this is equivalent to doing a sign-extension into 32-bit.如果数据是 24 位小端整数,这相当于对 32 位进行符号扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM