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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Fuction_Audio_Processing_combine_files.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1TKJBO1bX2CGo6awtPhr6KE2bBMUGlYrs
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# Ru a Pytho Script in Google Colab
|
| 11 |
+
# # python "/content/class_audio_processing_for_aglc_alphet_phonetic_langues_africaines_tonal_languages (2).py"
|
| 12 |
+
|
| 13 |
+
# !python /content/class_audio_processing_for_aglc_alphet_phonetic_langues_africaines_tonal_languages.py
|
| 14 |
+
|
| 15 |
+
"""# Install libraries"""
|
| 16 |
+
|
| 17 |
+
# pip install natsort
|
| 18 |
+
|
| 19 |
+
# pip install pydub
|
| 20 |
+
|
| 21 |
+
"""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.
|
| 22 |
+
|
| 23 |
+
# get_audio() Functions
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
from posix import mkdir
|
| 27 |
+
audio_path1 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language1/"
|
| 28 |
+
audio_path2 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language2/"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# mkdir result
|
| 32 |
+
|
| 33 |
+
import os
|
| 34 |
+
|
| 35 |
+
# Define the path where the "result" directory should be created
|
| 36 |
+
result_path = audio_path2 + "result/"
|
| 37 |
+
|
| 38 |
+
# Create the "result" directory if it doesn't exist
|
| 39 |
+
if not os.path.exists(result_path):
|
| 40 |
+
mkdir(result_path)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# G:\.shortcut-targets-by-id\1kt35DKTNYSvIP56JDRN5ukyvOBlyXEwi\Mbú'ŋwɑ̀'nì\test_audio\Laguage2
|
| 44 |
+
|
| 45 |
+
import glob
|
| 46 |
+
import os
|
| 47 |
+
|
| 48 |
+
from natsort import natsorted
|
| 49 |
+
|
| 50 |
+
directory_path1 = audio_path1 +"*mp3" # Update with the actual directory path and file extension
|
| 51 |
+
directory_path2 = audio_path2 +"*mp3" # Update with the actual directory path and file extension
|
| 52 |
+
|
| 53 |
+
# Use glob to get the file names in the directory
|
| 54 |
+
file_names1 = glob.glob(directory_path1)
|
| 55 |
+
file_names2 = glob.glob(directory_path2)
|
| 56 |
+
|
| 57 |
+
# Sort the file names based on the Windows initial key
|
| 58 |
+
sorted_file_names1 = natsorted(file_names1, key=lambda x: x.lower())
|
| 59 |
+
sorted_file_names2 = natsorted(file_names2, key=lambda x: x.lower())
|
| 60 |
+
|
| 61 |
+
# Print the sorted file names
|
| 62 |
+
for file_name in sorted_file_names1:
|
| 63 |
+
print(file_name)
|
| 64 |
+
|
| 65 |
+
class AGLC:
|
| 66 |
+
|
| 67 |
+
# global len_chuck
|
| 68 |
+
|
| 69 |
+
def __init__(self,audio_path):
|
| 70 |
+
self.vow_map = {'à': 'à', 'á': 'á', 'ā': 'ā', 'ǎ': 'ǎ', 'â': 'â', 'è': 'è', 'é': 'é', 'ē': 'ē', 'ě': 'ě', 'ê': 'ê', 'ì': 'ì', 'í': 'í', 'ī': 'ī', 'ǐ': 'ǐ', 'î': 'î', 'ò': 'ò', 'ó': 'ó', 'ō': 'ō', 'ǒ': 'ǒ', 'ô': 'ô', 'ù': 'ù', 'ú': 'ú', 'ū': 'ū', 'ǔ': 'ǔ', 'û': 'û'}
|
| 71 |
+
self.audio_path = audio_path
|
| 72 |
+
# self.speed = play_speed
|
| 73 |
+
# self.input = input
|
| 74 |
+
def vowel_mapping(self,s):
|
| 75 |
+
output = ""
|
| 76 |
+
for char in s:
|
| 77 |
+
if char in self.vow_map:
|
| 78 |
+
output += self.vow_map[char]
|
| 79 |
+
else:
|
| 80 |
+
output += char
|
| 81 |
+
return output
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def get_audio_files_dictionary(self,list_of_audio_files_including_extension):
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
The function takes a list of audio files including extensions as input: ['a1.mp3','a2.mp3']
|
| 88 |
+
It creates:
|
| 89 |
+
- a dictionary where the keys are the audio file names without extensions, and the values are
|
| 90 |
+
the audio file names including extensions.
|
| 91 |
+
- a new dictionary where the keys are combinations of two audio file names separated by a space
|
| 92 |
+
and the values are lists of the corresponding audio file names including extensions.
|
| 93 |
+
'a1 a2': ['a1.mp3', 'a2.mp3']
|
| 94 |
+
The combinations are formed by iterating over the sorted list of audio file names and adding all pairs of
|
| 95 |
+
names that come after the current name in the sorted list.
|
| 96 |
+
Finally, the function returns the new dictionary.
|
| 97 |
+
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
import os
|
| 101 |
+
list_of_audio_files_including_extension_base = [ os.path.basename(i) for i in list_of_audio_files_including_extension]
|
| 102 |
+
audio_files_no_ext = [i.split(".")[0] for i in list_of_audio_files_including_extension_base]
|
| 103 |
+
|
| 104 |
+
audio_files_dict = dict(zip(audio_files_no_ext, list_of_audio_files_including_extension))
|
| 105 |
+
|
| 106 |
+
new_dict = {}
|
| 107 |
+
|
| 108 |
+
sorted_list = sorted(list(audio_files_dict))
|
| 109 |
+
for i, val in enumerate(sorted_list):
|
| 110 |
+
for j in range(i,len(sorted_list)):
|
| 111 |
+
val_j = sorted_list[j]
|
| 112 |
+
new_dict[f"{val} {val_j}"] = [audio_files_dict[val], audio_files_dict[val_j]]
|
| 113 |
+
return new_dict
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# Define a function to normalize a chunk to a target amplitude.
|
| 118 |
+
def match_target_amplitude(self,aChunk, target_dBFS):
|
| 119 |
+
''' Normalize given audio chunk
|
| 120 |
+
The code normalizes an audio chunk to a target amplitude level given as `target_dBFS`.
|
| 121 |
+
It calculates the change in decibels (dBFS) required to achieve the target amplitude level and applies it to the input audio chunk.'''
|
| 122 |
+
change_in_dBFS = target_dBFS - aChunk.dBFS
|
| 123 |
+
return aChunk.apply_gain(change_in_dBFS)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def get_audio(self,audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False):
|
| 127 |
+
|
| 128 |
+
"""
|
| 129 |
+
The code defines a function named get_audio that takes in three parameters:
|
| 130 |
+
audio_path, ext and check_subfolders. It imports the necessary modules such as os and glob.
|
| 131 |
+
The function searches for audio files in a directory specified by the audio_path parameter and returns two lists;
|
| 132 |
+
the first list containing the absolute path of the audio files and the second list containing the relative path of the audio files.
|
| 133 |
+
The function has an optional parameter ext that takes a list of audio file extensions to be searched for, and check_subfolders
|
| 134 |
+
is another optional parameter that specifies whether to search for audio files recursively in subfolders or not.
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
import os
|
| 138 |
+
import glob
|
| 139 |
+
|
| 140 |
+
# List of audio file extensions to search for
|
| 141 |
+
# AUDIO_EXTENSIONS = ext
|
| 142 |
+
|
| 143 |
+
# Directory to search for audio files
|
| 144 |
+
directory = audio_path # Replace with the path to your directory
|
| 145 |
+
|
| 146 |
+
if check_subfolders == False:
|
| 147 |
+
# Search for audio files using glob
|
| 148 |
+
audio_files = []
|
| 149 |
+
for extension in ext:
|
| 150 |
+
audio_files.extend(glob.glob(directory + "*" + extension, recursive=check_subfolders))
|
| 151 |
+
else:
|
| 152 |
+
|
| 153 |
+
# Search for audio files using glob
|
| 154 |
+
audio_files = []
|
| 155 |
+
for extension in ext:
|
| 156 |
+
audio_files.extend(glob.glob(directory + "/**/" + extension, recursive=check_subfolders))
|
| 157 |
+
|
| 158 |
+
audio_base_names = [os.path.basename(i) for i in audio_files]
|
| 159 |
+
|
| 160 |
+
return audio_files, audio_base_names
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
# define function to play audio given input string and list of audio files
|
| 167 |
+
def play_audio(self,input_string_space_separated, list_of_audio_files_including_extension):
|
| 168 |
+
# import required module
|
| 169 |
+
from pydub import AudioSegment
|
| 170 |
+
|
| 171 |
+
# create dictionary of audio files and their corresponding inputs
|
| 172 |
+
files = self.get_audio_files_dictionary(list_of_audio_files_including_extension)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
# if the input is not in the dictionary, try reversing the input and checking again
|
| 176 |
+
if input_string_space_separated not in files:
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
input_string_space_separated = " ".join(input_string_space_separated.split()[::-1])
|
| 180 |
+
# print("...........",files[input_string_space_separated])
|
| 181 |
+
invert_dict = {}
|
| 182 |
+
invert_dict[input_string_space_separated] = files[input_string_space_separated][::-1]
|
| 183 |
+
files.update(invert_dict)
|
| 184 |
+
|
| 185 |
+
# create a silent audio segment for a 2 second pause between audio files
|
| 186 |
+
silence = AudioSegment.silent(duration=2000)
|
| 187 |
+
|
| 188 |
+
# get the list of audio files corresponding to the input string
|
| 189 |
+
audio_files = files.get(input_string_space_separated)
|
| 190 |
+
# print(audio_files)
|
| 191 |
+
|
| 192 |
+
# raise an error if the input string is invalid
|
| 193 |
+
if audio_files is None:
|
| 194 |
+
raise ValueError("Invalid input string value")
|
| 195 |
+
|
| 196 |
+
# create an empty audio segment to add the audio files to
|
| 197 |
+
combined_audio = AudioSegment.empty()
|
| 198 |
+
|
| 199 |
+
# loop through the list of audio files and add each one to the combined audio segment
|
| 200 |
+
for filename in audio_files:
|
| 201 |
+
audio_segment = AudioSegment.from_file(f"{filename}")
|
| 202 |
+
combined_audio += audio_segment + silence
|
| 203 |
+
|
| 204 |
+
# return the combined audio segment
|
| 205 |
+
return combined_audio
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def play_audio_with_various_space_between_chunks(self,song, min_cut_silence_len = 1.5, silence_padding_duration = 2.5):
|
| 209 |
+
|
| 210 |
+
"""
|
| 211 |
+
The function takes an audio file and an optional silence duration as input and plays the audio with various spaces between each chunk.
|
| 212 |
+
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
|
| 213 |
+
and then each chunk is padded with silence of duration silence_padding_duration.
|
| 214 |
+
For example, if min_cut_silence_len = 1.5, silence_padding_duration = 2.5
|
| 215 |
+
The function then normalizes each chunk and exports them as separate audio files in the mp3 format.
|
| 216 |
+
Finally, the function returns the entire audio file with the various spaces between each chunk as an AudioSegment object.
|
| 217 |
+
|
| 218 |
+
"""
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# Import the AudioSegment class for processing audio and the
|
| 222 |
+
# split_on_silence function for separating out silent chunks.
|
| 223 |
+
from pydub import AudioSegment
|
| 224 |
+
from pydub.silence import split_on_silence
|
| 225 |
+
import os
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
# Load your audio.
|
| 229 |
+
# song = AudioSegment.from_mp3(audio_file_path)
|
| 230 |
+
|
| 231 |
+
# Split track where the silence is 2 seconds or more and get chunks using
|
| 232 |
+
# the imported function.
|
| 233 |
+
chunks = split_on_silence (
|
| 234 |
+
# Use the loaded audio.
|
| 235 |
+
song,
|
| 236 |
+
# Specify that a silent chunk must be at least 2 seconds or 2000 ms long.
|
| 237 |
+
# min_silence_len = 2000,
|
| 238 |
+
min_silence_len = int(min_cut_silence_len*1000),
|
| 239 |
+
# Consider a chunk silent if it's quieter than -16 dBFS.
|
| 240 |
+
# (You may want to adjust this parameter.)
|
| 241 |
+
silence_thresh = -50
|
| 242 |
+
)
|
| 243 |
+
silence_duration = int(silence_padding_duration*1000)
|
| 244 |
+
|
| 245 |
+
audio_chunk = AudioSegment.silent(duration=1000)
|
| 246 |
+
silence_chunk = AudioSegment.silent(duration=silence_duration)
|
| 247 |
+
# Process each chunk with your parameters
|
| 248 |
+
for i, chunk in enumerate(chunks):
|
| 249 |
+
# Create a silence chunk that's 0.5 seconds (or 500 ms) long for padding.
|
| 250 |
+
|
| 251 |
+
# print(i, len(chunks))
|
| 252 |
+
# Add the padding chunk to beginning and end of the entire chunk.
|
| 253 |
+
|
| 254 |
+
if i < len(chunks)-1:
|
| 255 |
+
audio_chunk += chunk + silence_chunk
|
| 256 |
+
if i == len(chunks)-1:
|
| 257 |
+
audio_chunk += chunk
|
| 258 |
+
|
| 259 |
+
# Normalize the entire chunk.
|
| 260 |
+
normalized_chunk = self.match_target_amplitude(audio_chunk, -20.0)
|
| 261 |
+
|
| 262 |
+
# Export the audio chunk with new bitrate.
|
| 263 |
+
# print(i)
|
| 264 |
+
# print(f"Exporting chunk{i}.mp3.")
|
| 265 |
+
|
| 266 |
+
# normalized_chunk.export(
|
| 267 |
+
# # ".//chunk{0}.mp3".format(i),
|
| 268 |
+
# f"_part{i}.mp3",
|
| 269 |
+
# bitrate = "192k",
|
| 270 |
+
# format = "mp3"
|
| 271 |
+
# )
|
| 272 |
+
return audio_chunk, len(chunks)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def play_audio_combine(self,input, speed = 2):
|
| 276 |
+
|
| 277 |
+
audio_files = self.get_audio(self.audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"])
|
| 278 |
+
|
| 279 |
+
list_of_audio_files_including_extension = audio_files[0]
|
| 280 |
+
# get_audio_files_dictionary(list_of_audio_files_including_extension)
|
| 281 |
+
|
| 282 |
+
in_ = self.vowel_mapping(input)
|
| 283 |
+
audio_result = self.play_audio(in_,list_of_audio_files_including_extension)
|
| 284 |
+
audio_result_modified = self.play_audio_with_various_space_between_chunks(audio_result, silence_padding_duration = float(speed))
|
| 285 |
+
|
| 286 |
+
return audio_result_modified
|
| 287 |
+
|
| 288 |
+
def main_func(self,audio_path1, audio_path2,result_path):
|
| 289 |
+
import os
|
| 290 |
+
from pydub import AudioSegment
|
| 291 |
+
from natsort import natsorted
|
| 292 |
+
from os.path import basename
|
| 293 |
+
|
| 294 |
+
silent_end = AudioSegment.silent(duration=1000)
|
| 295 |
+
silent_begin = AudioSegment.silent(duration=3000)
|
| 296 |
+
|
| 297 |
+
audio_files_plus_ext1, audio_files1 = self.get_audio(audio_path1, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False)
|
| 298 |
+
audio_files_plus_ext2, audio_files2 = self.get_audio(audio_path2, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
# Sort the file names alphabtically
|
| 302 |
+
sorted_audio_file_names1 = natsorted(audio_files1, key=lambda x: x.lower())
|
| 303 |
+
sorted_audio_file_names2 = natsorted(audio_files2, key=lambda x: x.lower())
|
| 304 |
+
|
| 305 |
+
sorted_audio_file_names_ext1 = natsorted(audio_files_plus_ext1, key=lambda x: x.lower())
|
| 306 |
+
sorted_audio_file_names_ext2 = natsorted(audio_files_plus_ext2, key=lambda x: x.lower())
|
| 307 |
+
|
| 308 |
+
# Using a dictionary to map indices to filenames for faster lookup
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
file_dict2 = { ''.join(filter(str.isdigit, basename(path))): basename(path) for path in sorted_audio_file_names_ext1 }
|
| 312 |
+
|
| 313 |
+
# Create the mapping in a more efficient way
|
| 314 |
+
mapping_fast = { basename(path1): file_dict2.get(''.join(filter(str.isdigit, basename(path1))), None)
|
| 315 |
+
for path1 in sorted_audio_file_names_ext2 if ''.join(filter(str.isdigit, basename(path1))) in file_dict2 }
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
audio_path1_ = [audio_path1 + i for i in mapping_fast.values()]
|
| 320 |
+
|
| 321 |
+
audio_path2_ = [audio_path2+i for i in mapping_fast.keys()]
|
| 322 |
+
|
| 323 |
+
silent_2s = AudioSegment.silent(duration=2000)
|
| 324 |
+
|
| 325 |
+
song = silent_2s
|
| 326 |
+
|
| 327 |
+
padding_duration = 3
|
| 328 |
+
|
| 329 |
+
for i in range (0,len(audio_path2_)):
|
| 330 |
+
# i = 1
|
| 331 |
+
song_name = os.path.basename(audio_path1_[i]).split("_")[0] + "_" + os.path.basename(audio_path2_[i])
|
| 332 |
+
print(song_name)
|
| 333 |
+
|
| 334 |
+
song_1 = AudioSegment.from_mp3(audio_path1_[i])
|
| 335 |
+
song_2 = AudioSegment.from_mp3(audio_path2_[i])
|
| 336 |
+
|
| 337 |
+
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)
|
| 338 |
+
|
| 339 |
+
print("number_of_chunks:",number_of_chunks)
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
if number_of_chunks == 1:
|
| 343 |
+
|
| 344 |
+
songi = song_1 + silent_begin + song_2 + silent_begin + song_2
|
| 345 |
+
|
| 346 |
+
else:
|
| 347 |
+
|
| 348 |
+
songi = song_1 + silent_begin + song_2
|
| 349 |
+
|
| 350 |
+
songi.export(
|
| 351 |
+
# ".//chunk{0}.mp3".format(i),
|
| 352 |
+
result_path+"/"+song_name,
|
| 353 |
+
bitrate = "192k",
|
| 354 |
+
format = "mp3"
|
| 355 |
+
);
|
| 356 |
+
songi
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
audio_files_plus_ext, audio_files = self.get_audio(result_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False)
|
| 360 |
+
|
| 361 |
+
# Sort the file names alphabtically
|
| 362 |
+
audio_files = natsorted(audio_files, key=lambda x: x.lower())
|
| 363 |
+
|
| 364 |
+
audio_files_plus_ext = natsorted(audio_files_plus_ext, key=lambda x: x.lower())
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
song = silent_begin
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
for i in range (0,len(audio_files_plus_ext)):
|
| 372 |
+
# i = 1
|
| 373 |
+
song_name = os.path.basename(audio_files_plus_ext[i])
|
| 374 |
+
print(song_name)
|
| 375 |
+
# song_name = f"{audio_path}reworked/pp_"+song_name
|
| 376 |
+
|
| 377 |
+
song_i = AudioSegment.from_mp3(audio_files_plus_ext[i])
|
| 378 |
+
|
| 379 |
+
# song += song_i+silent_end+flip_page_sound+silent_begin
|
| 380 |
+
|
| 381 |
+
song += song_i
|
| 382 |
+
|
| 383 |
+
song_name = f"{result_path}1_CombineAudio.mp3"
|
| 384 |
+
|
| 385 |
+
song.export(
|
| 386 |
+
# ".//chunk{0}.mp3".format(i),
|
| 387 |
+
song_name,
|
| 388 |
+
bitrate = "192k",
|
| 389 |
+
format = "mp3"
|
| 390 |
+
);
|
| 391 |
+
|
| 392 |
+
audio_path1 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language1/"
|
| 393 |
+
audio_path2 = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/Language2/"
|
| 394 |
+
|
| 395 |
+
result_path = "/content/drive/MyDrive/Resulam/Mbú'ŋwɑ̀'nì/test_audio/res/"
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
player = AGLC(audio_path1)
|
| 399 |
+
# player_yoruba = AGLC(audio_path_yoruba)
|
| 400 |
+
|
| 401 |
+
player.main_func(audio_path1, audio_path2,result_path)
|
| 402 |
+
|
| 403 |
+
def main(audio_path1, audio_path2,result_path):
|
| 404 |
+
player.main_func(audio_path1, audio_path2,result_path)
|
| 405 |
+
|
| 406 |
+
def normalize_audio_for_ACX_Amazon(song):
|
| 407 |
+
|
| 408 |
+
desired_sample_rate = 44100 # Replace this with your desired sample rate
|
| 409 |
+
song = song.set_frame_rate(desired_sample_rate)
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
desired_dBFS = -20 # Target volume level in dBFS
|
| 413 |
+
current_dBFS = song.dBFS
|
| 414 |
+
gain_needed = desired_dBFS - current_dBFS
|
| 415 |
+
|
| 416 |
+
song_with_desired_volume = song.apply_gain(gain_needed)
|
| 417 |
+
# song_with_desired_volume.export("/content/output.mp3", format="mp3", bitrate="192000")
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
return song_with_desired_volume
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
"""# Build Interface With Gradio"""
|
| 425 |
+
|
| 426 |
+
pip install gradio
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
import gradio as gr
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
iface = gr.Interface(fn=main,
|
| 435 |
+
inputs=[gr.Textbox(label="Laguage1 Path: "),
|
| 436 |
+
gr.Textbox(value=1, label="Laguage2 Path: "),
|
| 437 |
+
gr.Textbox(label="Results Path: ")],
|
| 438 |
+
outputs="audio",
|
| 439 |
+
title="Combine Audios")
|
| 440 |
+
|
| 441 |
+
iface.launch(share=True,debug=True)
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
# pip install transformers
|
| 448 |
+
|
| 449 |
+
demo = gr.Blocks()
|
| 450 |
+
|
| 451 |
+
with demo:
|
| 452 |
+
input_func1 = gr.Audio(type="filepath")
|
| 453 |
+
output_func1 = gr.Textbox()
|
| 454 |
+
output_func2 = gr.Label()
|
| 455 |
+
|
| 456 |
+
b1 = gr.Button("Recognize Speech")
|
| 457 |
+
b2 = gr.Button("Classify Sentiment")
|
| 458 |
+
|
| 459 |
+
b1.click(Func1, inputs=input_func1, outputs=output_func1)
|
| 460 |
+
b2.click(Func2, inputs=output_func1, outputs=output_func2)
|
| 461 |
+
|
| 462 |
+
demo.launch(share=True)
|
| 463 |
+
|
| 464 |
+
# get_audio(audio_path, ext = ["*.mp3", "*.wav", "*.ogg", "*.flac"],check_subfolders=False):
|
| 465 |
+
# return audio_files, audio_base_names
|
| 466 |
+
|
| 467 |
+
# def get_audio_files_dictionary(list_of_audio_files_including_extension):
|
| 468 |
+
# return new_dict
|
| 469 |
+
|
| 470 |
+
# play_audio(input_string_space_separated, list_of_audio_files_including_extension):
|
| 471 |
+
# return combined_audio
|
| 472 |
+
|
| 473 |
+
# play_audio_with_various_space_between_chunks(song, silence_duration = .5):
|
| 474 |
+
# return audio_chunk
|
| 475 |
+
|
| 476 |
+
# list_of_audio_files_including_extension = get_audio()[0]
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
# audio_result = play_audio("bǎ bá",list_of_audio_files_including_extension)
|
| 480 |
+
# audio_result_modified = play_audio_with_various_space_between_chunks(audio_result, silence_duration = .2)
|
| 481 |
+
|
| 482 |
+
# gr.Textbox(value=1, label="Duration in seconds"),
|
| 483 |
+
|
| 484 |
+
# inputs=[gr.Textbox(label="words: Example: ǎ bà"),
|
| 485 |
+
# gr.Textbox(value=1, label="Duration in seconds"),
|
| 486 |
+
# gr.Textbox(label="Path")],
|
| 487 |
+
|
| 488 |
+
def func1(x):
|
| 489 |
+
x = float(x)
|
| 490 |
+
return x**2
|
| 491 |
+
|
| 492 |
+
def func2(y):
|
| 493 |
+
y = float(y)
|
| 494 |
+
return y**2
|
| 495 |
+
|
| 496 |
+
def func3(a):
|
| 497 |
+
a = float(a)
|
| 498 |
+
# b = float(b)
|
| 499 |
+
return 2*a
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
demo = gr.Blocks()
|
| 504 |
+
|
| 505 |
+
with demo:
|
| 506 |
+
input_func1 = gr.Textbox(label = "val1")
|
| 507 |
+
input_func2 = gr.Textbox(label = "val2")
|
| 508 |
+
# input_func3 = [gr.Textbox(label = "val3")
|
| 509 |
+
|
| 510 |
+
output_func1 = gr.Label(label = "result1")
|
| 511 |
+
output_func2 = gr.Label(label = "result2")
|
| 512 |
+
output_func3 = gr.Label(label = "result3")
|
| 513 |
+
|
| 514 |
+
b1 = gr.Button("x^2")
|
| 515 |
+
b2 = gr.Button("y^2")
|
| 516 |
+
b3 = gr.Button("x^2+y^2")
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
b1.click(func1, inputs=input_func1, outputs=output_func1)
|
| 520 |
+
b2.click(func2, inputs=input_func2, outputs=output_func2)
|
| 521 |
+
b3.click(func3, inputs=output_func1, outputs=output_func3)
|
| 522 |
+
|
| 523 |
+
# b2.click(play_audio, inputs=output_func1, outputs=output_func2)
|
| 524 |
+
|
| 525 |
+
demo.launch(share=True, debug=True)
|
| 526 |
+
|
| 527 |
+
def main_func(input_string, speed = 1):
|
| 528 |
+
audio_result = play_audio(input_string)
|
| 529 |
+
|
| 530 |
+
audio_result_modified = play_audio_with_various_space_between_chunks(audio_result, silence_duration = float(speed))
|
| 531 |
+
return audio_result_modified
|
| 532 |
+
|
| 533 |
+
main_func("ǎ bà",".1")
|