# -*- coding: utf-8 -*- """Fuction_Audio_Processing_combine_files.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1TKJBO1bX2CGo6awtPhr6KE2bBMUGlYrs """ # Ru a Pytho Script in Google Colab # # python "/content/class_audio_processing_for_aglc_alphet_phonetic_langues_africaines_tonal_languages (2).py" # !python /content/class_audio_processing_for_aglc_alphet_phonetic_langues_africaines_tonal_languages.py """# Install libraries""" # pip install natsort # pip install pydub """The code defines a function named **get_audio** that takes in three parameters: **audio_path**, **ext** and **check_subfolders**. It imports the necessary modules such as os and glob. The function searches for audio files in a directory specified by the **audio_path** parameter and returns two lists; the first list containing the absolute path of the audio files and the second list containing the relative path of the audio files. The function has an optional parameter **ext** that takes a list of audio file extensions to be searched for, and **check_subfolders** is another optional parameter that specifies whether to search for audio files recursively in subfolders or not. # get_audio() Functions """ from posix import mkdir audio_path1 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language1/" audio_path2 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language2/" import os # Define the path where the "result" directory should be created result_path = audio_path2 + "result/" # G:\.shortcut-targets-by-id\1kt35DKTNYSvIP56JDRN5ukyvOBlyXEwi\Mbú'ŋwɑ̀'nì\test_audio\Laguage2 import glob import os from natsort import natsorted directory_path1 = audio_path1 +"*mp3" # Update with the actual directory path and file extension directory_path2 = audio_path2 +"*mp3" # Update with the actual directory path and file extension # Use glob to get the file names in the directory file_names1 = glob.glob(directory_path1) file_names2 = glob.glob(directory_path2) # Sort the file names based on the Windows initial key sorted_file_names1 = natsorted(file_names1, key=lambda x: x.lower()) sorted_file_names2 = natsorted(file_names2, key=lambda x: x.lower()) # Print the sorted file names for file_name in sorted_file_names1: print(file_name) class AGLC: # global len_chuck def __init__(self,audio_path): self.vow_map = {'à': 'à', 'á': 'á', 'ā': 'ā', 'ǎ': 'ǎ', 'â': 'â', 'è': 'è', 'é': 'é', 'ē': 'ē', 'ě': 'ě', 'ê': 'ê', 'ì': 'ì', 'í': 'í', 'ī': 'ī', 'ǐ': 'ǐ', 'î': 'î', 'ò': 'ò', 'ó': 'ó', 'ō': 'ō', 'ǒ': 'ǒ', 'ô': 'ô', 'ù': 'ù', 'ú': 'ú', 'ū': 'ū', 'ǔ': 'ǔ', 'û': 'û'} self.audio_path = audio_path # self.speed = play_speed # self.input = input def vowel_mapping(self,s): output = "" for char in s: if char in self.vow_map: output += self.vow_map[char] else: output += char return output def get_audio_files_dictionary(self,list_of_audio_files_including_extension): """ The function takes a list of audio files including extensions as input: ['a1.mp3','a2.mp3'] It creates: - a dictionary where the keys are the audio file names without extensions, and the values are the audio file names including extensions. - a new dictionary where the keys are combinations of two audio file names separated by a space and the values are lists of the corresponding audio file names including extensions. 'a1 a2': ['a1.mp3', 'a2.mp3'] The combinations are formed by iterating over the sorted list of audio file names and adding all pairs of names that come after the current name in the sorted list. Finally, the function returns the new dictionary. """ import os list_of_audio_files_including_extension_base = [ os.path.basename(i) for i in list_of_audio_files_including_extension] audio_files_no_ext = [i.split(".")[0] for i in list_of_audio_files_including_extension_base] audio_files_dict = dict(zip(audio_files_no_ext, list_of_audio_files_including_extension)) new_dict = {} sorted_list = sorted(list(audio_files_dict)) for i, val in enumerate(sorted_list): for j in range(i,len(sorted_list)): val_j = sorted_list[j] new_dict[f"{val} {val_j}"] = [audio_files_dict[val], audio_files_dict[val_j]] return new_dict # Define a function to normalize a chunk to a target amplitude. def match_target_amplitude(self,aChunk, target_dBFS): ''' Normalize given audio chunk The code normalizes an audio chunk to a target amplitude level given as `target_dBFS`. It calculates the change in decibels (dBFS) required to achieve the target amplitude level and applies it to the input audio chunk.''' change_in_dBFS = target_dBFS - aChunk.dBFS return aChunk.apply_gain(change_in_dBFS) def get_audio(self,audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False): """ The code defines a function named get_audio that takes in three parameters: audio_path, ext and check_subfolders. It imports the necessary modules such as os and glob. The function searches for audio files in a directory specified by the audio_path parameter and returns two lists; the first list containing the absolute path of the audio files and the second list containing the relative path of the audio files. The function has an optional parameter ext that takes a list of audio file extensions to be searched for, and check_subfolders is another optional parameter that specifies whether to search for audio files recursively in subfolders or not. """ import os import glob # List of audio file extensions to search for # AUDIO_EXTENSIONS = ext # Directory to search for audio files directory = audio_path # Replace with the path to your directory if check_subfolders == False: # Search for audio files using glob audio_files = [] for extension in ext: audio_files.extend(glob.glob(directory + "*" + extension, recursive=check_subfolders)) else: # Search for audio files using glob audio_files = [] for extension in ext: audio_files.extend(glob.glob(directory + "/**/" + extension, recursive=check_subfolders)) audio_base_names = [os.path.basename(i) for i in audio_files] return audio_files, audio_base_names # define function to play audio given input string and list of audio files def play_audio(self,input_string_space_separated, list_of_audio_files_including_extension): # import required module from pydub import AudioSegment # create dictionary of audio files and their corresponding inputs files = self.get_audio_files_dictionary(list_of_audio_files_including_extension) # if the input is not in the dictionary, try reversing the input and checking again if input_string_space_separated not in files: input_string_space_separated = " ".join(input_string_space_separated.split()[::-1]) # print("...........",files[input_string_space_separated]) invert_dict = {} invert_dict[input_string_space_separated] = files[input_string_space_separated][::-1] files.update(invert_dict) # create a silent audio segment for a 2 second pause between audio files silence = AudioSegment.silent(duration=2000) # get the list of audio files corresponding to the input string audio_files = files.get(input_string_space_separated) # print(audio_files) # raise an error if the input string is invalid if audio_files is None: raise ValueError("Invalid input string value") # create an empty audio segment to add the audio files to combined_audio = AudioSegment.empty() # loop through the list of audio files and add each one to the combined audio segment for filename in audio_files: audio_segment = AudioSegment.from_file(f"{filename}") combined_audio += audio_segment + silence # return the combined audio segment return combined_audio def play_audio_with_various_space_between_chunks(self,song, min_cut_silence_len = 1.5, silence_padding_duration = 2.5): """ The function takes an audio file and an optional silence duration as input and plays the audio with various spaces between each chunk. The pydub library is used to split the audio file into chunks where there is silence for a specified duration determined by min_cut_silence_len and then each chunk is padded with silence of duration silence_padding_duration. For example, if min_cut_silence_len = 1.5, silence_padding_duration = 2.5 The function then normalizes each chunk and exports them as separate audio files in the mp3 format. Finally, the function returns the entire audio file with the various spaces between each chunk as an AudioSegment object. """ # Import the AudioSegment class for processing audio and the # split_on_silence function for separating out silent chunks. from pydub import AudioSegment from pydub.silence import split_on_silence import os # Load your audio. # song = AudioSegment.from_mp3(audio_file_path) # Split track where the silence is 2 seconds or more and get chunks using # the imported function. chunks = split_on_silence ( # Use the loaded audio. song, # Specify that a silent chunk must be at least 2 seconds or 2000 ms long. # min_silence_len = 2000, min_silence_len = int(min_cut_silence_len*1000), # Consider a chunk silent if it's quieter than -16 dBFS. # (You may want to adjust this parameter.) silence_thresh = -50 ) silence_duration = int(silence_padding_duration*1000) audio_chunk = AudioSegment.silent(duration=1000) silence_chunk = AudioSegment.silent(duration=silence_duration) # Process each chunk with your parameters for i, chunk in enumerate(chunks): # Create a silence chunk that's 0.5 seconds (or 500 ms) long for padding. # print(i, len(chunks)) # Add the padding chunk to beginning and end of the entire chunk. if i < len(chunks)-1: audio_chunk += chunk + silence_chunk if i == len(chunks)-1: audio_chunk += chunk # Normalize the entire chunk. normalized_chunk = self.match_target_amplitude(audio_chunk, -20.0) # Export the audio chunk with new bitrate. # print(i) # print(f"Exporting chunk{i}.mp3.") # normalized_chunk.export( # # ".//chunk{0}.mp3".format(i), # f"_part{i}.mp3", # bitrate = "192k", # format = "mp3" # ) return audio_chunk, len(chunks) def play_audio_combine(self,input, speed = 2): audio_files = self.get_audio(self.audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"]) list_of_audio_files_including_extension = audio_files[0] # get_audio_files_dictionary(list_of_audio_files_including_extension) in_ = self.vowel_mapping(input) audio_result = self.play_audio(in_,list_of_audio_files_including_extension) audio_result_modified = self.play_audio_with_various_space_between_chunks(audio_result, silence_padding_duration = float(speed)) return audio_result_modified def main_func(self,audio_path1, audio_path2,result_path): import os from pydub import AudioSegment from natsort import natsorted from os.path import basename silent_end = AudioSegment.silent(duration=1000) silent_begin = AudioSegment.silent(duration=3000) audio_files_plus_ext1, audio_files1 = self.get_audio(audio_path1, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False) audio_files_plus_ext2, audio_files2 = self.get_audio(audio_path2, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False) # Sort the file names alphabtically sorted_audio_file_names1 = natsorted(audio_files1, key=lambda x: x.lower()) sorted_audio_file_names2 = natsorted(audio_files2, key=lambda x: x.lower()) sorted_audio_file_names_ext1 = natsorted(audio_files_plus_ext1, key=lambda x: x.lower()) sorted_audio_file_names_ext2 = natsorted(audio_files_plus_ext2, key=lambda x: x.lower()) # Using a dictionary to map indices to filenames for faster lookup file_dict2 = { ''.join(filter(str.isdigit, basename(path))): basename(path) for path in sorted_audio_file_names_ext1 } # Create the mapping in a more efficient way mapping_fast = { basename(path1): file_dict2.get(''.join(filter(str.isdigit, basename(path1))), None) for path1 in sorted_audio_file_names_ext2 if ''.join(filter(str.isdigit, basename(path1))) in file_dict2 } audio_path1_ = [audio_path1 + i for i in mapping_fast.values()] audio_path2_ = [audio_path2+i for i in mapping_fast.keys()] silent_2s = AudioSegment.silent(duration=2000) song = silent_2s padding_duration = 3 for i in range (0,len(audio_path2_)): # i = 1 song_name = os.path.basename(audio_path1_[i]).split("_")[0] + "_" + os.path.basename(audio_path2_[i]) print(song_name) song_1 = AudioSegment.from_mp3(audio_path1_[i]) song_2 = AudioSegment.from_mp3(audio_path2_[i]) song_out, number_of_chunks = self.play_audio_with_various_space_between_chunks(song_2, min_cut_silence_len = 1.2, silence_padding_duration = padding_duration) print("number_of_chunks:",number_of_chunks) if number_of_chunks == 1: songi = song_1 + silent_begin + song_2 + silent_begin + song_2 else: songi = song_1 + silent_begin + song_2 songi.export( # ".//chunk{0}.mp3".format(i), result_path+"/"+song_name, bitrate = "192k", format = "mp3" ); songi audio_files_plus_ext, audio_files = self.get_audio(result_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False) # Sort the file names alphabtically audio_files = natsorted(audio_files, key=lambda x: x.lower()) audio_files_plus_ext = natsorted(audio_files_plus_ext, key=lambda x: x.lower()) song = silent_begin for i in range (0,len(audio_files_plus_ext)): # i = 1 song_name = os.path.basename(audio_files_plus_ext[i]) print(song_name) # song_name = f"{audio_path}reworked/pp_"+song_name song_i = AudioSegment.from_mp3(audio_files_plus_ext[i]) # song += song_i+silent_end+flip_page_sound+silent_begin song += song_i song_name = f"{result_path}1_CombineAudio.mp3" song.export( # ".//chunk{0}.mp3".format(i), song_name, bitrate = "192k", format = "mp3" ); # audio_path1 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language1/" # audio_path2 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language2/" # result_path = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/res/" # # # player_yoruba = AGLC(audio_path_yoruba) def main(audio_path1, audio_path2,result_path): player = AGLC(audio_path1) player.main_func(audio_path1, audio_path2,result_path) def normalize_audio_for_ACX_Amazon(song): desired_sample_rate = 44100 # Replace this with your desired sample rate song = song.set_frame_rate(desired_sample_rate) desired_dBFS = -20 # Target volume level in dBFS current_dBFS = song.dBFS gain_needed = desired_dBFS - current_dBFS song_with_desired_volume = song.apply_gain(gain_needed) # song_with_desired_volume.export("/content/output.mp3", format="mp3", bitrate="192000") return song_with_desired_volume """# Build Interface With Gradio""" import gradio as gr iface = gr.Interface(fn=main, inputs=[gr.Textbox(label="Laguage1 Path: "), gr.Textbox(value=1, label="Laguage2 Path: "), gr.Textbox(label="Results Path: ")], outputs="audio", title="Combine Audios") # iface.launch(share=True,debug=True) iface.launch(debug=True) # pip install transformers demo = gr.Blocks() with demo: input_func1 = gr.Audio(type="filepath") output_func1 = gr.Textbox() output_func2 = gr.Label() b1 = gr.Button("Recognize Speech") b2 = gr.Button("Classify Sentiment") b1.click(Func1, inputs=input_func1, outputs=output_func1) b2.click(Func2, inputs=output_func1, outputs=output_func2) demo.launch(share=True) # get_audio(audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False): # return audio_files, audio_base_names # def get_audio_files_dictionary(list_of_audio_files_including_extension): # return new_dict # play_audio(input_string_space_separated, list_of_audio_files_including_extension): # return combined_audio # play_audio_with_various_space_between_chunks(song, silence_duration = .5): # return audio_chunk # list_of_audio_files_including_extension = get_audio()[0] # audio_result = play_audio("bǎ bá",list_of_audio_files_including_extension) # audio_result_modified = play_audio_with_various_space_between_chunks(audio_result, silence_duration = .2) # gr.Textbox(value=1, label="Duration in seconds"), # inputs=[gr.Textbox(label="words: Example: ǎ bà"), # gr.Textbox(value=1, label="Duration in seconds"), # gr.Textbox(label="Path")], def func1(x): x = float(x) return x**2 def func2(y): y = float(y) return y**2 def func3(a): a = float(a) # b = float(b) return 2*a demo = gr.Blocks() with demo: input_func1 = gr.Textbox(label = "val1") input_func2 = gr.Textbox(label = "val2") # input_func3 = [gr.Textbox(label = "val3") output_func1 = gr.Label(label = "result1") output_func2 = gr.Label(label = "result2") output_func3 = gr.Label(label = "result3") b1 = gr.Button("x^2") b2 = gr.Button("y^2") b3 = gr.Button("x^2+y^2") b1.click(func1, inputs=input_func1, outputs=output_func1) b2.click(func2, inputs=input_func2, outputs=output_func2) b3.click(func3, inputs=output_func1, outputs=output_func3) # b2.click(play_audio, inputs=output_func1, outputs=output_func2) demo.launch(share=True, debug=True) def main_func(input_string, speed = 1): audio_result = play_audio(input_string) audio_result_modified = play_audio_with_various_space_between_chunks(audio_result, silence_duration = float(speed)) return audio_result_modified main_func("ǎ bà",".1")