【问题标题】:Wave file sample editing using Python AudioTools使用 Python AudioTools 编辑 Wave 文件样本
【发布时间】:2014-10-27 17:49:54
【问题描述】:

我正在尝试创建一个 Python 脚本,该脚本能够打开音频文件、读取 PCM 数据并使用此 PCM 数据输出另一个音频文件。 (最终我希望能够对数据应用流程)

我目前正在使用 python audiotools,因为它似乎可以用几种格式来做到这一点。

我已设法读取数据并转换为非常适合编辑的浮点值 FramesList,尽管我无法完成将处理后的 FramesList 写回 .wav 文件的过程。 to_bytes() 将数据转换为原始 pcm 字符串,但我不确定如何将这些原始数据恢复为 wav 文件?

from audiotools import *
from argparse import ArgumentParser

def get_info(audio_file, main_args):
    """
    create a dictionary of information for the audiofile object.

    """
    info = {}
    info["channels"] = audio_file.channels()
    info["channel_mask"] = audio_file.channel_mask()
    info["bits"] = audio_file.bits_per_sample()
    info["sample_rate"] = audio_file.sample_rate()
    info["frames"] = audio_file.total_frames()
    info["length"] = audio_file.seconds_length()
    info["seekable"] = audio_file.seekable()
    info["verified"] = audio_file.verify()
    info["chunks"] = audio_file.has_foreign_wave_chunks()
    info["available"] = audio_file.available(BIN)
    info["header"], info["footer"] = audio_file.wave_header_footer()

    if main_args.verbose:
        print "No. of Channels:\t\t", info["channels"]
        print "Channel mask:\t\t\t", info["channel_mask"]
        print "Bits per sample:\t\t", info["bits"], "BIT"
        print "Sample Rate:\t\t\t", (info["sample_rate"]/1000.0), "k"
        print "Number of Frames:\t\t", info["frames"]
        print "Audio Length:\t\t\t", info["length"], "seconds"
        print "Audio File Seekable?:\t\t", info["seekable"]
        print "File has foreign chunks?:\t", info["chunks"]
        print "Correct Binaries present?:\t", info["available"]
    return info

def main():
    parser = ArgumentParser()
    parser.add_argument(
        "-v",
        "--verbose",
        help = "Run program verbosely",
        default = False,
        action = "store_true",
    )

    main_args = parser.parse_args()

    #open audio file as an AudioFile object
    audio_file =  open("/Users/samperry/piano2.wav")
    file_info = get_info(audio_file, main_args)

    #Creates a WaveReader object from the AudioFile Object
    pcm_data = audio_file.to_pcm()

    #Creates a FrameList object from WaveReader object. Currently reads all
    #frames in file
    frame_list = pcm_data.read(file_info["frames"])

    #Convert samples to floats (-1.0 - +1.0)
    float_frame_list = frame_list.to_float()

    #eventually do some signal processing here...

    #Convert back to integer FrameList
    output_framelist = float_frame_list.to_int(file_info["bits"])

    #now back to raw bytes
    output_data = output_framelist.to_bytes(False, True)

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python python-2.7 audio


    【解决方案1】:

    你试过PyDub吗?它允许您使用简单易用的高级界面来操作音频。

    【讨论】:

    • 我见过 PyDub,但不幸的是它没有提供我项目所需的逐个样本编辑级别的样本。
    【解决方案2】:

    如果您只是使用 wav 文件,那么您可以使用 wave python 模块。它比音频工具简单得多。

    如果您需要处理其他文件格式,并且不介意使用外部程序,您可以将原始数据输出到文件中,然后使用 sox 将其转换为您想要的任何格式。
    你可以看看this answer关于读取数据以获得一些想法。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2017-04-08
      • 2023-04-03
      • 2016-03-23
      • 2021-09-28
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多