-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsource-separation-uvr.swift
More file actions
64 lines (55 loc) · 1.72 KB
/
source-separation-uvr.swift
File metadata and controls
64 lines (55 loc) · 1.72 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
// swift-api-examples/source-separation-uvr.swift
//
// Copyright (c) 2026 Xiaomi Corporation
// This file demonstrates how to use source separation with the
// UVR (MDX-Net) model.
//
// Please refer to
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/source-separation-models
// to download files used in this script
import Foundation
func run() {
let config = SourceSeparationConfig(
uvr: .init(model: "./UVR-MDX-NET-Voc_FT.onnx"),
numThreads: 1,
debug: true
)
guard let separator = SourceSeparator(config: config) else {
print("Failed to create SourceSeparator")
return
}
guard let input = AudioData(filename: "./qi-feng-le-zh.wav") else {
print("Failed to read ./qi-feng-le-zh.wav")
return
}
print(
"Input: channels=\(input.channelCount), samples=\(input.samplesPerChannel), sampleRate=\(input.sampleRate)"
)
let start = CFAbsoluteTimeGetCurrent()
guard let stems = separator.process(buffer: input) else {
print("Source separation failed")
return
}
let elapsed = CFAbsoluteTimeGetCurrent() - start
let audioDuration = Double(input.samplesPerChannel) / Double(input.sampleRate)
let rtf = elapsed / audioDuration
print("Output: \(stems.count) stems, sampleRate=\(input.sampleRate)")
print(
"Elapsed: \(String(format: "%.2f", elapsed))s, Audio: \(String(format: "%.2f", audioDuration))s, RTF = \(String(format: "%.3f", rtf))"
)
let stemNames = ["uvr-vocals", "uvr-non-vocals"]
for i in 0..<min(stems.count, stemNames.count) {
let filename = "\(stemNames[i]).wav"
if stems[i].save(to: filename) {
print("Saved \(filename)")
} else {
print("Failed to save \(filename)")
}
}
}
@main
struct App {
static func main() {
run()
}
}