Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process 24bit wavs using pydub

Tags:

python

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. I'm using pydub and it all works, however the results are distorted. 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.

So needing some small blip of code that converts the wav to 16 bit before it enters 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')
like image 698
tambourine Avatar asked Oct 29 '22 18:10

tambourine


1 Answers

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). My suggestion would be to initially read in the audio using PySoundFile and then create a pydub.AudioSegment using that data.

like image 76
michael_question_answerer Avatar answered Nov 15 '22 07:11

michael_question_answerer