### Basic Diarization Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/Sortformer/README.md Load audio and print speaker segments. ```swift import MLXAudioCore import MLXAudioVAD let (_, audio) = try loadAudioArray(from: audioURL) let model = try await SortformerModel.fromPretrained( "mlx-community/diar_streaming_sortformer_4spk-v2.1-fp16" ) let result = try await model.generate(audio: audio, threshold: 0.5) for seg in result.segments { print("Speaker \(seg.speaker): \(seg.start)s - \(seg.end)s") } ``` -------------------------------- ### Quick Start Sortformer Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/Sortformer/README.md Initialize the model from a pretrained repository and perform basic inference. ```swift import MLXAudioCore import MLXAudioVAD let model = try await SortformerModel.fromPretrained( "mlx-community/diar_streaming_sortformer_4spk-v2.1-fp16" ) let result = try await model.generate(audio: audioData, threshold: 0.5, verbose: true) print(result.text) ``` -------------------------------- ### Download Metal Toolchain Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/CONTRIBUTING.md Install the Metal toolchain to mirror CI checks more closely during local development. ```bash xcodebuild -downloadComponent MetalToolchain ``` -------------------------------- ### Build and Run mlx-audio-swift-stt Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-stt/README.md Basic command to build and run the tool. Ensure you have Swift installed and the project dependencies are met. ```bash swift run mlx-audio-swift-stt \ --audio /path/to/audio.wav \ --output-path /tmp/transcript ``` -------------------------------- ### Clone Repository and Open Package.swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/CONTRIBUTING.md Clone the MLX Audio Swift repository and open the Package.swift file to begin development setup. ```bash git clone git@github.com:/mlx-audio-swift.git cd mlx-audio-swift open Package.swift ``` -------------------------------- ### LFM2.5-Audio Text-to-Speech Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Generate speech from text using the LFM2.5-Audio model. Specify the output file with -o. ```bash swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode tts \ --text "Hello, welcome to MLX Audio!" \ --system "Perform TTS. Use a UK male voice." \ -o /tmp/lfm_tts.wav ``` -------------------------------- ### Quick Start: Load Model and Separate Audio Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Demonstrates loading a pre-trained SAMAudio model and performing source separation on an input audio file. Prints model sample rate and output shapes. ```swift import Foundation import MLXAudioSTS @main struct Demo { static func main() async throws { let model = try await SAMAudio.fromPretrained("mlx-community/sam-audio-large") let result = try await model.separate( audioPaths: ["input.wav"], descriptions: ["speech"] ) print("sampleRate:", model.sampleRate) print("target shape:", result.target[0].shape) print("residual shape:", result.residual[0].shape) print("peak memory (GB):", result.peakMemoryGB ?? -1) } } ``` -------------------------------- ### LFM2.5-Audio Speech-to-Speech Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Convert speech to speech using the LFM2.5-Audio model. Specify the output audio file with -o. ```bash swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode sts \ --audio /path/to/audio.wav \ -o /tmp/lfm_sts.wav ``` -------------------------------- ### Streaming Generation with MLXAudio Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Example demonstrating streaming generation, processing events like generated tokens, audio chunks, and informational messages. This is useful for real-time applications. ```swift for try await event in model.generateStream(text: text, parameters: parameters) { switch event { case .token(let token): print("Generated token: \(token)") case .audio(let audio): print("Final audio shape: \(audio.shape)") case .info(let info): print(info.summary) } } ``` -------------------------------- ### Streaming Example with mlx-audio-swift-stt Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-stt/README.md Utilize the streaming capability for real-time speech-to-text processing. This is useful for applications requiring low latency. ```bash swift run mlx-audio-swift-stt \ --model mlx-community/Qwen3-ASR-0.6B-4bit \ --audio /path/to/audio.wav \ --output-path /tmp/transcript \ --stream ``` -------------------------------- ### LFM2.5-Audio Text-to-Text Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Perform text-to-text generation using the LFM2.5-Audio model. The --stream flag outputs the response as it's generated. ```bash swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode t2t \ --text "What is 2 + 2?" \ --system "Answer briefly." \ --stream ``` -------------------------------- ### Load MMS-LID-256 Model and Predict Language Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Quick start example for loading the Wav2Vec2-based MMS-LID-256 model and predicting the spoken language from an audio file. Ensure audio is 16 kHz mono; loadAudioArray handles resampling. ```swift import MLXAudioCore import MLXAudioLID let model = try await Wav2Vec2ForSequenceClassification.fromPretrained("facebook/mms-lid-256") let (_, audio) = try loadAudioArray(from: audioURL) let output = model.predict(waveform: audio, topK: 5) print("Language: \(output.language) (\(output.confidence * 100)ப்பட்டன)") ``` -------------------------------- ### Example: Codec Roundtrip Reconstruction Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-codec/README.md Perform a codec roundtrip reconstruction by specifying the model, input audio, and output path. Defaults are used if output is not specified. ```bash swift run mlx-audio-swift-codec \ --model mlx-community/dacvae-watermarked \ --audio /path/to/input.wav \ --output /tmp/reconstructed.wav ``` -------------------------------- ### LFM2.5-Audio Speech-to-Text Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Transcribe speech to text using the LFM2.5-Audio model. Use --stream to output transcription as it's generated. ```bash swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode stt \ --audio /path/to/audio.wav \ --stream ``` -------------------------------- ### Load ECAPA-TDNN Model and Predict Language Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Quick start example for loading the ECAPA-TDNN-based VoxLingua107 model and predicting the spoken language from an audio file. This model is significantly faster and smaller than MMS-LID-256. ```swift import MLXAudioCore import MLXAudioLID let model = try await EcapaTdnn.fromPretrained("beshkenadze/lang-id-voxlingua107-ecapa-mlx") let (_, audio) = try loadAudioArray(from: audioURL) let output = model.predict(waveform: audio, topK: 5) print("Language: \(output.language) (\(output.confidence * 100)ப்பட்டன)") ``` -------------------------------- ### Generate Multilingual Speech Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/Kokoro/README.md Examples for generating speech in different languages, including auto-detection and explicit language specification. ```swift let audio = try await model.generate( text: "Hola, esto es una prueba.", voice: "ef_dora" // Spanish auto-detected from "e" prefix ) ``` ```swift let audio = try await model.generate( text: "Bonjour le monde", voice: "ff_siwis", language: "fr" ) ``` -------------------------------- ### ASR Example with mlx-audio-swift-stt Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-stt/README.md Perform Automatic Speech Recognition (ASR) using a specified model. Requires an audio file and an output path. Supports JSON output format and language specification. ```bash swift run mlx-audio-swift-stt \ --model mlx-community/Qwen3-ASR-0.6B-4bit \ --audio /path/to/audio.wav \ --output-path /tmp/transcript \ --format json \ --language English ``` -------------------------------- ### Text-to-Speech Generation with MLXAudioTTS Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Example of generating speech from text using the Soprano TTS model. Ensure you have MLXAudioTTS and MLXAudioCore imported. The generated audio can be saved to a file. ```swift import MLXAudioTTS import MLXAudioCore // Load a TTS model from HuggingFace let model = try await SopranoModel.fromPretrained("mlx-community/Soprano-80M-bf16") // Generate audio let audio = try await model.generate( text: "Hello from MLX Audio Swift!", parameters: GenerateParameters( maxTokens: 200, temperature: 0.7, topP: 0.95 ) ) // Save to file try saveAudioArray(audio, sampleRate: Double(model.sampleRate), to: outputURL) ``` -------------------------------- ### Forced Aligner Example with mlx-audio-swift-stt Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-stt/README.md Perform forced alignment using a dedicated model. This requires the audio file, the exact text to align, and an output path. JSON format is supported. ```bash swift run mlx-audio-swift-stt \ --model mlx-community/Qwen3-ForcedAligner-0.6B-4bit \ --audio /path/to/audio.wav \ --text "The transcript to align" \ --language English \ --output-path /tmp/alignment \ --format json ``` -------------------------------- ### DeepFilterNet Enhancement Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Enhance noisy audio using DeepFilterNet. Specify the model path and output target file. ```bash swift run mlx-audio-swift-sts \ --model /path/to/DeepFilterNet3 \ --audio /path/to/noisy.wav \ --output-target /tmp/deepfilternet.wav ``` -------------------------------- ### SAM Audio Streaming Mode Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Perform speech separation using SAM Audio in streaming mode. Configure chunk and overlap durations. ```bash swift run mlx-audio-swift-sts \ --audio /path/to/mix.wav \ --mode stream \ --chunk-seconds 10 \ --overlap-seconds 3 ``` -------------------------------- ### SAM Audio Short Mode Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Perform speech separation using SAM Audio in short mode (default). Specify the output target file with --output-target. ```bash swift run mlx-audio-swift-sts \ --audio /path/to/mix.wav \ --description speech \ --mode short \ --output-target /tmp/target.wav ``` -------------------------------- ### Run VyvoTTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/Qwen3/README.md Use this command to generate speech from text using the VyvoTTS model from the command line. Ensure mlx-audio-swift is installed. ```bash mlx-audio-swift-tts --model mlx-community/VyvoTTS-EN-Beta-4bit --text "Hello world." ``` -------------------------------- ### Speech-to-Text Transcription with MLXAudioSTT Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Example of transcribing speech to text using the GLMASR model. Load your audio file and import MLXAudioSTT and MLXAudioCore. The output text is printed to the console. ```swift import MLXAudioSTT import MLXAudioCore // Load audio file let (sampleRate, audioData) = try loadAudioArray(from: audioURL) // Load STT model let model = try await GLMASRModel.fromPretrained("mlx-community/GLM-ASR-Nano-2512-4bit") // Transcribe let output = model.generate(audio: audioData) print(output.text) ``` -------------------------------- ### Predict Language with Top-K Results Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Demonstrates how to use the predict method with a specified topK value to get the number of top language predictions. The output includes the top predicted language, its confidence, and a list of top-K predictions. ```swift let output = model.predict( waveform: audioData, // MLXArray — 1-D audio samples (16 kHz) topK: 5 // number of top language predictions to return ) ``` -------------------------------- ### Generate Speech using MOSS-TTS in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/MossTTS/README.md Load a MOSS-TTS model and generate speech programmatically in Swift. This example shows basic text generation with optional parameters. ```swift import Foundation import MLXAudioCore import MLXAudioTTS let model = try await TTS.loadModel(modelRepo: "OpenMOSS-Team/MOSS-TTS") let audio = try await model.generate( text: "Hello, this is MOSS-TTS running from Swift.", voice: nil, refAudio: nil, refText: nil, language: "English", generationParameters: GenerateParameters( maxTokens: 120, temperature: 1.1, topP: 0.9 ) ) ``` -------------------------------- ### Quick Start Smart Turn v3 Endpoint Detection Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/SmartTurn/README.md Load the Smart Turn model and predict the endpoint of an audio segment. Input audio is resampled to 16 kHz, and segments shorter than 8 seconds are zero-padded, while longer segments use the latest 8 seconds. ```swift import MLX import MLXAudioVAD let model = try await SmartTurnModel.fromPretrained("mlx-community/smart-turn-v3") let audio = MLXArray.zeros([16000], type: Float.self) let result = try model.predictEndpoint(audio, sampleRate: 16000, threshold: 0.5) print(result.prediction) // 0 or 1 print(result.probability) // sigmoid probability ``` -------------------------------- ### Load Silero VAD Model and Get Speech Timestamps Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/SileroVAD/README.md Loads a pretrained Silero VAD model and extracts speech timestamps from an audio file. Ensure MLXAudioVAD and MLXAudioCore are imported. The audio must be loaded into a sample rate and audio array. ```swift import MLXAudioVAD import MLXAudioCore let model = try await SileroVAD.fromPretrained("mlx-community/silero-vad") let (sampleRate, audio) = try loadAudioArray(from: audioURL) let timestamps = try model.getSpeechTimestamps(audio, sampleRate: sampleRate) for ts in timestamps { let start = Float(ts.start) / Float(sampleRate) let end = Float(ts.end) / Float(sampleRate) print("[\(start)s - \(end)s]") } ``` -------------------------------- ### Speaker Diarization with MLXAudioVAD Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Example of detecting who is speaking when in an audio file using the Sortformer model. Load the audio and import MLXAudioVAD and MLXAudioCore. The output segments include speaker ID and time ranges. ```swift import MLXAudioVAD import MLXAudioCore // Load audio file let (sampleRate, audioData) = try loadAudioArray(from: audioURL) // Load diarization model let model = try await SortformerModel.fromPretrained( "mlx-community/diar_streaming_sortformer_4spk-v2.1-fp16" ) // Detect who is speaking when let output = try await model.generate(audio: audioData, threshold: 0.5) for segment in output.segments { print("Speaker \(segment.speaker): \(segment.start)s - \(segment.end)s") } ``` -------------------------------- ### Build and Run Help Command Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Run the help command to see available options for the mlx-audio-swift-sts tool. ```bash swift run mlx-audio-swift-sts --help ``` -------------------------------- ### Initialize VoicesApp Configuration Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Examples/VoicesApp/README.md Create a local configuration file from the provided template before building the project. ```sh cp Examples/VoicesApp/Config/Local.xcconfig.template Examples/VoicesApp/Config/Local.xcconfig ``` -------------------------------- ### Build and Run mlx-audio-swift-codec Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-codec/README.md Build and run the command-line tool. Specify the input audio file using the --audio flag. ```bash swift run mlx-audio-swift-codec --audio /path/to/input.wav ``` -------------------------------- ### fromPretrained(_:) Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Downloads and loads a model from Hugging Face. ```APIDOC ## fromPretrained(_:) ### Description Download and load a model from Hugging Face. Uses HF_TOKEN environment variable or Info.plist key. ### Parameters #### Arguments - **modelID** (String) - Required - The Hugging Face model identifier ``` -------------------------------- ### Transcription with Custom Prompt Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/GraniteSpeech/README.md Performs speech-to-text transcription using a custom prompt to guide the model's output. ```swift // With custom prompt let output = model.generate(audio: audio, prompt: "Translate the speech to text.") print(output.text) ``` -------------------------------- ### Load Audio and Initialize Model Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/GraniteSpeech/README.md Loads audio data from a URL and initializes the Granite Speech model from a pre-trained checkpoint. Ensure MLXAudioCore and MLXAudioSTT are imported. ```swift import MLXAudioCore import MLXAudioSTT let (_, audio) = try loadAudioArray(from: audioURL) let model = try await GraniteSpeechModel.fromPretrained("mlx-community/granite-4.0-1b-speech-5bit") ``` -------------------------------- ### Use CLI Tool Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Execute various generation modes including TTS, STT, STS, and T2T via the command line. ```bash # Text-to-Speech swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode tts \ --text "Hello, welcome to MLX Audio!" \ -o output.wav # Speech-to-Text swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode stt \ --audio input.wav \ --stream # Speech-to-Speech swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode sts \ --audio input.wav \ --stream \ -o response.wav # Text-to-Text swift run mlx-audio-swift-sts \ --model mlx-community/LFM2.5-Audio-1.5B-6bit \ --mode t2t \ --text "What is machine learning?" \ --stream ``` -------------------------------- ### DeepFilterNet Streaming Enhancement Example Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Enhance noisy audio using DeepFilterNet in streaming mode. Configure chunk duration for processing. ```bash swift run mlx-audio-swift-sts \ --model /path/to/DeepFilterNet3 \ --audio /path/to/noisy.wav \ --mode stream \ --chunk-seconds 0.48 \ --output-target /tmp/deepfilternet_stream.wav ``` -------------------------------- ### Run basic text-to-speech synthesis Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-tts/README.md Executes the tool with a simple text input to generate audio. ```bash swift run mlx-audio-swift-tts --text "Hello world" ``` -------------------------------- ### Create Local Config File Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Examples/SimpleChat/README.md Copy the template configuration file to create a local configuration. Ensure you edit the file to set unique APP_BUNDLE_ID and your DEVELOPMENT_TEAM. ```sh cp Examples/SimpleChat/Config/Local.xcconfig.template Examples/SimpleChat/Config/Local.xcconfig ``` -------------------------------- ### Run SenseVoice via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/SenseVoice/README.md Execute the SenseVoice model from the command line using the mlx-audio-swift-stt tool. Specify model, audio input, output path, and format. ```bash .build/debug/mlx-audio-swift-stt \ --model mlx-community/SenseVoiceSmall \ --audio Tests/media/conversational_a.wav \ --output-path /tmp/sensevoice \ --format txt ``` -------------------------------- ### Build for Simulator (CI) Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Examples/SimpleChat/README.md Use this command to build the project for the simulator in a CI environment without code signing. This is useful for automated testing. ```sh xcodebuild \ -project Examples/SimpleChat/SimpleChat.xcodeproj \ -scheme SimpleChat \ -sdk iphonesimulator \ -configuration Debug \ CODE_SIGNING_ALLOWED=NO ``` -------------------------------- ### Build VoicesApp for Simulator in CI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Examples/VoicesApp/README.md Use this command to build the application for the simulator in a CI environment where code signing is not required. ```sh xcodebuild \ -scheme VoicesApp \ -sdk iphonesimulator \ -configuration Debug \ CODE_SIGNING_ALLOWED=NO ``` -------------------------------- ### Load Audio and Initialize DeepFilterNet Model (Swift) Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/DeepFilterNet/README.md Loads audio data and initializes the DeepFilterNet model. Supports loading default V3, specific versions, or from a local path. ```swift import MLXAudioCore import MLXAudioSTS let (_, audio) = try loadAudioArray(from: audioURL, sampleRate: 48_000) // Default: loads V3 from mlx-community/DeepFilterNet-mlx let model = try await DeepFilterNetModel.fromPretrained() // Or specify a version let modelV2 = try await DeepFilterNetModel.fromPretrained(subfolder: "v2") // Or load from a local path let modelLocal = try await DeepFilterNetModel.fromPretrained("/path/to/DeepFilterNet3") let enhanced = try model.enhance(audio) // enhanced is a mono MLXArray of shape [samples] in [-1, 1] ``` -------------------------------- ### Perform ASR with Qwen3 in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/Qwen3ASR/README.md Loads a pretrained ASR model and generates text from an audio file. ```swift import MLXAudioCore import MLXAudioSTT let (sampleRate, audio) = try loadAudioArray(from: audioURL) _ = sampleRate let model = try await Qwen3ASRModel.fromPretrained("mlx-community/Qwen3-ASR-0.6B-4bit") let output = model.generate(audio: audio, language: "English") print(output.text) ``` -------------------------------- ### Load and Generate with SenseVoice in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/SenseVoice/README.md Load audio and use the SenseVoice model for speech understanding tasks. Requires MLXAudioCore and MLXAudioSTT imports. ```swift import MLXAudioCore import MLXAudioSTT let (_, audio) = try loadAudioArray(from: audioURL, sampleRate: 16000) let model = try await SenseVoiceModel.fromPretrained("mlx-community/SenseVoiceSmall") let output = model.generate(audio: audio) print(output.text) print(output.language ?? "unknown") ``` -------------------------------- ### Low-Level Model Forward Pass Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Provides examples of the low-level forward pass for both Wav2Vec2 and ECAPA-TDNN models, returning raw logits or log-probabilities. ECAPA-TDNN requires computing a mel spectrogram first. ```swift // Wav2Vec2: raw waveform → logits let logits = wav2vec2Model(waveform) // ECAPA-TDNN: mel spectrogram → log-probabilities let mel = EcapaMelSpectrogram.compute(audio: waveform) let logProbs = ecapaModel(mel) ``` -------------------------------- ### Download and Load Pre-trained Models Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioLID/README.md Shows how to download and load pre-trained language identification models from Hugging Face using the `fromPretrained` static method. Authentication can be handled via the `HF_TOKEN` environment variable or an Info.plist key. ```swift let wav2vec2 = try await Wav2Vec2ForSequenceClassification.fromPretrained("facebook/mms-lid-256") let ecapa = try await EcapaTdnn.fromPretrained("beshkenadze/lang-id-voxlingua107-ecapa-mlx") ``` -------------------------------- ### Initialize model with custom G2P settings Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/KittenTTS/README.md Instantiates the model with textProcessor set to nil to bypass default phonemization for pre-phonemized IPA input. ```swift let model = try await KittenTTSModel.fromPretrained( "mlx-community/kitten-tts-mini-0.8", textProcessor: nil ) ``` -------------------------------- ### Define Temporal Anchors for Source Separation Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Specifies temporal anchors to guide the source separation process. Anchors define time spans where a target sound is expected ('+') or not expected ('-'). ```swift anchors: [[("+", 1.0, 2.5), ("-", 4.0, 6.0)]] ``` -------------------------------- ### Generate Speech with Qwen3-TTS in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/Qwen3TTS/README.md Load a base model and generate audio from text, then save the output to a WAV file. ```swift import Foundation import MLXAudioCore import MLXAudioTTS let model = try await TTS.loadModel( modelRepo: "mlx-community/Qwen3-TTS-12Hz-0.6B-Base-8bit" ) let audio = try await model.generate( text: "Hello from Qwen3-TTS.", voice: nil, refAudio: nil, refText: nil, language: "English" ) try AudioUtils.writeWavFile( samples: audio.asArray(Float.self), sampleRate: Double(model.sampleRate), fileURL: URL(fileURLWithPath: "/tmp/qwen3-tts.wav") ) ``` -------------------------------- ### Run Local Integration Test Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Executes a local integration test for SAMAudio that loads fixtures without requiring network access. This is useful for verifying basic functionality. ```bash swift test --filter fromPretrainedLoadsLocalFixture ``` -------------------------------- ### Build with Xcode for Metal Support Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-sts/README.md Build the project with Xcode to ensure the Metal library is generated and loaded for model inference on macOS. ```bash xcodebuild build -scheme mlx-audio-swift-sts -destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO ``` -------------------------------- ### Run synthesis with reference audio Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-tts/README.md Performs voice cloning by providing a reference audio file and its corresponding transcript. ```bash swift run mlx-audio-swift-tts \ --model Marvis-AI/marvis-tts-250m-v0.2-MLX-8bit \ --text "Hello from MLX Audio" \ --ref_audio /path/to/reference.wav \ --ref_text "Reference transcript" \ --output /tmp/tts_ref.wav ``` -------------------------------- ### Load, Encode, and Decode Audio with SNAC Codec Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Demonstrates loading a pre-trained SNAC codec, encoding audio into tokens, and decoding tokens back to audio. Ensure the MLXAudioCodecs library is imported. ```swift import MLXAudioCodecs // Load SNAC codec let snac = try await SNAC.fromPretrained("mlx-community/snac_24khz") // Encode audio to tokens let tokens = try snac.encode(audio) // Decode tokens back to audio let reconstructed = try snac.decode(tokens) ``` -------------------------------- ### Supported Models Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Lists the available LFM2.5-Audio models with their sizes and quantization methods. ```APIDOC ## Supported Models ### Description Details the various configurations of the LFM2.5-Audio model available for use, including size and quantization. | Model | Size | Quantization | |-------------------------------------------|---------|--------------| | `mlx-community/LFM2.5-Audio-1.5B-bf16` | ~3 GB | bfloat16 | | `mlx-community/LFM2.5-Audio-1.5B-8bit` | ~1.7 GB | 8-bit | | `mlx-community/LFM2.5-Audio-1.5B-6bit` | ~1.3 GB | 6-bit | | `mlx-community/LFM2.5-Audio-1.5B-5bit` | ~1.1 GB | 5-bit | | `mlx-community/LFM2.5-Audio-1.5B-4bit` | ~0.9 GB | 4-bit | ``` -------------------------------- ### Pre-warm Kokoro Processor Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/Kokoro/README.md Pre-load language resources to avoid latency on the first generation call. ```swift let model = try await TTS.loadModel(modelRepo: "mlx-community/Kokoro-82M-bf16") // Pre-warm: downloads lexicon/model and loads into memory if let kokoro = model as? KokoroModel, let processor = kokoro.textProcessor as? KokoroMultilingualProcessor { try await processor.prepare(for: "es") } // First generate() is now fast let audio = try await model.generate(text: "Hola", voice: "ef_dora") ``` -------------------------------- ### Real-time Streaming Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/Sortformer/README.md Process live input streams like a microphone. ```swift var state = model.initStreamingState() for try await chunk in microphoneStream { let (result, newState) = try await model.feed( chunk: chunk, state: state, threshold: 0.5 ) state = newState for seg in result.segments { print("Speaker \(seg.speaker): \(seg.start)s - \(seg.end)s") } } ``` -------------------------------- ### Generate Speech in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/PocketTTS/README.md Initializes the model and generates audio from text using the Swift library. ```swift import MLXAudioTTS let model = try await PocketTTSModel.fromPretrained("mlx-community/pocket-tts") let audio = try await model.generate( text: "Hello world.", voice: "alba", generationParameters: GenerateParameters() ) ``` -------------------------------- ### Load SAMAudio Model from Pre-trained Repository Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Loads a pre-trained SAMAudio model from a Hugging Face repository. Ensure the repository name is correct. ```swift let model = try await SAMAudio.fromPretrained("mlx-community/sam-audio-large") ``` -------------------------------- ### Configure MLX runtime environment Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-lid/README.md Sets the DYLD_FRAMEWORK_PATH to ensure the MLX runtime can locate metal shader resources. ```bash export DYLD_FRAMEWORK_PATH="$(swift build --show-bin-path)" swift run mlx-audio-swift-lid --audio Tests/media/intention.wav ``` -------------------------------- ### Configure LFM Generation Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Define generation parameters such as token limits, sampling temperatures, and top-K/top-P settings for text and audio. ```swift let config = LFMGenerationConfig( maxNewTokens: 2048, // Maximum tokens to generate temperature: 0.7, // Text sampling temperature topK: 50, // Text top-K sampling topP: 1.0, // Text nucleus sampling audioTemperature: 0.8, // Audio sampling temperature audioTopK: 4 // Audio top-K sampling ) ``` -------------------------------- ### Run LID inference Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-lid/README.md Executes the LID tool on a specified audio file. ```bash swift run mlx-audio-swift-lid --audio Tests/media/intention.wav ``` -------------------------------- ### Run KittenTTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/KittenTTS/README.md Executes text-to-speech synthesis directly from the command line. ```bash mlx-audio-swift-tts \ --model mlx-community/kitten-tts-mini-0.8 \ --voice Bella \ --text "Hello from Kitten TTS." ``` -------------------------------- ### Configure ODE Solver Options Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Sets the Ordinary Differential Equation (ODE) solver options for the separation process, controlling the trade-off between quality and speed. '.midpoint' is generally higher quality but slower, while '.euler' is faster. The stepSize must be between 0 and 1. ```swift let ode = SAMAudioODEOptions(method: .midpoint, stepSize: 2.0 / 32.0) ``` -------------------------------- ### Add MLXAudio to Project with Swift Package Manager Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/README.md Instructions for adding the MLXAudio Swift Package Manager dependencies to your project. Remember to import only the necessary modules. ```swift dependencies: [ .package(url: "https://github.com/Blaizzy/mlx-audio-swift.git", branch: "main") ] // Import only what you need .product(name: "MLXAudioTTS", package: "mlx-audio-swift"), .product(name: "MLXAudioCore", package: "mlx-audio-swift") ``` -------------------------------- ### Post-processing Configuration Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/Sortformer/README.md Adjust threshold, minimum duration, and merge gap for refined output. ```swift let result = try await model.generate( audio: audio, threshold: 0.4, minDuration: 0.25, // ignore segments shorter than 250ms mergeGap: 0.5 // merge segments within 500ms of each other ) ``` -------------------------------- ### Load and Generate Speech with Kokoro Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/Kokoro/README.md Basic usage for loading the Kokoro model and generating audio from text. ```swift import MLXAudioTTS let model = try await TTS.loadModel(modelRepo: "mlx-community/Kokoro-82M-bf16") let audio = try await model.generate( text: "Hello from Kokoro!", voice: "af_heart" ) ``` -------------------------------- ### Run Network-Enabled Integration Test Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Executes an integration test that requires network access to download model weights. Set the MLXAUDIO_ENABLE_NETWORK_TESTS environment variable to 1 and specify the model repository. ```bash MLXAUDIO_ENABLE_NETWORK_TESTS=1 \ MLXAUDIO_SAMAUDIO_REPO=mlx-community/sam-audio-large \ swift test --filter fromPretrainedLoadsRealWeightsNetwork ``` -------------------------------- ### Load and generate audio with KittenTTS Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/StyleTTS2/KittenTTS/README.md Loads a pre-trained model from a Hugging Face repository and generates audio from text. ```swift import MLXAudioTTS let model = try await TTS.loadModel(modelRepo: "mlx-community/kitten-tts-mini-0.8") let audio = try await model.generate( text: "Hello from Kitten TTS.", voice: "Bella" ) ``` -------------------------------- ### Run synthesis with specific model and voice Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-tts/README.md Generates audio using a specific Hugging Face model, voice ID, and enables word timestamps. ```bash swift run mlx-audio-swift-tts \ --model mlx-community/VyvoTTS-EN-Beta-4bit \ --text "Hello from MLX Audio" \ --voice en-us-1 \ --timestamps \ --output /tmp/tts.wav ``` -------------------------------- ### Run LID with custom model and top-k Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/Tools/mlx-audio-swift-lid/README.md Specifies a custom model and the number of top results to return. ```bash swift run mlx-audio-swift-lid \ --audio Tests/media/intention.wav \ --model facebook/mms-lid-256 \ --top-k 3 ``` -------------------------------- ### CLI Tool Usage Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Command-line interface options for running TTS, STT, STS, and T2T tasks. ```APIDOC ## CLI Tool: mlx-audio-swift-sts ### Description Command-line interface for performing multimodal audio and text tasks. ### Parameters #### Options - **--model** (string) - Required - HuggingFace model repository - **--mode** (string) - Optional - Generation mode (t2t, tts, stt, sts). Default: sts - **-t, --text** (string) - Optional - Input text - **-i, --audio** (path) - Optional - Input audio file path - **--max-new-tokens** (int) - Optional - Maximum tokens to generate. Default: 512 - **--temperature** (float) - Optional - Text sampling temperature. Default: 0.7 - **-o, --output-target** (path) - Optional - Audio output path. Default: lfm_output.wav ``` -------------------------------- ### Run Standard MOSS-TTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/MossTTS/README.md Use this command to generate speech with the standard MOSS-TTS model. Specify the model repository, input text, and output file path. ```bash swift run mlx-audio-swift-tts \ --model OpenMOSS-Team/MOSS-TTS \ --text "Hello, this is MOSS-TTS running from Swift." \ --output moss-tts.wav ``` -------------------------------- ### Run Soprano TTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/Soprano/README.md Execute text-to-speech synthesis directly from the command line using the Soprano model. ```bash mlx-audio-swift-tts --model mlx-community/Soprano-80M-bf16 --text "Hello world." ``` -------------------------------- ### Separate Short Audio with Anchors and ODE Options Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Performs source separation on short audio clips using specified audio paths, descriptions, optional temporal anchors, and custom ODE options for quality/speed. ```swift let result = try await model.separate( audioPaths: ["input.wav"], descriptions: ["speech"], anchors: [[("+", 1.5, 3.0)]], // optional ode: SAMAudioODEOptions(method: .midpoint, stepSize: 2.0 / 32.0) ) ``` -------------------------------- ### Load and Transcribe Audio with Parakeet Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/Parakeet/README.md Initializes a Parakeet model from a Hugging Face repository and performs speech-to-text transcription on loaded audio data. ```swift import MLXAudioCore import MLXAudioSTT let (_, audio) = try loadAudioArray(from: audioURL) let model = try await ParakeetModel.fromPretrained("mlx-community/parakeet-tdt-0.6b-v3") let output = model.generate(audio: audio) print(output.text) ``` -------------------------------- ### Generate Speech with Fish Audio S2 Pro Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/FishSpeech/README.md Load the model and generate audio from text. Note that voice and language parameters are currently unused and should be set to nil. ```swift import Foundation import MLXAudioCore import MLXAudioTTS let model = try await TTS.loadModel(modelRepo: "mlx-community/fish-audio-s2-pro-8bit") let audio = try await model.generate( text: "Hello from Fish Speech.", voice: nil, refAudio: nil, refText: nil, language: nil ) try AudioUtils.writeWavFile( samples: audio.asArray(Float.self), sampleRate: model.sampleRate, fileURL: URL(fileURLWithPath: "/tmp/output.wav") ) ``` -------------------------------- ### Load SAMAudio Model with Hugging Face Token Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/SAMAudio/README.md Loads a pre-trained SAMAudio model from a gated Hugging Face repository using an authentication token. The token should be provided via the HF_TOKEN environment variable. ```swift let model = try await SAMAudio.fromPretrained( "facebook/sam-audio-large", hfToken: ProcessInfo.processInfo.environment["HF_TOKEN"] ) ``` -------------------------------- ### LFM2AudioModel Generation Methods Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Methods for generating text and audio tokens using the LFM2AudioModel class. ```APIDOC ## LFM2AudioModel.generateInterleaved ### Description Generates interleaved text and audio tokens, yielding tuples of (token, modality). ### Parameters #### Request Body - **textTokens** (MLXArray) - Optional - Input text tokens - **audioFeatures** (MLXArray) - Optional - Input audio features - **audioCodes** (MLXArray) - Optional - Input audio codes - **modalities** (MLXArray) - Optional - Modality indicators - **config** (LFMGenerationConfig) - Required - Generation configuration parameters ### Response - **AsyncThrowingStream** - Yields (MLXArray, LFMModality) tuples --- ## LFM2AudioModel.generateSequential ### Description Generates all text first, then all audio. Recommended for TTS applications. ### Parameters #### Request Body - **textTokens** (MLXArray) - Optional - Input text tokens - **audioFeatures** (MLXArray) - Optional - Input audio features - **audioCodes** (MLXArray) - Optional - Input audio codes - **modalities** (MLXArray) - Optional - Modality indicators - **config** (LFMGenerationConfig) - Required - Generation configuration parameters ### Response - **AsyncThrowingStream** - Yields (MLXArray, LFMModality) tuples ``` -------------------------------- ### Build for Testing with Xcodebuild Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/CONTRIBUTING.md Perform a local build for testing using xcodebuild, specifying the scheme, destination, deployment target, and disabling code signing. ```bash xcodebuild build-for-testing \ -scheme MLXAudio-Package \ -destination 'platform=macOS' \ MACOSX_DEPLOYMENT_TARGET=14.0 \ CODE_SIGNING_ALLOWED=NO ``` -------------------------------- ### Run Pocket TTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/PocketTTS/README.md Executes text-to-speech generation using the command line interface. ```bash mlx-audio-swift-tts --model mlx-community/pocket-tts --text "Hello world." ``` -------------------------------- ### Load Audio and Transcribe with Voxtral Mini 4B Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/VoxtralRealtime/README.md Loads audio from a URL and performs realtime speech transcription using the Voxtral Realtime Model. Ensure MLXAudioCore and MLXAudioSTT are imported. ```swift import MLXAudioCore import MLXAudioSTT let (_, audio) = try loadAudioArray(from: audioURL) let model = try await VoxtralRealtimeModel.fromPretrained("mlx-community/Voxtral-Mini-4B-Realtime-2602-4bit") let output = model.generate(audio: audio) print(output.text) ``` -------------------------------- ### Stream Text Generation with GLM-ASR Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/GLMASR/README.md Demonstrates streaming the speech-to-text output from the GLM-ASR model. This allows for real-time processing of audio, receiving tokens as they are generated and the final result. ```swift for try await event in model.generateStream(audio: audio) { switch event { case .token(let token): print(token, terminator: "") case .result(let result): print("\nFinal text: \(result.text)") case .info: break } } ``` -------------------------------- ### Run Dialogue Model MOSS-TTSD via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/MossTTS/README.md Use this command to generate speech with the MOSS-TTSD dialogue model. Note the specific text format for dialogue. ```bash swift run mlx-audio-swift-tts \ --model OpenMOSS-Team/MOSS-TTSD-v1.0 \ --text "[S1] Hello. [S2] Hi, this is MOSS-TTSD running from Swift." \ --output moss-ttsd.wav ``` -------------------------------- ### Voice Cloning with MOSS-TTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/MossTTS/README.md Perform voice cloning by providing a reference audio clip and its corresponding transcript. This allows the model to mimic a specific voice. ```bash swift run mlx-audio-swift-tts \ --model OpenMOSS-Team/MOSS-TTS \ --text "This is a short cloned voice sample." \ --ref_audio speaker.wav \ --ref_text "Reference transcript for the speaker." \ --output moss-clone.wav ``` -------------------------------- ### Generation with Advanced Parameters Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/GraniteSpeech/README.md Generates output using advanced parameters like `maxTokens`, `temperature` for decoding strategy, a custom `prompt`, and `verbose` for timing information. ```swift let output = model.generate( audio: audio, maxTokens: 4096, temperature: 0.0, // 0 = greedy decoding prompt: "Translate the speech to text.", verbose: true // print timing info ) ``` -------------------------------- ### Generate Speech with Echo TTS in Swift Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/EchoTTS/README.md This Swift code demonstrates how to load a pre-trained Echo TTS model, load a reference audio clip, and generate speech from text using voice cloning. Ensure necessary frameworks are imported and audio files are correctly path-ed. ```swift import Foundation import MLXAudioCore import MLXAudioTTS let model = try await EchoTTSModel.fromPretrained("mlx-community/echo-tts-base") let (_, refAudio) = try loadAudioArray( from: URL(fileURLWithPath: "speaker.wav"), sampleRate: model.sampleRate ) let audio = try await model.generate( text: "Hello from Echo TTS.", voice: nil, refAudio: refAudio, refText: nil, language: nil ) ``` -------------------------------- ### Run Llama TTS from CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/Llama/README.md Execute Orpheus TTS using the mlx-audio-swift-tts command-line tool. Specify the model, voice, and text to generate speech. ```bash mlx-audio-swift-tts --model mlx-community/orpheus-3b-0.1-ft-bf16 --voice tara --text "Hello world." ``` -------------------------------- ### Load Audio and Generate Text with GLM-ASR Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/GLMASR/README.md Loads an audio file and generates its text transcription using the GLM-ASR model. Requires MLXAudioCore and MLXAudioSTT libraries. Ensure the audioURL is valid. ```swift import MLXAudioCore import MLXAudioSTT let (sampleRate, audio) = try loadAudioArray(from: audioURL) _ = sampleRate let model = try await GLMASRModel.fromPretrained("mlx-community/GLM-ASR-Nano-2512-4bit") let output = model.generate(audio: audio) print(output.text) ``` -------------------------------- ### Run Local-Transformer MOSS-TTS via CLI Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/Models/MossTTS/README.md Use this command to generate speech with the MOSS local-transformer model. This variant offers different architectural properties. ```bash swift run mlx-audio-swift-tts \ --model OpenMOSS-Team/MOSS-TTS-Local-Transformer \ --text "Hello, this is the MOSS local-transformer model running from Swift." \ --output moss-local-transformer.wav ``` -------------------------------- ### Process Audio and Text with LFM2AudioProcessor Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTS/Models/LFMAudio/README.md Provides methods for tokenizing text, decoding tokens, and preprocessing audio data for the model. ```swift public class LFM2AudioProcessor { public func tokenize(_ text: String) -> [Int] public func decodeText(_ tokens: [Int]) -> String public func preprocessAudio(_ audio: MLXArray, sampleRate: Int) -> MLXArray } ``` -------------------------------- ### Perform Speech-to-Text Inference with FireRed ASR 2 Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioSTT/Models/FireRedASR2/README.md Loads a pretrained model and generates text from audio input. Requires audio to be in mono 16 kHz format. ```swift import MLXAudioCore import MLXAudioSTT let (_, audio) = try loadAudioArray(from: audioURL, sampleRate: 16000) let model = try await FireRedASR2Model.fromPretrained("mlx-community/FireRedASR2-AED-mlx") let output = model.generate(audio: audio) print(output.text) ``` -------------------------------- ### Streaming from File Source: https://github.com/blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioVAD/Models/Sortformer/README.md Process a file using the streaming API. ```swift for try await result in model.generateStream( audio: audio, chunkDuration: 5.0, verbose: true ) { for seg in result.segments { print("Speaker \(seg.speaker): \(seg.start)s - \(seg.end)s") } } ```