打开并读取wave文件
PyWave的Python项目详细描述
pywave
打开并读取波形文件
pywave是一个小的extension扩展,允许您open和read任何wave-riff文件的数据。
它支持pcm、ieee-float、可扩展和其他一些波形格式(包括32位波形)。
微型文档
关于pywave
pywave应该replace内置的python扩展wave
,它不支持32位wave。
使用pywave
要安装pywave,可以使用pypi:
pip install PyWave
要在脚本中使用pywave,必须使用
import PyWave
或通配符导入:
from PyWave import *
波类
您可以使用Wave
类打开并读取wave文件:
Wave(path[, auto_read = False])
path - File path to a wave file
auto_read - (optional) Can be set to True to read the data automatically
如果启用auto_read
,数据将存储在Wave.data
以下方法由Wave
类提供:
Wave.read([max_bytes = 4096]) -> <bytes> data
    Reads and returns at most <max_bytes> bytes of data.
Wave.read_samples(number_of_samples) -> <bytes> data
Reads and returns at most <number_of_samples> samples of data.
Wave.seek(offset[, whence = 0]) -> None
Sets the current position in the data stream.
If <whence> is 0, <offset> is the absolute position of the
data stream in bytes.
If <whence> is 1, <offset> is added to the current position
in the data stream in bytes.
If <whence> is 2, the position will be set to the end of
the file plus <offset>.
Wave.tell() -> <int> position
Returns the current position in the data stream.
Wave.close() -> None
Closes the file handle.
它有以下成员:
Wave.format <int>
Format of the audio data. Can be any of:
- WAVE_FORMAT_PCM
- WAVE_FORMAT_IEEE_FLOAT
- WAVE_FORMAT_ALAW
- WAVE_FORMAT_MULAW
- WAVE_FORMAT_EXTENSIBLE
Wave.channels <int>
The number of audio channels present in the audio stream
Wave.frequency <int>
Sample rate of the audio stream
Wave.bitrate <int>
Number of bits per second
Wave.bits_per_sample <int>
Number of bits per sample (usually 8, 16 or 32)
Wave.samples <int>
Total number of samples in the audio data
Wave.data <bytes>
(only if <auto_read> was set to True)
Audio data as bytes
示例
from PyWave import *
PATH = "path/to/a/wave/file.wav"
wf = Wave(PATH)
print("This WAVE file has the following properties:")
print(wf.channels, "channels")
print(wf.frequency, "Hz sample rate")
print(wf.bitrate, "bits per second")
print(wf.samples, "total samples")