Im trying to write some code to first process two audio samples using sox transformer, then combine the two audio files using sox combiner. Im also trying to do the processing first in memory to speed things up.
I am getting this error when using .TemporaryFile()
and .SpooledTemporaryFile()
but it works fine when using .mkstemp()
:
Traceback (most recent call last): File "...", line 128, in <module> generate_new_audio_files(user_library_folder_pathlist, new_folder_path, file_count) File "...", line 101, in generate_new_audio_files combine_two_files_mild(sample1_file_path, sample2_file_path, new_folder_path, count) File "...", line 30, in combine_two_files_mild _, sample1_audio = tempfile.SpooledTemporaryFile() ValueError: not enough values to unpack (expected 2, got 0)
Here is my code:
def combine_two_files_mild(file_path_1, file_path_2, parent_folder_path, count=None):
combiner = sox.Combiner()
tfm1 = sox.Transformer()
tfm2 = sox.Transformer()
tfm1.set_input_format(rate=44100)
tfm2.set_input_format(rate=44100)
combiner.set_input_format(channels=[2, 2])
combiner.gain(normalize=True)
# will eventually insert here some randomized settings for the two transformers
# this determins the file category based on it's file name
file_main_category, file_sub_category = Category.get_file_category(file_path_1)
sorted_file_folder = parent_folder_path + "/" + file_main_category + "/" + file_sub_category
new_file_path = sorted_file_folder + "/" + file_sub_category + " Combined " + \
str(count + 1) + " Mild.wav"
# load the audio data for the two samples
_, sample1_audio = tempfile.SpooledTemporaryFile()
tfm1.build(file_path_1, sample1_audio)
_, sample2_audio = tempfile.SpooledTemporaryFile()
tfm2.build(file_path_2, sample2_audio)
# combine the two files and write to disk
combiner.build([sample1_audio, sample2_audio], new_file_path, 'mix-power',
input_volumes=[0.5, 0.5])
# clear the 'memory'
os.remove(sample1_audio)
os.remove(sample2_audio)
The API for SpooledTemporaryFile
is different from mkstemp
. mkstemp
returns a tuple of two objects (which you are setting as _
and sample1_audio
respectively).
SpooledTemporaryFile
will return a single file-like object for you to use. Usually SpooledTemporaryFile
and TemporaryFile
are used as context managers like so:
with tempfile.SpooledTemporaryFile() as sample1_audio:
tfm1.build(file_path_1, sample1_audio)
Note that after the with
block, the temporary file will be deleted, so either keep all of your code that requires the temporary files in this block, or you can manually handle the file opening and closing yourself:
sample1_audio = tempfile.SpooledTemporaryFile()
tfm1.build(file_path_1, sample1_audio)
... # Other stuff you want to do
sample1_audio.close()
Additionally, the tempfile
module is specifically made to create temporary files which are removed automatically when they are no longer needed, so you should not need your calls to os.remove
at the end!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.