-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsource-separation-spleeter.swift
More file actions
69 lines (59 loc) · 1.87 KB
/
source-separation-spleeter.swift
File metadata and controls
69 lines (59 loc) · 1.87 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
// swift-api-examples/source-separation-spleeter.swift
//
// Copyright (c) 2026 Xiaomi Corporation
// This file demonstrates how to use source separation with the
// Spleeter 2-stems model (UNet-based).
//
// 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 modelDir = "./sherpa-onnx-spleeter-2stems-fp16"
let config = SourceSeparationConfig(
spleeter: .init(
vocals: "\(modelDir)/vocals.fp16.onnx",
accompaniment: "\(modelDir)/accompaniment.fp16.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 = ["vocals", "accompaniment"]
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()
}
}