From ac5fe8b2bee9d7656ca875594f895ff581074c04 Mon Sep 17 00:00:00 2001 From: Tasha Upchurch Date: Sun, 20 Aug 2023 13:44:40 -0400 Subject: [PATCH] Added JoinAudioFiles class to concatenate multiple audio files This commit introduces the JoinAudioFiles class within the ETK/audio category. The class provides functionality to concatenate a list of audio files into a single file, using librosa and soundfile libraries. Metadata such as duration and samplerate are also retrieved. The code complies with the preferred import style. --- audio.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/audio.py b/audio.py index c28ee2d..8ecc715 100644 --- a/audio.py +++ b/audio.py @@ -71,3 +71,54 @@ class AudioFileListFromPathPattern: l_metadata.append(GetAudioMetaData().get_audio_metadata(path=fpath)[0]) return (l_fpaths, l_bytes, l_metadata,) + + +import librosa +import soundfile as sf +import numpy as np + +@ETK_audio_base +class JoinAudioFiles: + """ Joins a list of audio files into a single audio file """ + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "paths": ("LIST", {"default": []}), + "output_path": ("STRING", {"multiline": False, "default": ""}), + } + } + + RETURN_TYPES = ("STRING", "BYTES", "DICT",) + FUNCTION = 'join_audio_files' + CATEGORY = 'ETK/audio' + + def join_audio_files(self, **kwargs): + kwargs = copy.deepcopy(kwargs) + paths = kwargs['paths'] + output_path = kwargs['output_path'] + + # Read the first file to get metadata + audio_data_list = [] + for path in paths: + audio_data, sr = librosa.load(path, sr=None) + audio_data_list.append(audio_data) + + # Concatenate all audio data + joined_audio_data = np.concatenate(audio_data_list) + + # Save the concatenated audio + sf.write(output_path, joined_audio_data, sr) + + # Get metadata + metadata = { + 'duration': librosa.get_duration(y=joined_audio_data, sr=sr), + 'samplerate': sr + } + + # Get bytes + with open(output_path, "rb") as f: + file_bytes = f.read() + + return (output_path, file_bytes, metadata,) \ No newline at end of file