python wave.readframes 返回什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2063565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:40:53  来源:igfitidea点击:

What is returned by wave.readframes?

pythonwave

提问by Roman

I assign a value to a variable xin the following way:

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

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

When I type x I get:

当我输入 x 时,我得到:

'\x1e\x00'

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

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

回答by AndiDog

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

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

回答by Razzad777

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.

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

This Linkexplains it really well.

这个链接很好地解释了它。

回答by Andrew McGregor

It's a two byte string:

这是一个两字节的字符串:

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

回答by gruszczy

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

这个字符串代表字节。我想你可以用struct包将它们转换成一个整数,它允许解释字节字符串。