如何正确使用带有pyaudio的struct.pack / unpack?

时间:2014-01-06 14:10:35

标签: python struct pack pyaudio

我的目标是将pyaudio提供的字符串正确解压缩到int16进行一些修改,然后重新打包以进行播放。

这是我到目前为止(从其他帖子复制的代码):

#data contains my string of interleaved int16 data

#this code should unpack it accordingly
# 1 short out of each 2 chars in data
count = len(data)/2
format = "%dh"%(count) #results in '2048h' as format: 2048 short
shorts = struct.unpack(format, data)

#here some modifications will take place but are left out to test packing

#now i need to pack my short data back to pyaudio compliant string
#i have tried the following with no success. just random noise
struct.pack(str(len(shorts)*2) + "s", str(shorts))

现在我的问题:

  • struct.pack将数据恢复为pyaudio字符串的正确参数是什么?

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案:

struct.pack("%dh"%(len(shorts)), *list(shorts))

为pyaudio生成格式正确的字符串。

然而,我很乐意接受任何其他答案,这解释了函数调用及其正确用法!