|
| 1 | +// c-api-examples/source-separation-spleeter-c-api.c |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Xiaomi Corporation |
| 4 | + |
| 5 | +// This file demonstrates how to use the source-separation C API |
| 6 | +// with the Spleeter 2-stems model. |
| 7 | +// |
| 8 | +// Usage: |
| 9 | +// |
| 10 | +// 1. Download the test model and audio |
| 11 | +// |
| 12 | +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/source-separation-models/sherpa-onnx-spleeter-2stems-fp16.tar.bz2 |
| 13 | +// tar xjf sherpa-onnx-spleeter-2stems-fp16.tar.bz2 |
| 14 | +// |
| 15 | +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/source-separation-models/qi-feng-le-zh.wav |
| 16 | +// |
| 17 | +// 2. Build |
| 18 | +// |
| 19 | +// cmake -DSHERPA_ONNX_ENABLE_C_API=ON .. |
| 20 | +// make source-separation-spleeter-c-api |
| 21 | +// |
| 22 | +// 3. Run |
| 23 | +// |
| 24 | +// ./bin/source-separation-spleeter-c-api |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "sherpa-onnx/c-api/c-api.h" |
| 31 | + |
| 32 | +int32_t main() { |
| 33 | + SherpaOnnxOfflineSourceSeparationConfig config; |
| 34 | + memset(&config, 0, sizeof(config)); |
| 35 | + config.model.spleeter.vocals = |
| 36 | + "./sherpa-onnx-spleeter-2stems-fp16/vocals.fp16.onnx"; |
| 37 | + config.model.spleeter.accompaniment = |
| 38 | + "./sherpa-onnx-spleeter-2stems-fp16/accompaniment.fp16.onnx"; |
| 39 | + config.model.num_threads = 1; |
| 40 | + |
| 41 | + const SherpaOnnxOfflineSourceSeparation *ss = |
| 42 | + SherpaOnnxCreateOfflineSourceSeparation(&config); |
| 43 | + if (!ss) { |
| 44 | + fprintf(stderr, "Failed to create source separation engine\n"); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + const SherpaOnnxMultiChannelWave *wave = |
| 49 | + SherpaOnnxReadWaveMultiChannel("./qi-feng-le-zh.wav"); |
| 50 | + if (!wave) { |
| 51 | + fprintf(stderr, "Failed to read ./qi-feng-le-zh.wav\n"); |
| 52 | + SherpaOnnxDestroyOfflineSourceSeparation(ss); |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + fprintf(stdout, "Input wave: channels=%d, samples_per_channel=%d, sample_rate=%d\n", |
| 57 | + wave->num_channels, wave->num_samples, wave->sample_rate); |
| 58 | + |
| 59 | + int32_t num_channels = wave->num_channels; |
| 60 | + |
| 61 | + const SherpaOnnxSourceSeparationOutput *output = |
| 62 | + SherpaOnnxOfflineSourceSeparationProcess( |
| 63 | + ss, wave->samples, num_channels, wave->num_samples, |
| 64 | + wave->sample_rate); |
| 65 | + |
| 66 | + if (!output) { |
| 67 | + fprintf(stderr, "Source separation failed\n"); |
| 68 | + SherpaOnnxFreeMultiChannelWave(wave); |
| 69 | + SherpaOnnxDestroyOfflineSourceSeparation(ss); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + |
| 73 | + fprintf(stdout, "Output: %d stems, sample_rate=%d\n", output->num_stems, |
| 74 | + output->sample_rate); |
| 75 | + |
| 76 | + // Write each stem to a separate multi-channel wave file. |
| 77 | + const char *stem_names[] = {"vocals", "accompaniment"}; |
| 78 | + for (int32_t s = 0; s < output->num_stems && s < 2; ++s) { |
| 79 | + int32_t nc = output->stems[s].num_channels; |
| 80 | + int32_t ns = output->stems[s].n; |
| 81 | + char filename[256]; |
| 82 | + snprintf(filename, sizeof(filename), "%s.wav", stem_names[s]); |
| 83 | + SherpaOnnxWriteWaveMultiChannel((const float *const *)output->stems[s].samples, ns, |
| 84 | + output->sample_rate, nc, filename); |
| 85 | + fprintf(stdout, "Saved %s (%d channels, %d samples, %d Hz)\n", filename, |
| 86 | + nc, ns, output->sample_rate); |
| 87 | + } |
| 88 | + |
| 89 | + // Cleanup |
| 90 | + SherpaOnnxDestroySourceSeparationOutput(output); |
| 91 | + SherpaOnnxFreeMultiChannelWave(wave); |
| 92 | + SherpaOnnxDestroyOfflineSourceSeparation(ss); |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
0 commit comments