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