在Python中创建的32位浮点wav文件? [英] Create 32bit float wav file in python?

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

问题描述

我想在Python(2.X)创建32位浮点WAV文件。虽然标准的WAV文件通常使用INT,许多专业音频应用程序(并保存)音频数据作为浮动。
标准波模块不能够做到这一点: http://bugs.python.org/issue16525
有没有人做到了这一点,而无需使用补丁模块? TNX的任何帮助。

I want to create 32bit float WAV files in Python (2.x). While "standard" WAV files usually use int, many professional audio applications process (and save) audio data as float. The standard wave module is not capable to do this: http://bugs.python.org/issue16525 Has anyone achieved this without using patched modules ? tnx for any help.

推荐答案

这听上去很不错(见我把手),所以我敲定了一些东西。也许你可以使用它。如果你的Python脚本生成落在之间的数值的单音波形[-1.0 ... 1.0],发在波形通过 sample_array 并指定 SAMPLE_RATE (例如:44100或48000)。这将返回给你,你可以写入磁盘作为.wav文件的数组。

This sounded like fun (see my handle), so I hammered out something. Maybe you can use it. If your Python script generates a monophonic waveform of numerical values that fall between [-1.0 .. 1.0], send that waveform in through sample_array and also specify sample_rate (e.g., 44100 or 48000). This will return to you an array that you can write to disk as a .wav file.

我测试在Windows Media Player,苹果的QuickTime播放器,和VLC所产生的.WAV输出(所有的Windows 7)。他们都玩过吧。

I tested the resulting .wav output in Windows Media Player, Apple QuickTime Player, and VLC (all on Windows 7). They all played it.

def float32_wav_file(sample_array, sample_rate):
  byte_count = (len(sample_array)) * 4  # 32-bit floats
  wav_file = ""
  # write the header
  wav_file += struct.pack('<ccccIccccccccIHHIIHH',
    'R', 'I', 'F', 'F',
    byte_count + 0x2c - 8,  # header size
    'W', 'A', 'V', 'E', 'f', 'm', 't', ' ',
    0x10,  # size of 'fmt ' header
    3,  # format 3 = floating-point PCM
    1,  # channels
    sample_rate,  # samples / second
    sample_rate * 4,  # bytes / second
    4,  # block alignment
    32)  # bits / sample
  wav_file += struct.pack('<ccccI',
    'd', 'a', 't', 'a', byte_count)
  for sample in sample_array:
    wav_file += struct.pack("<f", sample)
  return wav_file

这篇关于在Python中创建的32位浮点wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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