打开并读取wave文件

PyWave的Python项目详细描述


pywave

打开并读取波形文件

pywave是一个小的extension扩展,允许您openread任何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")

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用Spring和JSF向客户端授予临时权限   java除了Array/ArrayList之外,还有其他保存矩阵元素的方法吗   java BeanValidation不适用于单选按钮   通过java程序连接到配置单元数据库时出错   java如何使用maven解决二级依赖关系   JfreeChart/Java中带有图例的数据表   合并数组时发生java运行时错误   安全性如何在JavaEE中保护WebSocket端点?   java有没有一种方法可以使用insert方法为树插入值   java编程“静态”的另一种方式:代码可以吗?   java阅读文本文件时如何跳过3行   %04X在C中的含义以及如何在java中编写   java我如何验证一个方法不是仅在一个测试范围内对模拟调用的?   java如何在mac中使用启动appium desktop 1.6.1。球棒   仅匹配最多10位小数的java正则表达式