openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Audio/GenerateSpeechTests.cs
86lines · modecode
| 1 | using Microsoft.ClientModel.TestFramework; |
| 2 | using NUnit.Framework; |
| 3 | using OpenAI.Audio; |
| 4 | using OpenAI.Tests.Utility; |
| 5 | using System; |
| 6 | using System.Text; |
| 7 | using System.Threading.Tasks; |
| 8 | using static OpenAI.Tests.TestHelpers; |
| 9 | |
| 10 | namespace OpenAI.Tests.Audio; |
| 11 | |
| 12 | [Category("Audio")] |
| 13 | public partial class GenerateSpeechTests : OpenAIRecordedTestBase |
| 14 | { |
| 15 | public GenerateSpeechTests(bool isAsync) : base(isAsync) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | [RecordedTest] |
| 20 | public async Task BasicTextToSpeechWorks() |
| 21 | { |
| 22 | AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_TTS); |
| 23 | |
| 24 | BinaryData audio = await client.GenerateSpeechAsync("Hello, world! This is a test.", GeneratedSpeechVoice.Shimmer); |
| 25 | |
| 26 | Assert.That(audio, Is.Not.Null); |
| 27 | await ValidateGeneratedAudio(audio, "hello"); |
| 28 | } |
| 29 | |
| 30 | [RecordedTest] |
| 31 | [TestCase(null)] |
| 32 | [TestCase("mp3")] |
| 33 | [TestCase("opus")] |
| 34 | [TestCase("aac")] |
| 35 | [TestCase("flac")] |
| 36 | [TestCase("wav")] |
| 37 | [TestCase("pcm")] |
| 38 | public async Task OutputFormatWorks(string responseFormat) |
| 39 | { |
| 40 | AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_TTS); |
| 41 | |
| 42 | SpeechGenerationOptions options = new(); |
| 43 | |
| 44 | if (!string.IsNullOrEmpty(responseFormat)) |
| 45 | { |
| 46 | options.ResponseFormat = responseFormat switch |
| 47 | { |
| 48 | "mp3" => GeneratedSpeechFormat.Mp3, |
| 49 | "opus" => GeneratedSpeechFormat.Opus, |
| 50 | "aac" => GeneratedSpeechFormat.Aac, |
| 51 | "flac" => GeneratedSpeechFormat.Flac, |
| 52 | "wav" => GeneratedSpeechFormat.Wav, |
| 53 | "pcm" => GeneratedSpeechFormat.Pcm, |
| 54 | _ => throw new ArgumentException("Invalid response format") |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | BinaryData audio = await client.GenerateSpeechAsync("Hello, world!", GeneratedSpeechVoice.Alloy, options); |
| 59 | |
| 60 | Assert.That(audio, Is.Not.Null); |
| 61 | |
| 62 | byte[] audioBytes = audio.ToArray(); |
| 63 | byte[] expectedFileHeader = responseFormat switch |
| 64 | { |
| 65 | "opus" => Encoding.ASCII.GetBytes("OggS"), |
| 66 | "flac" => Encoding.ASCII.GetBytes("fLaC"), |
| 67 | "wav" => Encoding.ASCII.GetBytes("RIFF"), |
| 68 | _ => [] |
| 69 | }; |
| 70 | |
| 71 | Assert.That(audioBytes.Length, Is.GreaterThanOrEqualTo(expectedFileHeader.Length)); |
| 72 | |
| 73 | for (int i = 0; i < expectedFileHeader.Length; i++) |
| 74 | { |
| 75 | Assert.That(audioBytes[i], Is.EqualTo(expectedFileHeader[i]), $"File header differs on byte {i}. Expected: {expectedFileHeader[i]}. Actual: {audioBytes[i]}."); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private async Task ValidateGeneratedAudio(BinaryData audio, string expectedSubstring) |
| 80 | { |
| 81 | AudioClient client = GetProxiedOpenAIClient<AudioClient>(TestScenario.Audio_Whisper); |
| 82 | AudioTranscription transcription = await client.TranscribeAudioAsync(audio.ToStream(), "hello_world.wav"); |
| 83 | |
| 84 | Assert.That(transcription.Text.ToLowerInvariant(), Contains.Substring(expectedSubstring)); |
| 85 | } |
| 86 | } |
| 87 | |