File size: 4,937 Bytes
a8cb0a6 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
from MusicAnalyzer.musicAnalyzer import preProcess
from MusicAnalyzer.commandGenerator import generateCommands
import os
import multiprocessing as mp
import pickle as pkl
import numpy as np
import soundfile as sf
import librosa
def preProcess_wrapper(params):
(
id,
audio_part,
sr,
instruments_dict,
scaling_dict,
initialBestMatchesLength,
simThresh,
binLength,
audio_id_dir_path,
amplitudeMode,
) = params
return id, preProcess(
audio_part,
sr,
instruments_dict,
scaling_dict,
initialBestMatchesLength,
simThresh,
binLength,
audio_id_dir_path,
amplitudeMode,
)
def combine_parallel_processing_results(results):
out = []
ct = 0
for data in results:
infos = data[1]
for info in infos:
out.append((ct, info[1]))
ct += 100
return out
def combine_results_sounds_files(sounds_folder_path, results_file_name):
combined_audio = np.array([])
audio_files_name = []
for file_name in os.listdir(sounds_folder_path):
if file_name[: len(results_file_name)] == results_file_name:
audio_files_name.append(file_name)
sr = 0
audio_files_name = sorted(audio_files_name)
for audio_file_name in audio_files_name:
audio_file_path = os.path.join(sounds_folder_path, audio_file_name)
audio, sr = librosa.load(audio_file_path, sr=None)
combined_audio = np.concatenate((combined_audio, audio))
os.remove(audio_file_path)
results_file_path = os.path.join(sounds_folder_path, results_file_name + ".mp3")
sf.write(results_file_path, combined_audio, sr, format="MP3")
return combined_audio, sr
def call_file_processing_logic_parallely(
mainAudioValues,
sr,
instruments_dict,
scaling_dict,
initialBestMatchesLength,
simThresh,
binLength,
audio_id_dir_path,
amplitudeMode,
parallel_processes_count
):
split_size = len(mainAudioValues) // parallel_processes_count
audio_parts = [
mainAudioValues[i * split_size : (i + 1) * split_size]
for i in range(parallel_processes_count)
]
audio_parts[-1] = mainAudioValues[(parallel_processes_count - 1) * split_size :]
params_list = [
(
i,
audio_parts[i],
sr,
instruments_dict,
scaling_dict,
initialBestMatchesLength,
simThresh,
binLength,
os.path.join(audio_id_dir_path, f"processed-{amplitudeMode}-{i}.mp3"),
amplitudeMode,
)
for i in range(parallel_processes_count)
]
with mp.Pool(processes=parallel_processes_count) as pool:
results = pool.map(preProcess_wrapper, params_list)
results = combine_parallel_processing_results(results)
if os.path.exists(
os.path.join(audio_id_dir_path, f"processed-{amplitudeMode}.mp3")
):
os.remove(os.path.join(audio_id_dir_path, f"processed-{amplitudeMode}.mp3"))
processed_audio, sr = combine_results_sounds_files(
audio_id_dir_path, f"processed-{amplitudeMode}"
)
with open(
os.path.join(audio_id_dir_path, f"result-{amplitudeMode}.pkl"), "wb"
) as f:
pkl.dump(results, f)
return processed_audio, sr
def convert_to_serializable(obj):
if isinstance(obj, dict):
return {k: convert_to_serializable(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_to_serializable(i) for i in obj]
elif isinstance(obj, tuple):
return tuple(convert_to_serializable(i) for i in obj)
elif isinstance(obj, np.ndarray):
return list(convert_to_serializable(i) for i in obj)
elif isinstance(obj, np.float32):
return float(obj)
else:
return obj
# # def create_zip_from_audios(sounds_folder_path):
# # import io
# # import zipfile
# # zip_buffer = io.BytesIO()
# # with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:
# # for filename in os.listdir(sounds_folder_path):
# # audio_id_dir_path = os.path.join(sounds_folder_path, filename)
# # zipf.write(audio_id_dir_path, os.path.basename(audio_id_dir_path))
# # zip_buffer.seek(0)
# # return zip_buffer
def call_command_generator(
data,
music_box_dict,
amplitude_dict,
hearable_range,
one_hundred_milli_horizontal_gap,
starting_coordinates,
one_floor_vertical_gap,
instant_repeater_zs,
pitch_mapping_shift,
sim_thresh,
):
return generateCommands(
data,
music_box_dict,
amplitude_dict,
hearable_range,
one_hundred_milli_horizontal_gap,
starting_coordinates,
one_floor_vertical_gap,
instant_repeater_zs,
pitch_mapping_shift,
sim_thresh,
)
|