audio_cpp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +15 -0
- data/LICENSE +21 -0
- data/README.md +320 -0
- data/docs/precompiled-gems.md +24 -0
- data/ext/audio_cpp/builder.rb +260 -0
- data/ext/audio_cpp/extconf.rb +38 -0
- data/lib/audio_cpp/audio.rb +109 -0
- data/lib/audio_cpp/errors.rb +70 -0
- data/lib/audio_cpp/handle.rb +35 -0
- data/lib/audio_cpp/library_path.rb +38 -0
- data/lib/audio_cpp/model.rb +221 -0
- data/lib/audio_cpp/models.rb +177 -0
- data/lib/audio_cpp/native.rb +263 -0
- data/lib/audio_cpp/registry.rb +55 -0
- data/lib/audio_cpp/request.rb +68 -0
- data/lib/audio_cpp/resampler.rb +38 -0
- data/lib/audio_cpp/result.rb +272 -0
- data/lib/audio_cpp/session.rb +182 -0
- data/lib/audio_cpp/speakers.rb +60 -0
- data/lib/audio_cpp/stream.rb +154 -0
- data/lib/audio_cpp/version.rb +10 -0
- data/lib/audio_cpp/wav.rb +110 -0
- data/lib/audio_cpp.rb +113 -0
- metadata +85 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 017f1693f655f31b13a62c250610c7925070ceffaeafdb377fad3326d927e9c6
|
|
4
|
+
data.tar.gz: a2b0185f74fb7d7ce8026cc0faccd5297105643a0d593780ea4f21f1ce249409
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0de906e14369c6b7edcae61b0e9a0f23eb2d56af7865477e4b1367cfebf3a4426e2db2f460fc8ec38201eea2cffd9a4420b8869c324f7c911cf73faa4319602e
|
|
7
|
+
data.tar.gz: c4a77007434f242a752195ec0160572ff3aee14ae186025e61b10dad96d3674a1ac21116076baeca1d411a0b33531e8d61410827f5ea5c5480417c20e46b26ab
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (unreleased)
|
|
4
|
+
|
|
5
|
+
- First version: FFI bindings to every function of audio.cpp's C API (ABI 0.4), with an ABI check at load.
|
|
6
|
+
- `AudioCpp::Model`, `Session`, `Stream`: block forms, `#close`, GC finalizers without double frees; native calls serialised per model; long calls release the GVL.
|
|
7
|
+
- `Session#transcribe`, `#diarize`, `#synthesize`, `#run`, `#stream`, `#stream_audio`; results as plain Ruby values (`Result`, `Event`, `Word`, `Segment`, `SpeakerTurn`, `Artifact`).
|
|
8
|
+
- `AudioCpp::Speakers.assign`: words of a transcript to the speakers of a diarization.
|
|
9
|
+
- `AudioCpp::Models`: pinned, SHA-256-checked, resumable downloads of the tested GGUFs.
|
|
10
|
+
- `examples/rails_demo`: a Rails 8 demo app (not part of the gem).
|
|
11
|
+
- Pure-Ruby WAV reader and writer (16-bit PCM, 32-bit float) and a linear resampler.
|
|
12
|
+
- The gem builds libaudiocpp at install time from audio.cpp `amx-2026-12-02-97bf100` (`dd52d2f`), SHA-verified: by default seven popular families (`AUDIOCPP_MODELS=full` for all, or a list); `AUDIOCPP_LIB` skips the build.
|
|
13
|
+
- Loading a model of a family that is not built in says so and names the `AUDIOCPP_MODELS` to reinstall with; `AudioCpp.build_info`.
|
|
14
|
+
- `AudioCpp::Models.fetch` takes `revision:`, `sha256:` and `size:` overrides.
|
|
15
|
+
- The library is installed as CMake lays it out (versioned file plus symlinks), so its SONAME / install name points at a real file.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ruslan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# audio_cpp
|
|
2
|
+
|
|
3
|
+
Ruby bindings to [audio.cpp](https://github.com/0xShug0/audio.cpp), a C++/ggml runtime for audio models: speech recognition (offline and streaming), speaker diarization, text to speech and more, in-process, on the CPU or a GPU.
|
|
4
|
+
|
|
5
|
+
The gem talks to audio.cpp's stable C API (`include/audiocpp.h`) through [FFI](https://github.com/ffi/ffi) and adds a small Ruby layer on top: models and sessions with deterministic cleanup, results as plain Ruby values, pull-based streaming, and a pure-Ruby WAV reader and writer.
|
|
6
|
+
|
|
7
|
+
Bindings to audio.cpp; not affiliated with its authors.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
gem install audio_cpp
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
There is no prebuilt binary yet: `gem install` fetches audio.cpp at a pinned commit, verifies the commit SHA, and builds `libaudiocpp` with CMake into the gem's directory. You need `cmake` (3.21+), `git` and a C++17 compiler (Debian/Ubuntu: `apt install cmake g++ git`; macOS: `xcode-select --install && brew install cmake`). `ninja` is used when present.
|
|
16
|
+
|
|
17
|
+
By default the gem builds a short list of popular families (about 1.5 minutes with 8 jobs on a recent x86 server; longer on a laptop):
|
|
18
|
+
|
|
19
|
+
| Family | What |
|
|
20
|
+
|---|---|
|
|
21
|
+
| `gigaam_asr` | GigaAM v3/v4/Multilingual speech recognition, offline and streaming |
|
|
22
|
+
| `crisperwhisper` | Whisper (`openai/whisper-*` checkpoints, e.g. large-v3-turbo) and CrisperWhisper, offline and streaming |
|
|
23
|
+
| `parakeet_tdt` | NVIDIA Parakeet-TDT, offline and streaming |
|
|
24
|
+
| `nemotron_3_diar` | Nemotron 3 Diarization (who spoke when) |
|
|
25
|
+
| `kokoro_tts` | Kokoro 82M text to speech (most languages need eSpeak-ng data, see audio.cpp's `docs/espeak_phonemizer.md`) |
|
|
26
|
+
| `gigaedge_tts` | GigaEdge TTS (Russian) |
|
|
27
|
+
| `cosyvoice3` | CosyVoice3 text to speech and voice cloning |
|
|
28
|
+
|
|
29
|
+
Silero VAD and MarbleNet VAD are always built in. Every family takes longer (about 5 minutes with 8 jobs on the same machine); a list builds exactly what it names:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
AUDIOCPP_MODELS=full gem install audio_cpp
|
|
33
|
+
AUDIOCPP_MODELS=gigaam_asr,nemotron_3_diar gem install audio_cpp
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`AudioCpp.families` lists what the installed library has, and `AudioCpp.build_info` how it was built. Loading a model of a family that is not built in raises an error that says so and which `AUDIOCPP_MODELS` to reinstall with.
|
|
37
|
+
|
|
38
|
+
To use a `libaudiocpp` you built yourself (with `-DAUDIOCPP_BUILD_C_API=ON`), skip the build:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
AUDIOCPP_LIB=/path/to/libaudiocpp.so gem install audio_cpp
|
|
42
|
+
export AUDIOCPP_LIB=/path/to/libaudiocpp.so # at run time too
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
At run time the library is looked up in this order: `ENV["AUDIOCPP_LIB"]`, the copy built into the gem, then the system loader's paths. The gem refuses a library with another C ABI major (or an older minor) than it was written for, with an `AudioCpp::AbiMismatchError`.
|
|
46
|
+
|
|
47
|
+
| Build variable | Meaning |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `AUDIOCPP_MODELS` | unset: the default list above; `full`: every family; `core`: only the built-in VADs; or a comma-separated list of model targets |
|
|
50
|
+
| `AUDIOCPP_LIB` | use this library, skip the build |
|
|
51
|
+
| `AUDIOCPP_JOBS` | parallel build jobs (default: processor count) |
|
|
52
|
+
| `AUDIOCPP_NATIVE` | `0` for portable CPU kernels instead of `-march=native` |
|
|
53
|
+
| `AUDIOCPP_CMAKE_ARGS` | extra CMake arguments, e.g. `-DENGINE_ENABLE_VULKAN=ON` or `-DENGINE_ENABLE_CUDA=ON` |
|
|
54
|
+
| `AUDIOCPP_SOURCE_DIR` | build this audio.cpp checkout instead of fetching one |
|
|
55
|
+
| `AUDIOCPP_GIT_URL`, `AUDIOCPP_GIT_REF`, `AUDIOCPP_COMMIT` | fetch another remote, ref or commit (the fetched commit must equal `AUDIOCPP_COMMIT`) |
|
|
56
|
+
| `AUDIOCPP_BUILD_DIR` | keep the sources and build tree there for incremental rebuilds |
|
|
57
|
+
|
|
58
|
+
Only WAV is read by the gem (16-bit PCM or 32-bit float, any rate and channel count). Convert anything else outside the gem, e.g. `ffmpeg -i in.mp3 -ac 1 -ar 16000 -c:a pcm_s16le in.wav`.
|
|
59
|
+
|
|
60
|
+
## Quick start
|
|
61
|
+
|
|
62
|
+
### Speech recognition
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
require "audio_cpp"
|
|
66
|
+
|
|
67
|
+
AudioCpp::Model.load("gigaam-v4-rnnt-q8_0.gguf") do |model|
|
|
68
|
+
model.family # => "gigaam_asr"
|
|
69
|
+
model.languages # => ["ru", "en"]
|
|
70
|
+
model.session(threads: 8) do |session|
|
|
71
|
+
result = session.transcribe("speech.wav", timestamps: true)
|
|
72
|
+
puts result.text
|
|
73
|
+
result.words.each { |w| puts format("%6.2f %s", w.start_seconds, w.text) }
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`transcribe` takes a WAV path, an `AudioCpp::Audio`, or an Array of 16 kHz mono Float samples. Session options are the model's `--session-option`s, request options its `--request-option`s:
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
model.session(options: {"gigaam_asr.head" => "raw"}) # lowercase, no punctuation
|
|
82
|
+
session.transcribe(audio, language: "es", options: {"hotwords" => "Kubernetes, Puma"})
|
|
83
|
+
model.options(:session).map(&:name) # what the model accepts
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Streaming
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
AudioCpp::Model.load("gigaam-v4-rnnt-q8_0.gguf") do |model|
|
|
90
|
+
model.session(mode: :streaming, options: {"gigaam_asr.future_context_ms" => 160}) do |session|
|
|
91
|
+
session.stream do |stream|
|
|
92
|
+
microphone.each_chunk(stream.chunk_frames) do |chunk| # 16 kHz mono Float Arrays or Audio
|
|
93
|
+
stream.push(chunk)
|
|
94
|
+
stream.each_event { |event| print event.text }
|
|
95
|
+
end
|
|
96
|
+
puts stream.finish.text # the complete transcript, including the tail
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Events carry what is new since the previous event. Text a model decodes only when the stream ends is in the `Result` of `finish`, not in an event, so take the final transcript from there. For a whole file there is a shortcut that pushes the model's preferred chunks:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
result = session.stream_audio("speech.wav") { |event| print event.text }
|
|
106
|
+
events = session.stream_audio("speech.wav") # an Enumerator; events.each { ... } returns the Result
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Diarization
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
audio = AudioCpp::Audio.read("meeting.wav").to_asr_input # mono 16 kHz
|
|
113
|
+
turns = AudioCpp::Model.load("nemotron-3-diarization-q8_0.gguf") do |model|
|
|
114
|
+
model.session(task: :diar) { |s| s.diarize(audio).speaker_turns }
|
|
115
|
+
end
|
|
116
|
+
turns.each { |t| puts format("%s %6.2f–%6.2f", t.speaker, t.start_seconds, t.end_seconds) }
|
|
117
|
+
|
|
118
|
+
words = asr_session.transcribe(audio, timestamps: true).words
|
|
119
|
+
AudioCpp::Speakers.assign(words, turns).each { |u| puts "#{u.speaker}: #{u.text}" }
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Text to speech
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
AudioCpp::Model.load("gigaedge-tts-f16.gguf") do |model|
|
|
126
|
+
model.session do |session|
|
|
127
|
+
audio = session.synthesize("Привет! Это синтез речи.", voice: "voice_2")
|
|
128
|
+
audio.sample_rate # => 24000
|
|
129
|
+
audio.to_wav("hello.wav")
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`synthesize` returns an `AudioCpp::Audio` (interleaved Float32 samples, sample rate, channels); `to_wav(path)` writes it, `to_wav_bytes` returns the file as a String. `voice:` is the CLI's `--voice-id`; family-specific settings go in `options:` (e.g. `{"speed" => 1.2, "seed" => 1}`).
|
|
135
|
+
|
|
136
|
+
### Anything else
|
|
137
|
+
|
|
138
|
+
`Session#run(audio:, text:, language:, voice:, options:)` runs any task and returns a `Result` with every output the task produced: `text`, `language`, `words`, `segments`, `speaker_turns`, `audio`, `named_audio`, `artifacts`, `custom_schema`/`custom_data`. `AudioCpp::Native` exposes every C function as-is if you need something the Ruby layer does not cover.
|
|
139
|
+
|
|
140
|
+
## Using it in a web app
|
|
141
|
+
|
|
142
|
+
### Recommended: Active Storage and a background job
|
|
143
|
+
|
|
144
|
+
Inference takes seconds to minutes, so keep it out of the request: save the upload, enqueue a job, show the result when it is there. Load the model once per worker process (after the fork, so lazily) and open a session per job.
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
# app/models/speech.rb
|
|
148
|
+
module Speech
|
|
149
|
+
LOCK = Mutex.new
|
|
150
|
+
|
|
151
|
+
# One model per process, loaded on first use.
|
|
152
|
+
def self.asr
|
|
153
|
+
LOCK.synchronize { @asr ||= AudioCpp::Model.load(ENV.fetch("ASR_GGUF")) }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Decodes any upload to 16 kHz mono WAV with ffmpeg.
|
|
157
|
+
def self.to_wav(input, dir)
|
|
158
|
+
File.join(dir, "input.wav").tap do |wav|
|
|
159
|
+
system("ffmpeg", "-nostdin", "-loglevel", "error", "-i", input, "-ac", "1", "-ar", "16000",
|
|
160
|
+
"-c:a", "pcm_s16le", wav, exception: true)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# app/controllers/recordings_controller.rb
|
|
166
|
+
def create
|
|
167
|
+
recording = Recording.create!(audio: params.require(:audio), status: "queued") # has_one_attached :audio
|
|
168
|
+
TranscribeJob.perform_later(recording)
|
|
169
|
+
redirect_to recording
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# app/jobs/transcribe_job.rb
|
|
173
|
+
class TranscribeJob < ApplicationJob
|
|
174
|
+
queue_as :speech
|
|
175
|
+
|
|
176
|
+
def perform(recording)
|
|
177
|
+
recording.audio.open do |file|
|
|
178
|
+
Dir.mktmpdir do |dir|
|
|
179
|
+
result = Speech.asr.session(threads: 4) do |session|
|
|
180
|
+
session.transcribe(Speech.to_wav(file.path, dir), timestamps: true)
|
|
181
|
+
end
|
|
182
|
+
recording.update!(status: "done", transcript: result.text,
|
|
183
|
+
words: result.words.map { |w| {text: w.text, start: w.start_seconds, end: w.end_seconds} })
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
For long files, show progress with Turbo Streams by pushing the audio through a streaming session (a model that supports `asr/streaming`, such as GigaAM-v4) and broadcasting the text as it grows:
|
|
191
|
+
|
|
192
|
+
```ruby
|
|
193
|
+
text = +""
|
|
194
|
+
last = Time.at(0)
|
|
195
|
+
result = Speech.asr.session(mode: :streaming, threads: 4) do |session|
|
|
196
|
+
session.stream_audio(wav) do |event|
|
|
197
|
+
text << event.text.to_s
|
|
198
|
+
next if Time.now - last < 2
|
|
199
|
+
|
|
200
|
+
last = Time.now
|
|
201
|
+
Turbo::StreamsChannel.broadcast_update_to(recording, target: "transcript", html: ERB::Util.h(text))
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
recording.update!(status: "done", transcript: result.text)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Give the speech queue as many job threads as you have models loaded (see "Threads" below): with one model per process, run that queue with concurrency 1 and `threads:` close to the number of cores.
|
|
208
|
+
|
|
209
|
+
### Small sites and demos: synchronous
|
|
210
|
+
|
|
211
|
+
For short clips you can answer in the request. Cap the input length, and bound concurrent inference with a small pool, one model per slot:
|
|
212
|
+
|
|
213
|
+
```ruby
|
|
214
|
+
# config/initializers/speech.rb
|
|
215
|
+
SPEECH_POOL = Thread::Queue.new.tap do |pool|
|
|
216
|
+
2.times { pool << AudioCpp::Model.load(ENV.fetch("ASR_GGUF")).session(threads: Etc.nprocessors / 2) }
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def with_speech_session
|
|
220
|
+
session = SPEECH_POOL.pop
|
|
221
|
+
yield session
|
|
222
|
+
ensure
|
|
223
|
+
SPEECH_POOL << session if session
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# app/controllers/transcriptions_controller.rb
|
|
227
|
+
def create
|
|
228
|
+
Dir.mktmpdir do |dir|
|
|
229
|
+
audio = AudioCpp::Audio.read(Speech.to_wav(params.require(:audio).path, dir))
|
|
230
|
+
return head(413) if audio.duration > 60 # hard cap on input length
|
|
231
|
+
|
|
232
|
+
render json: {text: with_speech_session { |s| s.transcribe(audio).text }}
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# app/controllers/speech_controller.rb (a TTS session from its own pool)
|
|
237
|
+
def show
|
|
238
|
+
text = params.require(:text).to_s[0, 500]
|
|
239
|
+
audio = with_tts_session { |s| s.synthesize(text, voice: "voice_1") }
|
|
240
|
+
send_data audio.to_wav_bytes, type: "audio/wav", disposition: "inline"
|
|
241
|
+
end
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Size the pool so that pool size × `threads:` is about the number of cores; a single `Mutex` around one session is the simplest version. With Puma in cluster mode every worker loads its own models, so count memory per worker.
|
|
245
|
+
|
|
246
|
+
### A complete example
|
|
247
|
+
|
|
248
|
+
[examples/rails_demo](examples/rails_demo) is a small Rails 8 app (Active Storage, Solid Queue in Puma, Turbo Streams) with transcription, diarization and speech pages, built on these patterns.
|
|
249
|
+
|
|
250
|
+
## Threads, the GVL and cleanup
|
|
251
|
+
|
|
252
|
+
- What the C API promises (`docs/c_api.md`): handles are not individually thread-safe; separate handles may be used concurrently from separate threads. It does not promise that two sessions of one loaded model can run at the same time (audio.cpp's own server runs one request per loaded model unless a family declares more).
|
|
253
|
+
- So the gem serialises all native calls on one `Model` and its sessions with the model's lock. Sharing a `Model` across threads is safe but serial. For parallel inference load one `Model` per worker. Do not share a `Session` between threads: it holds per-call state (a stream in flight); misuse is serialised by the lock, but streams from two threads would interleave.
|
|
254
|
+
- Long calls (model load, session creation, runs, stream push/next/finish/reset) release the GVL, so other Ruby threads (Puma's, Sidekiq's) keep running while one thread is inside audio.cpp.
|
|
255
|
+
- `audiocpp_last_error` is thread-local; the gem reads it right after the failing call on the same thread. With Ruby's M:N thread scheduler (`RUBY_MN_THREADS=1`) a Ruby thread may change native threads between calls; errors may then lose their detail text.
|
|
256
|
+
- Every handle is freed exactly once: by the block form, by `#close`, or by a GC finalizer as a safety net. Freeing out of order is safe (a session keeps its model alive internally).
|
|
257
|
+
- `AudioCpp.on_log(level: :warn) { |level, message| ... }` routes the engine's log lines, `AudioCpp.silence_logs` drops them, `AudioCpp.on_log` without a block restores stderr. The block may run on an engine thread: keep it short.
|
|
258
|
+
|
|
259
|
+
## Errors
|
|
260
|
+
|
|
261
|
+
Failed C calls raise a subclass of `AudioCpp::Error` with `#status` (a Symbol), `#status_name`, `#detail` (`audiocpp_last_error`) and `#operation`: `InvalidArgumentError`, `UnsupportedFamilyError`, `ModelLoadError`, `EngineError`, `OutOfMemoryError`, `OutOfRangeError`, `NotAvailableError`. Loading problems raise `LibraryError` / `AbiMismatchError`, use after `#close` raises `ClosedError`, an unreadable WAV `UnsupportedFormatError`.
|
|
262
|
+
|
|
263
|
+
## Models
|
|
264
|
+
|
|
265
|
+
audio.cpp loads GGUF files (and some families' original safetensors packages). Pass the file path; the family is identified from it, or give `family:`.
|
|
266
|
+
|
|
267
|
+
| Model | Family | Where |
|
|
268
|
+
|---|---|---|
|
|
269
|
+
| GigaAM-v4 RNN-T / CTC (Russian, English; offline and streaming) | `gigaam_asr` | [amx-community/GigaAM-v4-GGUF](https://huggingface.co/amx-community/GigaAM-v4-GGUF) |
|
|
270
|
+
| GigaAM v3, Multilingual, Multilingual-v2 | `gigaam_asr` | [audio-cpp/audio.cpp-gguf](https://huggingface.co/audio-cpp/audio.cpp-gguf/tree/main/GigaAM-ASR-GGUF) |
|
|
271
|
+
| GigaEdge TTS (Russian, two voices) | `gigaedge_tts` | [amx-community/GigaEdge-TTS-GGUF](https://huggingface.co/amx-community/GigaEdge-TTS-GGUF) |
|
|
272
|
+
| Nemotron 3 Diarization (up to 8 speakers) | `nemotron_3_diar` | [audio-cpp/Nemotron-3-Diarization-GGUF](https://huggingface.co/audio-cpp/Nemotron-3-Diarization-GGUF) |
|
|
273
|
+
| Most other families (Whisper, Parakeet, Kokoro, ...) | see below | [audio-cpp/audio.cpp-gguf](https://huggingface.co/audio-cpp/audio.cpp-gguf) and the other `audio-cpp/*` repositories named in audio.cpp's `model_specs/` |
|
|
274
|
+
|
|
275
|
+
Each model has its own license, separate from this gem's and audio.cpp's; check the model card before use.
|
|
276
|
+
|
|
277
|
+
The files these bindings are tested with can be downloaded by the gem, pinned to a Hugging Face commit and checked against their SHA-256 (an interrupted download resumes):
|
|
278
|
+
|
|
279
|
+
```ruby
|
|
280
|
+
AudioCpp::Models.catalog.map(&:id) # => ["gigaam-v4", "gigaam-multilingual-v2", "nemotron-3-diarization", "gigaedge-tts"]
|
|
281
|
+
path = AudioCpp::Models.fetch("gigaam-v4", dir: "models", progress: ->(done, total) { print "." })
|
|
282
|
+
AudioCpp::Models.fetch("gigaam-v4", dir: "models", revision: "<commit>", sha256: "<sha256>") # another revision
|
|
283
|
+
AudioCpp::Models.fetch(AudioCpp::Models::Spec.new(id: "mine", repo: "org/repo", revision: "<commit>",
|
|
284
|
+
file: "model.gguf", sha256: "<sha256>"), dir: "models") # any file
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
Silero VAD ships with audio.cpp; the gem build installs it next to the library, and `AudioCpp.silero_vad_path` returns its path (for example for `"gigaam_asr.vad_model_path"`).
|
|
288
|
+
|
|
289
|
+
## Supported families
|
|
290
|
+
|
|
291
|
+
`AudioCpp.families` lists what the loaded library was built with (the default build has the seven families above, the names Parakeet-TDT also answers to — `fluid_speech`, `haas_asr`, `phonon2_asr`, `underdog_bark3` — and the two VADs). A full build of the pinned commit (`AUDIOCPP_MODELS=full`) has 164:
|
|
292
|
+
|
|
293
|
+
`ace_step`, `apollo`, `ast_audioset`, `audio8_asr`, `audio8_tts`, `audio_flamingo`, `audiosr`, `auk`, `breeze_tts`, `bs_roformer`, `builtin_audio_utils`, `canary_asr`, `ced`, `chatterbox`, `chatterbox_turbo`, `citrinet_asr`, `cohere_asr`, `confucius4_r2t2`, `confucius4_tts`, `controlfoley`, `cosyvoice3`, `crisperwhisper`, `dettato`, `ditto`, `dots_tts`, `dramabox`, `earshot_vad`, `echo_tts`, `edda_asr`, `f5_tts`, `firered_audio`, `firered_vad`, `fireredtts3`, `fish_audio`, `fish_drama3`, `fluid_speech`, `foundation_1`, `fun_asr_nano`, `gigaam_asr`, `gigaedge_tts`, `glm_tts`, `gradium_phonon`, `granite5asr`, `granite_speech5`, `haas_asr`, `heartmula`, `higgs_audio_stt`, `higgs_audio_tts`, `htdemucs`, `htdemucs_6stems`, `hviske_asr`, `hviske_asr_v6`, `index_echo`, `index_tts2`, `inflect_v2`, `irodori_tts`, `kale_tts`, `kitten_tts`, `kitten_tts2`, `kokoro_tts`, `kroko_asr`, `kugelaudio`, `latentsync`, `lfm2_audio`, `lfm3_audio`, `lfm_d1_omni`, `liveavatar`, `m5_tts`, `magenta_realtime_2`, `magpie_tts`, `marblenet_vad`, `maya1`, `meanvc2`, `mel_band_roformer`, `micro_wake_word`, `midashenglm_gen`, `ming3_audio`, `ming3_tts`, `minimax_h3`, `minimax_music3`, `miocodec`, `miotts`, `mira_tts`, `mms_forced_aligner`, `moonshine_asr`, `moss_soundeffect`, `moss_transcribe_diarize`, `moss_tts_local`, `moss_tts_nano`, `moss_tts_v15`, `moss_ttsd`, `moss_voicegen`, `mossformer2`, `mulacover`, `muscriptor`, `nemo_conformer_ctc`, `nemotron_3_diar`, `nemotron_asr`, `nemotron_transcribe`, `neutts`, `niagara_asr`, `omnivoice`, `open_wake_word`, `outetts`, `owsm`, `owsm_ctc`, `parakeet_tdt`, `personaplex`, `phonon2_asr`, `piper_tts`, `plapre`, `pocket_tts`, `pocket_tts_small`, `pulsevad`, `qwen3_asr`, `qwen3_forced_aligner`, `qwen3_tts`, `realtime_reuse`, `reuse`, `reverb`, `rosvot`, `rvc`, `sam_audio`, `samsone`, `sanotts`, `seed_vc`, `sense_asr`, `sheetsage2`, `sherpa_kws`, `sidon`, `silero_vad`, `smart_turn`, `soprano_tts`, `sopro_tts`, `sortformer_diar`, `sortformer_diar_v2`, `soulx_flashhead`, `soulx_singer`, `stable_audio`, `supertonic`, `talkinghead`, `tf_gridnet`, `tiny_voca`, `tone_color_vc`, `underdog_bark3`, `universr`, `vevo2`, `vibeasr`, `vibevoice`, `vibevoice_asr`, `vibevoice_asr_streaming`, `vieneu_v3_turbo`, `voice_isolator`, `voxcpm1`, `voxcpm2`, `voxmel_tts`, `voxtral_realtime`, `voxtral_tts`, `whistle_asr`, `xtts_v2`, `yue2`, `zipvoice`, `zonos2`, `zonos3`.
|
|
294
|
+
|
|
295
|
+
## Platforms
|
|
296
|
+
|
|
297
|
+
- Linux x86_64, CPU. The pinned audio.cpp is the [amx-community fork](https://github.com/amx-community/audio.cpp), which uses Intel AMX where the CPU has it (Sapphire Rapids and newer) and matches upstream elsewhere. The build also works against upstream `0xShug0/audio.cpp` (`AUDIOCPP_GIT_URL`, `AUDIOCPP_GIT_REF`, `AUDIOCPP_COMMIT`).
|
|
298
|
+
- macOS arm64, CPU or Metal. The library is built with Metal (shaders embedded), but sessions run on the CPU unless you ask: `model.session(backend: "metal")`. The default backend is `"cpu"` on every platform.
|
|
299
|
+
- Other backends (CUDA, Vulkan, ...) through `AUDIOCPP_CMAKE_ARGS`; untested by this gem.
|
|
300
|
+
- Windows is not supported yet.
|
|
301
|
+
|
|
302
|
+
Ruby 3.1 or newer.
|
|
303
|
+
|
|
304
|
+
## Development
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
bundle install
|
|
308
|
+
bundle exec rake compile # builds lib/audio_cpp/native/libaudiocpp.* (same variables as gem install)
|
|
309
|
+
bundle exec rake test # unit tests, no models needed
|
|
310
|
+
AUDIO_CPP_TEST_GIGAAM=gigaam-v4-rnnt-q8_0.gguf AUDIO_CPP_TEST_TTS=gigaedge-tts-f16.gguf \
|
|
311
|
+
AUDIO_CPP_TEST_DIAR=nemotron-3-diarization-q8_0.gguf AUDIO_CPP_TEST_ASSETS=audio.cpp/assets/resources \
|
|
312
|
+
AUDIO_CPP_TEST_CLI=audio.cpp/build/bin/audiocpp_cli bundle exec rake test:integration
|
|
313
|
+
bundle exec rake standard
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
The integration tests skip what their variables do not point at. `AUDIO_CPP_TEST_CLI` is an `audiocpp_cli` built from the same audio.cpp commit; the offline transcript must be byte-identical to it. See [docs/precompiled-gems.md](docs/precompiled-gems.md) for the plan for binary gems.
|
|
317
|
+
|
|
318
|
+
## License
|
|
319
|
+
|
|
320
|
+
The gem's code is MIT (see [LICENSE](LICENSE)). audio.cpp, which the gem builds and loads, is Apache-2.0. Models have their own licenses.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Plan: precompiled platform gems
|
|
2
|
+
|
|
3
|
+
Today `gem install audio_cpp` builds libaudiocpp from source, which needs CMake and a compiler and takes minutes. Platform gems would ship the library prebuilt. Not implemented yet; this is the plan.
|
|
4
|
+
|
|
5
|
+
## Targets
|
|
6
|
+
|
|
7
|
+
- `x86_64-linux` (glibc): CPU backend, portable kernels with runtime dispatch (`ENGINE_ENABLE_CPU_ALL_VARIANTS=ON`, which builds the ggml CPU backend as per-ISA plugins loaded beside the library), so one gem serves AVX2, AVX-512 and AMX machines. Build on the oldest glibc we support (a manylinux-style container) so the library loads on older distributions.
|
|
8
|
+
- `arm64-darwin`: Metal with the embedded shader library, OpenMP off, deployment target 13.3.
|
|
9
|
+
- Later, if asked for: `aarch64-linux`, `x86_64-linux-musl`, a Vulkan variant.
|
|
10
|
+
|
|
11
|
+
## Pipeline (GitHub Actions)
|
|
12
|
+
|
|
13
|
+
1. A tag `v*` triggers a release workflow (actions pinned to full SHAs, `permissions: contents: read` for the build jobs).
|
|
14
|
+
2. One job per platform runs `rake compile` with `AUDIOCPP_NATIVE=0` and the full model set (the library is installed as CMake lays it out: the versioned file plus its SONAME / install-name symlinks; check that RubyGems keeps the symlinks, or ship the versioned name only), then `rake native:gem` (to be written): copies the gemspec, sets `spec.platform`, drops `spec.extensions`, adds `lib/audio_cpp/native/**` (library, CPU plugins, Silero VAD, `BUILD_INFO`) to `spec.files`, and builds `audio_cpp-<version>-<platform>.gem`.
|
|
15
|
+
3. Each platform gem is installed into a clean runner and the unit tests run against it (no compiler on the path, to prove nothing is built).
|
|
16
|
+
4. The `.gem` files are uploaded as workflow artifacts. Publishing stays manual: the maintainer downloads them, checks them and runs `gem push` with MFA. No RubyGems credentials live in CI.
|
|
17
|
+
|
|
18
|
+
The source gem (`ruby` platform) keeps building from source and stays the fallback for every other platform. `AUDIOCPP_LIB` keeps working with both.
|
|
19
|
+
|
|
20
|
+
## Open points
|
|
21
|
+
|
|
22
|
+
- Size: a full-model library is large; check it against RubyGems' gem size limit, and consider a smaller default model set for the binary gems.
|
|
23
|
+
- Licenses: the binary gems redistribute audio.cpp (Apache-2.0) and its vendored dependencies; include their license and NOTICE files in the gem.
|
|
24
|
+
- macOS signing/notarization is not needed for a dylib loaded by Ruby, but quarantine attributes on downloaded gems should be checked.
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "etc"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "open3"
|
|
6
|
+
require "rbconfig"
|
|
7
|
+
require "shellwords"
|
|
8
|
+
|
|
9
|
+
module AudioCppBuild
|
|
10
|
+
# The audio.cpp commit this gem version is built and tested against.
|
|
11
|
+
GIT_URL = "https://github.com/amx-community/audio.cpp.git"
|
|
12
|
+
GIT_TAG = "amx-2026-12-02-97bf100"
|
|
13
|
+
COMMIT = "dd52d2f1431c9434ffe9b7d3edbba4e47034f9c8"
|
|
14
|
+
|
|
15
|
+
class BuildError < StandardError; end
|
|
16
|
+
|
|
17
|
+
# Built when AUDIOCPP_MODELS is not set: popular speech families, a few
|
|
18
|
+
# minutes to build. Silero and MarbleNet VAD are always built in.
|
|
19
|
+
DEFAULT_MODELS = %w[
|
|
20
|
+
gigaam_asr crisperwhisper parakeet_tdt nemotron_3_diar kokoro_tts gigaedge_tts cosyvoice3
|
|
21
|
+
].freeze
|
|
22
|
+
|
|
23
|
+
# Fetches audio.cpp at a verified commit and builds libaudiocpp with CMake.
|
|
24
|
+
#
|
|
25
|
+
# Environment:
|
|
26
|
+
# AUDIOCPP_MODELS unset: DEFAULT_MODELS; "full": every family; "core":
|
|
27
|
+
# only the built-in VADs; or a comma-separated list of
|
|
28
|
+
# model targets, e.g. "gigaam_asr,nemotron_3_diar"
|
|
29
|
+
# AUDIOCPP_SOURCE_DIR build this existing checkout instead of fetching
|
|
30
|
+
# AUDIOCPP_GIT_URL fetch from another remote or a local mirror
|
|
31
|
+
# AUDIOCPP_GIT_REF tag or branch to fetch (default: the pinned tag)
|
|
32
|
+
# AUDIOCPP_COMMIT full SHA the fetched ref must resolve to; alone,
|
|
33
|
+
# it is fetched directly
|
|
34
|
+
# AUDIOCPP_BUILD_DIR keep sources and the build tree here (reused by
|
|
35
|
+
# later builds); by default a temporary tree is
|
|
36
|
+
# deleted once the library is installed
|
|
37
|
+
# AUDIOCPP_JOBS parallel build jobs (default: processor count)
|
|
38
|
+
# AUDIOCPP_NATIVE "0" builds portable CPU kernels instead of -march=native
|
|
39
|
+
# AUDIOCPP_CMAKE_ARGS extra CMake arguments, e.g. "-DENGINE_ENABLE_VULKAN=ON"
|
|
40
|
+
class Builder
|
|
41
|
+
def initialize(install_dir:, work_dir:, env: ENV, out: $stdout)
|
|
42
|
+
@install_dir = install_dir
|
|
43
|
+
@env = env
|
|
44
|
+
@out = out
|
|
45
|
+
@keep = !blank?(env["AUDIOCPP_BUILD_DIR"])
|
|
46
|
+
@work_dir = @keep ? File.expand_path(env["AUDIOCPP_BUILD_DIR"]) : work_dir
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def run
|
|
50
|
+
check_tools
|
|
51
|
+
source = source_dir
|
|
52
|
+
build = File.join(@work_dir, "build")
|
|
53
|
+
configure(source, build)
|
|
54
|
+
compile(build)
|
|
55
|
+
install(source, build)
|
|
56
|
+
FileUtils.rm_rf(@work_dir) unless @keep
|
|
57
|
+
@install_dir
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The library file name CMake produces and the gem loads.
|
|
61
|
+
def self.library_name
|
|
62
|
+
RbConfig::CONFIG["host_os"].match?(/darwin/) ? "libaudiocpp.dylib" : "libaudiocpp.so"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def check_tools
|
|
68
|
+
missing = []
|
|
69
|
+
missing << "cmake (3.21 or newer)" unless which("cmake")
|
|
70
|
+
missing << "git" unless which("git") || !blank?(@env["AUDIOCPP_SOURCE_DIR"])
|
|
71
|
+
missing << "a C++17 compiler (g++ or clang++)" unless compiler
|
|
72
|
+
return if missing.empty?
|
|
73
|
+
|
|
74
|
+
raise BuildError, <<~MSG
|
|
75
|
+
audio_cpp needs #{missing.join(", ")} to build libaudiocpp from source.
|
|
76
|
+
Install them (Debian/Ubuntu: apt install cmake g++ git; macOS: xcode-select --install && brew install cmake),
|
|
77
|
+
or point AUDIOCPP_LIB at a libaudiocpp you built yourself and reinstall.
|
|
78
|
+
MSG
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def compiler
|
|
82
|
+
candidates = [@env["CXX"], "c++", "g++", "clang++"].reject { |name| blank?(name) }
|
|
83
|
+
candidates.find { |name| which(name.split.first) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def source_dir
|
|
87
|
+
local = @env["AUDIOCPP_SOURCE_DIR"]
|
|
88
|
+
return File.expand_path(local) unless blank?(local)
|
|
89
|
+
|
|
90
|
+
fetch
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def fetch
|
|
94
|
+
url = @env.fetch("AUDIOCPP_GIT_URL", GIT_URL)
|
|
95
|
+
commit = @env.fetch("AUDIOCPP_COMMIT", COMMIT)
|
|
96
|
+
# A commit given without a ref is fetched by its SHA (GitHub allows that).
|
|
97
|
+
ref = @env.fetch("AUDIOCPP_GIT_REF") { @env.key?("AUDIOCPP_COMMIT") ? commit : GIT_TAG }
|
|
98
|
+
raise BuildError, "AUDIOCPP_COMMIT must be a full 40-character SHA, got #{commit.inspect}" unless commit.match?(/\A\h{40}\z/)
|
|
99
|
+
|
|
100
|
+
source = File.join(@work_dir, "audio.cpp")
|
|
101
|
+
if File.directory?(File.join(source, ".git")) && head(source) == commit
|
|
102
|
+
log "audio.cpp #{commit[0, 12]} already in #{source}"
|
|
103
|
+
return source
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
log "fetching audio.cpp #{ref} from #{url}"
|
|
107
|
+
FileUtils.rm_rf(source)
|
|
108
|
+
FileUtils.mkdir_p(source)
|
|
109
|
+
sh "git", "init", "--quiet", source
|
|
110
|
+
sh "git", "-C", source, "fetch", "--quiet", "--depth", "1", url, ref
|
|
111
|
+
sh "git", "-C", source, "-c", "advice.detachedHead=false", "checkout", "--quiet", "FETCH_HEAD"
|
|
112
|
+
actual = head(source)
|
|
113
|
+
unless actual == commit
|
|
114
|
+
raise BuildError, "audio.cpp #{ref} from #{url} is #{actual}, expected #{commit}; refusing to build it"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
log "audio.cpp verified at #{commit}"
|
|
118
|
+
source
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def head(source)
|
|
122
|
+
stdout, status = Open3.capture2("git", "-C", source, "rev-parse", "HEAD")
|
|
123
|
+
status.success? ? stdout.strip : nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def configure(source, build)
|
|
127
|
+
args = ["cmake", "-S", source, "-B", build, *generator, *cmake_flags]
|
|
128
|
+
args.concat(Shellwords.split(@env["AUDIOCPP_CMAKE_ARGS"])) unless blank?(@env["AUDIOCPP_CMAKE_ARGS"])
|
|
129
|
+
log "configuring: #{model_set_description}"
|
|
130
|
+
sh(*args)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def generator
|
|
134
|
+
which("ninja") ? ["-G", "Ninja"] : []
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def cmake_flags
|
|
138
|
+
flags = %w[
|
|
139
|
+
-DCMAKE_BUILD_TYPE=Release
|
|
140
|
+
-DCMAKE_POLICY_VERSION_MINIMUM=3.5
|
|
141
|
+
-DAUDIOCPP_BUILD_C_API=ON
|
|
142
|
+
-DAUDIOCPP_DEPLOYMENT_BUILD=ON
|
|
143
|
+
-DAUDIOCPP_BUILD_NATIVE_MODEL_MANAGER=OFF
|
|
144
|
+
-DENGINE_BUILD_TESTS=OFF
|
|
145
|
+
-DENGINE_BUILD_EXAMPLES=OFF
|
|
146
|
+
]
|
|
147
|
+
flags << "-DENGINE_ENABLE_NATIVE_CPU=#{(@env["AUDIOCPP_NATIVE"] == "0") ? "OFF" : "ON"}"
|
|
148
|
+
flags.concat(platform_flags)
|
|
149
|
+
flags.concat(model_flags)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def platform_flags
|
|
153
|
+
if macos?
|
|
154
|
+
# AppleClang has no OpenMP; the Metal shaders are embedded so no `metal`
|
|
155
|
+
# compiler (absent from the Command Line Tools) is needed.
|
|
156
|
+
%w[-DENGINE_ENABLE_METAL=ON -DGGML_METAL_EMBED_LIBRARY=ON -DENGINE_ENABLE_OPENMP=OFF -DENGINE_ENABLE_ANE=OFF]
|
|
157
|
+
else
|
|
158
|
+
%w[-DENGINE_ENABLE_METAL=OFF]
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def models
|
|
163
|
+
requested = @env["AUDIOCPP_MODELS"].to_s.strip
|
|
164
|
+
requested.empty? ? DEFAULT_MODELS.join(",") : requested.delete(" ")
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def model_flags
|
|
168
|
+
case models
|
|
169
|
+
when "full" then ["-DAUDIOCPP_MODEL_SET=full"]
|
|
170
|
+
when "core" then ["-DAUDIOCPP_MODEL_SET=core"]
|
|
171
|
+
else ["-DAUDIOCPP_MODEL_SET=custom", "-DAUDIOCPP_MODELS=#{models.tr(",", ";")}"]
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def model_set_description
|
|
176
|
+
case models
|
|
177
|
+
when "full" then "every model family"
|
|
178
|
+
when DEFAULT_MODELS.join(",") then "the default families #{models} (AUDIOCPP_MODELS=full builds all)"
|
|
179
|
+
else "models: #{models}"
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def compile(build)
|
|
184
|
+
jobs = @env.fetch("AUDIOCPP_JOBS", Etc.nprocessors.to_s)
|
|
185
|
+
log "building libaudiocpp with #{jobs} jobs (this takes a while)"
|
|
186
|
+
sh "cmake", "--build", build, "--target", "audiocpp", "--parallel", jobs
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def install(source, build)
|
|
190
|
+
library = File.join(build, "bin", self.class.library_name)
|
|
191
|
+
raise BuildError, "the build finished but #{library} is missing" unless File.exist?(library)
|
|
192
|
+
|
|
193
|
+
FileUtils.mkdir_p(@install_dir)
|
|
194
|
+
target = install_library_chain(library)
|
|
195
|
+
vad = File.join(source, "assets", "framework", "models", "silero_vad")
|
|
196
|
+
if File.directory?(vad)
|
|
197
|
+
FileUtils.mkdir_p(File.join(@install_dir, "assets", "framework", "models"))
|
|
198
|
+
FileUtils.cp_r(vad, File.join(@install_dir, "assets", "framework", "models"))
|
|
199
|
+
end
|
|
200
|
+
write_build_info(source)
|
|
201
|
+
log "installed #{target}"
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Recreates CMake's layout: the real, versioned file and the symlinks that
|
|
205
|
+
# lead to it (libaudiocpp.so -> .so.0 -> .so.0.1.0 on Linux,
|
|
206
|
+
# libaudiocpp.dylib -> .0.dylib -> .0.1.0.dylib on macOS), so the SONAME /
|
|
207
|
+
# install name inside the library names a file that exists.
|
|
208
|
+
def install_library_chain(library)
|
|
209
|
+
links = []
|
|
210
|
+
current = library
|
|
211
|
+
while File.symlink?(current)
|
|
212
|
+
links << [File.basename(current), File.basename(File.readlink(current))]
|
|
213
|
+
current = File.expand_path(File.readlink(current), File.dirname(current))
|
|
214
|
+
end
|
|
215
|
+
real = File.join(@install_dir, File.basename(current))
|
|
216
|
+
([File.basename(current)] + links.map(&:first)).each { |name| FileUtils.rm_f(File.join(@install_dir, name)) }
|
|
217
|
+
FileUtils.cp(current, real)
|
|
218
|
+
links.each { |name, target| File.symlink(target, File.join(@install_dir, name)) }
|
|
219
|
+
File.join(@install_dir, self.class.library_name)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def write_build_info(source)
|
|
223
|
+
info = {
|
|
224
|
+
"commit" => head(source) || "unknown",
|
|
225
|
+
"models" => models,
|
|
226
|
+
"native" => @env["AUDIOCPP_NATIVE"] != "0",
|
|
227
|
+
"cmake_args" => @env["AUDIOCPP_CMAKE_ARGS"].to_s
|
|
228
|
+
}
|
|
229
|
+
File.write(File.join(@install_dir, "BUILD_INFO"), info.map { |key, value| "#{key}=#{value}\n" }.join)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def sh(*command)
|
|
233
|
+
log "$ #{command.shelljoin}"
|
|
234
|
+
return if system(*command)
|
|
235
|
+
|
|
236
|
+
raise BuildError, "command failed (#{$?.exitstatus}): #{command.shelljoin}"
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def which(name)
|
|
240
|
+
return nil if blank?(name)
|
|
241
|
+
return name if File.executable?(name) && !File.directory?(name)
|
|
242
|
+
|
|
243
|
+
ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).map { |dir| File.join(dir, name) }
|
|
244
|
+
.find { |path| File.executable?(path) && !File.directory?(path) }
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def macos?
|
|
248
|
+
RbConfig::CONFIG["host_os"].match?(/darwin/)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def blank?(value)
|
|
252
|
+
value.nil? || value.strip.empty?
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def log(message)
|
|
256
|
+
@out.puts("[audio_cpp] #{message}")
|
|
257
|
+
@out.flush
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|