|
| 1 | +// Live Audio Transcription — Foundry Local C++ SDK Example |
| 2 | +// |
| 3 | +// This sample is based on the live-audio C++ APIs added in PR #655: |
| 4 | +// - OpenAIAudioClient::CreateLiveTranscriptionSession() |
| 5 | +// - LiveAudioTranscriptionSession::{Start, Append, TryGetNext, Stop} |
| 6 | + |
| 7 | +#include <chrono> |
| 8 | +#include <climits> |
| 9 | +#include <algorithm> |
| 10 | +#include <cmath> |
| 11 | +#include <cstdint> |
| 12 | +#include <iostream> |
| 13 | +#include <string> |
| 14 | +#include <thread> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "foundry_local.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | +std::vector<uint8_t> GenerateSineWavePcm(int sampleRate, int durationSeconds, double frequencyHz) { |
| 21 | + const auto totalSamples = static_cast<size_t>(sampleRate * durationSeconds); |
| 22 | + std::vector<uint8_t> pcm(totalSamples * 2, 0); // 16-bit mono |
| 23 | + |
| 24 | + for (size_t i = 0; i < totalSamples; ++i) { |
| 25 | + const double t = static_cast<double>(i) / static_cast<double>(sampleRate); |
| 26 | + const auto sample = static_cast<int16_t>( |
| 27 | + static_cast<double>(INT16_MAX) * 0.5 * std::sin(2.0 * 3.14159265358979323846 * frequencyHz * t)); |
| 28 | + const auto b = reinterpret_cast<const uint8_t*>(&sample); |
| 29 | + pcm[i * 2] = b[0]; |
| 30 | + pcm[i * 2 + 1] = b[1]; |
| 31 | + } |
| 32 | + return pcm; |
| 33 | +} |
| 34 | +} // namespace |
| 35 | + |
| 36 | +int main() { |
| 37 | + try { |
| 38 | + // Manager/model bootstrapping follows the same pattern as other Foundry Local SDK samples. |
| 39 | + foundry_local::Configuration config; |
| 40 | + config.appName = "foundry_local_samples"; |
| 41 | + |
| 42 | + auto manager = foundry_local::FoundryLocalManager::Create(config); |
| 43 | + auto catalog = manager->GetCatalog(); |
| 44 | + auto model = catalog.GetModel("nemotron"); |
| 45 | + if (!model) { |
| 46 | + throw std::runtime_error("Model \"nemotron\" not found in catalog"); |
| 47 | + } |
| 48 | + |
| 49 | + model->Download(); |
| 50 | + model->Load(); |
| 51 | + |
| 52 | + auto audioClient = model->GetAudioClient(); |
| 53 | + auto session = audioClient.CreateLiveTranscriptionSession(); |
| 54 | + |
| 55 | + session->Settings().sample_rate = 16000; |
| 56 | + session->Settings().channels = 1; |
| 57 | + session->Settings().bits_per_sample = 16; |
| 58 | + session->Settings().language = "en"; |
| 59 | + session->Start(); |
| 60 | + |
| 61 | + std::cout << "Session started. Pushing synthetic audio..." << std::endl; |
| 62 | + const auto pcm = GenerateSineWavePcm(16000, 3, 440.0); |
| 63 | + const size_t chunkSize = static_cast<size_t>(16000 / 10 * 2); // 100ms |
| 64 | + for (size_t offset = 0; offset < pcm.size(); offset += chunkSize) { |
| 65 | + const size_t len = std::min(chunkSize, pcm.size() - offset); |
| 66 | + session->Append(pcm.data() + offset, len); |
| 67 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 68 | + } |
| 69 | + |
| 70 | + foundry_local::LiveAudioTranscriptionResponse result; |
| 71 | + while (true) { |
| 72 | + const auto status = session->TryGetNext(result, std::chrono::milliseconds(500)); |
| 73 | + if (status == foundry_local::TranscriptionStatus::Result) { |
| 74 | + if (result.is_final) { |
| 75 | + std::cout << "\n[FINAL] " << result.text << std::endl; |
| 76 | + } else { |
| 77 | + std::cout << result.text << std::flush; |
| 78 | + } |
| 79 | + } else if (status == foundry_local::TranscriptionStatus::Timeout) { |
| 80 | + break; |
| 81 | + } else if (status == foundry_local::TranscriptionStatus::Closed) { |
| 82 | + break; |
| 83 | + } else { |
| 84 | + std::cerr << "Transcription stream error: " << session->GetErrorMessage() << std::endl; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + session->Stop(); |
| 90 | + model->Unload(); |
| 91 | + return 0; |
| 92 | + } catch (const std::exception& ex) { |
| 93 | + std::cerr << "Error: " << ex.what() << std::endl; |
| 94 | + return 1; |
| 95 | + } |
| 96 | +} |
0 commit comments