-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (107 loc) · 5.17 KB
/
Program.cs
File metadata and controls
125 lines (107 loc) · 5.17 KB
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
using Logger;
using OwnaudioNET.Features.Vocalremover;
namespace HTDemucsExample
{
/// <summary>
/// Example program demonstrating HTDemucs audio stem separation
/// Separates audio into 4 stems: vocals, drums, bass, and other instruments
/// </summary>
class Program
{
static void Main(string[] args)
{
Log.Info("HTDemucs Audio Stem Separation Example");
Log.Info("======================================");
// Configuration
string audioFilePath = @"path/to/your";
string outputDirectory = @"path/to/output";
// Check if example paths need to be updated
if (audioFilePath.Contains("path/to/your"))
{
Log.Info("Please update the audioFilePath in Program.cs");
Log.Info("Press any key to exit...");
Console.ReadKey();
return;
}
try
{
// Create separation options using EMBEDDED htdemucs.onnx model
var options = new HTDemucsSeparationOptions
{
Model = InternalModel.HTDemucs, // Use embedded model
OutputDirectory = outputDirectory,
ChunkSizeSeconds = 10, // Process in 10-second chunks
OverlapFactor = 0.25f, // 25% overlap between chunks
EnableGPU = true, // Use GPU if available
TargetStems = HTDemucsStem.All // Extract all stems
};
Log.Info($"Input file: {audioFilePath}");
Log.Info($"Model: Embedded HTDemucs");
Log.Info($"Output directory: {outputDirectory}");
Log.Info($"Chunk size: {options.ChunkSizeSeconds}s");
Log.Info($"GPU acceleration: {(options.EnableGPU ? "Enabled" : "Disabled")}");
// Alternative: Use external model file
// options.ModelPath = @"path/to/htdemucs.onnx";
// options.Model = InternalModel.None;
// Create and initialize the separator
using var separator = new HTDemucsAudioSeparator(options);
// Subscribe to progress events
separator.ProgressChanged += (s, progress) =>
{
// Készíts EGY teljes status stringet
string statusLine = $"{progress.Status}: {progress.OverallProgress:F1}%";
if (progress.TotalChunks > 0)
{
statusLine += $" [{progress.ProcessedChunks}/{progress.TotalChunks} chunks]";
}
// PadRight(80) törli a régi hosszabb szöveget
Console.Write($"\r{statusLine.PadRight(80)}");
};
separator.ProcessingCompleted += (s, result) =>
{
Log.Info($"\nProcessing completed in {result.ProcessingTime.TotalSeconds:F1}s");
Log.Info($"Audio duration: {result.AudioDuration.TotalSeconds:F1}s");
Log.Info($"Realtime factor: {result.AudioDuration.TotalSeconds / result.ProcessingTime.TotalSeconds:F1}x");
};
// Initialize model
Log.Info("Initializing HTDemucs model...");
separator.Initialize();
Log.Info("Model initialized successfully!");
// Process the audio file
Log.Info("Starting stem separation...");
var result = separator.Separate(audioFilePath);
// Display results
Log.Info("Separation Results:");
Log.Info("==================");
foreach (var stem in result.StemPaths)
{
var fileInfo = new FileInfo(stem.Value);
Log.Info($" {stem.Key,-8}: {Path.GetFileName(stem.Value)} ({fileInfo.Length / 1024.0 / 1024.0:F2} MB)");
}
Log.Info($"All stems saved to: {outputDirectory}");
// Example: Using helper extension methods
Log.Info("You can also use helper methods:");
Log.Info(" var separator = HTDemucsExtensions.CreateDefaultSeparator();");
Log.Info(" var separator = HTDemucsExtensions.CreateStemSelector(HTDemucsStem.Vocals | HTDemucsStem.Other);");
}
catch (FileNotFoundException ex)
{
Log.Info($"\nError - File not found: {ex.Message}");
Log.Info("Please check that the audio file path is correct.");
}
catch (InvalidOperationException ex)
{
Log.Info($"\nError - Invalid operation: {ex.Message}");
Log.Info("The embedded htdemucs.onnx model may be missing from the assembly.");
}
catch (Exception ex)
{
Log.Info($"\nError occurred: {ex.Message}");
Log.Info($"Stack trace: {ex.StackTrace}");
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}