diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Azure.AI.Speech.Transcription.sln b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Azure.AI.Speech.Transcription.sln new file mode 100644 index 000000000000..0e27ceffdc2c --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Azure.AI.Speech.Transcription.sln @@ -0,0 +1,56 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29709.97 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{ECC730C1-4AEA-420C-916A-66B19B79E4DC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.AI.Speech.Transcription", "src\Azure.AI.Speech.Transcription.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.AI.Speech.Transcription.Tests", "tests\Azure.AI.Speech.Transcription.Tests.csproj", "{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU + {8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU + {ECC730C1-4AEA-420C-916A-66B19B79E4DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ECC730C1-4AEA-420C-916A-66B19B79E4DC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ECC730C1-4AEA-420C-916A-66B19B79E4DC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ECC730C1-4AEA-420C-916A-66B19B79E4DC}.Release|Any CPU.Build.0 = Release|Any CPU + {A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU + {FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU + {85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU + {28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU + {1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE} + EndGlobalSection +EndGlobal diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/CHANGELOG.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/CHANGELOG.md new file mode 100644 index 000000000000..8b33f0fedccc --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/CHANGELOG.md @@ -0,0 +1,11 @@ +# Release History + +## 1.0.0-beta.1 (Unreleased) + +### Features Added + +### Breaking Changes + +### Bugs Fixed + +### Other Changes \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Directory.Build.props b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Directory.Build.props new file mode 100644 index 000000000000..63bd836ad44b --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/Directory.Build.props @@ -0,0 +1,6 @@ + + + + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/README.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/README.md new file mode 100644 index 000000000000..ef9c504869f2 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/README.md @@ -0,0 +1,107 @@ +# Azure AI Speech Transcription client library for .NET + +Azure.AI.Speech.Transcription is a managed service that helps developers get secret simply and securely. + +Use the client library for to: + +* [Get secret](https://docs.microsoft.com/azure) + +[Source code][source_root] | [Package (NuGet)][package] | [API reference documentation][reference_docs] | [Product documentation][azconfig_docs] | [Samples][source_samples] + + [Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src) | [Package (NuGet)](https://www.nuget.org/packages) | [API reference documentation](https://azure.github.io/azure-sdk-for-net) | [Product documentation](https://docs.microsoft.com/azure) + +## Getting started + +This section should include everything a developer needs to do to install and create their first client connection *very quickly*. + +### Install the package + +First, provide instruction for obtaining and installing the package or library. This section might include only a single line of code, like `dotnet add package package-name`, but should enable a developer to successfully install the package from NuGet, npm, or even cloning a GitHub repository. + +Install the client library for .NET with [NuGet](https://www.nuget.org/ ): + +```dotnetcli +dotnet add package Azure.AI.Speech.Transcription --prerelease +``` + +### Prerequisites + +Include a section after the install command that details any requirements that must be satisfied before a developer can [authenticate](#authenticate-the-client) and test all of the snippets in the [Examples](#examples) section. For example, for Cosmos DB: + +> You must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/) and [Cosmos DB account](https://docs.microsoft.com/azure/cosmos-db/account-overview) (SQL API). In order to take advantage of the C# 8.0 syntax, it is recommended that you compile using the [.NET Core SDK](https://dotnet.microsoft.com/download) 3.0 or higher with a [language version](https://docs.microsoft.com/dotnet/csharp/language-reference/configure-language-version#override-a-default) of `latest`. It is also possible to compile with the .NET Core SDK 2.1.x using a language version of `preview`. + +### Authenticate the client + +If your library requires authentication for use, such as for Azure services, include instructions and example code needed for initializing and authenticating. + +For example, include details on obtaining an account key and endpoint URI, setting environment variables for each, and initializing the client object. + +### Service API versions + +The client library targets the latest service API version by default. A client instance accepts an optional service API version parameter from its options to specify which API version service to communicate. + +#### Select a service API version + +You have the flexibility to explicitly select a supported service API version when instantiating a client by configuring its associated options. This ensures that the client can communicate with services using the specified API version. + +For example, + +```C# Snippet:CreateTranscriptionClientForSpecificApiVersion +Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new("your apikey"); +TranscriptionClientOptions options = new TranscriptionClientOptions(TranscriptionClientOptions.ServiceVersion.V2025_10_15); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); +``` + +When selecting an API version, it's important to verify that there are no breaking changes compared to the latest API version. If there are significant differences, API calls may fail due to incompatibility. + +Always ensure that the chosen API version is fully supported and operational for your specific use case and that it aligns with the service's versioning policy. + +## Key concepts + +The *Key concepts* section should describe the functionality of the main classes. Point out the most important and useful classes in the package (with links to their reference pages) and explain how those classes work together. Feel free to use bulleted lists, tables, code blocks, or even diagrams for clarity. + +Include the *Thread safety* and *Additional concepts* sections below at the end of your *Key concepts* section. You may remove or add links depending on what your library makes use of: + +### Thread safety + +We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads. + +### Additional concepts + +[Client options](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#configuring-service-clients-using-clientoptions) | +[Accessing the response](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#accessing-http-response-details-using-responset) | +[Long-running operations](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#consuming-long-running-operations-using-operationt) | +[Handling failures](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#reporting-errors-requestfailedexception) | +[Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md) | +[Mocking](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#mocking) | +[Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/) + + +## Examples + +You can familiarize yourself with different APIs using [Samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples). + +## Troubleshooting + +Describe common errors and exceptions, how to "unpack" them if necessary, and include guidance for graceful handling and recovery. + +Provide information to help developers avoid throttling or other service-enforced errors they might encounter. For example, provide guidance and examples for using retry or connection policies in the API. + +If the package or a related package supports it, include tips for logging or enabling instrumentation to help them debug their code. + +## Next steps + +* Provide a link to additional code examples, ideally to those sitting alongside the README in the package's `/samples` directory. +* If appropriate, point users to other packages that might be useful. +* If you think there's a good chance that developers might stumble across your package in error (because they're searching for specific functionality and mistakenly think the package provides that functionality), point them to the packages they might be looking for. + +## Contributing + +This is a template, but your SDK readme should include details on how to contribute code to the repo/package. + + +[style-guide-msft]: https://docs.microsoft.com/style-guide/capitalization +[style-guide-cloud]: https://aka.ms/azsdk/cloud-style-guide + +![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net/sdk/cognitiveservices/Azure.AI.Speech.Transcription/README.png) \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.net8.0.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.net8.0.cs new file mode 100644 index 000000000000..dab85f3c4e19 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.net8.0.cs @@ -0,0 +1,170 @@ +namespace Azure.AI.Speech.Transcription +{ + public static partial class AISpeechTranscriptionModelFactory + { + public static Azure.AI.Speech.Transcription.EnhancedModeProperties EnhancedModeProperties(bool? enabled = default(bool?), string task = null, string targetLanguage = null, System.Collections.Generic.IEnumerable prompt = null) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions TranscriptionDiarizationOptions(bool? enabled = default(bool?), int? maxSpeakers = default(int?)) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionOptions TranscriptionOptions(System.Uri audioUri = null, System.Collections.Generic.IEnumerable locales = null, System.Collections.Generic.IDictionary models = null, Azure.AI.Speech.Transcription.ProfanityFilterMode? profanityFilterMode = default(Azure.AI.Speech.Transcription.ProfanityFilterMode?), Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions diarizationOptions = null, System.Collections.Generic.IEnumerable activeChannels = null, Azure.AI.Speech.Transcription.EnhancedModeProperties enhancedMode = null, Azure.AI.Speech.Transcription.PhraseListProperties phraseList = null) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionResult TranscriptionResult(int durationMilliseconds = 0) { throw null; } + } + public partial class AzureAISpeechTranscriptionContext : System.ClientModel.Primitives.ModelReaderWriterContext + { + internal AzureAISpeechTranscriptionContext() { } + public static Azure.AI.Speech.Transcription.AzureAISpeechTranscriptionContext Default { get { throw null; } } + protected override bool TryGetTypeBuilderCore(System.Type type, out System.ClientModel.Primitives.ModelReaderWriterTypeBuilder builder) { throw null; } + } + public partial class EnhancedModeProperties : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public EnhancedModeProperties() { } + public bool? Enabled { get { throw null; } } + public System.Collections.Generic.IList Prompt { get { throw null; } } + public string TargetLanguage { get { throw null; } set { } } + public string Task { get { throw null; } set { } } + Azure.AI.Speech.Transcription.EnhancedModeProperties System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.EnhancedModeProperties System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class PhraseListProperties : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public PhraseListProperties() { } + public float? BiasingWeight { get { throw null; } set { } } + public System.Collections.Generic.IList Phrases { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.PhraseListProperties System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.PhraseListProperties System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct ProfanityFilterMode : System.IEquatable + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public ProfanityFilterMode(string value) { throw null; } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Masked { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode None { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Removed { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Tags { get { throw null; } } + public bool Equals(Azure.AI.Speech.Transcription.ProfanityFilterMode other) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + public override bool Equals(object obj) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + public override int GetHashCode() { throw null; } + public static bool operator ==(Azure.AI.Speech.Transcription.ProfanityFilterMode left, Azure.AI.Speech.Transcription.ProfanityFilterMode right) { throw null; } + public static implicit operator Azure.AI.Speech.Transcription.ProfanityFilterMode (string value) { throw null; } + public static bool operator !=(Azure.AI.Speech.Transcription.ProfanityFilterMode left, Azure.AI.Speech.Transcription.ProfanityFilterMode right) { throw null; } + public override string ToString() { throw null; } + } + public partial class TranscribedPhrase : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscribedPhrase() { } + public float Confidence { get { throw null; } } + public System.TimeSpan Duration { get { throw null; } } + public string Locale { get { throw null; } } + public System.TimeSpan Offset { get { throw null; } } + public int? Speaker { get { throw null; } } + public string Text { get { throw null; } } + public System.Collections.Generic.IReadOnlyList Words { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedPhrase System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedPhrase System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscribedPhrases + { + public int? Channel; + public System.Collections.Generic.IEnumerable Phrases; + public TranscribedPhrases(int? Channel, string Text, System.Collections.Generic.IEnumerable Phrases) { } + public string Text { get { throw null; } } + } + public partial class TranscribedWord : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscribedWord() { } + public System.TimeSpan Duration { get { throw null; } } + public System.TimeSpan Offset { get { throw null; } } + public string Text { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedWord System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedWord System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionClient + { + protected TranscriptionClient() { } + public TranscriptionClient(System.Uri endpoint, Azure.AzureKeyCredential credential) { } + public TranscriptionClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.AI.Speech.Transcription.TranscriptionClientOptions options) { } + public TranscriptionClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { } + public TranscriptionClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.AI.Speech.Transcription.TranscriptionClientOptions options) { } + public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } + public virtual Azure.Response Transcribe(Azure.AI.Speech.Transcription.TranscriptionOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task> TranscribeAsync(Azure.AI.Speech.Transcription.TranscriptionOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + } + public partial class TranscriptionClientOptions : Azure.Core.ClientOptions + { + public TranscriptionClientOptions(Azure.AI.Speech.Transcription.TranscriptionClientOptions.ServiceVersion version = Azure.AI.Speech.Transcription.TranscriptionClientOptions.ServiceVersion.V2025_10_15) { } + public enum ServiceVersion + { + V2025_10_15 = 1, + } + } + public partial class TranscriptionDiarizationOptions : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public TranscriptionDiarizationOptions() { } + public bool? Enabled { get { throw null; } } + public int? MaxSpeakers { get { throw null; } set { } } + Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionOptions : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public TranscriptionOptions() { } + public TranscriptionOptions(System.IO.Stream audioStream) { } + public TranscriptionOptions(System.Uri audioUri) { } + public System.Collections.Generic.IList ActiveChannels { get { throw null; } } + public System.Uri AudioUri { get { throw null; } } + public Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions DiarizationOptions { get { throw null; } set { } } + public Azure.AI.Speech.Transcription.EnhancedModeProperties EnhancedMode { get { throw null; } set { } } + public System.Collections.Generic.IList Locales { get { throw null; } } + public System.Collections.Generic.IDictionary Models { get { throw null; } } + public Azure.AI.Speech.Transcription.PhraseListProperties PhraseList { get { throw null; } set { } } + public Azure.AI.Speech.Transcription.ProfanityFilterMode? ProfanityFilterMode { get { throw null; } set { } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionOptions System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionOptions System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionResult : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscriptionResult() { } + public System.TimeSpan Duration { get { throw null; } } + public System.Collections.Generic.IEnumerable PhrasesByChannel { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionResult System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionResult System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } +} +namespace Microsoft.Extensions.Azure +{ + public static partial class AISpeechTranscriptionClientBuilderExtensions + { + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, System.Uri endpoint) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential { throw null; } + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, System.Uri endpoint, Azure.AzureKeyCredential credential) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilder { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Requires unreferenced code until we opt into EnableConfigurationBindingGenerator.")] + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, TConfiguration configuration) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration { throw null; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.netstandard2.0.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.netstandard2.0.cs new file mode 100644 index 000000000000..18307e0ee5ed --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/api/Azure.AI.Speech.Transcription.netstandard2.0.cs @@ -0,0 +1,169 @@ +namespace Azure.AI.Speech.Transcription +{ + public static partial class AISpeechTranscriptionModelFactory + { + public static Azure.AI.Speech.Transcription.EnhancedModeProperties EnhancedModeProperties(bool? enabled = default(bool?), string task = null, string targetLanguage = null, System.Collections.Generic.IEnumerable prompt = null) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions TranscriptionDiarizationOptions(bool? enabled = default(bool?), int? maxSpeakers = default(int?)) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionOptions TranscriptionOptions(System.Uri audioUri = null, System.Collections.Generic.IEnumerable locales = null, System.Collections.Generic.IDictionary models = null, Azure.AI.Speech.Transcription.ProfanityFilterMode? profanityFilterMode = default(Azure.AI.Speech.Transcription.ProfanityFilterMode?), Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions diarizationOptions = null, System.Collections.Generic.IEnumerable activeChannels = null, Azure.AI.Speech.Transcription.EnhancedModeProperties enhancedMode = null, Azure.AI.Speech.Transcription.PhraseListProperties phraseList = null) { throw null; } + public static Azure.AI.Speech.Transcription.TranscriptionResult TranscriptionResult(int durationMilliseconds = 0) { throw null; } + } + public partial class AzureAISpeechTranscriptionContext : System.ClientModel.Primitives.ModelReaderWriterContext + { + internal AzureAISpeechTranscriptionContext() { } + public static Azure.AI.Speech.Transcription.AzureAISpeechTranscriptionContext Default { get { throw null; } } + protected override bool TryGetTypeBuilderCore(System.Type type, out System.ClientModel.Primitives.ModelReaderWriterTypeBuilder builder) { throw null; } + } + public partial class EnhancedModeProperties : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public EnhancedModeProperties() { } + public bool? Enabled { get { throw null; } } + public System.Collections.Generic.IList Prompt { get { throw null; } } + public string TargetLanguage { get { throw null; } set { } } + public string Task { get { throw null; } set { } } + Azure.AI.Speech.Transcription.EnhancedModeProperties System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.EnhancedModeProperties System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class PhraseListProperties : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public PhraseListProperties() { } + public float? BiasingWeight { get { throw null; } set { } } + public System.Collections.Generic.IList Phrases { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.PhraseListProperties System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.PhraseListProperties System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] + public readonly partial struct ProfanityFilterMode : System.IEquatable + { + private readonly object _dummy; + private readonly int _dummyPrimitive; + public ProfanityFilterMode(string value) { throw null; } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Masked { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode None { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Removed { get { throw null; } } + public static Azure.AI.Speech.Transcription.ProfanityFilterMode Tags { get { throw null; } } + public bool Equals(Azure.AI.Speech.Transcription.ProfanityFilterMode other) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + public override bool Equals(object obj) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + public override int GetHashCode() { throw null; } + public static bool operator ==(Azure.AI.Speech.Transcription.ProfanityFilterMode left, Azure.AI.Speech.Transcription.ProfanityFilterMode right) { throw null; } + public static implicit operator Azure.AI.Speech.Transcription.ProfanityFilterMode (string value) { throw null; } + public static bool operator !=(Azure.AI.Speech.Transcription.ProfanityFilterMode left, Azure.AI.Speech.Transcription.ProfanityFilterMode right) { throw null; } + public override string ToString() { throw null; } + } + public partial class TranscribedPhrase : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscribedPhrase() { } + public float Confidence { get { throw null; } } + public System.TimeSpan Duration { get { throw null; } } + public string Locale { get { throw null; } } + public System.TimeSpan Offset { get { throw null; } } + public int? Speaker { get { throw null; } } + public string Text { get { throw null; } } + public System.Collections.Generic.IReadOnlyList Words { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedPhrase System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedPhrase System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscribedPhrases + { + public int? Channel; + public System.Collections.Generic.IEnumerable Phrases; + public TranscribedPhrases(int? Channel, string Text, System.Collections.Generic.IEnumerable Phrases) { } + public string Text { get { throw null; } } + } + public partial class TranscribedWord : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscribedWord() { } + public System.TimeSpan Duration { get { throw null; } } + public System.TimeSpan Offset { get { throw null; } } + public string Text { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedWord System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscribedWord System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionClient + { + protected TranscriptionClient() { } + public TranscriptionClient(System.Uri endpoint, Azure.AzureKeyCredential credential) { } + public TranscriptionClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.AI.Speech.Transcription.TranscriptionClientOptions options) { } + public TranscriptionClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { } + public TranscriptionClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.AI.Speech.Transcription.TranscriptionClientOptions options) { } + public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } + public virtual Azure.Response Transcribe(Azure.AI.Speech.Transcription.TranscriptionOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task> TranscribeAsync(Azure.AI.Speech.Transcription.TranscriptionOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + } + public partial class TranscriptionClientOptions : Azure.Core.ClientOptions + { + public TranscriptionClientOptions(Azure.AI.Speech.Transcription.TranscriptionClientOptions.ServiceVersion version = Azure.AI.Speech.Transcription.TranscriptionClientOptions.ServiceVersion.V2025_10_15) { } + public enum ServiceVersion + { + V2025_10_15 = 1, + } + } + public partial class TranscriptionDiarizationOptions : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public TranscriptionDiarizationOptions() { } + public bool? Enabled { get { throw null; } } + public int? MaxSpeakers { get { throw null; } set { } } + Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionOptions : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + public TranscriptionOptions() { } + public TranscriptionOptions(System.IO.Stream audioStream) { } + public TranscriptionOptions(System.Uri audioUri) { } + public System.Collections.Generic.IList ActiveChannels { get { throw null; } } + public System.Uri AudioUri { get { throw null; } } + public Azure.AI.Speech.Transcription.TranscriptionDiarizationOptions DiarizationOptions { get { throw null; } set { } } + public Azure.AI.Speech.Transcription.EnhancedModeProperties EnhancedMode { get { throw null; } set { } } + public System.Collections.Generic.IList Locales { get { throw null; } } + public System.Collections.Generic.IDictionary Models { get { throw null; } } + public Azure.AI.Speech.Transcription.PhraseListProperties PhraseList { get { throw null; } set { } } + public Azure.AI.Speech.Transcription.ProfanityFilterMode? ProfanityFilterMode { get { throw null; } set { } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionOptions System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionOptions System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } + public partial class TranscriptionResult : System.ClientModel.Primitives.IJsonModel, System.ClientModel.Primitives.IPersistableModel + { + internal TranscriptionResult() { } + public System.TimeSpan Duration { get { throw null; } } + public System.Collections.Generic.IEnumerable PhrasesByChannel { get { throw null; } } + protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionResult System.ClientModel.Primitives.IJsonModel.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + void System.ClientModel.Primitives.IJsonModel.Write(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { } + Azure.AI.Speech.Transcription.TranscriptionResult System.ClientModel.Primitives.IPersistableModel.Create(System.BinaryData data, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + string System.ClientModel.Primitives.IPersistableModel.GetFormatFromOptions(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + System.BinaryData System.ClientModel.Primitives.IPersistableModel.Write(System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; } + } +} +namespace Microsoft.Extensions.Azure +{ + public static partial class AISpeechTranscriptionClientBuilderExtensions + { + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, System.Uri endpoint) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithCredential { throw null; } + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, System.Uri endpoint, Azure.AzureKeyCredential credential) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilder { throw null; } + public static Azure.Core.Extensions.IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, TConfiguration configuration) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration { throw null; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/README.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/README.md new file mode 100644 index 000000000000..072ceaaa6277 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/README.md @@ -0,0 +1,19 @@ +--- +page_type: sample +languages: +- csharp +products: +- azure +- azure-speech +name: Azure.AI.Speech.Transcription samples for .NET +description: Samples for the Azure.AI.Speech.Transcription client library +--- + +# Azure AI Speech Transcription client SDK samples + +- [Transcribe a Local File](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_1_TranscribeLocalFile.md) +- [Transcribe a Remote File](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_2_TranscribeRemoteFile.md) +- [Transcribe with Options](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_3_TranscribeWithOptions.md) +- [Advanced Remote File Transcription](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_4_AdvancedRemoteTranscription.md) +- [Mock a Client for Testing](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_5_MockClient.md) + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_1_TranscribeLocalFile.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_1_TranscribeLocalFile.md new file mode 100644 index 000000000000..15b2ce65b74e --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_1_TranscribeLocalFile.md @@ -0,0 +1,55 @@ +# Transcribe a Local File + +This sample shows how to transcribe a local file using the `Azure.AI.Speech.Transcription` SDK. + +## Create a Transcription Client + +To create a Transcription Client, you will need the service endpoint and credentials of your AI Foundry resource or Speech Service resource. You can specify the service version by providing a TranscriptionClientOptions instance. + +```C# Snippet:CreateTranscriptionClientForSpecificApiVersion +Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new("your apikey"); +TranscriptionClientOptions options = new TranscriptionClientOptions(TranscriptionClientOptions.ServiceVersion.V2025_10_15); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); +``` + +## Transcribe Local File (Synchronous) + +To transcribe a local file synchronously, create a stream from the file and call `Transcribe` on the `TranscriptionClient` clientlet, which returns the transcribed phrases and total duration of the file + +```C# Snippet:TranscribeLocalFileSync +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var request = new TranscriptionContent { Audio = fileStream }; + var response = client.Transcribe(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + +## Transcribe Local File (Asynchronous) + +To transcribe a local file asynchronously, create a stream from the file and call `TranscribeAsync` on the `TranscriptionClient` clientlet, which returns the transcribed phrases and total duration of the file + +```C# Snippet:TranscribeLocalFileAsync +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var request = new TranscriptionContent { Audio = fileStream }; + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_2_TranscribeRemoteFile.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_2_TranscribeRemoteFile.md new file mode 100644 index 000000000000..20c366b84015 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_2_TranscribeRemoteFile.md @@ -0,0 +1,54 @@ +# Transcribe a Remote File + +This sample shows how to transcribe a remote file using the `Azure.AI.Speech.Transcription` SDK. + +## Create a Transcription Client + +To create a Transcription Client, you will need the service endpoint and credentials of your AI Foundry resource or Speech Service resource. You can specify the service version by providing a TranscriptionClientOptions instance. + +```C# Snippet:CreateTranscriptionClientForSpecificApiVersion +Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new("your apikey"); +TranscriptionClientOptions options = new TranscriptionClientOptions(TranscriptionClientOptions.ServiceVersion.V2025_10_15); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); +``` + +## Transcribe Remote File (Synchronous) + +To transcribe a remote file synchronously, create a stream from url of the file and call `Transcribe` on the `TranscriptionClient` clientlet, which returns the transcribed phrases and total duration of the file + +```C# Snippet:TranscribeRemoteFileSync +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using HttpClient httpClient = new HttpClient(); +using HttpResponseMessage httpResponse = httpClient.GetAsync("https://your-domain.com/your-file.mp3").Result; +using Stream stream = httpResponse.Content.ReadAsStreamAsync().Result; + +var request = new TranscriptionContent { Audio = stream }; +var response = client.Transcribe(request); + +Console.WriteLine($"File Duration: {response.Value.Duration}"); +foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) +{ + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); +} +``` + +## Transcribe Remote File (Asynchronous) + +To transcribe a remote file ssynchronously, create a stream from url of the file and call `TranscribeAsync` on the `TranscriptionClient` clientlet, which returns the transcribed phrases and total duration of the file + +```C# Snippet:TranscribeRemoteFileAsync +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using HttpClient httpClient = new HttpClient(); +using HttpResponseMessage httpResponse = await httpClient.GetAsync("https://your-domain.com/your-file.mp3"); +using Stream stream = await httpResponse.Content.ReadAsStreamAsync(); + +var request = new TranscriptionContent { Audio = stream }; +var response = await client.TranscribeAsync(request); + +Console.WriteLine($"File Duration: {response.Value.Duration}"); +foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) +{ + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); +} +``` diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_3_TranscribeWithOptions.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_3_TranscribeWithOptions.md new file mode 100644 index 000000000000..c018c3e069c0 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_3_TranscribeWithOptions.md @@ -0,0 +1,169 @@ +# Transcribe with Options + +This sample shows how to transcribe files using the options of the `Azure.AI.Speech.Transcription` SDK. + +## Create a Transcription Client + +To create a Transcription Client, you will need the service endpoint and credentials of your AI Foundry resource or Speech Service resource. You can specify the service version by providing a TranscriptionClientOptions instance. + +```C# Snippet:CreateTranscriptionClientForSpecificApiVersion +Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new("your apikey"); +TranscriptionClientOptions options = new TranscriptionClientOptions(TranscriptionClientOptions.ServiceVersion.V2025_10_15); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); +``` + +## Transcribe with Locale Options + +To transcribe a file using manually specified locales, create a stream from the file, add the locale in the `TranscriptionOptions` and call `TranscribeAsync` on the `TranscriptionClient` clientlet. This method returns the transcribed phrases and total duration of the file. + +If not specified, the locale of the speech in the audio is detected automatically from all supported locales. + +```C# Snippet:TranscribeWithLocales +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var options = new TranscriptionOptions(); + options.Locales.Add("en-US"); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + +## Transcribe with Model Options + +To transcribe a file using specific models for specific locales, create a stream from the file, add the model mapping in the `TranscriptionOptions` and call `TranscribeAsync` on the `TranscriptionClient` clientlet. This method returns the transcribed phrases and total duration of the file. + +If no mapping is given, the default model for the locale is used. + +```C# Snippet:TranscribeWithModels +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var options = new TranscriptionOptions(); + options.Models.Add("en-US", new Uri("https://myaccount.api.cognitive.microsoft.com/speechtotext/models/your-model-uuid")); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + +## Transcribe with Profanity Filter Options + +To transcribe a file using profanity filters, create a stream from the file, specify the filter mode in the `TranscriptionOptions` and call `TranscribeAsync` on the `TranscriptionClient` clientlet. This method returns the transcribed phrases and total duration of the file. + +```C# Snippet:TranscribeWithProfinityFilter +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var options = new TranscriptionOptions(); + options.ProfanityFilterMode = ProfanityFilterMode.Masked; + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + +## Transcribe with Active Channels Options + +To transcribe a file using only a subset of the channels, create a stream from the file, specify the 0-based indices of the active channels in the `TranscriptionOptions` and call `TranscribeAsync` on the `TranscriptionClient` clientlet. This method returns the transcribed phrases and total duration of the file. + +If not specified, multiple channels are merged and transcribed jointly. Only up to two channels are supported. + +```C# Snippet:TranscribeWithActiveChannels +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var options = new TranscriptionOptions(); + options.ActiveChannels.Add(0); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } +} +``` + +## Transcribe with Diarization Options + +To transcribe a file with speaker identification, create a stream from the file, specify the diarization options in the `TranscriptionOptions` and call `TranscribeAsync` on the `TranscriptionClient` clientlet. This method returns the transcribed phrases and total duration of the file. + +If not specified, no speaker information is included in the transcribed phrases. + +```C# Snippet:TranscribeWithDiarization +string filePath = "path/to/audio.wav"; +TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +using (FileStream fileStream = File.Open(filePath, FileMode.Open)) +{ + var options = new TranscriptionOptions() + { + DiarizationOptions = new() + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 2 + } + }; + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration} [{phrase.Speaker}]: {phrase.Text}"); + } +} +``` + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_4_AdvancedRemoteTranscription.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_4_AdvancedRemoteTranscription.md new file mode 100644 index 000000000000..b06513e1932f --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_4_AdvancedRemoteTranscription.md @@ -0,0 +1,153 @@ +# Advanced Remote File Transcription + +This sample demonstrates advanced scenarios for transcribing audio files from remote locations using the `Azure.AI.Speech.Transcription` SDK. + +## Transcribe from URL + +Transcribe an audio file directly from a public URL without downloading it first. + +```C# Snippet:TranscribeFromUrl +// Specify the URL of the audio file to transcribe +Uri audioUrl = new Uri("https://example.com/audio/sample.wav"); + +// Configure transcription to use the remote URL +TranscriptionOptions options = new TranscriptionOptions(audioUrl); + +// No audio stream needed - the service fetches the file from the URL +Response response = await client.TranscribeAsync(options); +TranscriptionResult result = response.Value; + +Console.WriteLine($"Transcribed audio from URL: {audioUrl}"); +Console.WriteLine($"Duration: {result.Duration}"); + +var channelPhrases = result.PhrasesByChannel.First(); +Console.WriteLine($"\nTranscription:\n{channelPhrases.Text}"); +``` + +## Download and Transcribe from HTTP + +Download an audio file from a remote location and transcribe it. + +```C# Snippet:TranscribeFromHttpStream +// Download the audio file from a remote location +string audioUrl = "https://example.com/audio/sample.wav"; + +using HttpClient httpClient = new HttpClient(); +using HttpResponseMessage httpResponse = await httpClient.GetAsync(audioUrl); +httpResponse.EnsureSuccessStatusCode(); + +// Get the audio stream from the HTTP response +using Stream audioStream = await httpResponse.Content.ReadAsStreamAsync(); + +// Create transcription request with the downloaded stream +TranscriptionContent request = new TranscriptionContent +{ + Audio = audioStream +}; + +Response response = await client.TranscribeAsync(request); +TranscriptionResult result = response.Value; + +Console.WriteLine($"Downloaded and transcribed audio from: {audioUrl}"); +Console.WriteLine($"Duration: {result.Duration}"); + +var channelPhrases = result.PhrasesByChannel.First(); +foreach (TranscribedPhrase phrase in channelPhrases.Phrases) +{ + Console.WriteLine($"[{phrase.Offset}] {phrase.Text}"); +} +``` + +## Transcribe from Azure Blob Storage + +Transcribe audio files stored in Azure Blob Storage using SAS URLs. + +```C# Snippet:TranscribeFromBlobStorage +// Azure Blob Storage URL with SAS token for access +Uri blobSasUrl = new Uri( + "https://mystorageaccount.blob.core.windows.net/audio-files/recording.wav?sv=2021-06-08&st=..."); + +TranscriptionOptions options = new TranscriptionOptions(blobSasUrl); + +Response response = await client.TranscribeAsync(options); +TranscriptionResult result = response.Value; + +Console.WriteLine($"Transcribed audio from Azure Blob Storage"); +Console.WriteLine($"Duration: {result.Duration}"); + +var channelPhrases = result.PhrasesByChannel.First(); +Console.WriteLine($"\nFull Transcription:\n{channelPhrases.Text}"); +``` + +## Transcribe Remote File with Options + +Combine remote file transcription with transcription options like locale and diarization. + +```C# Snippet:TranscribeRemoteFileWithOptions +Uri audioUrl = new Uri("https://example.com/audio/spanish-interview.mp3"); + +// Configure transcription options for remote audio +TranscriptionOptions options = new TranscriptionOptions(audioUrl) +{ + ProfanityFilterMode = ProfanityFilterMode.Masked, + DiarizationOptions = new TranscriptionDiarizationOptions + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 2 + } +}; + +// Add Spanish locale +options.Locales.Add("es-ES"); + +Response response = await client.TranscribeAsync(options); +TranscriptionResult result = response.Value; + +Console.WriteLine("Remote transcription with options:"); +Console.WriteLine($"Duration: {result.Duration}"); + +var channelPhrases = result.PhrasesByChannel.First(); +foreach (TranscribedPhrase phrase in channelPhrases.Phrases) +{ + Console.WriteLine($"Speaker {phrase.Speaker}: {phrase.Text}"); +} +``` + +## Process Multiple Remote Files + +Process multiple audio files from different sources in parallel. + +```C# Snippet:TranscribeMultipleRemoteFiles +// List of audio files to transcribe +Uri[] audioUrls = new[] +{ + new Uri("https://example.com/audio/file1.wav"), + new Uri("https://example.com/audio/file2.wav"), + new Uri("https://example.com/audio/file3.wav") +}; + +// Create tasks for parallel transcription +Task>[] transcriptionTasks = audioUrls + .Select(url => + { + TranscriptionOptions options = new TranscriptionOptions(url); + + return client.TranscribeAsync(options); + }) + .ToArray(); + +// Wait for all transcriptions to complete +Response[] responses = await Task.WhenAll(transcriptionTasks); + +// Process results +for (int i = 0; i < responses.Length; i++) +{ + TranscriptionResult result = responses[i].Value; + Console.WriteLine($"\nFile {i + 1} ({audioUrls[i]}):"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"Text: {channelPhrases.Text}"); +} +``` + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_5_MockClient.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_5_MockClient.md new file mode 100644 index 000000000000..30ed92bc7155 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples/Sample_5_MockClient.md @@ -0,0 +1,208 @@ +# Mock Client for Testing + +This sample demonstrates how to mock the `TranscriptionClient` for unit testing without making actual API calls to Azure. + +## Why Mock the Client? + +Mocking the `TranscriptionClient` is useful for: + +- Unit testing application logic without network calls +- Testing error handling scenarios +- Running tests in CI/CD pipelines without Azure credentials +- Faster test execution + +## Create a Mock Client + +The `TranscriptionClient` provides a protected constructor for creating mock instances. + +```C# Snippet:CreateMockTranscriptionClient +// TranscriptionClient provides a protected constructor for mocking +// You can create a derived class for testing purposes + +// Example: Create a test-specific derived class +var mockClient = new MockTranscriptionClient(); + +// Use the mock client in your tests +// It won't make actual API calls +``` + +## Mock Transcription Behavior + +Configure the mock client to return predefined results for testing. + +```C# Snippet:MockTranscriptionBehavior +// Create a mock client that returns predefined results +var mockClient = new MockTranscriptionClient(); + +// Configure the mock to return a specific result +var expectedText = "This is a mock transcription result"; +mockClient.SetMockResult(expectedText); + +// Create a test request +using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); +TranscriptionContent request = new TranscriptionContent +{ + Audio = audioStream +}; + +// Call the mock client +Response response = await mockClient.TranscribeAsync(request); + +// Verify the result +Assert.IsNotNull(response); +Assert.IsNotNull(response.Value); + +// The mock client returns the configured result +var phrases = response.Value.PhrasesByChannel.FirstOrDefault(); +if (phrases != null) +{ + Console.WriteLine($"Mock transcription: {phrases.Text}"); +} +``` + +## Use InMemoryTransport for Testing + +Use Azure SDK's `MockTransport` to test without network calls. + +```C# Snippet:UseInMemoryTransport +// Create a mock response that the client will return +var mockResponseContent = @"{ + ""durationMilliseconds"": 5000, + ""combinedPhrases"": [ + { + ""channel"": 0, + ""text"": ""This is a test transcription"" + } + ], + ""phrases"": [ + { + ""channel"": 0, + ""offsetMilliseconds"": 0, + ""durationMilliseconds"": 5000, + ""text"": ""This is a test transcription"", + ""words"": [], + ""locale"": ""en-US"", + ""confidence"": 0.95 + } + ] +}"; + +// Create options with a mock transport +var mockTransport = new MockTransport(new MockResponse(200) +{ + ContentStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(mockResponseContent)) +}); + +TranscriptionClientOptions options = new TranscriptionClientOptions(); +options.Transport = mockTransport; + +// Create client with mock transport +Uri endpoint = new Uri("https://mock.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new AzureKeyCredential("mock-key"); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + +// Make a request - it will use the mock response +using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); +TranscriptionContent request = new TranscriptionContent +{ + Audio = audioStream +}; + +Response response = await client.TranscribeAsync(request); + +// Verify the mock response was returned +Console.WriteLine($"Duration: {response.Value.Duration}"); +var phrases = response.Value.PhrasesByChannel.First(); +Console.WriteLine($"Transcription: {phrases.Text}"); +``` + +## Mock Error Scenarios + +Test error handling by mocking error responses. + +```C# Snippet:MockErrorScenarios +// Create a mock transport that returns an error +var mockTransport = new MockTransport(new MockResponse(401) +{ + ContentStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes( + @"{""error"": {""code"": ""Unauthorized"", ""message"": ""Invalid API key""}}" + )) +}); + +TranscriptionClientOptions options = new TranscriptionClientOptions(); +options.Transport = mockTransport; + +Uri endpoint = new Uri("https://mock.api.cognitive.microsoft.com/"); +AzureKeyCredential credential = new AzureKeyCredential("invalid-key"); +TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + +// Test error handling +try +{ + using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + await client.TranscribeAsync(request); + Assert.Fail("Expected RequestFailedException was not thrown"); +} +catch (RequestFailedException ex) +{ + // Verify error handling works correctly + Assert.AreEqual(401, ex.Status); + Console.WriteLine($"Successfully caught error: {ex.Message}"); +} +``` + +## Example: Testing Application Logic + +Here's a complete example of testing application code that uses `TranscriptionClient`: + +```csharp +// Your application code +public class TranscriptionService +{ + private readonly TranscriptionClient _client; + + public TranscriptionService(TranscriptionClient client) + { + _client = client; + } + + public async Task GetTranscriptionAsync(Stream audioStream) + { + var request = new TranscriptionContent { Audio = audioStream }; + var response = await _client.TranscribeAsync(request); + return response.Value.PhrasesByChannel.First().Text; + } +} + +// Your test code +[Test] +public async Task TestTranscriptionService() +{ + // Arrange: Create mock client + var mockClient = new MockTranscriptionClient(); + mockClient.SetMockResult("Test transcription"); + + var service = new TranscriptionService(mockClient); + + // Act: Call your service + using var audioStream = new MemoryStream(new byte[] { 0x00 }); + string result = await service.GetTranscriptionAsync(audioStream); + + // Assert: Verify the result + Assert.AreEqual("Test transcription", result); +} +``` + +## Best Practices + +- Use mocks for unit tests; use real client for integration tests +- Test both success and error scenarios +- Verify your application handles all possible response states +- Use dependency injection to make your code testable +- Keep mock data realistic to catch serialization issues + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Azure.AI.Speech.Transcription.csproj b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Azure.AI.Speech.Transcription.csproj new file mode 100644 index 000000000000..f0ad75e9138e --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Azure.AI.Speech.Transcription.csproj @@ -0,0 +1,20 @@ + + + This is the Azure.AI.Speech.Transcription client library for developing .NET applications with rich experience. + Azure SDK Code Generation Azure.AI.Speech.Transcription for Azure Data Plane + 1.0.0-beta.1 + Azure.AI.Speech.Transcription + $(RequiredTargetFrameworks) + true + + + + + + + + + + + + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/AISpeechTranscriptionModelFactory.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/AISpeechTranscriptionModelFactory.cs new file mode 100644 index 000000000000..f9a4af7acf63 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/AISpeechTranscriptionModelFactory.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// Model factory for models. + public static partial class AISpeechTranscriptionModelFactory + { + /// Initializes a new instance of . + /// The duration of the audio in milliseconds. + /// A new instance for mocking. + public static TranscriptionResult TranscriptionResult(int durationMilliseconds = default) + { + return new TranscriptionResult(durationMilliseconds, new List(), new List(), serializedAdditionalRawData: null); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/EnhancedModeProperties.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/EnhancedModeProperties.Serialization.cs new file mode 100644 index 000000000000..ac944cf40f11 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/EnhancedModeProperties.Serialization.cs @@ -0,0 +1,81 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class EnhancedModeProperties + { + /// + /// Custom serialization to auto-enable enhanced mode when properties are set. + /// The 'enabled' property is automatically set to true when task, targetLanguage, or prompt are specified. + /// + private void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(EnhancedModeProperties)} does not support writing '{format}' format."); + } + + // Auto-enable if any property is configured + bool shouldEnable = Optional.IsDefined(Task) || + Optional.IsDefined(TargetLanguage) || + Optional.IsCollectionDefined(Prompt); + + if (shouldEnable) + { + writer.WritePropertyName("enabled"u8); + writer.WriteBooleanValue(true); + } + else if (Optional.IsDefined(Enabled)) + { + // Only write explicit enabled value if nothing else is set (for deserialization round-trip) + writer.WritePropertyName("enabled"u8); + writer.WriteBooleanValue(Enabled.Value); + } + + if (Optional.IsDefined(Task)) + { + writer.WritePropertyName("task"u8); + writer.WriteStringValue(Task); + } + if (Optional.IsDefined(TargetLanguage)) + { + writer.WritePropertyName("targetLanguage"u8); + writer.WriteStringValue(TargetLanguage); + } + if (Optional.IsCollectionDefined(Prompt)) + { + writer.WritePropertyName("prompt"u8); + writer.WriteStartArray(); + foreach (var item in Prompt) + { + writer.WriteStringValue(item); + } + writer.WriteEndArray(); + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrase.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrase.cs new file mode 100644 index 000000000000..272deaf26008 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrase.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; + +namespace Azure.AI.Speech.Transcription; + +public partial class TranscribedPhrase +{ + /// The start offset of the phrase. + public TimeSpan Offset + { + get + { + return TimeSpan.FromMilliseconds(OffsetMilliseconds); + } + } + + /// The duration of the phrase. + public TimeSpan Duration + { + get + { + return TimeSpan.FromMilliseconds(DurationMilliseconds); + } + } + + /// The 0-based channel index. Only present if channel separation is enabled. + internal int? Channel { get; } + /// The start offset of the phrase in milliseconds. + internal int OffsetMilliseconds { get; } + /// The duration of the phrase in milliseconds. + internal int DurationMilliseconds { get; } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrases.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrases.cs new file mode 100644 index 000000000000..e201d10d517c --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedPhrases.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// TranscribedPhrases. + public partial class TranscribedPhrases + { + /// + /// Initializes a new instance of the class. + /// + /// The 0-based channel index. Only present if channel separation is enabled. + /// The complete transcribed text for the channel. + /// The transcription results segmented into phrases. + public TranscribedPhrases(int? Channel, string Text, IEnumerable Phrases) + { + this.Channel = Channel; + this.Text = Text; + this.Phrases = Phrases; + } + + /// The 0-based channel index. Only present if channel separation is enabled. + public int? Channel; + + /// The complete transcribed text for the channel. + public string Text { get; } + + /// The transcription results segmented into phrases. + + public IEnumerable Phrases; + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedWord.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedWord.cs new file mode 100644 index 000000000000..e76946264d18 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscribedWord.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; + +namespace Azure.AI.Speech.Transcription; + +public partial class TranscribedWord +{ + /// The start offset of the word. + public TimeSpan Offset + { + get + { + return TimeSpan.FromMilliseconds(OffsetMilliseconds); + } + } + + /// The duration of the word. + public TimeSpan Duration + { + get + { + return TimeSpan.FromMilliseconds(DurationMilliseconds); + } + } + + /// The start offset of the word in milliseconds. + internal int OffsetMilliseconds { get; } + /// The duration of the word in milliseconds. + internal int DurationMilliseconds { get; } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionClient.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionClient.cs new file mode 100644 index 000000000000..0a91498dde16 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionClient.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable disable + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.AI.Speech.Transcription +{ + // Data plane generated client. + /// The Transcription service client. + public partial class TranscriptionClient + { + /// Transcribes the provided audio stream. + /// The transcription options containing audio and configuration. + /// The cancellation token to use. + /// is null. + public virtual async Task> TranscribeAsync(TranscriptionOptions options, CancellationToken cancellationToken = default) + { + TranscriptionContent body = new TranscriptionContent(options, options.AudioStream, null); + return await TranscribeAsync(body, cancellationToken).ConfigureAwait(false); + } + + /// Transcribes the provided audio stream. + /// The transcription options containing audio and configuration. + /// The cancellation token to use. + /// is null. + public virtual Response Transcribe(TranscriptionOptions options, CancellationToken cancellationToken = default) + { + TranscriptionContent body = new TranscriptionContent(options, options.AudioStream, null); + return Transcribe(body, cancellationToken); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionDiarizationOptions.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionDiarizationOptions.Serialization.cs new file mode 100644 index 000000000000..38fe8b5f0320 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionDiarizationOptions.Serialization.cs @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscriptionDiarizationOptions + { + /// + /// Custom serialization to auto-enable diarization when MaxSpeakers is set. + /// The 'enabled' property is automatically set to true when maxSpeakers is specified. + /// + private void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionDiarizationOptions)} does not support writing '{format}' format."); + } + + // Auto-enable if MaxSpeakers is configured + bool shouldEnable = Optional.IsDefined(MaxSpeakers); + + if (shouldEnable) + { + writer.WritePropertyName("enabled"u8); + writer.WriteBooleanValue(true); + } + else if (Optional.IsDefined(Enabled)) + { + // Only write explicit enabled value if MaxSpeakers is not set (for deserialization round-trip) + writer.WritePropertyName("enabled"u8); + writer.WriteBooleanValue(Enabled.Value); + } + + if (Optional.IsDefined(MaxSpeakers)) + { + writer.WritePropertyName("maxSpeakers"u8); + writer.WriteNumberValue(MaxSpeakers.Value); + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionOptions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionOptions.cs new file mode 100644 index 000000000000..1b0867a710a4 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionOptions.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#nullable disable + +using System; +using System.Collections.Generic; +using System.IO; + +namespace Azure.AI.Speech.Transcription +{ + #pragma warning disable SCM0005 + /// Metadata for a transcription request. + public partial class TranscriptionOptions + { + /// Initializes a new instance of with an audio URI. + /// The URL of the audio to be transcribed. + public TranscriptionOptions(Uri audioUri):this() + { + AudioUri = audioUri; + } + + /// Initializes a new instance of with an audio stream. + /// The audio stream to be transcribed. + public TranscriptionOptions(Stream audioStream):this() + { + AudioStream = audioStream; + } + + /// Initializes a new instance of . + public TranscriptionOptions() + { + Locales = new ChangeTrackingList(); + Models = new ChangeTrackingDictionary(); + ActiveChannels = new ChangeTrackingList(); + } + + /// The URL of the audio to be transcribed. The audio must be shorter than 2 hours in audio duration and smaller than 250 MB in size. If both Audio and AudioUrl are provided, Audio is used. + public Uri AudioUri { get; } + internal Stream AudioStream { get; } + } + #pragma warning restore SCM0005 +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionResult.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionResult.cs new file mode 100644 index 000000000000..0dbd3229db86 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Custom/TranscriptionResult.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Azure.AI.Speech.Transcription; + +public partial class TranscriptionResult +{ + /// The duration of the audio. + public TimeSpan Duration + { + get + { + return TimeSpan.FromMilliseconds(DurationMilliseconds); + } + } + + private IEnumerable _TranscribedPhrases; + + /// The transcripted phrases by their channel. + public IEnumerable PhrasesByChannel + { + get + { + if (_TranscribedPhrases != null) + { + return _TranscribedPhrases; + } + var TranscribedPhrases = new List(); + + var CombinedPhrases = this.CombinedPhrases.ToDictionary((phrase) => phrase.Channel ?? -1); + var Phrases = this.Phrases.GroupBy((phrase) => phrase.Channel).ToDictionary((e) => e.Key ?? -1, (e) => e.ToList()); + foreach (var key in CombinedPhrases.Keys) + { + var CombinedPhrase = CombinedPhrases[key]; + var Phrase = Phrases[key]; + TranscribedPhrases.Add(new TranscribedPhrases(key == -1 ? null : key, CombinedPhrase.Text, Phrase)); + } + + _TranscribedPhrases = TranscribedPhrases; + return _TranscribedPhrases; + } + } + + /// The duration of the audio in milliseconds. + internal int DurationMilliseconds { get; } + + /// The full transcript for each channel. + internal IReadOnlyList CombinedPhrases { get; } + /// The transcription results segmented into phrases. + internal IReadOnlyList Phrases { get; } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionClientBuilderExtensions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionClientBuilderExtensions.cs new file mode 100644 index 000000000000..440116bb9441 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionClientBuilderExtensions.cs @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Diagnostics.CodeAnalysis; +using Azure; +using Azure.AI.Speech.Transcription; +using Azure.Core.Extensions; + +namespace Microsoft.Extensions.Azure +{ + /// Extension methods to add to client builder. + public static partial class AISpeechTranscriptionClientBuilderExtensions + { + /// Registers a instance. + /// The builder to register with. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + /// A credential used to authenticate to an Azure Service. + public static IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, Uri endpoint, AzureKeyCredential credential) + where TBuilder : IAzureClientFactoryBuilder + { + return builder.RegisterClientFactory((options) => new TranscriptionClient(endpoint, credential, options)); + } + + /// Registers a instance. + /// The builder to register with. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + public static IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, Uri endpoint) + where TBuilder : IAzureClientFactoryBuilderWithCredential + { + return builder.RegisterClientFactory((options, cred) => new TranscriptionClient(endpoint, cred, options)); + } + + /// Registers a instance. + /// The builder to register with. + /// The configuration values. + [RequiresUnreferencedCode("Requires unreferenced code until we opt into EnableConfigurationBindingGenerator.")] + [RequiresDynamicCode("Requires unreferenced code until we opt into EnableConfigurationBindingGenerator.")] + public static IAzureClientBuilder AddTranscriptionClient(this TBuilder builder, TConfiguration configuration) + where TBuilder : IAzureClientFactoryBuilderWithConfiguration + { + return builder.RegisterClientFactory(configuration); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionModelFactory.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionModelFactory.cs new file mode 100644 index 000000000000..a6e1c8319275 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/AISpeechTranscriptionModelFactory.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Azure.AI.Speech.Transcription +{ + /// Model factory for models. + public static partial class AISpeechTranscriptionModelFactory + { + /// Initializes a new instance of . + /// The URL of the audio to be transcribed. The audio must be shorter than 2 hours in audio duration and smaller than 250 MB in size. If both Audio and AudioUrl are provided, Audio is used. + /// A list of possible locales for the transcription. If not specified, the locale of the speech in the audio is detected automatically from all supported locales. + /// Maps some or all candidate locales to a model URI to be used for transcription. If no mapping is given, the default model for the locale is used. + /// Mode of profanity filtering. + /// Mode of diarization. + /// The 0-based indices of the channels to be transcribed separately. If not specified, multiple channels are merged and transcribed jointly. Only up to two channels are supported. + /// Enhanced mode properties. + /// Phrase list properties. + /// A new instance for mocking. + public static TranscriptionOptions TranscriptionOptions(Uri audioUri = null, IEnumerable locales = null, IDictionary models = null, ProfanityFilterMode? profanityFilterMode = null, TranscriptionDiarizationOptions diarizationOptions = null, IEnumerable activeChannels = null, EnhancedModeProperties enhancedMode = null, PhraseListProperties phraseList = null) + { + locales ??= new List(); + models ??= new Dictionary(); + activeChannels ??= new List(); + + return new TranscriptionOptions( + audioUri, + locales?.ToList(), + models, + profanityFilterMode, + diarizationOptions, + activeChannels?.ToList(), + enhancedMode, + phraseList, + serializedAdditionalRawData: null); + } + + /// Initializes a new instance of . + /// Enable speaker diarization. This is automatically set to true when maxSpeakers is specified. + /// Gets or sets a hint for the maximum number of speakers for diarization. Must be greater than 1 and less than 36. + /// A new instance for mocking. + public static TranscriptionDiarizationOptions TranscriptionDiarizationOptions(bool? enabled = null, int? maxSpeakers = null) + { + return new TranscriptionDiarizationOptions(enabled, maxSpeakers, serializedAdditionalRawData: null); + } + + /// Initializes a new instance of . + /// Enable enhanced mode for transcription. This is automatically set to true when task, targetLanguage, or prompt are specified. + /// Task type for enhanced mode. + /// Target language for enhanced mode. + /// A list of user prompts. + /// A new instance for mocking. + public static EnhancedModeProperties EnhancedModeProperties(bool? enabled = null, string task = null, string targetLanguage = null, IEnumerable prompt = null) + { + prompt ??= new List(); + + return new EnhancedModeProperties(enabled, task, targetLanguage, prompt?.ToList(), serializedAdditionalRawData: null); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.Serialization.cs new file mode 100644 index 000000000000..c04ef1bf1b9d --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.Serialization.cs @@ -0,0 +1,157 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + internal partial class ChannelCombinedPhrases : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChannelCombinedPhrases)} does not support writing '{format}' format."); + } + + if (Optional.IsDefined(Channel)) + { + writer.WritePropertyName("channel"u8); + writer.WriteNumberValue(Channel.Value); + } + writer.WritePropertyName("text"u8); + writer.WriteStringValue(Text); + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + ChannelCombinedPhrases IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChannelCombinedPhrases)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeChannelCombinedPhrases(document.RootElement, options); + } + + internal static ChannelCombinedPhrases DeserializeChannelCombinedPhrases(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + int? channel = default; + string text = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("channel"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + channel = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("text"u8)) + { + text = property.Value.GetString(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new ChannelCombinedPhrases(channel, text, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(ChannelCombinedPhrases)} does not support writing '{options.Format}' format."); + } + } + + ChannelCombinedPhrases IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeChannelCombinedPhrases(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(ChannelCombinedPhrases)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static ChannelCombinedPhrases FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeChannelCombinedPhrases(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.cs new file mode 100644 index 000000000000..f17b153c52d6 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ChannelCombinedPhrases.cs @@ -0,0 +1,79 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// The full transcript per channel. + internal partial class ChannelCombinedPhrases + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// The complete transcribed text for the channel. + /// is null. + internal ChannelCombinedPhrases(string text) + { + Argument.AssertNotNull(text, nameof(text)); + + Text = text; + } + + /// Initializes a new instance of . + /// The 0-based channel index. Only present if channel separation is enabled. + /// The complete transcribed text for the channel. + /// Keeps track of any properties unknown to the library. + internal ChannelCombinedPhrases(int? channel, string text, IDictionary serializedAdditionalRawData) + { + Channel = channel; + Text = text; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Initializes a new instance of for deserialization. + internal ChannelCombinedPhrases() + { + } + + /// The 0-based channel index. Only present if channel separation is enabled. + public int? Channel { get; } + /// The complete transcribed text for the channel. + public string Text { get; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.Serialization.cs new file mode 100644 index 000000000000..127039c273e0 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.Serialization.cs @@ -0,0 +1,144 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class EnhancedModeProperties : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + EnhancedModeProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(EnhancedModeProperties)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeEnhancedModeProperties(document.RootElement, options); + } + + internal static EnhancedModeProperties DeserializeEnhancedModeProperties(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + bool? enabled = default; + string task = default; + string targetLanguage = default; + IList prompt = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("enabled"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + enabled = property.Value.GetBoolean(); + continue; + } + if (property.NameEquals("task"u8)) + { + task = property.Value.GetString(); + continue; + } + if (property.NameEquals("targetLanguage"u8)) + { + targetLanguage = property.Value.GetString(); + continue; + } + if (property.NameEquals("prompt"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(item.GetString()); + } + prompt = array; + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new EnhancedModeProperties(enabled, task, targetLanguage, prompt ?? new ChangeTrackingList(), serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(EnhancedModeProperties)} does not support writing '{options.Format}' format."); + } + } + + EnhancedModeProperties IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeEnhancedModeProperties(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(EnhancedModeProperties)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static EnhancedModeProperties FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeEnhancedModeProperties(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.cs new file mode 100644 index 000000000000..765b7ecc7074 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/EnhancedModeProperties.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// Enhanced mode properties for transcription. + public partial class EnhancedModeProperties + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + public EnhancedModeProperties() + { + Prompt = new ChangeTrackingList(); + } + + /// Initializes a new instance of . + /// Enable enhanced mode for transcription. This is automatically set to true when task, targetLanguage, or prompt are specified. + /// Task type for enhanced mode. + /// Target language for enhanced mode. + /// A list of user prompts. + /// Keeps track of any properties unknown to the library. + internal EnhancedModeProperties(bool? enabled, string task, string targetLanguage, IList prompt, IDictionary serializedAdditionalRawData) + { + Enabled = enabled; + Task = task; + TargetLanguage = targetLanguage; + Prompt = prompt; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Enable enhanced mode for transcription. This is automatically set to true when task, targetLanguage, or prompt are specified. + public bool? Enabled { get; } + /// Task type for enhanced mode. + public string Task { get; set; } + /// Target language for enhanced mode. + public string TargetLanguage { get; set; } + /// A list of user prompts. + public IList Prompt { get; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Argument.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Argument.cs new file mode 100644 index 000000000000..f7300d83d72b --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Argument.cs @@ -0,0 +1,129 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + internal static class Argument + { + public static void AssertNotNull(T value, string name) + { + if (value is null) + { + throw new ArgumentNullException(name); + } + } + + public static void AssertNotNull(T? value, string name) + where T : struct + { + if (!value.HasValue) + { + throw new ArgumentNullException(name); + } + } + + public static void AssertNotNullOrEmpty(IEnumerable value, string name) + { + if (value is null) + { + throw new ArgumentNullException(name); + } + if (value is ICollection collectionOfT && collectionOfT.Count == 0) + { + throw new ArgumentException("Value cannot be an empty collection.", name); + } + if (value is ICollection collection && collection.Count == 0) + { + throw new ArgumentException("Value cannot be an empty collection.", name); + } + using IEnumerator e = value.GetEnumerator(); + if (!e.MoveNext()) + { + throw new ArgumentException("Value cannot be an empty collection.", name); + } + } + + public static void AssertNotNullOrEmpty(string value, string name) + { + if (value is null) + { + throw new ArgumentNullException(name); + } + if (value.Length == 0) + { + throw new ArgumentException("Value cannot be an empty string.", name); + } + } + + public static void AssertNotNullOrWhiteSpace(string value, string name) + { + if (value is null) + { + throw new ArgumentNullException(name); + } + if (string.IsNullOrWhiteSpace(value)) + { + throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name); + } + } + + public static void AssertNotDefault(ref T value, string name) + where T : struct, IEquatable + { + if (value.Equals(default)) + { + throw new ArgumentException("Value cannot be empty.", name); + } + } + + public static void AssertInRange(T value, T minimum, T maximum, string name) + where T : notnull, IComparable + { + if (minimum.CompareTo(value) > 0) + { + throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed."); + } + if (maximum.CompareTo(value) < 0) + { + throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed."); + } + } + + public static void AssertEnumDefined(Type enumType, object value, string name) + { + if (!Enum.IsDefined(enumType, value)) + { + throw new ArgumentException($"Value not defined for {enumType.FullName}.", name); + } + } + + public static T CheckNotNull(T value, string name) + where T : class + { + AssertNotNull(value, name); + return value; + } + + public static string CheckNotNullOrEmpty(string value, string name) + { + AssertNotNullOrEmpty(value, name); + return value; + } + + public static void AssertNull(T value, string name, string message = null) + { + if (value != null) + { + throw new ArgumentException(message ?? "Value must be null.", name); + } + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingDictionary.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingDictionary.cs new file mode 100644 index 000000000000..e01597dc4bf1 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingDictionary.cs @@ -0,0 +1,167 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + internal class ChangeTrackingDictionary : IDictionary, IReadOnlyDictionary where TKey : notnull + { + private IDictionary _innerDictionary; + + public ChangeTrackingDictionary() + { + } + + public ChangeTrackingDictionary(IDictionary dictionary) + { + if (dictionary == null) + { + return; + } + _innerDictionary = new Dictionary(dictionary); + } + + public ChangeTrackingDictionary(IReadOnlyDictionary dictionary) + { + if (dictionary == null) + { + return; + } + _innerDictionary = new Dictionary(); + foreach (var pair in dictionary) + { + _innerDictionary.Add(pair); + } + } + + public bool IsUndefined => _innerDictionary == null; + + public int Count => IsUndefined ? 0 : EnsureDictionary().Count; + + public bool IsReadOnly => IsUndefined ? false : EnsureDictionary().IsReadOnly; + + public ICollection Keys => IsUndefined ? Array.Empty() : EnsureDictionary().Keys; + + public ICollection Values => IsUndefined ? Array.Empty() : EnsureDictionary().Values; + + public TValue this[TKey key] + { + get + { + if (IsUndefined) + { + throw new KeyNotFoundException(nameof(key)); + } + return EnsureDictionary()[key]; + } + set + { + EnsureDictionary()[key] = value; + } + } + + IEnumerable IReadOnlyDictionary.Keys => Keys; + + IEnumerable IReadOnlyDictionary.Values => Values; + + public IEnumerator> GetEnumerator() + { + if (IsUndefined) + { + IEnumerator> enumerateEmpty() + { + yield break; + } + return enumerateEmpty(); + } + return EnsureDictionary().GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Add(KeyValuePair item) + { + EnsureDictionary().Add(item); + } + + public void Clear() + { + EnsureDictionary().Clear(); + } + + public bool Contains(KeyValuePair item) + { + if (IsUndefined) + { + return false; + } + return EnsureDictionary().Contains(item); + } + + public void CopyTo(KeyValuePair[] array, int index) + { + if (IsUndefined) + { + return; + } + EnsureDictionary().CopyTo(array, index); + } + + public bool Remove(KeyValuePair item) + { + if (IsUndefined) + { + return false; + } + return EnsureDictionary().Remove(item); + } + + public void Add(TKey key, TValue value) + { + EnsureDictionary().Add(key, value); + } + + public bool ContainsKey(TKey key) + { + if (IsUndefined) + { + return false; + } + return EnsureDictionary().ContainsKey(key); + } + + public bool Remove(TKey key) + { + if (IsUndefined) + { + return false; + } + return EnsureDictionary().Remove(key); + } + + public bool TryGetValue(TKey key, out TValue value) + { + if (IsUndefined) + { + value = default; + return false; + } + return EnsureDictionary().TryGetValue(key, out value); + } + + public IDictionary EnsureDictionary() + { + return _innerDictionary ??= new Dictionary(); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingList.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingList.cs new file mode 100644 index 000000000000..77d58589fa88 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ChangeTrackingList.cs @@ -0,0 +1,153 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Azure.AI.Speech.Transcription +{ + internal class ChangeTrackingList : IList, IReadOnlyList + { + private IList _innerList; + + public ChangeTrackingList() + { + } + + public ChangeTrackingList(IList innerList) + { + if (innerList != null) + { + _innerList = innerList; + } + } + + public ChangeTrackingList(IReadOnlyList innerList) + { + if (innerList != null) + { + _innerList = innerList.ToList(); + } + } + + public bool IsUndefined => _innerList == null; + + public int Count => IsUndefined ? 0 : EnsureList().Count; + + public bool IsReadOnly => IsUndefined ? false : EnsureList().IsReadOnly; + + public T this[int index] + { + get + { + if (IsUndefined) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + return EnsureList()[index]; + } + set + { + if (IsUndefined) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + EnsureList()[index] = value; + } + } + + public void Reset() + { + _innerList = null; + } + + public IEnumerator GetEnumerator() + { + if (IsUndefined) + { + IEnumerator enumerateEmpty() + { + yield break; + } + return enumerateEmpty(); + } + return EnsureList().GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Add(T item) + { + EnsureList().Add(item); + } + + public void Clear() + { + EnsureList().Clear(); + } + + public bool Contains(T item) + { + if (IsUndefined) + { + return false; + } + return EnsureList().Contains(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + if (IsUndefined) + { + return; + } + EnsureList().CopyTo(array, arrayIndex); + } + + public bool Remove(T item) + { + if (IsUndefined) + { + return false; + } + return EnsureList().Remove(item); + } + + public int IndexOf(T item) + { + if (IsUndefined) + { + return -1; + } + return EnsureList().IndexOf(item); + } + + public void Insert(int index, T item) + { + EnsureList().Insert(index, item); + } + + public void RemoveAt(int index) + { + if (IsUndefined) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + EnsureList().RemoveAt(index); + } + + public IList EnsureList() + { + return _innerList ??= new List(); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ModelSerializationExtensions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ModelSerializationExtensions.cs new file mode 100644 index 000000000000..a6ee28b11c9b --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/ModelSerializationExtensions.cs @@ -0,0 +1,409 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Text.Json; +using System.Xml; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + internal static class ModelSerializationExtensions + { + internal static readonly JsonDocumentOptions JsonDocumentOptions = new JsonDocumentOptions { MaxDepth = 256 }; + internal static readonly ModelReaderWriterOptions WireOptions = new ModelReaderWriterOptions("W"); + internal static readonly ModelReaderWriterOptions WireV3Options = new ModelReaderWriterOptions("W|v3"); + internal static readonly ModelReaderWriterOptions JsonV3Options = new ModelReaderWriterOptions("J|v3"); + internal static readonly BinaryData SentinelValue = BinaryData.FromBytes("\"__EMPTY__\""u8.ToArray()); + + public static object GetObject(this JsonElement element) + { + switch (element.ValueKind) + { + case JsonValueKind.String: + return element.GetString(); + case JsonValueKind.Number: + if (element.TryGetInt32(out int intValue)) + { + return intValue; + } + if (element.TryGetInt64(out long longValue)) + { + return longValue; + } + return element.GetDouble(); + case JsonValueKind.True: + return true; + case JsonValueKind.False: + return false; + case JsonValueKind.Undefined: + case JsonValueKind.Null: + return null; + case JsonValueKind.Object: + var dictionary = new Dictionary(); + foreach (var jsonProperty in element.EnumerateObject()) + { + dictionary.Add(jsonProperty.Name, jsonProperty.Value.GetObject()); + } + return dictionary; + case JsonValueKind.Array: + var list = new List(); + foreach (var item in element.EnumerateArray()) + { + list.Add(item.GetObject()); + } + return list.ToArray(); + default: + throw new NotSupportedException($"Not supported value kind {element.ValueKind}"); + } + } + + public static byte[] GetBytesFromBase64(this JsonElement element, string format) + { + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + + return format switch + { + "U" => TypeFormatters.FromBase64UrlString(element.GetRequiredString()), + "D" => element.GetBytesFromBase64(), + _ => throw new ArgumentException($"Format is not supported: '{format}'", nameof(format)) + }; + } + + public static DateTimeOffset GetDateTimeOffset(this JsonElement element, string format) => format switch + { + "U" when element.ValueKind == JsonValueKind.Number => DateTimeOffset.FromUnixTimeSeconds(element.GetInt64()), + _ => TypeFormatters.ParseDateTimeOffset(element.GetString(), format) + }; + + public static TimeSpan GetTimeSpan(this JsonElement element, string format) => TypeFormatters.ParseTimeSpan(element.GetString(), format); + + public static char GetChar(this JsonElement element) + { + if (element.ValueKind == JsonValueKind.String) + { + var text = element.GetString(); + if (text == null || text.Length != 1) + { + throw new NotSupportedException($"Cannot convert \"{text}\" to a char"); + } + return text[0]; + } + else + { + throw new NotSupportedException($"Cannot convert {element.ValueKind} to a char"); + } + } + + [Conditional("DEBUG")] + public static void ThrowNonNullablePropertyIsNull(this JsonProperty property) + { + throw new JsonException($"A property '{property.Name}' defined as non-nullable but received as null from the service. This exception only happens in DEBUG builds of the library and would be ignored in the release build"); + } + + public static string GetRequiredString(this JsonElement element) + { + var value = element.GetString(); + if (value == null) + { + throw new InvalidOperationException($"The requested operation requires an element of type 'String', but the target element has type '{element.ValueKind}'."); + } + return value; + } + + public static void WriteStringValue(this Utf8JsonWriter writer, DateTimeOffset value, string format) + { + writer.WriteStringValue(TypeFormatters.ToString(value, format)); + } + + public static void WriteStringValue(this Utf8JsonWriter writer, DateTime value, string format) + { + writer.WriteStringValue(TypeFormatters.ToString(value, format)); + } + + public static void WriteStringValue(this Utf8JsonWriter writer, TimeSpan value, string format) + { + writer.WriteStringValue(TypeFormatters.ToString(value, format)); + } + + public static void WriteStringValue(this Utf8JsonWriter writer, char value) + { + writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); + } + + public static void WriteBase64StringValue(this Utf8JsonWriter writer, byte[] value, string format) + { + if (value == null) + { + writer.WriteNullValue(); + return; + } + switch (format) + { + case "U": + writer.WriteStringValue(TypeFormatters.ToBase64UrlString(value)); + break; + case "D": + writer.WriteBase64StringValue(value); + break; + default: + throw new ArgumentException($"Format is not supported: '{format}'", nameof(format)); + } + } + + public static void WriteNumberValue(this Utf8JsonWriter writer, DateTimeOffset value, string format) + { + if (format != "U") + { + throw new ArgumentOutOfRangeException(nameof(format), "Only 'U' format is supported when writing a DateTimeOffset as a Number."); + } + writer.WriteNumberValue(value.ToUnixTimeSeconds()); + } + + public static void WriteObjectValue(this Utf8JsonWriter writer, T value, ModelReaderWriterOptions options = null) + { + switch (value) + { + case null: + writer.WriteNullValue(); + break; + case IJsonModel jsonModel: + jsonModel.Write(writer, options ?? WireOptions); + break; + case IUtf8JsonSerializable serializable: + serializable.Write(writer); + break; + case byte[] bytes: + writer.WriteBase64StringValue(bytes); + break; + case BinaryData bytes0: + writer.WriteBase64StringValue(bytes0); + break; + case JsonElement json: + json.WriteTo(writer); + break; + case int i: + writer.WriteNumberValue(i); + break; + case decimal d: + writer.WriteNumberValue(d); + break; + case double d0: + if (double.IsNaN(d0)) + { + writer.WriteStringValue("NaN"); + } + else + { + writer.WriteNumberValue(d0); + } + break; + case float f: + writer.WriteNumberValue(f); + break; + case long l: + writer.WriteNumberValue(l); + break; + case string s: + writer.WriteStringValue(s); + break; + case bool b: + writer.WriteBooleanValue(b); + break; + case Guid g: + writer.WriteStringValue(g); + break; + case DateTimeOffset dateTimeOffset: + writer.WriteStringValue(dateTimeOffset, "O"); + break; + case DateTime dateTime: + writer.WriteStringValue(dateTime, "O"); + break; + case IEnumerable> enumerable: + writer.WriteStartObject(); + foreach (var pair in enumerable) + { + writer.WritePropertyName(pair.Key); + writer.WriteObjectValue(pair.Value, options); + } + writer.WriteEndObject(); + break; + case IEnumerable objectEnumerable: + writer.WriteStartArray(); + foreach (var item in objectEnumerable) + { + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + break; + case TimeSpan timeSpan: + writer.WriteStringValue(timeSpan, "P"); + break; + default: + throw new NotSupportedException($"Not supported type {value.GetType()}"); + } + } + + public static void WriteObjectValue(this Utf8JsonWriter writer, object value, ModelReaderWriterOptions options = null) + { + writer.WriteObjectValue(value, options); + } + + internal static bool IsSentinelValue(BinaryData value) + { + ReadOnlySpan sentinelSpan = SentinelValue.ToMemory().Span; + ReadOnlySpan valueSpan = value.ToMemory().Span; + return sentinelSpan.SequenceEqual(valueSpan); + } + + internal static class TypeFormatters + { + private const string RoundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ"; + public const string DefaultNumberFormat = "G"; + + public static string ToString(bool value) => value ? "true" : "false"; + + public static string ToString(DateTime value, string format) => value.Kind switch + { + DateTimeKind.Utc => ToString((DateTimeOffset)value, format), + _ => throw new NotSupportedException($"DateTime {value} has a Kind of {value.Kind}. Azure SDK requires it to be UTC. You can call DateTime.SpecifyKind to change Kind property value to DateTimeKind.Utc.") + }; + + public static string ToString(DateTimeOffset value, string format) => format switch + { + "D" => value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), + "U" => value.ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture), + "O" => value.ToUniversalTime().ToString(RoundtripZFormat, CultureInfo.InvariantCulture), + "o" => value.ToUniversalTime().ToString(RoundtripZFormat, CultureInfo.InvariantCulture), + "R" => value.ToString("r", CultureInfo.InvariantCulture), + _ => value.ToString(format, CultureInfo.InvariantCulture) + }; + + public static string ToString(TimeSpan value, string format) => format switch + { + "P" => XmlConvert.ToString(value), + _ => value.ToString(format, CultureInfo.InvariantCulture) + }; + + public static string ToString(byte[] value, string format) => format switch + { + "U" => ToBase64UrlString(value), + "D" => Convert.ToBase64String(value), + _ => throw new ArgumentException($"Format is not supported: '{format}'", nameof(format)) + }; + + public static string ToBase64UrlString(byte[] value) + { + int numWholeOrPartialInputBlocks = checked(value.Length + 2) / 3; + int size = checked(numWholeOrPartialInputBlocks * 4); + char[] output = new char[size]; + + int numBase64Chars = Convert.ToBase64CharArray(value, 0, value.Length, output, 0); + + int i = 0; + for (; i < numBase64Chars; i++) + { + char ch = output[i]; + if (ch == '+') + { + output[i] = '-'; + } + else + { + if (ch == '/') + { + output[i] = '_'; + } + else + { + if (ch == '=') + { + break; + } + } + } + } + + return new string(output, 0, i); + } + + public static byte[] FromBase64UrlString(string value) + { + int paddingCharsToAdd = (value.Length % 4) switch + { + 0 => 0, + 2 => 2, + 3 => 1, + _ => throw new InvalidOperationException("Malformed input") + }; + char[] output = new char[(value.Length + paddingCharsToAdd)]; + int i = 0; + for (; i < value.Length; i++) + { + char ch = value[i]; + if (ch == '-') + { + output[i] = '+'; + } + else + { + if (ch == '_') + { + output[i] = '/'; + } + else + { + output[i] = ch; + } + } + } + + for (; i < output.Length; i++) + { + output[i] = '='; + } + + return Convert.FromBase64CharArray(output, 0, output.Length); + } + + public static DateTimeOffset ParseDateTimeOffset(string value, string format) => format switch + { + "U" => DateTimeOffset.FromUnixTimeSeconds(long.Parse(value, CultureInfo.InvariantCulture)), + _ => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) + }; + + public static TimeSpan ParseTimeSpan(string value, string format) => format switch + { + "P" => XmlConvert.ToTimeSpan(value), + _ => TimeSpan.ParseExact(value, format, CultureInfo.InvariantCulture) + }; + + public static string ConvertToString(object value, string format = null) => value switch + { + null => "null", + string s => s, + bool b => ToString(b), + int or float or double or long or decimal => ((IFormattable)value).ToString(DefaultNumberFormat, CultureInfo.InvariantCulture), + byte[] b0 when format != null => ToString(b0, format), + IEnumerable s0 => string.Join(",", s0), + DateTimeOffset dateTime when format != null => ToString(dateTime, format), + TimeSpan timeSpan when format != null => ToString(timeSpan, format), + TimeSpan timeSpan0 => XmlConvert.ToString(timeSpan0), + Guid guid => guid.ToString(), + BinaryData binaryData => ConvertToString(binaryData.ToArray(), format), + _ => value.ToString() + }; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/MultipartFormDataRequestContent.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/MultipartFormDataRequestContent.cs new file mode 100644 index 000000000000..15d0a18f86b6 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/MultipartFormDataRequestContent.cs @@ -0,0 +1,203 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Globalization; +using System.IO; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.AI.Speech.Transcription +{ + internal class MultipartFormDataRequestContent : RequestContent + { + private readonly System.Net.Http.MultipartFormDataContent _multipartContent; + private static readonly Random _random = new Random(); + private static readonly char[] _boundaryValues = "0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray(); + + public MultipartFormDataRequestContent() + { + _multipartContent = new System.Net.Http.MultipartFormDataContent(CreateBoundary()); + } + + public string ContentType + { + get + { + return _multipartContent.Headers.ContentType.ToString(); + } + } + + internal HttpContent HttpContent => _multipartContent; + + private static string CreateBoundary() + { + Span chars = new char[70]; + byte[] random = new byte[70]; + _random.NextBytes(random); + int mask = 255 >> 2; + for (int i = 0; i < 70; i++) + { + chars[i] = _boundaryValues[random[i] & mask]; + } + return chars.ToString(); + } + + public void Add(string content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + Add(new StringContent(content), name, filename, contentType); + } + + public void Add(int content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(long content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(float content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(double content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(decimal content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(bool content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + string value = content ? "true" : "false"; + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(Stream content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + Add(new StreamContent(content), name, filename, contentType); + } + + public void Add(byte[] content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + Add(new ByteArrayContent(content), name, filename, contentType); + } + + public void Add(BinaryData content, string name, string filename = null, string contentType = null) + { + Argument.AssertNotNull(content, nameof(content)); + Argument.AssertNotNullOrEmpty(name, nameof(name)); + + Add(new ByteArrayContent(content.ToArray()), name, filename, contentType); + } + + private void Add(HttpContent content, string name, string filename, string contentType) + { + if (filename != null) + { + Argument.AssertNotNullOrEmpty(filename, nameof(filename)); + AddFilenameHeader(content, name, filename); + } + if (contentType != null) + { + Argument.AssertNotNullOrEmpty(contentType, nameof(contentType)); + AddContentTypeHeader(content, contentType); + } + _multipartContent.Add(content, name); + } + + public static void AddFilenameHeader(HttpContent content, string name, string filename) + { + ContentDispositionHeaderValue header = new ContentDispositionHeaderValue("form-data") { Name = name, FileName = filename }; + content.Headers.ContentDisposition = header; + } + + public static void AddContentTypeHeader(HttpContent content, string contentType) + { + MediaTypeHeaderValue header = new MediaTypeHeaderValue(contentType); + content.Headers.ContentType = header; + } + + public override bool TryComputeLength(out long length) + { + if (_multipartContent.Headers.ContentLength is long contentLength) + { + length = contentLength; + return true; + } + length = 0; + return false; + } + + public override void WriteTo(Stream stream, CancellationToken cancellationToken = default) + { +#if NET6_0_OR_GREATER + _multipartContent.CopyTo(stream, default, cancellationToken); +#else +#pragma warning disable AZC0107 + _multipartContent.CopyToAsync(stream).EnsureCompleted(); +#pragma warning restore AZC0107 +#endif + } + + public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default) + { +#if NET6_0_OR_GREATER + await _multipartContent.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); +#else + await _multipartContent.CopyToAsync(stream).ConfigureAwait(false); +#endif + } + + public override void Dispose() + { + _multipartContent.Dispose(); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Optional.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Optional.cs new file mode 100644 index 000000000000..e4dce3223742 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Optional.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System.Collections.Generic; +using System.Text.Json; + +namespace Azure.AI.Speech.Transcription +{ + internal static class Optional + { + public static bool IsCollectionDefined(IEnumerable collection) + { + return !(collection is ChangeTrackingList changeTrackingList && changeTrackingList.IsUndefined); + } + + public static bool IsCollectionDefined(IDictionary collection) + { + return !(collection is ChangeTrackingDictionary changeTrackingDictionary && changeTrackingDictionary.IsUndefined); + } + + public static bool IsCollectionDefined(IReadOnlyDictionary collection) + { + return !(collection is ChangeTrackingDictionary changeTrackingDictionary && changeTrackingDictionary.IsUndefined); + } + + public static bool IsDefined(T? value) + where T : struct + { + return value.HasValue; + } + + public static bool IsDefined(object value) + { + return value != null; + } + + public static bool IsDefined(JsonElement value) + { + return value.ValueKind != JsonValueKind.Undefined; + } + + public static bool IsDefined(string value) + { + return value != null; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Utf8JsonRequestContent.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Utf8JsonRequestContent.cs new file mode 100644 index 000000000000..d8cda3de5867 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Internal/Utf8JsonRequestContent.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System.IO; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + internal class Utf8JsonRequestContent : RequestContent + { + private readonly MemoryStream _stream; + private readonly RequestContent _content; + + public Utf8JsonRequestContent() + { + _stream = new MemoryStream(); + _content = Create(_stream); + JsonWriter = new Utf8JsonWriter(_stream); + } + + public Utf8JsonWriter JsonWriter { get; } + + public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default) + { + await JsonWriter.FlushAsync().ConfigureAwait(false); + await _content.WriteToAsync(stream, cancellationToken).ConfigureAwait(false); + } + + public override void WriteTo(Stream stream, CancellationToken cancellationToken = default) + { + JsonWriter.Flush(); + _content.WriteTo(stream, cancellationToken); + } + + public override bool TryComputeLength(out long length) + { + length = JsonWriter.BytesCommitted + JsonWriter.BytesPending; + return true; + } + + public override void Dispose() + { + JsonWriter.Dispose(); + _content.Dispose(); + _stream.Dispose(); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Models/AzureAISpeechTranscriptionContext.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Models/AzureAISpeechTranscriptionContext.cs new file mode 100644 index 000000000000..526bea229220 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/Models/AzureAISpeechTranscriptionContext.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System.ClientModel.Primitives; + +namespace Azure.AI.Speech.Transcription +{ + /// + /// Context class which will be filled in by the System.ClientModel.SourceGeneration. + /// For more information see 'https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/System.ClientModel/src/docs/ModelReaderWriterContext.md' + /// + [ModelReaderWriterBuildable(typeof(ChannelCombinedPhrases))] + [ModelReaderWriterBuildable(typeof(EnhancedModeProperties))] + [ModelReaderWriterBuildable(typeof(PhraseListProperties))] + [ModelReaderWriterBuildable(typeof(ResponseError))] + [ModelReaderWriterBuildable(typeof(TranscribedPhrase))] + [ModelReaderWriterBuildable(typeof(TranscribedWord))] + [ModelReaderWriterBuildable(typeof(TranscriptionContent))] + [ModelReaderWriterBuildable(typeof(TranscriptionDiarizationOptions))] + [ModelReaderWriterBuildable(typeof(TranscriptionOptions))] + [ModelReaderWriterBuildable(typeof(TranscriptionResult))] + public partial class AzureAISpeechTranscriptionContext : ModelReaderWriterContext + { + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.Serialization.cs new file mode 100644 index 000000000000..c596a0f0f76b --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.Serialization.cs @@ -0,0 +1,174 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class PhraseListProperties : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(PhraseListProperties)} does not support writing '{format}' format."); + } + + if (Optional.IsCollectionDefined(Phrases)) + { + writer.WritePropertyName("phrases"u8); + writer.WriteStartArray(); + foreach (var item in Phrases) + { + writer.WriteStringValue(item); + } + writer.WriteEndArray(); + } + if (Optional.IsDefined(BiasingWeight)) + { + writer.WritePropertyName("biasingWeight"u8); + writer.WriteNumberValue(BiasingWeight.Value); + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + PhraseListProperties IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(PhraseListProperties)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializePhraseListProperties(document.RootElement, options); + } + + internal static PhraseListProperties DeserializePhraseListProperties(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + IList phrases = default; + float? biasingWeight = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("phrases"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(item.GetString()); + } + phrases = array; + continue; + } + if (property.NameEquals("biasingWeight"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + biasingWeight = property.Value.GetSingle(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new PhraseListProperties(phrases ?? new ChangeTrackingList(), biasingWeight, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(PhraseListProperties)} does not support writing '{options.Format}' format."); + } + } + + PhraseListProperties IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializePhraseListProperties(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(PhraseListProperties)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static PhraseListProperties FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializePhraseListProperties(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.cs new file mode 100644 index 000000000000..873782d4adf4 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/PhraseListProperties.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// Phrase list properties for transcription. + public partial class PhraseListProperties + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + public PhraseListProperties() + { + Phrases = new ChangeTrackingList(); + } + + /// Initializes a new instance of . + /// List of phrases for recognition. + /// Biasing weight for phrase list (1.0 to 20.0). + /// Keeps track of any properties unknown to the library. + internal PhraseListProperties(IList phrases, float? biasingWeight, IDictionary serializedAdditionalRawData) + { + Phrases = phrases; + BiasingWeight = biasingWeight; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// List of phrases for recognition. + public IList Phrases { get; } + /// Biasing weight for phrase list (1.0 to 20.0). + public float? BiasingWeight { get; set; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ProfanityFilterMode.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ProfanityFilterMode.cs new file mode 100644 index 000000000000..76d2e82b1c2a --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/ProfanityFilterMode.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ComponentModel; + +namespace Azure.AI.Speech.Transcription +{ + /// Mode of profanity filtering. + public readonly partial struct ProfanityFilterMode : IEquatable + { + private readonly string _value; + + /// Initializes a new instance of . + /// is null. + public ProfanityFilterMode(string value) + { + _value = value ?? throw new ArgumentNullException(nameof(value)); + } + + private const string NoneValue = "None"; + private const string RemovedValue = "Removed"; + private const string TagsValue = "Tags"; + private const string MaskedValue = "Masked"; + + /// Disable profanity filtering. + public static ProfanityFilterMode None { get; } = new ProfanityFilterMode(NoneValue); + /// Remove profanity. + public static ProfanityFilterMode Removed { get; } = new ProfanityFilterMode(RemovedValue); + /// Add "profanity" XML tags</Profanity>. + public static ProfanityFilterMode Tags { get; } = new ProfanityFilterMode(TagsValue); + /// Mask the profanity with * except of the first letter, e.g., f***. + public static ProfanityFilterMode Masked { get; } = new ProfanityFilterMode(MaskedValue); + /// Determines if two values are the same. + public static bool operator ==(ProfanityFilterMode left, ProfanityFilterMode right) => left.Equals(right); + /// Determines if two values are not the same. + public static bool operator !=(ProfanityFilterMode left, ProfanityFilterMode right) => !left.Equals(right); + /// Converts a to a . + public static implicit operator ProfanityFilterMode(string value) => new ProfanityFilterMode(value); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => obj is ProfanityFilterMode other && Equals(other); + /// + public bool Equals(ProfanityFilterMode other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => _value != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(_value) : 0; + /// + public override string ToString() => _value; + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.Serialization.cs new file mode 100644 index 000000000000..50b5c049f3bd --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.Serialization.cs @@ -0,0 +1,241 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscribedPhrase : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscribedPhrase)} does not support writing '{format}' format."); + } + + if (Optional.IsDefined(Channel)) + { + writer.WritePropertyName("channel"u8); + writer.WriteNumberValue(Channel.Value); + } + if (Optional.IsDefined(Speaker)) + { + writer.WritePropertyName("speaker"u8); + writer.WriteNumberValue(Speaker.Value); + } + writer.WritePropertyName("offsetMilliseconds"u8); + writer.WriteNumberValue(OffsetMilliseconds); + writer.WritePropertyName("durationMilliseconds"u8); + writer.WriteNumberValue(DurationMilliseconds); + writer.WritePropertyName("text"u8); + writer.WriteStringValue(Text); + if (Optional.IsCollectionDefined(Words)) + { + writer.WritePropertyName("words"u8); + writer.WriteStartArray(); + foreach (var item in Words) + { + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + } + if (Optional.IsDefined(Locale)) + { + writer.WritePropertyName("locale"u8); + writer.WriteStringValue(Locale); + } + writer.WritePropertyName("confidence"u8); + writer.WriteNumberValue(Confidence); + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + TranscribedPhrase IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscribedPhrase)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscribedPhrase(document.RootElement, options); + } + + internal static TranscribedPhrase DeserializeTranscribedPhrase(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + int? channel = default; + int? speaker = default; + int offsetMilliseconds = default; + int durationMilliseconds = default; + string text = default; + IReadOnlyList words = default; + string locale = default; + float confidence = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("channel"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + channel = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("speaker"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + speaker = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("offsetMilliseconds"u8)) + { + offsetMilliseconds = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("durationMilliseconds"u8)) + { + durationMilliseconds = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("text"u8)) + { + text = property.Value.GetString(); + continue; + } + if (property.NameEquals("words"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(TranscribedWord.DeserializeTranscribedWord(item, options)); + } + words = array; + continue; + } + if (property.NameEquals("locale"u8)) + { + locale = property.Value.GetString(); + continue; + } + if (property.NameEquals("confidence"u8)) + { + confidence = property.Value.GetSingle(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscribedPhrase( + channel, + speaker, + offsetMilliseconds, + durationMilliseconds, + text, + words ?? new ChangeTrackingList(), + locale, + confidence, + serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(TranscribedPhrase)} does not support writing '{options.Format}' format."); + } + } + + TranscribedPhrase IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscribedPhrase(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscribedPhrase)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscribedPhrase FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscribedPhrase(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.cs new file mode 100644 index 000000000000..29ff82109554 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedPhrase.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// A transcribed phrase. + public partial class TranscribedPhrase + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// The start offset of the phrase in milliseconds. + /// The duration of the phrase in milliseconds. + /// The transcribed text of the phrase. + /// The confidence value for the phrase. + /// is null. + internal TranscribedPhrase(int offsetMilliseconds, int durationMilliseconds, string text, float confidence) + { + Argument.AssertNotNull(text, nameof(text)); + + OffsetMilliseconds = offsetMilliseconds; + DurationMilliseconds = durationMilliseconds; + Text = text; + Words = new ChangeTrackingList(); + Confidence = confidence; + } + + /// Initializes a new instance of . + /// The 0-based channel index. Only present if channel separation is enabled. + /// A unique integer number that is assigned to each speaker detected in the audio without particular order. Only present if speaker diarization is enabled. + /// The start offset of the phrase in milliseconds. + /// The duration of the phrase in milliseconds. + /// The transcribed text of the phrase. + /// The words that make up the phrase. Only present if word-level timestamps are enabled. + /// The locale of the phrase. + /// The confidence value for the phrase. + /// Keeps track of any properties unknown to the library. + internal TranscribedPhrase(int? channel, int? speaker, int offsetMilliseconds, int durationMilliseconds, string text, IReadOnlyList words, string locale, float confidence, IDictionary serializedAdditionalRawData) + { + Channel = channel; + Speaker = speaker; + OffsetMilliseconds = offsetMilliseconds; + DurationMilliseconds = durationMilliseconds; + Text = text; + Words = words; + Locale = locale; + Confidence = confidence; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Initializes a new instance of for deserialization. + internal TranscribedPhrase() + { + } + /// A unique integer number that is assigned to each speaker detected in the audio without particular order. Only present if speaker diarization is enabled. + public int? Speaker { get; } + /// The transcribed text of the phrase. + public string Text { get; } + /// The words that make up the phrase. Only present if word-level timestamps are enabled. + public IReadOnlyList Words { get; } + /// The locale of the phrase. + public string Locale { get; } + /// The confidence value for the phrase. + public float Confidence { get; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.Serialization.cs new file mode 100644 index 000000000000..e011c7df267c --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.Serialization.cs @@ -0,0 +1,158 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscribedWord : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscribedWord)} does not support writing '{format}' format."); + } + + writer.WritePropertyName("text"u8); + writer.WriteStringValue(Text); + writer.WritePropertyName("offsetMilliseconds"u8); + writer.WriteNumberValue(OffsetMilliseconds); + writer.WritePropertyName("durationMilliseconds"u8); + writer.WriteNumberValue(DurationMilliseconds); + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + TranscribedWord IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscribedWord)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscribedWord(document.RootElement, options); + } + + internal static TranscribedWord DeserializeTranscribedWord(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + string text = default; + int offsetMilliseconds = default; + int durationMilliseconds = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("text"u8)) + { + text = property.Value.GetString(); + continue; + } + if (property.NameEquals("offsetMilliseconds"u8)) + { + offsetMilliseconds = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("durationMilliseconds"u8)) + { + durationMilliseconds = property.Value.GetInt32(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscribedWord(text, offsetMilliseconds, durationMilliseconds, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(TranscribedWord)} does not support writing '{options.Format}' format."); + } + } + + TranscribedWord IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscribedWord(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscribedWord)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscribedWord FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscribedWord(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.cs new file mode 100644 index 000000000000..12f8969f1b3a --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscribedWord.cs @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// Time-stamped word in the display form. + public partial class TranscribedWord + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// The recognized word, including punctuation. + /// The start offset of the word in milliseconds. + /// The duration of the word in milliseconds. + /// is null. + internal TranscribedWord(string text, int offsetMilliseconds, int durationMilliseconds) + { + Argument.AssertNotNull(text, nameof(text)); + + Text = text; + OffsetMilliseconds = offsetMilliseconds; + DurationMilliseconds = durationMilliseconds; + } + + /// Initializes a new instance of . + /// The recognized word, including punctuation. + /// The start offset of the word in milliseconds. + /// The duration of the word in milliseconds. + /// Keeps track of any properties unknown to the library. + internal TranscribedWord(string text, int offsetMilliseconds, int durationMilliseconds, IDictionary serializedAdditionalRawData) + { + Text = text; + OffsetMilliseconds = offsetMilliseconds; + DurationMilliseconds = durationMilliseconds; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Initializes a new instance of for deserialization. + internal TranscribedWord() + { + } + + /// The recognized word, including punctuation. + public string Text { get; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClient.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClient.cs new file mode 100644 index 000000000000..4b6b22c71d9a --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClient.cs @@ -0,0 +1,228 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.AI.Speech.Transcription +{ + // Data plane generated client. + /// The Transcription service client. + public partial class TranscriptionClient + { + private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; + private readonly AzureKeyCredential _keyCredential; + private static readonly string[] AuthorizationScopes = new string[] { "https://cognitiveservices.azure.com/.default" }; + private readonly TokenCredential _tokenCredential; + private readonly HttpPipeline _pipeline; + private readonly Uri _endpoint; + private readonly string _apiVersion; + + /// The ClientDiagnostics is used to provide tracing support for the client library. + internal ClientDiagnostics ClientDiagnostics { get; } + + /// The HTTP pipeline for sending and receiving REST requests and responses. + public virtual HttpPipeline Pipeline => _pipeline; + + /// Initializes a new instance of TranscriptionClient for mocking. + protected TranscriptionClient() + { + } + + /// Initializes a new instance of TranscriptionClient. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + /// A credential used to authenticate to an Azure Service. + /// or is null. + public TranscriptionClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new TranscriptionClientOptions()) + { + } + + /// Initializes a new instance of TranscriptionClient. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + /// A credential used to authenticate to an Azure Service. + /// or is null. + public TranscriptionClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new TranscriptionClientOptions()) + { + } + + /// Initializes a new instance of TranscriptionClient. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + /// A credential used to authenticate to an Azure Service. + /// The options for configuring the client. + /// or is null. + public TranscriptionClient(Uri endpoint, AzureKeyCredential credential, TranscriptionClientOptions options) + { + Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(credential, nameof(credential)); + options ??= new TranscriptionClientOptions(); + + ClientDiagnostics = new ClientDiagnostics(options, true); + _keyCredential = credential; + _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { new AzureKeyCredentialPolicy(_keyCredential, AuthorizationHeader) }, new ResponseClassifier()); + _endpoint = endpoint; + _apiVersion = options.Version; + } + + /// Initializes a new instance of TranscriptionClient. + /// Supported Cognitive Services endpoints (protocol and hostname, for example: https://westus.api.cognitive.microsoft.com. + /// A credential used to authenticate to an Azure Service. + /// The options for configuring the client. + /// or is null. + public TranscriptionClient(Uri endpoint, TokenCredential credential, TranscriptionClientOptions options) + { + Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(credential, nameof(credential)); + options ??= new TranscriptionClientOptions(); + + ClientDiagnostics = new ClientDiagnostics(options, true); + _tokenCredential = credential; + _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier()); + _endpoint = endpoint; + _apiVersion = options.Version; + } + + /// Transcribes the provided audio stream. + /// The body of the multipart request. + /// The cancellation token to use. + /// is null. + internal virtual async Task> TranscribeAsync(TranscriptionContent body, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(body, nameof(body)); + + using MultipartFormDataRequestContent content = body.ToMultipartRequestContent(); + RequestContext context = FromCancellationToken(cancellationToken); + Response response = await TranscribeAsync(content, content.ContentType, context).ConfigureAwait(false); + return Response.FromValue(TranscriptionResult.FromResponse(response), response); + } + + /// Transcribes the provided audio stream. + /// The body of the multipart request. + /// The cancellation token to use. + /// is null. + internal virtual Response Transcribe(TranscriptionContent body, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(body, nameof(body)); + + using MultipartFormDataRequestContent content = body.ToMultipartRequestContent(); + RequestContext context = FromCancellationToken(cancellationToken); + Response response = Transcribe(content, content.ContentType, context); + return Response.FromValue(TranscriptionResult.FromResponse(response), response); + } + + /// + /// [Protocol Method] Transcribes the provided audio stream. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// Please try the simpler convenience overload with strongly typed models first. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// request content type. Allowed values: "multipart/form-data". + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual async Task TranscribeAsync(RequestContent content, string contentType, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("TranscriptionClient.Transcribe"); + scope.Start(); + try + { + using HttpMessage message = CreateTranscribeRequest(content, contentType, context); + return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// + /// [Protocol Method] Transcribes the provided audio stream. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// Please try the simpler convenience overload with strongly typed models first. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// request content type. Allowed values: "multipart/form-data". + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual Response Transcribe(RequestContent content, string contentType, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("TranscriptionClient.Transcribe"); + scope.Start(); + try + { + using HttpMessage message = CreateTranscribeRequest(content, contentType, context); + return _pipeline.ProcessMessage(message, context); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + internal HttpMessage CreateTranscribeRequest(RequestContent content, string contentType, RequestContext context) + { + var message = _pipeline.CreateMessage(context, ResponseClassifier200); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(_endpoint); + uri.AppendRaw("/speechtotext", false); + uri.AppendPath("/transcriptions:transcribe", false); + uri.AppendQuery("api-version", _apiVersion, true); + request.Uri = uri; + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("Content-Type", contentType); + request.Content = content; + return message; + } + + private static RequestContext DefaultRequestContext = new RequestContext(); + internal static RequestContext FromCancellationToken(CancellationToken cancellationToken = default) + { + if (!cancellationToken.CanBeCanceled) + { + return DefaultRequestContext; + } + + return new RequestContext() { CancellationToken = cancellationToken }; + } + + private static ResponseClassifier _responseClassifier200; + private static ResponseClassifier ResponseClassifier200 => _responseClassifier200 ??= new StatusCodeClassifier(stackalloc ushort[] { 200 }); + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClientOptions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClientOptions.cs new file mode 100644 index 000000000000..fffd32297731 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionClientOptions.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + /// Client options for TranscriptionClient. + public partial class TranscriptionClientOptions : ClientOptions + { + private const ServiceVersion LatestVersion = ServiceVersion.V2025_10_15; + + /// The version of the service to use. + public enum ServiceVersion + { + /// Service version "2025-10-15". + V2025_10_15 = 1, + } + + internal string Version { get; } + + /// Initializes new instance of TranscriptionClientOptions. + public TranscriptionClientOptions(ServiceVersion version = LatestVersion) + { + Version = version switch + { + ServiceVersion.V2025_10_15 => "2025-10-15", + _ => throw new NotSupportedException() + }; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.Serialization.cs new file mode 100644 index 000000000000..48911edc471c --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.Serialization.cs @@ -0,0 +1,203 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + internal partial class TranscriptionContent : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionContent)} does not support writing '{format}' format."); + } + + if (Optional.IsDefined(Options)) + { + writer.WritePropertyName("definition"u8); + writer.WriteObjectValue(Options, options); + } + if (Optional.IsDefined(Audio)) + { + writer.WritePropertyName("audio"u8); +#if NET6_0_OR_GREATER + writer.WriteRawValue(global::System.BinaryData.FromStream(Audio)); +#else + using (JsonDocument document = JsonDocument.Parse(BinaryData.FromStream(Audio), ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + TranscriptionContent IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionContent)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscriptionContent(document.RootElement, options); + } + + internal static TranscriptionContent DeserializeTranscriptionContent(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + TranscriptionOptions definition = default; + Stream audio = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("definition"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + definition = TranscriptionOptions.DeserializeTranscriptionOptions(property.Value, options); + continue; + } + if (property.NameEquals("audio"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + audio = BinaryData.FromString(property.Value.GetRawText()).ToStream(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscriptionContent(definition, audio, serializedAdditionalRawData); + } + + private BinaryData SerializeMultipart(ModelReaderWriterOptions options) + { + using MultipartFormDataRequestContent content = ToMultipartRequestContent(); + using MemoryStream stream = new MemoryStream(); + content.WriteTo(stream); + if (stream.Position > int.MaxValue) + { + return BinaryData.FromStream(stream); + } + else + { + return new BinaryData(stream.GetBuffer().AsMemory(0, (int)stream.Position)); + } + } + + internal virtual MultipartFormDataRequestContent ToMultipartRequestContent() + { + MultipartFormDataRequestContent content = new MultipartFormDataRequestContent(); + if (Optional.IsDefined(Options)) + { + content.Add(ModelReaderWriter.Write(Options, ModelSerializationExtensions.WireOptions, AzureAISpeechTranscriptionContext.Default), "definition"); + } + if (Optional.IsDefined(Audio)) + { + content.Add(Audio, "audio", "audio", "application/octet-stream"); + } + return content; + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + case "MFD": + return SerializeMultipart(options); + default: + throw new FormatException($"The model {nameof(TranscriptionContent)} does not support writing '{options.Format}' format."); + } + } + + TranscriptionContent IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionContent(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscriptionContent)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "MFD"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscriptionContent FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionContent(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.cs new file mode 100644 index 000000000000..3194f75f442e --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionContent.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; +using System.IO; + +namespace Azure.AI.Speech.Transcription +{ + /// Request model for transcription operation. + internal partial class TranscriptionContent + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + public TranscriptionContent() + { + } + + /// Initializes a new instance of . + /// Metadata for a transcription request. This field contains a JSON-serialized object of type `TranscriptionOptions`. + /// The content of the audio file to be transcribed. The audio file must be shorter than 2 hours in audio duration and smaller than 250 MB in size. Optional if audioUrl is provided in the definition. + /// Keeps track of any properties unknown to the library. + internal TranscriptionContent(TranscriptionOptions options, Stream audio, IDictionary serializedAdditionalRawData) + { + Options = options; + Audio = audio; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Metadata for a transcription request. This field contains a JSON-serialized object of type `TranscriptionOptions`. + public TranscriptionOptions Options { get; set; } + /// The content of the audio file to be transcribed. The audio file must be shorter than 2 hours in audio duration and smaller than 250 MB in size. Optional if audioUrl is provided in the definition. + public Stream Audio { get; set; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.Serialization.cs new file mode 100644 index 000000000000..c5c8c9680cde --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.Serialization.cs @@ -0,0 +1,127 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscriptionDiarizationOptions : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + TranscriptionDiarizationOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionDiarizationOptions)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscriptionDiarizationOptions(document.RootElement, options); + } + + internal static TranscriptionDiarizationOptions DeserializeTranscriptionDiarizationOptions(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + bool? enabled = default; + int? maxSpeakers = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("enabled"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + enabled = property.Value.GetBoolean(); + continue; + } + if (property.NameEquals("maxSpeakers"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + maxSpeakers = property.Value.GetInt32(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscriptionDiarizationOptions(enabled, maxSpeakers, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(TranscriptionDiarizationOptions)} does not support writing '{options.Format}' format."); + } + } + + TranscriptionDiarizationOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionDiarizationOptions(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscriptionDiarizationOptions)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscriptionDiarizationOptions FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionDiarizationOptions(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.cs new file mode 100644 index 000000000000..d991f16e8bb7 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionDiarizationOptions.cs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// The Speaker Diarization settings. Diarization settings must be specified to enable speaker diarization. + public partial class TranscriptionDiarizationOptions + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + public TranscriptionDiarizationOptions() + { + } + + /// Initializes a new instance of . + /// Enable speaker diarization. This is automatically set to true when maxSpeakers is specified. + /// Gets or sets a hint for the maximum number of speakers for diarization. Must be greater than 1 and less than 36. + /// Keeps track of any properties unknown to the library. + internal TranscriptionDiarizationOptions(bool? enabled, int? maxSpeakers, IDictionary serializedAdditionalRawData) + { + Enabled = enabled; + MaxSpeakers = maxSpeakers; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Enable speaker diarization. This is automatically set to true when maxSpeakers is specified. + public bool? Enabled { get; } + /// Gets or sets a hint for the maximum number of speakers for diarization. Must be greater than 1 and less than 36. + public int? MaxSpeakers { get; set; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.Serialization.cs new file mode 100644 index 000000000000..6c64997100a6 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.Serialization.cs @@ -0,0 +1,306 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscriptionOptions : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionOptions)} does not support writing '{format}' format."); + } + + if (Optional.IsDefined(AudioUri)) + { + writer.WritePropertyName("audioUrl"u8); + writer.WriteStringValue(AudioUri.AbsoluteUri); + } + if (Optional.IsCollectionDefined(Locales)) + { + writer.WritePropertyName("locales"u8); + writer.WriteStartArray(); + foreach (var item in Locales) + { + writer.WriteStringValue(item); + } + writer.WriteEndArray(); + } + if (Optional.IsCollectionDefined(Models)) + { + writer.WritePropertyName("models"u8); + writer.WriteStartObject(); + foreach (var item in Models) + { + writer.WritePropertyName(item.Key); + if (item.Value == null) + { + writer.WriteNullValue(); + continue; + } + writer.WriteStringValue(item.Value.AbsoluteUri); + } + writer.WriteEndObject(); + } + if (Optional.IsDefined(ProfanityFilterMode)) + { + writer.WritePropertyName("profanityFilterMode"u8); + writer.WriteStringValue(ProfanityFilterMode.Value.ToString()); + } + if (Optional.IsDefined(DiarizationOptions)) + { + writer.WritePropertyName("diarization"u8); + writer.WriteObjectValue(DiarizationOptions, options); + } + if (Optional.IsCollectionDefined(ActiveChannels)) + { + writer.WritePropertyName("channels"u8); + writer.WriteStartArray(); + foreach (var item in ActiveChannels) + { + writer.WriteNumberValue(item); + } + writer.WriteEndArray(); + } + if (Optional.IsDefined(EnhancedMode)) + { + writer.WritePropertyName("enhancedMode"u8); + writer.WriteObjectValue(EnhancedMode, options); + } + if (Optional.IsDefined(PhraseList)) + { + writer.WritePropertyName("phraseList"u8); + writer.WriteObjectValue(PhraseList, options); + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + TranscriptionOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionOptions)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscriptionOptions(document.RootElement, options); + } + + internal static TranscriptionOptions DeserializeTranscriptionOptions(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + Uri audioUrl = default; + IList locales = default; + IDictionary models = default; + ProfanityFilterMode? profanityFilterMode = default; + TranscriptionDiarizationOptions diarization = default; + IList channels = default; + EnhancedModeProperties enhancedMode = default; + PhraseListProperties phraseList = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("audioUrl"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + audioUrl = new Uri(property.Value.GetString()); + continue; + } + if (property.NameEquals("locales"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(item.GetString()); + } + locales = array; + continue; + } + if (property.NameEquals("models"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + Dictionary dictionary = new Dictionary(); + foreach (var property0 in property.Value.EnumerateObject()) + { + if (property0.Value.ValueKind == JsonValueKind.Null) + { + dictionary.Add(property0.Name, null); + } + else + { + dictionary.Add(property0.Name, new Uri(property0.Value.GetString())); + } + } + models = dictionary; + continue; + } + if (property.NameEquals("profanityFilterMode"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + profanityFilterMode = new ProfanityFilterMode(property.Value.GetString()); + continue; + } + if (property.NameEquals("diarization"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + diarization = TranscriptionDiarizationOptions.DeserializeTranscriptionDiarizationOptions(property.Value, options); + continue; + } + if (property.NameEquals("channels"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(item.GetInt32()); + } + channels = array; + continue; + } + if (property.NameEquals("enhancedMode"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + enhancedMode = EnhancedModeProperties.DeserializeEnhancedModeProperties(property.Value, options); + continue; + } + if (property.NameEquals("phraseList"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + phraseList = PhraseListProperties.DeserializePhraseListProperties(property.Value, options); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscriptionOptions( + audioUrl, + locales ?? new ChangeTrackingList(), + models ?? new ChangeTrackingDictionary(), + profanityFilterMode, + diarization, + channels ?? new ChangeTrackingList(), + enhancedMode, + phraseList, + serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(TranscriptionOptions)} does not support writing '{options.Format}' format."); + } + } + + TranscriptionOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionOptions(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscriptionOptions)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscriptionOptions FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionOptions(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.cs new file mode 100644 index 000000000000..01b9b246a198 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionOptions.cs @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Speech.Transcription +{ + /// Metadata for a transcription request. + public partial class TranscriptionOptions + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// The URL of the audio to be transcribed. The audio must be shorter than 2 hours in audio duration and smaller than 250 MB in size. If both Audio and AudioUrl are provided, Audio is used. + /// A list of possible locales for the transcription. If not specified, the locale of the speech in the audio is detected automatically from all supported locales. + /// Maps some or all candidate locales to a model URI to be used for transcription. If no mapping is given, the default model for the locale is used. + /// Mode of profanity filtering. + /// Mode of diarization. + /// The 0-based indices of the channels to be transcribed separately. If not specified, multiple channels are merged and transcribed jointly. Only up to two channels are supported. + /// Enhanced mode properties. + /// Phrase list properties. + /// Keeps track of any properties unknown to the library. + internal TranscriptionOptions(Uri audioUri, IList locales, IDictionary models, ProfanityFilterMode? profanityFilterMode, TranscriptionDiarizationOptions diarizationOptions, IList activeChannels, EnhancedModeProperties enhancedMode, PhraseListProperties phraseList, IDictionary serializedAdditionalRawData) + { + AudioUri = audioUri; + Locales = locales; + Models = models; + ProfanityFilterMode = profanityFilterMode; + DiarizationOptions = diarizationOptions; + ActiveChannels = activeChannels; + EnhancedMode = enhancedMode; + PhraseList = phraseList; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + /// A list of possible locales for the transcription. If not specified, the locale of the speech in the audio is detected automatically from all supported locales. + public IList Locales { get; } + /// Maps some or all candidate locales to a model URI to be used for transcription. If no mapping is given, the default model for the locale is used. + public IDictionary Models { get; } + /// Mode of profanity filtering. + public ProfanityFilterMode? ProfanityFilterMode { get; set; } + /// Mode of diarization. + public TranscriptionDiarizationOptions DiarizationOptions { get; set; } + /// The 0-based indices of the channels to be transcribed separately. If not specified, multiple channels are merged and transcribed jointly. Only up to two channels are supported. + public IList ActiveChannels { get; } + /// Enhanced mode properties. + public EnhancedModeProperties EnhancedMode { get; set; } + /// Phrase list properties. + public PhraseListProperties PhraseList { get; set; } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.Serialization.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.Serialization.cs new file mode 100644 index 000000000000..e1301919afea --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.Serialization.cs @@ -0,0 +1,178 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Speech.Transcription +{ + public partial class TranscriptionResult : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionResult)} does not support writing '{format}' format."); + } + + writer.WritePropertyName("durationMilliseconds"u8); + writer.WriteNumberValue(DurationMilliseconds); + writer.WritePropertyName("combinedPhrases"u8); + writer.WriteStartArray(); + foreach (var item in CombinedPhrases) + { + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + writer.WritePropertyName("phrases"u8); + writer.WriteStartArray(); + foreach (var item in Phrases) + { + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value, ModelSerializationExtensions.JsonDocumentOptions)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + TranscriptionResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(TranscriptionResult)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeTranscriptionResult(document.RootElement, options); + } + + internal static TranscriptionResult DeserializeTranscriptionResult(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + int durationMilliseconds = default; + IReadOnlyList combinedPhrases = default; + IReadOnlyList phrases = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("durationMilliseconds"u8)) + { + durationMilliseconds = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("combinedPhrases"u8)) + { + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(ChannelCombinedPhrases.DeserializeChannelCombinedPhrases(item, options)); + } + combinedPhrases = array; + continue; + } + if (property.NameEquals("phrases"u8)) + { + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(TranscribedPhrase.DeserializeTranscribedPhrase(item, options)); + } + phrases = array; + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new TranscriptionResult(durationMilliseconds, combinedPhrases, phrases, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options, AzureAISpeechTranscriptionContext.Default); + default: + throw new FormatException($"The model {nameof(TranscriptionResult)} does not support writing '{options.Format}' format."); + } + } + + TranscriptionResult IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionResult(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(TranscriptionResult)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static TranscriptionResult FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions); + return DeserializeTranscriptionResult(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.cs new file mode 100644 index 000000000000..193c3027f4e4 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Generated/TranscriptionResult.cs @@ -0,0 +1,82 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Azure.AI.Speech.Transcription +{ + /// The result of the transcribe operation. + public partial class TranscriptionResult + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// The duration of the audio in milliseconds. + /// The full transcript for each channel. + /// The transcription results segmented into phrases. + /// or is null. + internal TranscriptionResult(int durationMilliseconds, IEnumerable combinedPhrases, IEnumerable phrases) + { + Argument.AssertNotNull(combinedPhrases, nameof(combinedPhrases)); + Argument.AssertNotNull(phrases, nameof(phrases)); + + DurationMilliseconds = durationMilliseconds; + CombinedPhrases = combinedPhrases.ToList(); + Phrases = phrases.ToList(); + } + + /// Initializes a new instance of . + /// The duration of the audio in milliseconds. + /// The full transcript for each channel. + /// The transcription results segmented into phrases. + /// Keeps track of any properties unknown to the library. + internal TranscriptionResult(int durationMilliseconds, IReadOnlyList combinedPhrases, IReadOnlyList phrases, IDictionary serializedAdditionalRawData) + { + DurationMilliseconds = durationMilliseconds; + CombinedPhrases = combinedPhrases; + Phrases = phrases; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Initializes a new instance of for deserialization. + internal TranscriptionResult() + { + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Properties/AssemblyInfo.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Properties/AssemblyInfo.cs new file mode 100644 index 000000000000..06d7756742df --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/src/Properties/AssemblyInfo.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Azure.AI.Speech.Transcription.Tests, PublicKey = 0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")] + +// Replace Microsoft.Test with the correct resource provider namepace for your service and uncomment. +// See https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/azure-services-resource-providers +// for the list of possible values. +[assembly: Azure.Core.AzureResourceProviderNamespace("Microsoft.Template")] diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Azure.AI.Speech.Transcription.Tests.csproj b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Azure.AI.Speech.Transcription.Tests.csproj new file mode 100644 index 000000000000..9e48a49ccb21 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Azure.AI.Speech.Transcription.Tests.csproj @@ -0,0 +1,20 @@ + + + $(RequiredTargetFrameworks) + + $(NoWarn);CS1591 + + + + + + + + + + + + + + + diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/GlobalUsings.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/GlobalUsings.cs new file mode 100644 index 000000000000..0bd2a0b2e440 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/GlobalUsings.cs @@ -0,0 +1,4 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +global using Azure.AI.Speech.Transcription; diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/README.md b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/README.md new file mode 100644 index 000000000000..f7c7b389755e --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/README.md @@ -0,0 +1,3 @@ +Source files in this directory are written as tests from which samples are +extracted. They are not intended to be viewed directly and help ensure our samples +compile and work correctly. See our [list of samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/cognitiveservices/Azure.AI.Speech.Transcription/samples) for more explanation about how to use this client library. diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample01_BasicTranscription.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample01_BasicTranscription.cs new file mode 100644 index 000000000000..fcea852c2fb6 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample01_BasicTranscription.cs @@ -0,0 +1,301 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Azure.Core; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples demonstrating basic transcription operations using the . + /// + public partial class Sample01_BasicTranscription : TranscriptionSampleBase + { + /// + /// Creates a new TranscriptionClient using an API key credential. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public new void CreateTranscriptionClient() + { + #region Snippet:CreateTranscriptionClient + // Get the endpoint and API key from your Speech resource in the Azure portal + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + + // Create the TranscriptionClient + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + #endregion Snippet:CreateTranscriptionClient + } + + /// + /// Creates a TranscriptionClient with custom client options. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void CreateTranscriptionClientWithOptions() + { + #region Snippet:CreateTranscriptionClientWithOptions + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + + // Configure client options + TranscriptionClientOptions options = new TranscriptionClientOptions( + TranscriptionClientOptions.ServiceVersion.V2025_10_15); + + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + #endregion Snippet:CreateTranscriptionClientWithOptions + } + + /// + /// Transcribes a local audio file synchronously. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void TranscribeLocalFile() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeLocalFile + // Path to your local audio file + string audioFilePath = "path/to/audio.wav"; + + // Open the audio file as a stream + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Create the transcription request + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + // Perform synchronous transcription + Response response = client.Transcribe(request); + TranscriptionResult result = response.Value; + + // Display the transcription results + Console.WriteLine($"Total audio duration: {result.Duration}"); + Console.WriteLine("\nTranscription:"); + + // Get the first channel's phrases (most audio files have a single channel) + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"[{phrase.Offset} - {phrase.Offset + phrase.Duration}] {phrase.Text}"); + Console.WriteLine($" Confidence: {phrase.Confidence:F2}"); + } + #endregion Snippet:TranscribeLocalFile + } + + /// + /// Transcribes a local audio file asynchronously. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeLocalFileAsync() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + // Path to your local audio file + string audioFilePath = "path/to/audio.wav"; + + // Open the audio file as a stream + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Create the transcription request + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + // Perform asynchronous transcription + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + // Display the transcription results + Console.WriteLine($"Total audio duration: {result.Duration}"); + Console.WriteLine("\nTranscription:"); + + // Get the first channel's phrases + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"[{phrase.Offset} - {phrase.Offset + phrase.Duration}] {phrase.Text}"); + Console.WriteLine($" Confidence: {phrase.Confidence:F2}"); + } + } + + /// + /// Demonstrates accessing individual words within transcribed phrases. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task AccessTranscribedWords() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + string audioFilePath = "path/to/audio.wav"; + + #region Snippet:AccessTranscribedWords + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + // Access individual words in each phrase + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"\nPhrase: {phrase.Text}"); + Console.WriteLine("Words:"); + + foreach (TranscribedWord word in phrase.Words) + { + Console.WriteLine($" [{word.Offset} - {word.Offset + word.Duration}] {word.Text}"); + } + } + #endregion Snippet:AccessTranscribedWords + } + + /// + /// Demonstrates accessing the combined text for a channel. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task AccessCombinedText() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + string audioFilePath = "path/to/audio.wav"; + + #region Snippet:AccessCombinedText + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + // Access the combined text for each channel + foreach (var channelPhrases in result.PhrasesByChannel) + { + Console.WriteLine($"Channel {channelPhrases.Channel ?? 0}:"); + Console.WriteLine($"Combined Text: {channelPhrases.Text}"); + Console.WriteLine(); + } + #endregion Snippet:AccessCombinedText + } + + /// + /// Demonstrates configuring logging and diagnostics for troubleshooting. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void ConfigureLogging() + { + #region Snippet:ConfigureLogging + // Enable Azure Core diagnostics for console logging + using Azure.Core.Diagnostics.AzureEventSourceListener listener = + Azure.Core.Diagnostics.AzureEventSourceListener.CreateConsoleLogger(); + + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + + // Configure diagnostics options + TranscriptionClientOptions options = new TranscriptionClientOptions + { + Diagnostics = + { + IsLoggingEnabled = true, + IsLoggingContentEnabled = true, // Log request/response content + IsTelemetryEnabled = true, + ApplicationId = "MyApp/1.0.0" // Custom application identifier + } + }; + + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + + // Logs will now be written to the console showing: + // - HTTP requests and responses + // - Request/response headers + // - Request/response content (when IsLoggingContentEnabled = true) + // - Client pipeline events + #endregion Snippet:ConfigureLogging + } + + /// + /// Demonstrates configuring custom retry policy for resilience. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void ConfigureRetryPolicy() + { + #region Snippet:ConfigureRetryPolicy + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + + // Configure retry policy + TranscriptionClientOptions options = new TranscriptionClientOptions + { + Retry = + { + MaxRetries = 5, // Maximum number of retry attempts + Delay = TimeSpan.FromSeconds(1), // Initial delay between retries + MaxDelay = TimeSpan.FromSeconds(30), // Maximum delay between retries + Mode = RetryMode.Exponential // Use exponential backoff + } + }; + + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + + // The client will now automatically retry failed requests + // using exponential backoff strategy + #endregion Snippet:ConfigureRetryPolicy + } + + /// + /// Demonstrates configuring custom timeout for transcription operations. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void ConfigureTimeout() + { + #region Snippet:ConfigureTimeout + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + + // Configure network timeout + TranscriptionClientOptions options = new TranscriptionClientOptions + { + Retry = + { + NetworkTimeout = TimeSpan.FromMinutes(5) // 5 minute timeout for network operations + } + }; + + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + + // All transcription operations will use the configured timeout + // This is useful for large audio files that take longer to process + #endregion Snippet:ConfigureTimeout + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample02_TranscriptionOptions.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample02_TranscriptionOptions.cs new file mode 100644 index 000000000000..29a4e8a60deb --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample02_TranscriptionOptions.cs @@ -0,0 +1,309 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Azure.Core; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples demonstrating advanced transcription options. + /// + public partial class Sample02_TranscriptionOptions : TranscriptionSampleBase + { + /// + /// Transcribe audio with specific locale settings. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithLocales() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + string audioFilePath = "path/to/audio.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Configure transcription options with specific locales + TranscriptionOptions options = new TranscriptionOptions(); + options.Locales.Add("en-US"); + options.Locales.Add("es-ES"); // Add Spanish as a secondary locale + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Transcription with locales: {string.Join(", ", options.Locales)}"); + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"[{phrase.Offset}] {phrase.Text}"); + } + } + + /// + /// Transcribe audio with profanity filtering enabled. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithProfanityFilter() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithProfanityFilter + string audioFilePath = "path/to/audio.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Configure profanity filtering + TranscriptionOptions options = new TranscriptionOptions + { + ProfanityFilterMode = ProfanityFilterMode.Masked // Masks profanity with asterisks + }; + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription with profanity filtering:"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine(channelPhrases.Text); + #endregion Snippet:TranscribeWithProfanityFilter + } + + /// + /// Transcribe audio with speaker diarization (identifying different speakers). + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithDiarization() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + string audioFilePath = "path/to/conversation.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Enable speaker diarization + TranscriptionOptions options = new TranscriptionOptions + { + DiarizationOptions = new TranscriptionDiarizationOptions + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 4 // Expect up to 4 speakers in the conversation + } + }; + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription with speaker diarization:"); + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + // Speaker information is included in the phrase + Console.WriteLine($"Speaker {phrase.Speaker}: {phrase.Text}"); + } + } + + /// + /// Transcribe audio with a custom phrase list to improve recognition accuracy. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithPhraseList() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithPhraseList + string audioFilePath = "path/to/technical-talk.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Add custom phrases to improve recognition of domain-specific terms + TranscriptionOptions options = new TranscriptionOptions + { + PhraseList = new PhraseListProperties + { + BiasingWeight = 5.0f // Increase weight to favor these phrases (1.0 - 20.0) + } + }; + + // Add technical terms that might be uncommon in general speech + options.PhraseList.Phrases.Add("Azure Cognitive Services"); + options.PhraseList.Phrases.Add("TranscriptionClient"); + options.PhraseList.Phrases.Add("speech-to-text"); + options.PhraseList.Phrases.Add("API endpoint"); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription with custom phrase list:"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine(channelPhrases.Text); + #endregion Snippet:TranscribeWithPhraseList + } + + /// + /// Transcribe specific channels from a multi-channel audio file. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithChannelSelection() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithChannelSelection + string audioFilePath = "path/to/stereo-audio.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Specify which channels to transcribe separately + TranscriptionOptions options = new TranscriptionOptions(); + options.ActiveChannels.Add(0); // Left channel + options.ActiveChannels.Add(1); // Right channel + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + // Each channel is transcribed separately + Console.WriteLine("Multi-channel transcription:"); + foreach (var channelPhrases in result.PhrasesByChannel) + { + Console.WriteLine($"\nChannel {channelPhrases.Channel}:"); + Console.WriteLine(channelPhrases.Text); + } + #endregion Snippet:TranscribeWithChannelSelection + } + + /// + /// Transcribe audio using a custom model. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithCustomModel() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithCustomModel + string audioFilePath = "path/to/audio.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Map locales to custom trained models + TranscriptionOptions options = new TranscriptionOptions(); + options.Locales.Add("en-US"); + + // Use a custom model for English transcription + Uri customModelEndpoint = new Uri( + "https://myaccount.api.cognitive.microsoft.com/speechtotext/models/your-custom-model-id"); + options.Models.Add("en-US", customModelEndpoint); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription with custom model:"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine(channelPhrases.Text); + #endregion Snippet:TranscribeWithCustomModel + } + + /// + /// Combines multiple transcription options for complex scenarios. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithMultipleOptions() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithMultipleOptions + string audioFilePath = "path/to/meeting.wav"; + using FileStream audioStream = File.OpenRead(audioFilePath); + + // Combine multiple options for a complete transcription solution + TranscriptionOptions options = new TranscriptionOptions + { + ProfanityFilterMode = ProfanityFilterMode.Masked, + DiarizationOptions = new TranscriptionDiarizationOptions + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 5 + }, + PhraseList = new PhraseListProperties + { + BiasingWeight = 3.0f + } + }; + + // Add locale and custom phrases + options.Locales.Add("en-US"); + options.PhraseList.Phrases.Add("quarterly report"); + options.PhraseList.Phrases.Add("action items"); + options.PhraseList.Phrases.Add("stakeholders"); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Meeting transcription ({result.Duration}):"); + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"[Speaker {phrase.Speaker}] {phrase.Text}"); + } + #endregion Snippet:TranscribeWithMultipleOptions + } + } +} \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample03_ErrorHandling.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample03_ErrorHandling.cs new file mode 100644 index 000000000000..d5d12b181edb --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample03_ErrorHandling.cs @@ -0,0 +1,346 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Net; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples demonstrating error handling in transcription operations. + /// + public partial class Sample03_ErrorHandling : TranscriptionSampleBase + { + /// + /// Demonstrates basic error handling for transcription requests. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task HandleTranscriptionErrors() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:HandleTranscriptionErrors + string audioFilePath = "path/to/audio.wav"; + + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Transcription successful. Duration: {result.Duration}"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"Text: {channelPhrases.Text}"); + } + catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.Unauthorized) + { + Console.WriteLine("Authentication failed. Please check your API key."); + Console.WriteLine($"Error: {ex.Message}"); + } + catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.BadRequest) + { + Console.WriteLine("Invalid request. Please check your audio file format and request parameters."); + Console.WriteLine($"Error: {ex.Message}"); + } + catch (RequestFailedException ex) when (ex.Status == 429) // TooManyRequests + { + Console.WriteLine("Rate limit exceeded. Please wait before making more requests."); + Console.WriteLine($"Error: {ex.Message}"); + } + catch (RequestFailedException ex) + { + Console.WriteLine($"Request failed with status code: {ex.Status}"); + Console.WriteLine($"Error: {ex.Message}"); + } + catch (FileNotFoundException) + { + Console.WriteLine($"Audio file not found: {audioFilePath}"); + } + catch (IOException ex) + { + Console.WriteLine($"Error reading audio file: {ex.Message}"); + } + #endregion Snippet:HandleTranscriptionErrors + } + + /// + /// Demonstrates handling errors with detailed diagnostics. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task HandleErrorsWithDiagnostics() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:HandleErrorsWithDiagnostics + string audioFilePath = "path/to/audio.wav"; + + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription completed successfully."); + } + catch (RequestFailedException ex) + { + // Access detailed error information + Console.WriteLine("Transcription request failed:"); + Console.WriteLine($" Status Code: {ex.Status}"); + Console.WriteLine($" Error Code: {ex.ErrorCode}"); + Console.WriteLine($" Message: {ex.Message}"); + + if (ex.InnerException != null) + { + Console.WriteLine($" Inner Exception: {ex.InnerException.Message}"); + } + + // Log the full exception for debugging + Console.WriteLine($"\nFull Exception Details:\n{ex}"); + } + #endregion Snippet:HandleErrorsWithDiagnostics + } + + /// + /// Demonstrates retry logic for transient errors. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task RetryOnTransientErrors() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:RetryOnTransientErrors + string audioFilePath = "path/to/audio.wav"; + int maxRetries = 3; + int retryDelayMilliseconds = 1000; + + for (int attempt = 1; attempt <= maxRetries; attempt++) + { + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription successful!"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"Text: {channelPhrases.Text}"); + break; // Success - exit retry loop + } + catch (RequestFailedException ex) when ( + ex.Status == 429 || // TooManyRequests + ex.Status == (int)HttpStatusCode.ServiceUnavailable || + ex.Status == (int)HttpStatusCode.GatewayTimeout) + { + if (attempt < maxRetries) + { + Console.WriteLine($"Transient error occurred. Retrying in {retryDelayMilliseconds}ms..."); + Console.WriteLine($" Attempt {attempt} of {maxRetries}"); + Console.WriteLine($" Error: {ex.Message}"); + + await Task.Delay(retryDelayMilliseconds); + retryDelayMilliseconds *= 2; // Exponential backoff + } + else + { + Console.WriteLine($"Failed after {maxRetries} attempts."); + throw; + } + } + } + #endregion Snippet:RetryOnTransientErrors + } + + /// + /// Demonstrates handling cancellation gracefully. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task HandleCancellation() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:HandleCancellation + string audioFilePath = "path/to/audio.wav"; + + // Create a cancellation token source with a timeout + using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); + + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + // Pass the cancellation token to the transcription request + Response response = await client.TranscribeAsync( + request, + cancellationToken: cts.Token); + + TranscriptionResult result = response.Value; + Console.WriteLine("Transcription completed successfully."); + } + catch (OperationCanceledException) + { + Console.WriteLine("Transcription operation was cancelled or timed out."); + } + catch (RequestFailedException ex) + { + Console.WriteLine($"Transcription failed: {ex.Message}"); + } + #endregion Snippet:HandleCancellation + } + + /// + /// Validates audio file before transcription. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task ValidateAudioFile() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:ValidateAudioFile + string audioFilePath = "path/to/audio.wav"; + + // Validate the audio file before sending + if (!File.Exists(audioFilePath)) + { + Console.WriteLine($"Error: Audio file not found at '{audioFilePath}'"); + return; + } + + FileInfo fileInfo = new FileInfo(audioFilePath); + + // Check file size (maximum 250 MB) + const long maxFileSizeBytes = 250 * 1024 * 1024; + if (fileInfo.Length > maxFileSizeBytes) + { + Console.WriteLine($"Error: Audio file is too large ({fileInfo.Length / 1024 / 1024} MB). Maximum size is 250 MB."); + return; + } + + if (fileInfo.Length == 0) + { + Console.WriteLine("Error: Audio file is empty."); + return; + } + + // Check file extension (common audio formats) + string[] supportedExtensions = { ".wav", ".mp3", ".ogg", ".flac", ".opus" }; + string extension = fileInfo.Extension.ToLowerInvariant(); + if (!Array.Exists(supportedExtensions, ext => ext == extension)) + { + Console.WriteLine($"Warning: File extension '{extension}' may not be supported."); + Console.WriteLine($"Supported formats include: {string.Join(", ", supportedExtensions)}"); + } + + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Transcription successful. Duration: {result.Duration}"); + } + catch (RequestFailedException ex) + { + Console.WriteLine($"Transcription failed: {ex.Message}"); + } + #endregion Snippet:ValidateAudioFile + } + + /// + /// Demonstrates handling errors when transcription options are invalid. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task HandleInvalidOptionsErrors() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:HandleInvalidOptionsErrors + string audioFilePath = "path/to/audio.wav"; + + try + { + using FileStream audioStream = File.OpenRead(audioFilePath); + + TranscriptionOptions options = new TranscriptionOptions + { + DiarizationOptions = new TranscriptionDiarizationOptions + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 50 // Invalid: must be between 2 and 35 + } + }; + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream, + Options = options + }; + + Response response = await client.TranscribeAsync(request); + } + catch (RequestFailedException ex) when (ex.Status == (int)HttpStatusCode.BadRequest) + { + Console.WriteLine("Invalid transcription options:"); + Console.WriteLine($" {ex.Message}"); + Console.WriteLine("\nPlease check:"); + Console.WriteLine(" - MaxSpeakers should be between 2 and 35"); + Console.WriteLine(" - Locale codes are valid (e.g., 'en-US', 'es-ES')"); + Console.WriteLine(" - Custom model endpoints are accessible"); + } + #endregion Snippet:HandleInvalidOptionsErrors + } + } +} \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample04_RemoteFileTranscription.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample04_RemoteFileTranscription.cs new file mode 100644 index 000000000000..06703ea5b79e --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample04_RemoteFileTranscription.cs @@ -0,0 +1,311 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using Azure.Core; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples demonstrating transcription of remote audio files. + /// + public partial class Sample04_RemoteFileTranscription : TranscriptionSampleBase + { + /// + /// Transcribes an audio file from a public URL. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeFromUrl() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeFromUrl + // Specify the URL of the audio file to transcribe + Uri audioUrl = new Uri("https://example.com/audio/sample.wav"); + + // Configure transcription to use the remote URL + TranscriptionOptions options = new TranscriptionOptions(audioUrl); + + // No audio stream needed - the service fetches the file from the URL + Response response = await client.TranscribeAsync(options); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Transcribed audio from URL: {audioUrl}"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"\nTranscription:\n{channelPhrases.Text}"); + #endregion Snippet:TranscribeFromUrl + } + + /// + /// Downloads and transcribes a remote audio file. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeFromHttpStream() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeFromHttpStream + // Download the audio file from a remote location + string audioUrl = "https://example.com/audio/sample.wav"; + + using HttpClient httpClient = new HttpClient(); + using HttpResponseMessage httpResponse = await httpClient.GetAsync(audioUrl); + httpResponse.EnsureSuccessStatusCode(); + + // Get the audio stream from the HTTP response + using Stream audioStream = await httpResponse.Content.ReadAsStreamAsync(); + + // Create transcription request with the downloaded stream + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Downloaded and transcribed audio from: {audioUrl}"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"[{phrase.Offset}] {phrase.Text}"); + } + #endregion Snippet:TranscribeFromHttpStream + } + + /// + /// Transcribes audio from Azure Blob Storage using a SAS URL. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeFromBlobStorage() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeFromBlobStorage + // Azure Blob Storage URL with SAS token for access + Uri blobSasUrl = new Uri( + "https://mystorageaccount.blob.core.windows.net/audio-files/recording.wav?sv=2021-06-08&st=..."); + + TranscriptionOptions options = new TranscriptionOptions(blobSasUrl); + + Response response = await client.TranscribeAsync(options); + TranscriptionResult result = response.Value; + + Console.WriteLine($"Transcribed audio from Azure Blob Storage"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"\nFull Transcription:\n{channelPhrases.Text}"); + #endregion Snippet:TranscribeFromBlobStorage + } + + /// + /// Transcribes remote audio with specific locale and options. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeRemoteFileWithOptions() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeRemoteFileWithOptions + Uri audioUrl = new Uri("https://example.com/audio/spanish-interview.mp3"); + + // Configure transcription options for remote audio + TranscriptionOptions options = new TranscriptionOptions(audioUrl) + { + ProfanityFilterMode = ProfanityFilterMode.Masked, + DiarizationOptions = new TranscriptionDiarizationOptions + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 2 + } + }; + + // Add Spanish locale + options.Locales.Add("es-ES"); + + Response response = await client.TranscribeAsync(options); + TranscriptionResult result = response.Value; + + Console.WriteLine("Remote transcription with options:"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + foreach (TranscribedPhrase phrase in channelPhrases.Phrases) + { + Console.WriteLine($"Speaker {phrase.Speaker}: {phrase.Text}"); + } + #endregion Snippet:TranscribeRemoteFileWithOptions + } + + /// + /// Demonstrates fallback from URL to stream when URL is not accessible. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeWithUrlFallback() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeWithUrlFallback + string audioUrlString = "https://example.com/audio/sample.wav"; + Uri audioUrl = new Uri(audioUrlString); + + try + { + // First, try to transcribe directly from URL + TranscriptionOptions options = new TranscriptionOptions(audioUrl); + + Response response = await client.TranscribeAsync(options); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcribed directly from URL"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine(channelPhrases.Text); + } + catch (RequestFailedException ex) when (ex.Status == 400) + { + // If URL method fails, fall back to downloading and streaming + Console.WriteLine("URL access failed, downloading file..."); + + using HttpClient httpClient = new HttpClient(); + using HttpResponseMessage httpResponse = await httpClient.GetAsync(audioUrlString); + httpResponse.EnsureSuccessStatusCode(); + + using Stream audioStream = await httpResponse.Content.ReadAsStreamAsync(); + + TranscriptionContent streamRequest = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(streamRequest); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcribed from downloaded stream"); + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine(channelPhrases.Text); + } + #endregion Snippet:TranscribeWithUrlFallback + } + + /// + /// Transcribes multiple remote files in parallel. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeMultipleRemoteFiles() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeMultipleRemoteFiles + // List of audio files to transcribe + Uri[] audioUrls = new[] + { + new Uri("https://example.com/audio/file1.wav"), + new Uri("https://example.com/audio/file2.wav"), + new Uri("https://example.com/audio/file3.wav") + }; + + // Create tasks for parallel transcription + Task>[] transcriptionTasks = audioUrls + .Select(url => + { + TranscriptionOptions options = new TranscriptionOptions(url); + + return client.TranscribeAsync(options); + }) + .ToArray(); + + // Wait for all transcriptions to complete + Response[] responses = await Task.WhenAll(transcriptionTasks); + + // Process results + for (int i = 0; i < responses.Length; i++) + { + TranscriptionResult result = responses[i].Value; + Console.WriteLine($"\nFile {i + 1} ({audioUrls[i]}):"); + Console.WriteLine($"Duration: {result.Duration}"); + + var channelPhrases = result.PhrasesByChannel.First(); + Console.WriteLine($"Text: {channelPhrases.Text}"); + } + #endregion Snippet:TranscribeMultipleRemoteFiles + } + + /// + /// Demonstrates streaming transcription of a large remote file with progress tracking. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task TranscribeRemoteFileWithProgress() + { + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("your-api-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential); + + #region Snippet:TranscribeRemoteFileWithProgress + string audioUrlString = "https://example.com/audio/large-file.wav"; + + Console.WriteLine("Downloading audio file..."); + + using HttpClient httpClient = new HttpClient(); + using HttpResponseMessage httpResponse = await httpClient.GetAsync( + audioUrlString, + HttpCompletionOption.ResponseHeadersRead); + + httpResponse.EnsureSuccessStatusCode(); + + long? totalBytes = httpResponse.Content.Headers.ContentLength; + if (totalBytes.HasValue) + { + Console.WriteLine($"File size: {totalBytes.Value / 1024 / 1024} MB"); + } + + using Stream audioStream = await httpResponse.Content.ReadAsStreamAsync(); + + Console.WriteLine("Starting transcription..."); + + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + TranscriptionResult result = response.Value; + + Console.WriteLine("Transcription complete!"); + Console.WriteLine($"Audio duration: {result.Duration}"); + + var channelPhrases = Enumerable.First(result.PhrasesByChannel); + Console.WriteLine($"Phrases transcribed: {Enumerable.Count(channelPhrases.Phrases)}"); + Console.WriteLine($"\nTranscription:\n{channelPhrases.Text}"); + #endregion Snippet:TranscribeRemoteFileWithProgress + } + } +} \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample05_MockClient.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample05_MockClient.cs new file mode 100644 index 000000000000..e7aa9f721cb7 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/Sample05_MockClient.cs @@ -0,0 +1,203 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples demonstrating how to mock TranscriptionClient for unit testing. + /// + public partial class Sample05_MockClient : TranscriptionSampleBase + { + /// + /// Demonstrates creating a mock TranscriptionClient for testing purposes. + /// This allows testing application logic without making actual API calls to Azure. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public void CreateMockClient() + { + #region Snippet:CreateMockTranscriptionClient + // TranscriptionClient provides a protected constructor for mocking + // You can create a derived class for testing purposes + + // Example: Create a test-specific derived class + var mockClient = new MockTranscriptionClient(); + + // Use the mock client in your tests + // It won't make actual API calls + #endregion Snippet:CreateMockTranscriptionClient + } + + /// + /// Demonstrates mocking TranscriptionClient behavior for unit tests. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task MockTranscriptionBehavior() + { + #region Snippet:MockTranscriptionBehavior + // Create a mock client that returns predefined results + var mockClient = new MockTranscriptionClient(); + + // Configure the mock to return a specific result + var expectedText = "This is a mock transcription result"; + mockClient.SetMockResult(expectedText); + + // Create a test request + using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + // Call the mock client + Response response = await mockClient.TranscribeAsync(request); + + // Verify the result + Assert.IsNotNull(response); + Assert.IsNotNull(response.Value); + + // The mock client returns the configured result + var phrases = response.Value.PhrasesByChannel.FirstOrDefault(); + if (phrases != null) + { + Console.WriteLine($"Mock transcription: {phrases.Text}"); + } + #endregion Snippet:MockTranscriptionBehavior + } + + /// + /// Demonstrates using InMemoryTransport for testing without network calls. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task UseInMemoryTransport() + { + #region Snippet:UseInMemoryTransport + // Create a mock response that the client will return + var mockResponseContent = @"{ + ""durationMilliseconds"": 5000, + ""combinedPhrases"": [ + { + ""channel"": 0, + ""text"": ""This is a test transcription"" + } + ], + ""phrases"": [ + { + ""channel"": 0, + ""offsetMilliseconds"": 0, + ""durationMilliseconds"": 5000, + ""text"": ""This is a test transcription"", + ""words"": [], + ""locale"": ""en-US"", + ""confidence"": 0.95 + } + ] + }"; + + // Create options with a mock transport + var mockTransport = new MockTransport(new MockResponse(200) + { + ContentStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(mockResponseContent)) + }); + + TranscriptionClientOptions options = new TranscriptionClientOptions(); + options.Transport = mockTransport; + + // Create client with mock transport + Uri endpoint = new Uri("https://mock.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("mock-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + + // Make a request - it will use the mock response + using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + Response response = await client.TranscribeAsync(request); + + // Verify the mock response was returned + Console.WriteLine($"Duration: {response.Value.Duration}"); + var phrases = response.Value.PhrasesByChannel.First(); + Console.WriteLine($"Transcription: {phrases.Text}"); + #endregion Snippet:UseInMemoryTransport + } + + /// + /// Demonstrates testing error scenarios with mock responses. + /// + [Test] + [Ignore("Only validating compilation of examples")] + public async Task MockErrorScenarios() + { + #region Snippet:MockErrorScenarios + // Create a mock transport that returns an error + var mockTransport = new MockTransport(new MockResponse(401) + { + ContentStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes( + @"{""error"": {""code"": ""Unauthorized"", ""message"": ""Invalid API key""}}" + )) + }); + + TranscriptionClientOptions options = new TranscriptionClientOptions(); + options.Transport = mockTransport; + + Uri endpoint = new Uri("https://mock.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new AzureKeyCredential("invalid-key"); + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + + // Test error handling + try + { + using var audioStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x02 }); + TranscriptionContent request = new TranscriptionContent + { + Audio = audioStream + }; + + await client.TranscribeAsync(request); + Assert.Fail("Expected RequestFailedException was not thrown"); + } + catch (RequestFailedException ex) + { + // Verify error handling works correctly + Assert.AreEqual(401, ex.Status); + Console.WriteLine($"Successfully caught error: {ex.Message}"); + } + #endregion Snippet:MockErrorScenarios + } + + /// + /// Example mock implementation for testing. + /// + private class MockTranscriptionClient : TranscriptionClient + { + private string _mockResultText = "Mock transcription"; + + public MockTranscriptionClient() : base() + { + } + + public void SetMockResult(string text) + { + _mockResultText = text; + } + + // Override methods to return mock results + // Note: In a real implementation, you would override the actual methods + // This is just an illustration of the pattern + } + } +} \ No newline at end of file diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/SampleSnippets.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/SampleSnippets.cs new file mode 100644 index 000000000000..b3fcc291d7ae --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/SampleSnippets.cs @@ -0,0 +1,313 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Samples that are used in the associated README.md file. + /// + public partial class SampleSnippets : SamplesBase + { + [Test] + public void CreateClientForSpecificApiVersion() + { +#if !SNIPPET + var endpoint = TestEnvironment.Endpoint; + var credential = TestEnvironment.Credential; +#endif + #region Snippet:CreateTranscriptionClientForSpecificApiVersion +#if SNIPPET + Uri endpoint = new Uri("https://myaccount.api.cognitive.microsoft.com/"); + AzureKeyCredential credential = new("your apikey"); +#endif + TranscriptionClientOptions options = new TranscriptionClientOptions(TranscriptionClientOptions.ServiceVersion.V2025_10_15); + TranscriptionClient client = new TranscriptionClient(endpoint, credential, options); + #endregion Snippet:CreateTranscriptionClientForSpecificApiVersion + } + + [Test] + public void TranscribeLocalFileSync() + { + #region Snippet:TranscribeLocalFileSync +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var request = new TranscriptionContent { Audio = fileStream }; + var response = client.Transcribe(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeLocalFileSync + } + + [Test] + public async Task TranscribeLocalFileAsync() + { + #region Snippet:TranscribeLocalFileAsync +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var request = new TranscriptionContent { Audio = fileStream }; + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeLocalFileAsync + } + + [Test] + public void TranscribeRemoteFileSync() + { + #region Snippet:TranscribeRemoteFileSync +#if SNIPPET + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + var client = CreateTestClient(); +#endif + using HttpClient httpClient = new HttpClient(); + using HttpResponseMessage httpResponse = httpClient.GetAsync("https://your-domain.com/your-file.mp3").Result; + using Stream stream = httpResponse.Content.ReadAsStreamAsync().Result; + + var request = new TranscriptionContent { Audio = stream }; + var response = client.Transcribe(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + #endregion Snippet:TranscribeRemoteFileSync + } + + [Test] + public async Task TranscribeRemoteFileAsync() + { + #region Snippet:TranscribeRemoteFileAsync +#if SNIPPET + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + var client = CreateTestClient(); +#endif + using HttpClient httpClient = new HttpClient(); + using HttpResponseMessage httpResponse = await httpClient.GetAsync("https://your-domain.com/your-file.mp3"); + using Stream stream = await httpResponse.Content.ReadAsStreamAsync(); + + var request = new TranscriptionContent { Audio = stream }; + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + #endregion Snippet:TranscribeRemoteFileAsync + } + + [Test] + public async Task TranscribeWithLocales() + { + #region Snippet:TranscribeWithLocales +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var options = new TranscriptionOptions(); + options.Locales.Add("en-US"); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeWithLocales + } + + [Test] + public async Task TranscribeWithModels() + { + #region Snippet:TranscribeWithModels +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var options = new TranscriptionOptions(); + options.Models.Add("en-US", new Uri("https://myaccount.api.cognitive.microsoft.com/speechtotext/models/your-model-uuid")); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeWithModels + } + + [Test] + public async Task TranscribeWithProfanityFilter() + { + #region Snippet:TranscribeWithProfinityFilter +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var options = new TranscriptionOptions(); + options.ProfanityFilterMode = ProfanityFilterMode.Masked; + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeWithProfinityFilter + } + + [Test] + public async Task TranscribeWithActiveChannels() + { + #region Snippet:TranscribeWithActiveChannels +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var options = new TranscriptionOptions(); + options.ActiveChannels.Add(0); + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration}: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeWithActiveChannels + } + + [Test] + public async Task TranscribeWithDiarization() + { + #region Snippet:TranscribeWithDiarization +#if SNIPPET + string filePath = "path/to/audio.wav"; + TranscriptionClient client = new TranscriptionClient(new Uri("https://myaccount.api.cognitive.microsoft.com/"), new AzureKeyCredential("your apikey")); +#else + string filePath = "path/to/audio.wav"; + var client = CreateTestClient(); +#endif + using (FileStream fileStream = File.Open(filePath, FileMode.Open)) + { + var options = new TranscriptionOptions() + { + DiarizationOptions = new() + { + // Enabled is automatically set to true when MaxSpeakers is specified + MaxSpeakers = 2 + } + }; + + var request = new TranscriptionContent + { + Audio = fileStream, + Options = options + }; + + var response = await client.TranscribeAsync(request); + + Console.WriteLine($"File Duration: {response.Value.Duration}"); + foreach (var phrase in response.Value.PhrasesByChannel.First().Phrases) + { + Console.WriteLine($"{phrase.Offset}-{phrase.Offset+phrase.Duration} [{phrase.Speaker}]: {phrase.Text}"); + } + } + #endregion Snippet:TranscribeWithDiarization + } + +#if !SNIPPET + private TranscriptionClient CreateTestClient() + { + return new TranscriptionClient(TestEnvironment.Endpoint, TestEnvironment.Credential); + } +#endif + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/TranscriptionSampleBase.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/TranscriptionSampleBase.cs new file mode 100644 index 000000000000..eec329a170f5 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/Samples/TranscriptionSampleBase.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Azure.Core; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Base class for transcription samples, providing helper methods for client creation. + /// + public class TranscriptionSampleBase : SamplesBase + { + /// + /// Creates an authenticated TranscriptionClient using API key authentication. + /// + protected TranscriptionClient CreateTranscriptionClient() + { + #region Snippet:CreateTranscriptionClientAuth + string endpoint = Environment.GetEnvironmentVariable("TRANSCRIPTION_ENDPOINT"); + string key = Environment.GetEnvironmentVariable("TRANSCRIPTION_API_KEY"); + +#if !SNIPPET + endpoint = TestEnvironment.Endpoint.ToString(); + key = TestEnvironment.Credential.Key; +#endif + + // Create a Transcription client + TranscriptionClient client = new TranscriptionClient( + new Uri(endpoint), + new AzureKeyCredential(key)); + + #endregion Snippet:CreateTranscriptionClientAuth + + return client; + } + + /// + /// Creates an authenticated TranscriptionClient with custom options. + /// + protected TranscriptionClient CreateTranscriptionClient(TranscriptionClientOptions options) + { + string endpoint = TestEnvironment.Endpoint.ToString(); + string key = TestEnvironment.Credential.Key; + + return new TranscriptionClient(new Uri(endpoint), new AzureKeyCredential(key), options); + } + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/TranscriptionClientTestEnvironment.cs b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/TranscriptionClientTestEnvironment.cs new file mode 100644 index 000000000000..effd6e464f39 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tests/TranscriptionClientTestEnvironment.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Azure.Core; +using Azure.Core.TestFramework; + +namespace Azure.AI.Speech.Transcription.Samples +{ + /// + /// Test environment for Transcription samples and tests. + /// + public class TranscriptionClientTestEnvironment : TestEnvironment + { + /// + /// Gets the endpoint for the Speech Transcription service. + /// + public Uri Endpoint => new Uri(GetRecordedVariable("TRANSCRIPTION_ENDPOINT", options => options.HasSecretConnectionStringParameter("endpoint"))); + + /// + /// Gets the API key credential for authentication. + /// + public new AzureKeyCredential Credential => new AzureKeyCredential( + GetRecordedVariable("TRANSCRIPTION_API_KEY", options => options.IsSecret())); + } +} diff --git a/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tsp-location.yaml b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tsp-location.yaml new file mode 100644 index 000000000000..d1a362b02095 --- /dev/null +++ b/sdk/cognitiveservices/Azure.AI.Speech.Transcription/tsp-location.yaml @@ -0,0 +1,5 @@ +directory: specification/cognitiveservices/Speech.Transcription +commit: 0a6b217899cb0279853604ae0f48557c4c1651a1 +repo: Azure/azure-rest-api-specs +additionalDirectories: [] +