如何转换为具有浮点值的数组

2024-09-30 02:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在用下面的代码录制音频

bytes = 1024
format = pyaudio.paInt16
channels = 1
outputFile = "C:/python27/test.wav"
rate = 12000
audioRecorder = pyaudio.PyAudio()
recorder = audioRecorder.open(format = format, channels = channels, rate = rate, input = True, frames_per_buffer = bytes)

for i in range(0, int(rate/bytes * duration)):
    data = recorder.read(bytes)
    myArr.append(data)

recorder.stop_stream()
recorder.close()
audioRecorder.terminate()

myArr

“myArr”是什么类型的数据类型?如何将其转换为具有浮点值的数组

编辑------------------------------------------

我发现“myArr”是一个类似这样的列表

 00: [b'\x01\x00\x04.....
 01: [b'\x01\x00\xfb.....

但我希望它是一个带有单浮点值的数组。我一直在尝试使用numpy.array,但我无法解决我的问题


Tags: 代码formatdatabytesrate数组音频浮点
1条回答
网友
1楼 · 发布于 2024-09-30 02:29:01

变量my myArr是字节数组。您可以使用

Myarr2 = []  
for arr in Myarr:
   content = int.from_bytes(arr, byteorder='big')
   Myarr2.append(content)

它获取每个值,然后将其转换回整数。如果要使用无符号整数,请更改

content = int.from_bytes(arr, byteorder='big')

content = int.from_bytes(arr, byteorder='big', signed=False)

相关问题 更多 >

    热门问题