lainlives commited on
Commit
c5dc401
·
1 Parent(s): 2247d0e
Files changed (1) hide show
  1. app.py +80 -0
app.py CHANGED
@@ -2505,6 +2505,86 @@ def _render_step_4(tab_config: MultiStepSongGenerationConfig) -> None:
2505
  )
2506
 
2507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2508
  def _render_step_5(
2509
  total_config: TotalConfig,
2510
  tab_config: MultiStepSongGenerationConfig,
 
2505
  )
2506
 
2507
 
2508
+ def separate_audio(
2509
+ audio_track: StrPath,
2510
+ song_dir: StrPath,
2511
+ model_name: SeparationModel,
2512
+ segment_size: int,
2513
+ ) -> tuple[Path, Path]:
2514
+ """
2515
+ Separate an audio track into a primary stem and a secondary stem.
2516
+
2517
+ Parameters
2518
+ ----------
2519
+ audio_track : StrPath
2520
+ The path to the audio track to separate.
2521
+ song_dir : StrPath
2522
+ The path to the song directory where the separated primary stem
2523
+ and secondary stem will be saved.
2524
+ model_name : str
2525
+ The name of the model to use for audio separation.
2526
+ segment_size : int
2527
+ The segment size to use for audio separation.
2528
+
2529
+ Returns
2530
+ -------
2531
+ primary_path : Path
2532
+ The path to the separated primary stem.
2533
+ secondary_path : Path
2534
+ The path to the separated secondary stem.
2535
+
2536
+ """
2537
+ audio_path = validate_audio_file_exists(audio_track, Entity.AUDIO_TRACK)
2538
+ song_dir_path = validate_audio_dir_exists(song_dir, Entity.SONG_DIR)
2539
+
2540
+ args_dict = SeparatedAudioMetaData(
2541
+ audio_track=FileMetaData(
2542
+ name=audio_path.name,
2543
+ hash_id=get_file_hash(audio_path),
2544
+ ),
2545
+ model_name=model_name,
2546
+ segment_size=segment_size,
2547
+ ).model_dump()
2548
+
2549
+ paths = [
2550
+ get_unique_base_path(
2551
+ song_dir_path,
2552
+ prefix,
2553
+ args_dict,
2554
+ ).with_suffix(suffix)
2555
+ for prefix in ["11_Stem_Primary", "11_Stem_Secondary"]
2556
+ for suffix in [".wav", ".json"]
2557
+ ]
2558
+
2559
+ (
2560
+ primary_path,
2561
+ primary_json_path,
2562
+ secondary_path,
2563
+ secondary_json_path,
2564
+ ) = paths
2565
+
2566
+ if not all(path.exists() for path in paths):
2567
+ audio_separator = _get_audio_separator(
2568
+ output_dir=song_dir_path,
2569
+ segment_size=segment_size,
2570
+ )
2571
+ audio_separator.load_model(model_name)
2572
+ audio_separator.separate(
2573
+ str(audio_path),
2574
+ custom_output_names={
2575
+ audio_separator.model_instance.primary_stem_name: str(
2576
+ primary_path.with_suffix(""),
2577
+ ),
2578
+ audio_separator.model_instance.secondary_stem_name: str(
2579
+ secondary_path.with_suffix(""),
2580
+ ),
2581
+ },
2582
+ )
2583
+ json_dump(args_dict, primary_json_path)
2584
+ json_dump(args_dict, secondary_json_path)
2585
+
2586
+ return primary_path, secondary_path
2587
+
2588
  def _render_step_5(
2589
  total_config: TotalConfig,
2590
  tab_config: MultiStepSongGenerationConfig,