aliceoq commited on
Commit
9aa27c3
1 Parent(s): ed9cf68

update overlay function

Browse files
Files changed (2) hide show
  1. app.py +18 -8
  2. requirements.txt +1 -1
app.py CHANGED
@@ -32,7 +32,8 @@ from config import Config
32
 
33
  from utils import load_audio, CSVutil
34
  import demucs.separate
35
- import audiosegment
 
36
 
37
  DoFormant = False
38
  Quefrency = 1.0
@@ -258,7 +259,7 @@ def vc_single(
258
  index_rate,
259
  if_f0,
260
  filter_radius,
261
- tgt_sr,
262
  resample_sr,
263
  rms_mix_rate,
264
  version,
@@ -273,7 +274,7 @@ def vc_single(
273
  if (overlay_audios_bool):
274
  t1 = time()
275
  progress(0.9, desc="Juntando vocal e instrumental...")
276
- (tgt_sr, audio_opt) = overlay_audios(tgt_sr, audio_opt, input_audio_path.replace("vocals", "no_vocals"))
277
  remove_separated_files(input_audio_path)
278
  t2 = time()
279
  times[4] = t2 - t1
@@ -473,14 +474,23 @@ def separate_vocals(audio_path):
473
  return vocals_path
474
  return None
475
 
476
- # aqui ainda não tá 100%
 
 
 
477
  def overlay_audios(sample_rate, np_array, accompaniment_path):
478
  if (not os.path.exists(accompaniment_path)):
479
  return (sample_rate, np_array)
480
- sound1 = audiosegment.from_numpy_array(np_array, sample_rate)
481
- sound2 = audiosegment.from_file(accompaniment_path)
482
- overlay = sound1.overlay(sound2, position=0)
483
- return (overlay.frame_rate, overlay.to_numpy_array())
 
 
 
 
 
 
484
 
485
  def remove_separated_files(vocals_path):
486
  parent_dir = os.path.dirname(vocals_path)
 
32
 
33
  from utils import load_audio, CSVutil
34
  import demucs.separate
35
+ import scipy.io.wavfile as wav
36
+ from pydub import AudioSegment
37
 
38
  DoFormant = False
39
  Quefrency = 1.0
 
259
  index_rate,
260
  if_f0,
261
  filter_radius,
262
+ 40000,
263
  resample_sr,
264
  rms_mix_rate,
265
  version,
 
274
  if (overlay_audios_bool):
275
  t1 = time()
276
  progress(0.9, desc="Juntando vocal e instrumental...")
277
+ (tgt_sr, audio_opt) = overlay_audios(40000, audio_opt, input_audio_path.replace("vocals", "no_vocals"))
278
  remove_separated_files(input_audio_path)
279
  t2 = time()
280
  times[4] = t2 - t1
 
474
  return vocals_path
475
  return None
476
 
477
+ def pydub_to_np(audio):
478
+ return audio.frame_rate, np.array(audio.get_array_of_samples(), dtype=np.float32).reshape((-1, audio.channels)) / (
479
+ 1 << (8 * audio.sample_width - 1))
480
+
481
  def overlay_audios(sample_rate, np_array, accompaniment_path):
482
  if (not os.path.exists(accompaniment_path)):
483
  return (sample_rate, np_array)
484
+
485
+ converted_vocals_path = accompaniment_path.replace('no_vocals', 'converted_vocals')
486
+ wav.write(converted_vocals_path, sample_rate, np_array)
487
+
488
+ sound1 = AudioSegment.from_file(accompaniment_path)
489
+ sound2 = AudioSegment.from_file(converted_vocals_path)
490
+
491
+ combined = sound2.overlay(sound1)
492
+ sample_rate, np_array = pydub_to_np(combined)
493
+ return (sample_rate, np_array)
494
 
495
  def remove_separated_files(vocals_path):
496
  parent_dir = os.path.dirname(vocals_path)
requirements.txt CHANGED
@@ -21,5 +21,5 @@ onnxruntime
21
  pyngrok==4.1.12
22
  torch
23
  yt-dlp==2023.07.06
24
- audiosegment==0.23.0
25
  demucs
 
21
  pyngrok==4.1.12
22
  torch
23
  yt-dlp==2023.07.06
24
+ pydub
25
  demucs