简体   繁体   English

使用pydub处理24bit WAV

[英]Process 24bit wavs using pydub

Im trying to write a basic program that picks 2 audio files at random, layers them, and writes it out as a new file. 我试图编写一个基本程序,随机选择2个音频文件,对其进行分层,然后将其写为新文件。 I'm using pydub and it all works, however the results are distorted. 我正在使用pydub,并且一切正常,但是结果却失真了。 I suspect it's because from what I've learnt, pydub cannot handle 24 bit wavs, which happen to be the norm in sample packs. 我怀疑是因为据我了解,pydub无法处理24位波形,而这恰恰是样本包中的标准。

So needing some small blip of code that converts the wav to 16 bit before it enters pydub. 因此,需要一些小的代码来将wav转换为16位,然后再输入pydub。 Hopefully not one that requires writing it to disc first. 希望没有人需要先将其写入光盘。

from pydub import AudioSegment
import os
import random
import shutil    


def process(user_folder):

new_library_folder = user_folder + " Generated Combo Samples"
files_list = []
for root, directory, files in os.walk(user_folder):
    for file in files:
        if file_is_valid_ext(file):
            filepath = str(root) + "/" + str(file)
            # print filepath
            files_list.append(filepath)

# removes previously created folder
shutil.rmtree(new_library_folder)
os.makedirs(new_library_folder)

i = 0
for number in range(gen_count): # global at 100
    i = i + 1

    file1 = random.choice(files_list)
    file2 = random.choice(files_list)

    sound1 = AudioSegment.from_file(file1)
    sound2 = AudioSegment.from_file(file2)
    sound1 = match_target_amplitude(sound1, -20)
    sound2 = match_target_amplitude(sound2, -20)

    combinedsound = sound1.overlay(sound2)
    combinedsoundnormalised = match_target_amplitude(combinedsound, -6)

    combinedsound_path = new_library_folder + "/" + "Sample " + str(i) + ".wav"

    combinedsoundnormalised.export(combinedsound_path, format='wav')

It has been some months since you posted this question but I will answer it for others who may need a solution to this. 自您发布此问题以来已有几个月,但我会为可能需要解决此问题的其他人回答。 As far as I have found, PySoundFile is the only python package that can deal with 24 bit audio (I am sure there are others but I haven't found them). 据我发现, PySoundFile是唯一可以处理24位音频的python软件包(我确定还有其他,但我还没有找到)。 My suggestion would be to initially read in the audio using PySoundFile and then create a pydub.AudioSegment using that data. 我的建议是先使用PySoundFile读取音频,然后使用该数据创建pydub.AudioSegment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM