-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsBackgroundService.cs
More file actions
264 lines (219 loc) · 12.2 KB
/
WindowsBackgroundService.cs
File metadata and controls
264 lines (219 loc) · 12.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NAudio.Wave;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace STTCloudService
{
public class WindowsBackgroundService : BackgroundService
{
private readonly ILogger<WindowsBackgroundService> _logger;
private static string STTKey;
private static string STTRegion;
private static string STTSourceLanguage;
private static string InputFolder;
private static string OutputFolder;
private static string ErrorFolder;
private static string ChannelsSplitFolder;
private static string SingleTxtTranslationTargetFile;
private static SpeechConfig speechConfig;
public WindowsBackgroundService(ILogger<WindowsBackgroundService> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Starting STT service, made in Italy by Giulio Fronterotta");
//Inject the configuration file in memory
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true);
var config = builder.Build();
//Extract values from Application Configuration file
STTKey = config["AzureSTTService:Key"];
STTRegion = config["AzureSTTService:Region"];
STTSourceLanguage = config["AzureSTTService:SourceLanguage"];
InputFolder = config["Folders:InputFolder"];
OutputFolder = config["Folders:OutputFolder"];
ErrorFolder = config["Folders:ErrorFolder"];
ChannelsSplitFolder = config["Folders:ChannelsSplitFolder"];
SingleTxtTranslationTargetFile = config["Folders:SingleTxtTranslationTargetFile"];
//Create directories if they not exits
Directory.CreateDirectory(InputFolder);
Directory.CreateDirectory(OutputFolder);
Directory.CreateDirectory(ErrorFolder);
Directory.CreateDirectory(ChannelsSplitFolder);
speechConfig = SpeechConfig.FromSubscription(STTKey, STTRegion);
_logger.LogInformation("STT Service Started");
while (!stoppingToken.IsCancellationRequested)
{
try
{
//Search Wav Files
var WavFileList = Directory
.EnumerateFiles(InputFolder, "*.wav")
.OrderByDescending(x => File.GetCreationTime(x));
//Log if any file found
if (WavFileList.Count() > 0)
{
string FileListWithLineFeed = String.Join("\r\n", WavFileList);
_logger.LogInformation(
String.Format("Found {0} file(s) to process, from oldest to newest\r\n{1}",
WavFileList.Count(),
FileListWithLineFeed)
);
_logger.LogInformation("Batch file processing started");
//Loop through file for processing
foreach (var wavFile in WavFileList)
{
await ProcessFileAsync(wavFile, STTSourceLanguage, OutputFolder, ErrorFolder, ChannelsSplitFolder, SingleTxtTranslationTargetFile);
}
_logger.LogInformation("Batch file processing finished");
}
}
catch (Exception exc)
{
_logger.LogError(exc.ToString());
throw;
}
//Set Polling Time
await Task.Delay(5000, stoppingToken);
}
_logger.LogInformation("Service Stopped");
}
private async Task ProcessFileAsync(string wavFile,string sourceLanguage, string destDir, string errorDir,string channelSplitFolder,string singleTxtTranslationTargetFile)
{
_logger.LogInformation(String.Format("Detected new file {0}", wavFile));
string sourcePath = wavFile;
string prefixTimestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_");
string destWavPath = Path.Combine(destDir, prefixTimestamp + Path.GetFileName(wavFile));
string destSttTxtPath = Path.Combine(destDir, prefixTimestamp + Path.GetFileNameWithoutExtension(wavFile) + ".txt");
string destErrorFilePath = Path.Combine(errorDir, prefixTimestamp + Path.GetFileName(wavFile));
Boolean channelSplit = false;
Boolean sampleRateConversion = false;
using (var reader = new WaveFileReader(sourcePath)) {
//Separate Channels
_logger.LogInformation(String.Format($"Found {reader.WaveFormat.Channels} channels"));
_logger.LogInformation(String.Format($"Bits Per Sample: {reader.WaveFormat.BitsPerSample}"));
_logger.LogInformation(String.Format($"Sample Rate: {reader.WaveFormat.SampleRate}"));
//Clean Audio Processing directory
string[] filePaths = Directory.GetFiles(channelSplitFolder);
foreach (string filePath in filePaths)
File.Delete(filePath);
//Split only multiple channels files (more than 2 stereo channels)
if (reader.WaveFormat.Channels > 2)
{
//Tag bool to notify split
channelSplit = true;
var writers = new WaveFileWriter[reader.WaveFormat.Channels];
for (int n = 0; n < writers.Length; n++)
{
var format = new WaveFormat(reader.WaveFormat.SampleRate, 16, 1);
string combinedPath = Path.Combine(channelSplitFolder, string.Format($"channel-{n + 1}.wav"));
writers[n] = new WaveFileWriter(combinedPath, format);
}
float[] buffer;
while ((buffer = reader.ReadNextSampleFrame())?.Length > 0)
{
for (int i = 0; i < buffer.Length; i++)
{
// write one sample for each channel (i is the channelNumber)
writers[i].WriteSample(buffer[i]);
}
}
for (int n = 0; n < writers.Length; n++)
{
writers[n].Dispose();
}
//reader.Dispose();
}
//Check if I need to downsample Files
if (reader.WaveFormat.SampleRate > 16000)
{
sampleRateConversion = true;
if (channelSplit == true)
{
//If a previous channel split has been performed...
foreach (string file in Directory.EnumerateFiles(channelSplitFolder))
{
//Convert file format with NAudio
//https://stackoverflow.com/questions/6647730/change-wav-file-to-16khz-and-8bit-with-using-naudio
string convertedStreamFilename = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + "-16KHz.wav");
using (var wavConverterReader = new WaveFileReader(file))
{
var newFormat = new WaveFormat(16000, 16, 1);
using (var conversionStream = new WaveFormatConversionStream(newFormat, wavConverterReader))
{
//Replace original file
WaveFileWriter.CreateWaveFile(convertedStreamFilename, conversionStream);
}
}
}
}
else
{
//Else I need to convert the rate of a single file
string convertedStreamFilename = Path.Combine(Path.GetDirectoryName(sourcePath), "channel-1-16KHz.wav");
using (var wavConverterReader = new WaveFileReader(sourcePath))
{
var newFormat = new WaveFormat(16000, 16, 1);
using (var conversionStream = new WaveFormatConversionStream(newFormat, wavConverterReader))
{
//Replace original file
WaveFileWriter.CreateWaveFile(convertedStreamFilename, conversionStream);
}
}
}
}
}
//This will be changed depending on type of file conversions required
string STTSourcePath = wavFile;
//Source path to submit to cloud is selected, depending on transformation previously performed.
if (channelSplit == true && sampleRateConversion == true) STTSourcePath = Path.Combine(channelSplitFolder, string.Format("channel-1-16KHz.wav"));
if (channelSplit == true && sampleRateConversion == false) STTSourcePath = Path.Combine(channelSplitFolder, string.Format("channel-1.wav"));
if (channelSplit == false && sampleRateConversion == true) STTSourcePath = Path.Combine(channelSplitFolder, string.Format("channel-1-16KHz.wav")); ;
if (channelSplit == false && sampleRateConversion == false) STTSourcePath = sourcePath;
//Perform STT
using AudioConfig audioConfig = AudioConfig.FromWavFileInput(STTSourcePath);
speechConfig.SpeechRecognitionLanguage = sourceLanguage;
speechConfig.EnableAudioLogging();
speechConfig.SetProperty(PropertyId.Conversation_Initial_Silence_Timeout, "5");
ResultReason STTResult;
using (var recognizer = new SpeechRecognizer(speechConfig, audioConfig))
{
_logger.LogInformation(String.Format("Sending file {0} to Azure STT Service", Path.GetFileName(STTSourcePath)));
//Do the real STT Job on cloud
var result = await recognizer.RecognizeOnceAsync();
STTResult = result.Reason;
if (result.Reason == ResultReason.RecognizedSpeech) {
_logger.LogInformation("Moving file from {0}\r\nto {1}", sourcePath, destWavPath);
_logger.LogInformation($"RECOGNIZED STT: Text={result.Text}");
//Dumping Json Response file to History Directory
await File.WriteAllTextAsync(destSttTxtPath, result.Text);
//Dumping Json Response file to fixed destination directory (This one is not really required, but introduced for a specific need)
await File.WriteAllTextAsync(singleTxtTranslationTargetFile, result.Text);
}
//Avoid File locking errors
recognizer.Dispose();
}
if (STTResult == ResultReason.RecognizedSpeech) {
//Move File to destination
File.Move(sourcePath, destWavPath);
_logger.LogInformation("File Moved");
}
else
{
_logger.LogError(String.Format("Error Processing {0} with Azure STT Service.\r\n ERROR:{1}", Path.GetFileName(wavFile), STTResult));
//Encountered Errors
//Move File to error Folders
File.Move(sourcePath, destErrorFilePath);
_logger.LogError(String.Format("Error Processing {0} with Azure STT Service.\r\n ERROR:{1}", Path.GetFileName(wavFile), STTResult));
}
}
}
}