-
Notifications
You must be signed in to change notification settings - Fork 300
Add Nemotron-ASR streaming inference to C++ SDK #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d2ef88d
update app.js
e31d9a1
fix: eliminate redundant PCM buffer copy in audio data handler
Copilot 4eb318d
Merge origin/main into ruiren/app-js, keeping live-audio-transcriptio…
Copilot 9269c46
Merge branch 'main' into ruiren/app-js
2155d82
Add live audio transcription streaming to C++ SDK
631eba8
Merge remote-tracking branch 'origin/main' into ruiren/live-audio-str…
6e9e5d5
Remove unrelated app.js from PR diff
25446d9
Address PR #655 review feedback from kunal-vaishnavi and bmehta001
ef253f0
Address Copilot review feedback: fix deadlock, validation, and safety
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <memory> | ||
| #include <thread> | ||
| #include <mutex> | ||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| #include <gsl/pointers> | ||
|
|
||
| #include "openai_live_audio_types.h" | ||
|
|
||
| namespace foundry_local::Internal { | ||
| struct IFoundryLocalCore; | ||
| template <typename T> class ThreadSafeQueue; | ||
| } // namespace foundry_local::Internal | ||
|
|
||
| namespace foundry_local { | ||
| class ILogger; | ||
|
|
||
| class LiveAudioTranscriptionSession final { | ||
| public: | ||
| LiveAudioTranscriptionSession(gsl::not_null<Internal::IFoundryLocalCore*> core, | ||
| std::string modelId, | ||
| gsl::not_null<ILogger*> logger); | ||
| ~LiveAudioTranscriptionSession() noexcept; | ||
|
|
||
| // Non-copyable, non-movable | ||
| LiveAudioTranscriptionSession(const LiveAudioTranscriptionSession&) = delete; | ||
| LiveAudioTranscriptionSession& operator=(const LiveAudioTranscriptionSession&) = delete; | ||
| LiveAudioTranscriptionSession(LiveAudioTranscriptionSession&&) = delete; | ||
| LiveAudioTranscriptionSession& operator=(LiveAudioTranscriptionSession&&) = delete; | ||
|
|
||
| /// Mutable settings reference; only effective before Start(). | ||
| LiveAudioTranscriptionOptions& Settings() { return settings_; } | ||
| /// Read-only settings reference. | ||
| const LiveAudioTranscriptionOptions& Settings() const { return settings_; } | ||
| /// Settings that were active when Start() was called. | ||
| const LiveAudioTranscriptionOptions& ActiveSettings() const { return activeSettings_; } | ||
|
|
||
| /// Begin the streaming session. Must be called before Append/TryAppend. | ||
| void Start(); | ||
|
|
||
| /// Enqueue PCM audio data. Blocks if the push queue is full. | ||
| void Append(const uint8_t* pcmData, size_t length); | ||
|
|
||
| /// Try to get the next transcription result within the given timeout. | ||
| TranscriptionStatus TryGetNext(LiveAudioTranscriptionResponse& result, | ||
| std::chrono::milliseconds timeout = std::chrono::seconds(5)); | ||
|
|
||
| /// Signal the end of audio input and stop the session. | ||
| void Stop(); | ||
|
|
||
| /// Returns the error message if the session is in an error state. | ||
| std::string GetErrorMessage() const; | ||
|
|
||
| /// Returns true if the session has been started. | ||
| bool IsStarted() const; | ||
|
|
||
| /// Returns true if the session has been stopped. | ||
| bool IsStopped() const; | ||
|
|
||
| private: | ||
| enum class SessionState { | ||
| Created, | ||
| Starting, | ||
| Started, | ||
| Stopped | ||
| }; | ||
|
|
||
| void PushWorkerLoop(); | ||
| void StopInternal(std::unique_lock<std::mutex>& lock); | ||
|
|
||
| gsl::not_null<Internal::IFoundryLocalCore*> core_; | ||
| std::string modelId_; | ||
| gsl::not_null<ILogger*> logger_; | ||
|
|
||
| LiveAudioTranscriptionOptions settings_; | ||
| LiveAudioTranscriptionOptions activeSettings_; | ||
|
|
||
| mutable std::mutex mutex_; | ||
| SessionState state_ = SessionState::Created; | ||
| std::string sessionHandle_; | ||
|
|
||
| using AudioChunk = std::vector<uint8_t>; | ||
| std::unique_ptr<Internal::ThreadSafeQueue<AudioChunk>> pushQueue_; | ||
| std::unique_ptr<Internal::ThreadSafeQueue<LiveAudioTranscriptionResponse>> resultQueue_; | ||
|
|
||
| std::thread pushThread_; | ||
| std::string errorMessage_; | ||
| LiveAudioTranscriptionResponse finalResult_; | ||
| bool hasFinalResult_ = false; | ||
| }; | ||
|
|
||
| } // namespace foundry_local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| #include <optional> | ||
|
|
||
| namespace foundry_local { | ||
|
|
||
| struct ContentPart { | ||
| std::string text; | ||
| std::string transcript; | ||
| }; | ||
|
|
||
| struct LiveAudioTranscriptionResponse { | ||
| std::string text; | ||
| bool is_final = false; | ||
| std::optional<double> start_time; | ||
| std::optional<double> end_time; | ||
| std::vector<ContentPart> content; | ||
|
|
||
| static LiveAudioTranscriptionResponse FromJson(const std::string& json); | ||
| }; | ||
|
|
||
| struct LiveAudioTranscriptionOptions { | ||
| int sample_rate = 16000; | ||
| int channels = 1; | ||
| int bits_per_sample = 16; | ||
| std::optional<std::string> language; | ||
| int push_queue_capacity = 100; | ||
| }; | ||
|
|
||
| struct CoreErrorResponse { | ||
| std::string code; | ||
| std::string message; | ||
| bool is_transient = false; | ||
|
|
||
| static std::optional<CoreErrorResponse> TryParse(const std::string& error_string); | ||
| }; | ||
|
|
||
| enum class TranscriptionStatus { | ||
| Result, | ||
| Timeout, | ||
| Closed, | ||
| Error | ||
| }; | ||
|
|
||
| } // namespace foundry_local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.