openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Audio/GenerateSpeechTests.cs

92lines · modecode

1using NUnit.Framework;
2using OpenAI.Audio;
3using OpenAI.Tests.Utility;
4using System;
5using System.Text;
6using System.Threading.Tasks;
7using static OpenAI.Tests.TestHelpers;
8
9namespace OpenAI.Tests.Audio;
10
11[TestFixture(true)]
12[TestFixture(false)]
13[Parallelizable(ParallelScope.All)]
14[Category("Audio")]
15public 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