Question

My goal is to unpack the string provided by pyaudio correctly to int16 for some modification and then pack it again to do the playback.

This is what i got so far (code copied from other post):

#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))

Now my question:

  • what would be the correct arguments for struct.pack to get my data back to pyaudio string?
Was it helpful?

Solution

Ok i found the answer elswhere:

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

results in a correctly formatted string for pyaudio.

Nevertheless i will happily accept any other answer, which explains the function calls and their correct usage!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top