如何在 Python 中编写 24 位 WAV 文件? [英] How do I write a 24-bit WAV file in Python?

查看:27
本文介绍了如何在 Python 中编写 24 位 WAV 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 2.7 从 -1 和 1 之间的浮点值数组生成一个 24 位 WAV 格式的音频文件.我不能使用 scipy.io.wavfile.write 因为它只支持 16 或 32 位.Python 自己的 wave 模块的文档没有指定它需要什么格式的数据.

I want to generate a 24-bit WAV-format audio file using Python 2.7 from an array of floating point values between -1 and 1. I can't use scipy.io.wavfile.write because it only supports 16 or 32 bits. The documentation for Python's own wave module doesn't specify what format of data it takes.

那么可以在 Python 中做到这一点吗?

So is it possible to do this in Python?

推荐答案

另一个选项在 wavio(也在 PyPI 上:https://pypi.python.org/pypi/wavio),我创建的一个小模块,用于解决 scipy 尚不支持 24 位 WAV 文件的问题.文件 wavio.py 包含函数write,将 numpy 数组写入 WAV 文件.要编写 24 位文件,请使用参数 sampwidth=3.wavio 唯一的依赖是 numpy;wavio 使用标准库 wave 来处理 WAV 文件格式.

Another option is available in wavio (also on PyPI: https://pypi.python.org/pypi/wavio), a small module I created as a work-around to the problem of scipy not yet supporting 24 bit WAV files. The file wavio.py contains the function write, which writes a numpy array to a WAV file. To write a 24-bit file, use the argument sampwidth=3. The only dependency of wavio is numpy; wavio uses the standard library wave to deal with the WAV file format.

例如

In [21]: import numpy as np

In [22]: import wavio

In [23]: rate = 22050             # samples per second

In [24]: T = 3                    # sample duration (seconds)

In [25]: f = 440.0                # sound frequency (Hz)

In [26]: t = np.linspace(0, T, T*rate, endpoint=False)

In [27]: sig = np.sin(2 * np.pi * f * t)

In [28]: wavio.write("sine24.wav", sig, rate, sampwidth=3)

这篇关于如何在 Python 中编写 24 位 WAV 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆