wave.readframes 返回什么?

发布于 2024-08-17 15:41:35 字数 382 浏览 5 评论 0 原文

我通过以下方式为变量 x 赋值:

import wave
w = wave.open('/usr/share/sounds/ekiga/voicemail.wav', 'r')
x = w.readframes(1)

当我输入 x 时,我得到:

'\x1e\x00'

所以 x 得到了一个值。但那是什么?是十六进制吗? type(x)type(x[0]) 告诉我 xx[0] a字符串。谁能告诉我应该如何解释这个字符串?我可以将它们转换为整数吗?

I assign a value to a variable x in the following way:

import wave
w = wave.open('/usr/share/sounds/ekiga/voicemail.wav', 'r')
x = w.readframes(1)

When I type x I get:

'\x1e\x00'

So x got a value. But what is that? Is it hexadecimal? type(x) and type(x[0]) tell me that x and x[0] a strings. Can anybody tell me how should I interpret this strings? Can I transform them into integer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半枫 2024-08-24 15:41:35

交互式解释器会回显这样的不可打印字符。该字符串包含两个字节:0x1E 和 0x00。您可以使用 struct.unpack(" 将其转换为整数(小尾数,2 字节,有符号)。

The interactive interpreter echoes unprintable characters like that. The string contains two bytes, 0x1E and 0x00. You can convert it to an integer with struct.unpack("<h", x) (little endian, 2 bytes, signed).

三五鸿雁 2024-08-24 15:41:35

是的,它是十六进制的,但它的含义取决于 wav 文件的其他输出,例如样本宽度和通道数。您的数据可以通过两种方式读取:2 通道和 1 字节样本宽度(立体声)或 1 通道和 2 字节样本宽度(单声道)。使用 x.getparams():第一个数字是通道数,第二个数字是样本宽度。

此链接解释得很好。

Yes, it is in hexadecimal, but what it means depends on the other outputs of the wav file e.g. the sample width and number of channels. Your data could be read in two ways, 2 channels and 1 byte sample width (stereo sound) or 1 channel and 2 byte sample width (mono sound). Use x.getparams(): the first number will be the number of channels and the second will be the sample width.

This Link explains it really well.

も让我眼熟你 2024-08-24 15:41:35

这是一个两字节字符串:

>>> x='\x1e\x00'
>>> map(ord, list(x))
[30, 0]
>>> [ord(i) for i in x]
[30, 0]

It's a two byte string:

>>> x='\x1e\x00'
>>> map(ord, list(x))
[30, 0]
>>> [ord(i) for i in x]
[30, 0]
娇女薄笑 2024-08-24 15:41:35

该字符串代表字节。我想你可以使用 struct 包将它们转换为整数,它允许解释以下字符串字节。

This strings represent bytes. I guess you can turn them into an integer with struct package, which allows interpreting strings of bytes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文