forked from sipsorcery-org/sipsorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·103 lines (92 loc) · 4.06 KB
/
Program.cs
File metadata and controls
executable file
·103 lines (92 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: An example program to send text-to-speech requests to the
// Azure Cognitive Speech Services API and save the results to PCM files.
//
// Update 4 Feb 2025: You need an "Azure AI services | Speech service" resource
// for this demo. Once the resource is set up, you can get the subscription key
// and region from the Overiew page on the Azure portal.
//
// References:
// https://learn.microsoft.com/en-us/azure/ai-services/speech-service/overview
//
// Notes:
// The audio format returned by the Azure Speech Service is:
// 16 bit signed PCM at 16KHz
//
// To convert to mp3 format using ffmpeg use:
// ffmpeg -f s16le -ar 16k -i 637267813207431881.pcm16k out.mp3
//
// To convert to a PCM format suitable for the PlaySounds SIP example
// program use:
//ffmpeg -f s16le -ar 16k -ac 1 -i 637267813207431881.pcm16k -f s16le -ac 1 -ar 8k out.raw
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 03 Jun 2020 Aaron Clauson Created, Dublin, Ireland.
// 04 Feb 2025 Aaron Clauson Checked still working with net9.0.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
namespace demo;
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Text to PCM Console:");
if (args == null || args.Length < 3)
{
Console.WriteLine("Usage: texttopcm <azure subscription key> <azure region> <text>");
Console.WriteLine("e.g. texttopcm cb965... westeurope \"Hello World\"");
Console.WriteLine("e.g. dotnet run -- cb965... westeurope \"Hello World\"");
}
else
{
var speechConfig = SpeechConfig.FromSubscription(args[0], args[1]);
string text = args[2];
TextToSpeechStream ttsOutStream = new TextToSpeechStream();
AudioConfig audioTtsConfig = AudioConfig.FromStreamOutput(ttsOutStream);
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig, audioTtsConfig);
using (var result = await speechSynthesizer.SpeakTextAsync(text))
{
if (result.Reason == ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
var buffer = ttsOutStream.GetPcmBuffer();
string saveFilename = $"{DateTime.Now.Ticks}.pcm16k";
using (StreamWriter sw = new StreamWriter(saveFilename))
{
for(int i=0; i<buffer.Length; i++)
{
sw.BaseStream.Write(BitConverter.GetBytes(buffer[i]));
}
}
Console.WriteLine($"Result saved to {saveFilename}.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
Console.WriteLine($"Speech synthesizer failed was cancelled, reason={cancellation.Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"Speech synthesizer cancelled: ErrorCode={cancellation.ErrorCode}");
Console.WriteLine($"Speech synthesizer cancelled: ErrorDetails=[{cancellation.ErrorDetails}]");
}
}
else
{
Console.WriteLine($"Speech synthesizer failed with result {result.Reason} for text [{text}].");
}
}
}
}
}