-
Notifications
You must be signed in to change notification settings - Fork 26
Draft for a DeepL Voice implementation #75
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
Open
DeeJayTC
wants to merge
2
commits into
main
Choose a base branch
from
tc/add-voice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DeepL { | ||
| /// <summary>Interface for creating Voice API streaming sessions.</summary> | ||
| public interface IVoiceManager : IDisposable { | ||
| /// <summary> | ||
| /// Creates a new Voice API streaming session for real-time speech transcription and translation. | ||
| /// This requests a session from the DeepL API and establishes a WebSocket connection. | ||
| /// </summary> | ||
| /// <param name="options">Options controlling session configuration including audio format, languages, etc.</param> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <returns>An <see cref="IVoiceSession" /> for streaming audio and receiving transcripts.</returns> | ||
| /// <exception cref="ArgumentException">If any option is invalid.</exception> | ||
| /// <exception cref="DeepLException"> | ||
| /// If any error occurs while communicating with the DeepL API, a | ||
| /// <see cref="DeepLException" /> or a derived class will be thrown. | ||
| /// </exception> | ||
| Task<IVoiceSession> CreateVoiceSessionAsync( | ||
| VoiceSessionOptions options, | ||
| CancellationToken cancellationToken = default); | ||
| } | ||
| } |
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,77 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using DeepL.Model; | ||
|
|
||
| namespace DeepL { | ||
| /// <summary> | ||
| /// Represents an active Voice API streaming session. Provides methods for sending audio data and receiving | ||
| /// real-time transcriptions and translations via events. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Events fire on a background thread. Consumers are responsible for marshaling to the appropriate | ||
| /// synchronization context if needed. Dispose the session to close the WebSocket connection. | ||
| /// </remarks> | ||
| public interface IVoiceSession : IDisposable { | ||
| /// <summary>Raised when a source transcript update is received from the server.</summary> | ||
| event EventHandler<TranscriptUpdate>? SourceTranscriptUpdated; | ||
|
|
||
| /// <summary>Raised when a target transcript update is received from the server.</summary> | ||
| event EventHandler<TranscriptUpdate>? TargetTranscriptUpdated; | ||
|
|
||
| /// <summary> | ||
| /// Raised when a target media audio chunk is received from the server. This feature is in closed beta. | ||
| /// </summary> | ||
| event EventHandler<TargetMediaChunk>? TargetMediaChunkReceived; | ||
|
|
||
| /// <summary>Raised when an error message is received from the WebSocket connection.</summary> | ||
| event EventHandler<VoiceStreamError>? ErrorReceived; | ||
|
|
||
| /// <summary>Raised when the end-of-stream message is received, indicating all outputs are complete.</summary> | ||
| event EventHandler? StreamEnded; | ||
|
|
||
| /// <summary>The unique session identifier.</summary> | ||
| string? SessionId { get; } | ||
|
|
||
| /// <summary>Whether the WebSocket connection is currently open.</summary> | ||
| bool IsConnected { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sends a chunk of audio data to the server. The audio encoding must match the | ||
| /// <see cref="VoiceSessionOptions.SourceMediaContentType" /> specified when creating the session. | ||
| /// </summary> | ||
| /// <param name="audioData">Audio data to send. Must not exceed 100 KB or 1 second duration.</param> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <exception cref="DeepLException">If the session is not connected or sending fails.</exception> | ||
| Task SendAudioAsync(byte[] audioData, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Sends a chunk of audio data to the server using a memory-efficient overload. | ||
| /// </summary> | ||
| /// <param name="audioData">Audio data to send. Must not exceed 100 KB or 1 second duration.</param> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <exception cref="DeepLException">If the session is not connected or sending fails.</exception> | ||
| Task SendAudioAsync(ArraySegment<byte> audioData, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Signals the end of the audio stream. Causes finalization of tentative transcript segments and | ||
| /// triggers emission of final transcript updates, end-of-transcript, and end-of-stream messages. | ||
| /// No more audio data can be sent after calling this method. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <exception cref="DeepLException">If the session is not connected or sending fails.</exception> | ||
| Task EndAudioAsync(CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Requests a reconnection token and establishes a new WebSocket connection, resuming the session. | ||
| /// This should be called when the WebSocket connection is lost unexpectedly. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> | ||
| /// <exception cref="DeepLException">If reconnection fails.</exception> | ||
| Task ReconnectAsync(CancellationToken cancellationToken = default); | ||
| } | ||
| } |
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,68 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace DeepL.Model { | ||
| /// <summary> | ||
| /// Represents a translated audio chunk from the Voice API. This feature is currently in closed beta. | ||
| /// Audio data is provided as an array of base64-encoded indivisible chunks. | ||
| /// </summary> | ||
| public sealed class TargetMediaChunk { | ||
| /// <summary>Initializes a new instance of <see cref="TargetMediaChunk" />.</summary> | ||
| /// <param name="contentType">The content type of the audio data. Present in the first message.</param> | ||
| /// <param name="headers">Number of header packets at the start of the data array, or null if all are audio.</param> | ||
| /// <param name="data">Array of base64-encoded audio data packets.</param> | ||
| /// <param name="text">Text corresponding to this audio chunk, for subtitle synchronization.</param> | ||
| /// <param name="language">The target language of this audio chunk.</param> | ||
| /// <param name="duration">Duration of this audio chunk in seconds.</param> | ||
| /// <remarks> | ||
| /// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it | ||
| /// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization. | ||
| /// In future this function may have backwards-incompatible changes. | ||
| /// </remarks> | ||
| [JsonConstructor] | ||
| public TargetMediaChunk( | ||
| string? contentType, | ||
| int? headers, | ||
| string[] data, | ||
| string? text, | ||
| string? language, | ||
| double? duration) { | ||
| ContentType = contentType; | ||
| Headers = headers; | ||
| Data = data; | ||
| Text = text; | ||
| Language = language; | ||
| Duration = duration; | ||
| } | ||
|
|
||
| /// <summary>The content type of the audio data. Present in the first message of a sequence.</summary> | ||
| [JsonPropertyName("content_type")] | ||
| public string? ContentType { get; } | ||
|
|
||
| /// <summary> | ||
| /// Number of packets at the start of <see cref="Data" /> that contain initialization/header data. | ||
| /// Null or absent when all packets are audio data. | ||
| /// </summary> | ||
| [JsonPropertyName("headers")] | ||
| public int? Headers { get; } | ||
|
|
||
| /// <summary>Array of base64-encoded indivisible audio data packets.</summary> | ||
| [JsonPropertyName("data")] | ||
| public string[] Data { get; } | ||
|
|
||
| /// <summary>Text corresponding to this audio chunk, for subtitle synchronization.</summary> | ||
| [JsonPropertyName("text")] | ||
| public string? Text { get; } | ||
|
|
||
| /// <summary>The target language of this audio chunk.</summary> | ||
| [JsonPropertyName("language")] | ||
| public string? Language { get; } | ||
|
|
||
| /// <summary>Duration of this audio chunk in seconds.</summary> | ||
| [JsonPropertyName("duration")] | ||
| public double? Duration { get; } | ||
| } | ||
| } |
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,29 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace DeepL.Model { | ||
| /// <summary>A single text segment within a Voice API transcript update.</summary> | ||
| public sealed class TranscriptSegment { | ||
| /// <summary>Initializes a new instance of <see cref="TranscriptSegment" />.</summary> | ||
| /// <param name="text">The text content of this segment.</param> | ||
| /// <remarks> | ||
| /// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it | ||
| /// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization. | ||
| /// In future this function may have backwards-incompatible changes. | ||
| /// </remarks> | ||
| [JsonConstructor] | ||
| public TranscriptSegment(string text) { | ||
| Text = text; | ||
| } | ||
|
|
||
| /// <summary>The text content of this segment.</summary> | ||
| [JsonPropertyName("text")] | ||
| public string Text { get; } | ||
|
|
||
| /// <summary>Returns the text content of this segment.</summary> | ||
| public override string ToString() => Text; | ||
| } | ||
| } |
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,41 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace DeepL.Model { | ||
| /// <summary> | ||
| /// Represents a transcript update from the Voice API, containing concluded (finalized) and tentative | ||
| /// (in-progress) text segments. Used for both source and target transcript updates. | ||
| /// </summary> | ||
| public sealed class TranscriptUpdate { | ||
| /// <summary>Initializes a new instance of <see cref="TranscriptUpdate" />.</summary> | ||
| /// <param name="concluded">Finalized text segments that will not change.</param> | ||
| /// <param name="tentative">Preliminary text segments that may be refined.</param> | ||
| /// <param name="language">The language code of this transcript update. Only present on target updates.</param> | ||
| /// <remarks> | ||
| /// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it | ||
| /// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization. | ||
| /// In future this function may have backwards-incompatible changes. | ||
| /// </remarks> | ||
| [JsonConstructor] | ||
| public TranscriptUpdate(TranscriptSegment[] concluded, TranscriptSegment[] tentative, string? language) { | ||
| Concluded = concluded; | ||
| Tentative = tentative; | ||
| Language = language; | ||
| } | ||
|
|
||
| /// <summary>Finalized text segments that will not change. These segments are sent once and remain fixed.</summary> | ||
| [JsonPropertyName("concluded")] | ||
| public TranscriptSegment[] Concluded { get; } | ||
|
|
||
| /// <summary>Preliminary text segments that may be refined as more audio context becomes available.</summary> | ||
| [JsonPropertyName("tentative")] | ||
| public TranscriptSegment[] Tentative { get; } | ||
|
|
||
| /// <summary>The language code of this transcript update. Only present on target transcript updates.</summary> | ||
| [JsonPropertyName("language")] | ||
| public string? Language { get; } | ||
| } | ||
| } |
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,40 @@ | ||
| // Copyright 2025 DeepL SE (https://www.deepl.com) | ||
| // Use of this source code is governed by an MIT | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace DeepL.Model { | ||
| /// <summary>Information about a Voice API session, received from the session request endpoint.</summary> | ||
| public sealed class VoiceSessionInfo { | ||
| /// <summary>Initializes a new instance of <see cref="VoiceSessionInfo" />.</summary> | ||
| /// <param name="streamingUrl">The WebSocket URL for establishing the stream connection.</param> | ||
| /// <param name="token">Ephemeral authentication token for the streaming endpoint.</param> | ||
| /// <param name="sessionId">Unique identifier for the session.</param> | ||
| /// <remarks> | ||
| /// The constructor for this class (and all other Model classes) should not be used by library users. Ideally it | ||
| /// would be marked <see langword="internal" />, but needs to be <see langword="public" /> for JSON deserialization. | ||
| /// In future this function may have backwards-incompatible changes. | ||
| /// </remarks> | ||
| [JsonConstructor] | ||
| public VoiceSessionInfo(string streamingUrl, string token, string? sessionId) { | ||
| StreamingUrl = streamingUrl; | ||
| Token = token; | ||
| SessionId = sessionId; | ||
| } | ||
|
|
||
| /// <summary>The WebSocket URL to use for establishing the stream connection.</summary> | ||
| [JsonPropertyName("streaming_url")] | ||
| public string StreamingUrl { get; } | ||
|
|
||
| /// <summary> | ||
| /// Ephemeral authentication token for the streaming endpoint. Valid for one-time use only. | ||
| /// </summary> | ||
| [JsonPropertyName("token")] | ||
| public string Token { get; } | ||
|
|
||
| /// <summary>Unique identifier for the session.</summary> | ||
| [JsonPropertyName("session_id")] | ||
| public string? SessionId { get; } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CreateVoiceSessionAsyncvalidatesTargetLanguagescount, but does not validateTargetMediaLanguages(max 5 per docs) nor enforce that target media languages are included intarget_languages. This can lead to avoidable API-side errors; consider mergingTargetMediaLanguagesintoTargetLanguages(deduping) and validating the combined count before sending the request.