File size: 1,159 Bytes
2573d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os

import soundfile as sf

import numpy as np





def combine_audio_files(folder, index):

    """
    Combine all audio files from the folder and return the concatenated audio data.
    Args:
        - folder (str): Path to the folder containing the .wav files.
        - index (int): The index of the last file to be concatenated.
    Returns: None
    """

    # Get a sorted list of .wav files by their numeric names
    wav_files = sorted(
        [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(".wav")],
        key=lambda x: int(os.path.splitext(os.path.basename(x))[0]),
    )

    # Raise an error if no .wav files are found
    if not wav_files:
        raise ValueError("No .wav files found in the input folder.")
    
    # Combine all .wav files
    audio_data = []
    samplerate = None
    for wav_file in wav_files[:index+1]:
        data, samplerate = sf.read(wav_file)
        audio_data.append(data)
        os.remove(wav_file)  # Clean up each file after reading
    
    # Return the concatenated audio data and the sampling rate
    final_audio = np.concatenate(audio_data)
    return final_audio, samplerate