-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAudioManager.cs
More file actions
516 lines (435 loc) · 19.5 KB
/
AudioManager.cs
File metadata and controls
516 lines (435 loc) · 19.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using TensorStack.Common;
using TensorStack.Common.Common;
using TensorStack.Common.Tensor;
namespace TensorStack.Audio.Windows
{
public static class AudioManager
{
private static string FFMpegPath = "ffmpeg.exe";
private static string FFProbePath = "ffprobe.exe";
private static string DirectoryTemp = "Temp";
/// <summary>
/// Configures the specified ffmpeg/ffprobe path.
/// </summary>
/// <param name="ffmpegPath">The ffmpeg path.</param>
/// <param name="ffprobePath">The ffprobe path.</param>
/// <param name="directoryTemp">The directory temporary.</param>
public static void Initialize(string ffmpegPath = default, string ffprobePath = default, string directoryTemp = default)
{
if (!string.IsNullOrEmpty(ffmpegPath))
FFMpegPath = ffmpegPath;
if (!string.IsNullOrEmpty(ffprobePath))
FFProbePath = ffprobePath;
if (!string.IsNullOrEmpty(directoryTemp))
DirectoryTemp = directoryTemp;
}
/// <summary>
/// Loads the audio information.
/// </summary>
/// <param name="filename">The filename.</param>
public static AudioInfo LoadInfo(string filename)
{
return ReadInfo(filename);
}
/// <summary>
/// Loads the audio information asynchronously.
/// </summary>
/// <param name="filename">The filename.</param>
public static async Task<AudioInfo> LoadInfoAsync(string filename)
{
return await ReadInfoAsync(filename);
}
/// <summary>
/// Loads the tensor.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="audioCodec">The audio codec.</param>
/// <param name="sampleRate">The sample rate.</param>
/// <param name="channels">The channels.</param>
public static AudioTensor LoadTensor(string filename, string audioCodec = "pcm_s16le", int sampleRate = 16000, int channels = 1)
{
return ReadAudio(filename, audioCodec, sampleRate, channels);
}
/// <summary>
/// Loads the tensor asynchronously.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="audioCodec">The audio codec.</param>
/// <param name="sampleRate">The sample rate.</param>
/// <param name="channels">The channels.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static async Task<AudioTensor> LoadTensorAsync(string filename, string audioCodec = "pcm_s16le", int sampleRate = 16000, int channels = 1, CancellationToken cancellationToken = default)
{
return await ReadAudioAsync(filename, audioCodec, sampleRate, channels, cancellationToken);
}
/// <summary>
/// Saves the audio to file.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="audioTensor">The audio tensor.</param>
public static void SaveAudio(string filename, AudioTensor audioTensor)
{
WriteAudio(filename, audioTensor);
}
/// <summary>
/// Saves the audio to file asynchronously.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="audioTensor">The audio tensor.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static async Task SaveAudioAync(string filename, AudioTensor audioTensor, CancellationToken cancellationToken = default)
{
await WriteAudioAsync(filename, audioTensor, cancellationToken);
}
/// <summary>
/// Adds the audio from source video to target video.
/// </summary>
/// <param name="targetVideoFile">The target video file.</param>
/// <param name="sourceVideoFile">The source video file.</param>
public static void AddAudio(string targetVideoFile, string sourceVideoFile)
{
MuxAudio(targetVideoFile, sourceVideoFile);
}
/// <summary>
/// Adds the audio from source video to target video asynchronously.
/// </summary>
/// <param name="targetVideoFile">The target video file.</param>
/// <param name="sourceVideoFile">The source video file.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public static async Task AddAudioAsync(string targetVideoFile, string sourceVideoFile, CancellationToken cancellationToken = default)
{
await MuxAudioAsync(targetVideoFile, sourceVideoFile, cancellationToken);
}
/// <summary>
/// Reads the audio data as AudioTensor
/// </summary>
/// <param name="audioInputFile">The audio input file.</param>
/// <param name="audioCodec">The audio codec.</param>
/// <param name="sampleRate">The sample rate.</param>
/// <param name="channels">The channels.</param>
/// <returns>AudioTensor.</returns>
private static AudioTensor ReadAudio(string audioInputFile, string audioCodec, int sampleRate, int channels)
{
using (var ffmpeg = CreateReader(audioInputFile, audioCodec, sampleRate, channels))
using (var audioStream = new MemoryStream())
{
ffmpeg.Start();
ffmpeg.StandardOutput.BaseStream.CopyTo(audioStream);
ffmpeg.WaitForExit();
var audioBytes = audioStream.ToArray();
return CreateAudioTensor(audioBytes, channels, sampleRate);
}
}
/// <summary>
/// Reads the audio data as AudioTensor asynchronously.
/// </summary>
/// <param name="audioInputFile">The audio input file.</param>
/// <param name="audioCodec">The audio codec.</param>
/// <param name="sampleRate">The sample rate.</param>
/// <param name="channels">The channels.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private static async Task<AudioTensor> ReadAudioAsync(string audioInputFile, string audioCodec, int sampleRate, int channels, CancellationToken cancellationToken = default)
{
using (var ffmpeg = CreateReader(audioInputFile, audioCodec, sampleRate, channels))
using (var audioStream = new MemoryStream())
{
ffmpeg.Start();
await ffmpeg.StandardOutput.BaseStream.CopyToAsync(audioStream, cancellationToken);
await ffmpeg.WaitForExitAsync(cancellationToken);
var audioBytes = audioStream.ToArray();
return CreateAudioTensor(audioBytes, channels, sampleRate);
}
}
/// <summary>
/// Writes the AudioTensor to file.
/// </summary>
/// <param name="audioOutputFile">The audio output file.</param>
/// <param name="audioTensor">The audio tensor.</param>
private static void WriteAudio(string audioOutputFile, AudioTensor audioTensor)
{
var samples = audioTensor.Samples;
var channels = audioTensor.Channels;
var sampleRate = audioTensor.SampleRate;
using (var ffmpeg = CreateWriter(audioOutputFile, sampleRate, channels))
{
ffmpeg.Start();
using (var audioStream = ffmpeg.StandardInput.BaseStream)
{
var audioBuffer = CreateAudioBuffer(audioTensor, channels, samples);
audioStream.Write(audioBuffer);
audioStream.Flush();
}
ffmpeg.WaitForExit();
}
}
/// <summary>
/// Writes the AudioTensor to file asynchronously.
/// </summary>
/// <param name="audioOutputFile">The audio output file.</param>
/// <param name="audioTensor">The audio tensor.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private static async Task WriteAudioAsync(string audioOutputFile, AudioTensor audioTensor, CancellationToken cancellationToken = default)
{
var samples = audioTensor.Samples;
var channels = audioTensor.Channels;
var sampleRate = audioTensor.SampleRate;
using (var ffmpeg = CreateWriter(audioOutputFile, sampleRate, channels))
{
ffmpeg.Start();
using (var audioStream = ffmpeg.StandardInput.BaseStream)
{
var audioBuffer = CreateAudioBuffer(audioTensor, channels, samples);
await audioStream.WriteAsync(audioBuffer, cancellationToken);
await audioStream.FlushAsync(cancellationToken);
}
await ffmpeg.WaitForExitAsync(cancellationToken);
}
}
/// <summary>
/// Muxes the audio.
/// </summary>
/// <param name="targetVideoFile">The target video file.</param>
/// <param name="sourceVideoFile">The source video file.</param>
internal static void MuxAudio(string targetVideoFile, string sourceVideoFile)
{
var tempFile = FileHelper.RandomFileName(DirectoryTemp, targetVideoFile);
try
{
using (var ffmpeg = CreateMuxer(targetVideoFile, sourceVideoFile, tempFile))
{
ffmpeg.Start();
ffmpeg.WaitForExit();
}
if (File.Exists(tempFile))
File.Move(tempFile, targetVideoFile, true);
}
finally
{
FileHelper.DeleteFile(tempFile);
}
}
/// <summary>
/// Muxes the audio asynchronously.
/// </summary>
/// <param name="targetVideoFile">The target video file.</param>
/// <param name="sourceVideoFile">The source video file.</param>
/// <param name="cancellationToken">The cancellation token.</param>
internal static async Task MuxAudioAsync(string targetVideoFile, string sourceVideoFile, CancellationToken cancellationToken = default)
{
var tempFile = FileHelper.RandomFileName(DirectoryTemp, targetVideoFile);
try
{
using (var ffmpeg = CreateMuxer(targetVideoFile, sourceVideoFile, tempFile))
{
ffmpeg.Start();
await ffmpeg.WaitForExitAsync(cancellationToken);
}
if (File.Exists(tempFile))
File.Move(tempFile, targetVideoFile, true);
}
finally
{
FileHelper.DeleteFile(tempFile);
}
}
/// <summary>
/// Reads the information.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>AudioInfo.</returns>
internal static AudioInfo ReadInfo(string filename)
{
using (var metadataReader = CreateMetadata(filename))
{
metadataReader.Start();
var videoInfo = default(AudioInfo);
using (var reader = metadataReader.StandardOutput)
{
videoInfo = ParseInfo(filename, reader.ReadToEnd());
}
metadataReader.WaitForExit();
return videoInfo;
}
}
/// <summary>
/// Reads the information asynchronously.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="cancellationToken">The cancellation token.</param>
internal static async Task<AudioInfo> ReadInfoAsync(string filename, CancellationToken cancellationToken = default)
{
using (var metadataReader = CreateMetadata(filename))
{
metadataReader.Start();
var videoInfo = default(AudioInfo);
using (var reader = metadataReader.StandardOutput)
{
videoInfo = ParseInfo(filename, await reader.ReadToEndAsync(cancellationToken));
}
await metadataReader.WaitForExitAsync(cancellationToken);
return videoInfo;
}
}
/// <summary>
/// Parses the information.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="jsonString">The json string.</param>
/// <returns>AudioInfo.</returns>
/// <exception cref="System.Exception">Failed to parse audio stream metadata</exception>
private static AudioInfo ParseInfo(string filename, string jsonString)
{
var metadata = JsonSerializer.Deserialize<AudioMetadata>(jsonString);
var stream = metadata.Streams.FirstOrDefault(x => x.Type == "audio");
if (stream is null)
throw new Exception("Failed to parse audio stream metadata");
return new AudioInfo
{
FileName = filename,
AudioCodec = stream.CodecName,
Channels = stream.Channels,
SampleRate = stream.SampleRate,
Samples = stream.SampleCount,
Duration = stream.Duration,
};
}
/// <summary>
/// Creates the audio buffer.
/// </summary>
/// <param name="audioTensor">The audio tensor.</param>
/// <param name="channels">The channels.</param>
/// <param name="samples">The samples.</param>
/// <returns>System.Byte[].</returns>
private static byte[] CreateAudioBuffer(AudioTensor audioTensor, int channels, int samples)
{
int offset = 0;
byte[] buffer = new byte[samples * channels * 4]; // float32 = 4 bytes
for (int i = 0; i < samples; i++)
{
for (int c = 0; c < channels; c++)
{
float sample = Math.Clamp(audioTensor[c, i], -1f, 1f);
byte[] bytes = BitConverter.GetBytes(sample);
Buffer.BlockCopy(bytes, 0, buffer, offset, 4);
offset += 4;
}
}
return buffer;
}
/// <summary>
/// Creates the audio tensor.
/// </summary>
/// <param name="audioBytes">The audio bytes.</param>
/// <param name="channels">The channels.</param>
/// <param name="sampleRate">The sample rate.</param>
/// <returns>AudioTensor.</returns>
private static AudioTensor CreateAudioTensor(byte[] audioBytes, int channels, int sampleRate)
{
// Convert PCM16 -> float32 [-1, 1]
var sampleCount = audioBytes.Length / 2 / channels;
var result = new Tensor<float>([channels, sampleCount]);
for (int i = 0, s = 0; i < audioBytes.Length; i += 2, s++)
{
short sample = BitConverter.ToInt16(audioBytes, i);
float normalized = sample / 32768f;
int channel = s % channels;
int frame = s / channels;
result[channel, frame] = normalized;
}
return result.AsAudioTensor(sampleRate);
}
#region FFMPEG / FFProbe
private static Process CreateProcess(string executable, string arguments)
{
var ffmpegProcess = new Process();
ffmpegProcess.StartInfo.FileName = executable;
ffmpegProcess.StartInfo.Arguments = arguments;
ffmpegProcess.StartInfo.UseShellExecute = false;
ffmpegProcess.StartInfo.CreateNoWindow = true;
return ffmpegProcess;
}
private static Process CreateReader(string inputFile, string audioCodec, int sampleRate, int channels)
{
var process = CreateProcess(FFMpegPath, $"-hide_banner -i \"{inputFile}\" -f s16le -acodec {audioCodec} -ac {channels} -ar {sampleRate} pipe:1");
process.StartInfo.RedirectStandardOutput = true;
return process;
}
private static Process CreateWriter(string audioOutputFile, int sampleRate, int channels)
{
var process = CreateProcess(FFMpegPath, $"-hide_banner -y -f f32le -ac {channels} -ar {sampleRate} -i pipe:0 \"{audioOutputFile}\"");
process.StartInfo.RedirectStandardInput = true;
return process;
}
private static Process CreateMuxer(string targetVideo, string sourceVideo, string tempFile)
{
var process = CreateProcess(FFMpegPath, $"-hide_banner -i \"{targetVideo}\" -i \"{sourceVideo}\" -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 -y \"{tempFile}\"");
process.StartInfo.RedirectStandardInput = true;
return process;
}
private static Process CreateMetadata(string inputFile)
{
var process = CreateProcess(FFProbePath, $"-v quiet -print_format json -show_format -show_streams {inputFile}");
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
return process;
}
private record AudioMetadata
{
[JsonPropertyName("format")]
public AudioFormat Format { get; set; }
[JsonPropertyName("streams")]
public List<AudioStream> Streams { get; set; }
}
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
private record AudioFormat
{
[JsonPropertyName("filename")]
public string FileName { get; set; }
[JsonPropertyName("nb_streams")]
public int StreamCount { get; set; }
[JsonPropertyName("format_name")]
public string FormatName { get; set; }
[JsonPropertyName("format_long_name")]
public string FormatLongName { get; set; }
[JsonPropertyName("size")]
public long Size { get; set; }
[JsonPropertyName("bit_rate")]
public long BitRate { get; set; }
}
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
private record AudioStream
{
[JsonPropertyName("codec_type")]
public string Type { get; set; }
[JsonPropertyName("codec_name")]
public string CodecName { get; set; }
[JsonPropertyName("codec_long_name")]
public string CodecLongName { get; set; }
[JsonPropertyName("sample_fmt")]
public string SampleFormat { get; set; }
[JsonPropertyName("sample_rate")]
public int SampleRate { get; set; }
[JsonPropertyName("duration_ts")]
public long SampleCount { get; set; }
[JsonPropertyName("channels")]
public int Channels { get; set; }
[JsonPropertyName("bits_per_sample")]
public int BitsPerSample { get; set; }
[JsonPropertyName("bit_rate")]
public int BitRate { get; set; }
[JsonPropertyName("duration")]
public float DurationSeconds { get; set; }
public TimeSpan Duration => TimeSpan.FromSeconds(DurationSeconds);
}
#endregion
}
}