'With Sox, how can I transform audio without building to disk, before I then combine audio files

Rookie here trying to write a little audio tool for my studio.

Im looking to recursively layer/combine audio files at random using Sox Combiner but first process the individual audio files separately with Sox FX. The combiner function can only input files from disk it seems and aside from input_volume, no other effects can be applied.

I can crudely do what I want by transforming each individual file to a new file on disk, but that seems excessive. I assume the transformation can be done in memory?

pseudocode:

read files 1 and 2 at random from list on disk
process both files using Sox transformer 
Combine both files into one file using Sox combiner

Here is where I'm up to:

def generate_new_audio_files(folder_path, file_count):
        # pick 2 samples from a folder
        file_list = Functions.get_list_of_valid_audio_files_in_root(folder_path)

        # delete and create the new folder for exported files
        export_folder_path = folder_path + " [Generated with Sample Sorter]"
        Functions.create_new_folder_after_deleting_existing_folder(export_folder_path)

        # determine sample category from folder name
        sample_category = Functions.get_file_folder_from_file_path(folder_path)

        random.shuffle(file_list)
        count = 0
        sample_rate = 44100

        tfm_raw = sox.Transformer()

        tfm_process1 = sox.Transformer()
        tfm_process2 = sox.Transformer()
        tfm_process1.pitch(3)
        tfm_process2.pitch(3)

        combiner = sox.Combiner()
        combiner.set_input_format(channels=[2, 2])
        combiner.gain(normalize=True)
        while count < (file_count):


                # get a file path from the list and remove it at the same time
                sample1_file_path = file_list.pop()
                sample2_file_path = file_list.pop()
                sample1_file_name_w_ext = Functions.get_file_name_w_ext_from_file_path(sample1_file_path)
                sample2_file_name_w_ext = Functions.get_file_name_w_ext_from_file_path(sample2_file_path)

                # process files
                sample1_array = tfm_process1.build_array(sample1_file_path, sample_rate_in=sample_rate)
                sample2_array = tfm_process2.build_array(sample2_file_path, sample_rate_in=sample_rate)
                export_sample1_file_path_processed = export_folder_path + "/_" + sample_category + " Combined " + \
                                                  str(count + 1) + " processed " + sample1_file_name_w_ext
                export_sample2_file_path_processed = export_folder_path + "/_" + sample_category + " Combined " + \
                                                  str(count + 1) + " processed " + sample2_file_name_w_ext

                # export processed files
                tfm_process1.build_file(input_array=sample1_array, sample_rate_in=sample_rate,
                                output_filepath=export_sample1_file_path_processed)
                tfm_process2.build_file(input_array=sample2_array, sample_rate_in=sample_rate,
                                output_filepath=export_sample2_file_path_processed)

                # export original files for comparison
                export_sample1_file_path = export_folder_path + "/_" + sample_category + " Combined " + \
                                           str(count + 1) + " " + sample1_file_name_w_ext
                export_sample2_file_path = export_folder_path + "/_" + sample_category + " Combined " + \
                                           str(count + 1) + " " + sample2_file_name_w_ext
                tfm_raw.build(sample1_file_path, export_sample1_file_path)
                tfm_raw.build(sample2_file_path, export_sample2_file_path)

                # export combined file
                export_combined_file_path = export_folder_path + "/" + sample_category + " Combined " + \
                                            str(count + 1) + ".wav"
                combiner.build([sample1_file_path, sample2_file_path],  export_combined_file_path, 'mix',
                               input_volumes=[0.5, 0.5])

                count = count + 1


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source