-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Recorder.cs
416 lines (356 loc) 路 14.7 KB
/
Recorder.cs
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
using EspionSpotify.AudioSessions;
using EspionSpotify.Enums;
using EspionSpotify.Extensions;
using EspionSpotify.Models;
using EspionSpotify.Native;
using NAudio.Lame;
using NAudio.Wave;
using System;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace EspionSpotify
{
public class Recorder : IRecorder, IDisposable
{
private bool _disposed = false;
public const int MP3_MAX_NUMBER_CHANNELS = 2;
public const int MP3_MAX_SAMPLE_RATE = 48000;
public const long WAV_MAX_SIZE_BYTES = 4000000000;
public int CountSeconds { get; set; }
public bool Running { get; set; }
private readonly UserSettings _userSettings;
private readonly Track _track;
private readonly IFrmEspionSpotify _form;
private readonly IMainAudioSession _audioSession;
private OutputFile _currentOutputFile;
private WasapiLoopbackCapture _waveIn;
private Stream _fileWriter;
private Stream _tempWaveWriter;
private string _tempFile;
private readonly FileManager _fileManager;
private readonly IFileSystem _fileSystem;
private bool _canBeSkippedValidated = false;
private CancellationTokenSource _cancellationTokenSource;
public bool IsSkipTrackActive
{
get => _userSettings.RecordRecordingsStatus == Enums.RecordRecordingsStatus.Skip
&& _fileManager.IsPathFileNameExists(_track, _userSettings, _fileSystem);
}
public Recorder() { }
public Recorder(IFrmEspionSpotify form, IMainAudioSession audioSession, UserSettings userSettings, Track track, IFileSystem fileSystem)
{
_userSettings = new UserSettings();
userSettings.CopyAllTo(_userSettings);
_form = form;
_audioSession = audioSession;
_fileSystem = fileSystem;
_track = track;
_fileManager = new FileManager(_userSettings, _track, fileSystem);
}
public async Task Run(CancellationTokenSource cancellationTokenSource)
{
_cancellationTokenSource = cancellationTokenSource;
if (_userSettings.InternalOrderNumber > _userSettings.OrderNumberMax) return;
Running = true;
await Task.Delay(50);
_tempFile = _fileManager.GetTempFile();
_waveIn = new WasapiLoopbackCapture(_audioSession.AudioMMDevicesManager.AudioEndPointDevice);
_waveIn.DataAvailable += WaveIn_DataAvailable;
_waveIn.RecordingStopped += WaveIn_RecordingStopped;
try
{
_tempWaveWriter = new WaveFileWriter(_tempFile, _waveIn.WaveFormat);
}
catch (Exception ex)
{
ForceStopRecording();
_form.WriteIntoConsole(I18nKeys.LogUnknownException, ex.Message);
Console.WriteLine(ex.Message);
Program.ReportException(ex);
return;
}
_waveIn.ShareMode = NAudio.CoreAudioApi.AudioClientShareMode.Shared;
_waveIn.StartRecording();
_form.WriteIntoConsole(I18nKeys.LogRecording, _track.ToString());
while (Running)
{
if (_cancellationTokenSource.IsCancellationRequested) return;
if (StopRecordingIfTrackCanBeSkipped()) return;
await Task.Delay(50);
}
_waveIn.StopRecording();
}
private bool StopRecordingIfTrackCanBeSkipped()
{
if (_canBeSkippedValidated || !_track.MetaDataUpdated) return false;
_canBeSkippedValidated = true;
if (IsSkipTrackActive)
{
_form.WriteIntoConsole(I18nKeys.LogTrackExists, _track.ToString());
ForceStopRecording();
return true;
}
return false;
}
private async void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (_tempWaveWriter == null || !Running) return;
if (_tempWaveWriter.Length < WAV_MAX_SIZE_BYTES / 2)
{
await _tempWaveWriter.WriteAsync(e.Buffer, 0, e.BytesRecorded);
}
else
{
_form.WriteIntoConsole(I18nKeys.LogRecordingDataExceeded, _track.ToString());
ForceStopRecording();
}
}
private async void WaveIn_RecordingStopped(object sender, StoppedEventArgs e)
{
if (_tempWaveWriter == null) return;
await _tempWaveWriter.FlushAsync();
_tempWaveWriter.Dispose();
try
{
_currentOutputFile = _fileManager.GetOutputFile();
_fileWriter = GetFileWriter();
await WriteTempWaveToMediaFile();
}
catch (Exception ex)
{
Running = false;
_form.WriteIntoConsole(I18nKeys.LogUnknownException, ex.Message);
Console.WriteLine(ex.Message);
Program.ReportException(ex);
return;
}
finally
{
if (_waveIn != null) _waveIn.Dispose();
if (_fileWriter != null) _fileWriter.Dispose();
}
DeleteTempFile();
if (CountSeconds < _userSettings.MinimumRecordedLengthSeconds)
{
_form.WriteIntoConsole(I18nKeys.LogDeleting, _currentOutputFile.File, _userSettings.MinimumRecordedLengthSeconds);
_fileManager.DeleteFile(_currentOutputFile.ToPendingFileString());
return;
}
var length = TimeSpan.FromSeconds(CountSeconds).ToString(@"mm\:ss");
_form.WriteIntoConsole(I18nKeys.LogRecorded, _track.ToString(), length);
_fileManager.UpdateOutputFileWithLatestTrackInfo(_currentOutputFile, _track, _userSettings);
_fileManager.RenameFile(_currentOutputFile.ToPendingFileString(), _currentOutputFile.ToString());
await UpdateOutputFileBasedOnMediaFormat();
_waveIn.DataAvailable -= WaveIn_DataAvailable;
_waveIn.RecordingStopped -= WaveIn_RecordingStopped;
Dispose();
}
private async Task WriteTempWaveToMediaFile()
{
var restrictions = _waveIn.WaveFormat.GetMP3RestrictionCode();
using (var reader = new WaveFileReader(_tempFile))
{
reader.Position = 0;
if (_userSettings.MediaFormat == MediaFormat.Mp3 && restrictions.Any())
{
await WriteWaveProviderReducerToMP3FileWriter(GetMp3WaveProvider(reader, _waveIn.WaveFormat));
}
else
{
await reader.CopyToAsync(_fileWriter, 81920, _cancellationTokenSource.Token);
}
}
}
private async Task UpdateOutputFileBasedOnMediaFormat()
{
switch (_userSettings.MediaFormat)
{
case MediaFormat.Mp3:
var mapper = new MediaTags.MapperID3(
_currentOutputFile.ToString(),
_track,
_userSettings);
await mapper.SaveMediaTags();
return;
default:
return;
}
}
public Stream GetFileWriter()
{
switch (_userSettings.MediaFormat)
{
case MediaFormat.Mp3:
var waveFormat = GetWaveFormatMP3Supported(_waveIn.WaveFormat);
return new LameMP3FileWriter(_currentOutputFile.ToPendingFileString(), waveFormat, _userSettings.Bitrate);
case MediaFormat.Wav:
return new WaveFileWriter(_currentOutputFile.ToPendingFileString(), _waveIn.WaveFormat);
default:
throw new Exception("Failed to get FileWriter");
}
}
public static bool TestFileWriter(IFrmEspionSpotify form, IMainAudioSession audioSession, UserSettings settings)
{
var waveIn = new WasapiLoopbackCapture(audioSession.AudioMMDevicesManager.AudioEndPointDevice);
switch (settings.MediaFormat)
{
case MediaFormat.Mp3:
try
{
using (var writer = new LameMP3FileWriter(new MemoryStream(), waveIn.WaveFormat, settings.Bitrate)) return true;
}
catch (ArgumentException ex)
{
return LogLameMP3FileWriterArgumentException(form, ex, waveIn.WaveFormat);
}
catch (Exception ex)
{
LogLameMP3FileWriterException(form, ex);
return false;
}
case MediaFormat.Wav:
try
{
using (var writer = new WaveFileWriter(new MemoryStream(), waveIn.WaveFormat)) return true;
}
catch (Exception ex)
{
form.UpdateIconSpotify(true, false);
form.WriteIntoConsole(I18nKeys.LogUnknownException, ex.Message);
Console.WriteLine(ex.Message);
Program.ReportException(ex);
return false;
}
default:
return false;
}
}
private static bool LogLameMP3FileWriterArgumentException(IFrmEspionSpotify form, ArgumentException ex, WaveFormat waveFormat)
{
var restrictions = waveFormat.GetMP3RestrictionCode();
if (restrictions.Any())
{
if (restrictions.Contains(WaveFormatMP3Restriction.Channel))
{
form.WriteIntoConsole(I18nKeys.LogUnsupportedNumberChannels, waveFormat.Channels);
}
if (restrictions.Contains(WaveFormatMP3Restriction.SampleRate))
{
form.WriteIntoConsole(I18nKeys.LogUnsupportedRate, waveFormat.SampleRate);
}
return true;
}
form.UpdateIconSpotify(true, false);
form.WriteIntoConsole(I18nKeys.LogUnknownException, ex.Message);
return false;
}
private static void LogLameMP3FileWriterException(IFrmEspionSpotify form, Exception ex)
{
if (ex.Message.Contains("Unable to load DLL"))
{
form.WriteIntoConsole(I18nKeys.LogMissingDlls);
}
else
{
Program.ReportException(ex);
form.WriteIntoConsole(I18nKeys.LogUnknownException, ex.Message);
}
form.UpdateIconSpotify(true, false);
Console.WriteLine(ex.Message);
}
private WaveFormat GetWaveFormatMP3Supported(WaveFormat waveFormat)
{
return WaveFormat.CreateIeeeFloatWaveFormat(
Math.Min(MP3_MAX_SAMPLE_RATE, waveFormat.SampleRate),
Math.Min(MP3_MAX_NUMBER_CHANNELS, waveFormat.Channels));
}
private IWaveProvider GetWaveProviderMP3ChannelReducer(IWaveProvider stream)
{
var waveProvider = new MultiplexingWaveProvider(new IWaveProvider[] { stream }, MP3_MAX_NUMBER_CHANNELS);
waveProvider.ConnectInputToOutput(0, 0);
waveProvider.ConnectInputToOutput(1, 1);
return waveProvider;
}
private IWaveProvider GetWaveProviderMP3SamplerReducer(IWaveProvider stream)
{
return new MediaFoundationResampler(stream, MP3_MAX_SAMPLE_RATE);
}
private async Task WriteWaveProviderReducerToMP3FileWriter(IWaveProvider stream)
{
var mp3WaveFormat = GetWaveFormatMP3Supported(_waveIn.WaveFormat);
byte[] data = new byte[mp3WaveFormat.Channels * mp3WaveFormat.SampleRate * _waveIn.WaveFormat.Channels];
int bytesRead;
while ((bytesRead = stream.Read(data, 0, data.Length)) > 0)
{
await _fileWriter.WriteAsync(data, 0, bytesRead);
}
}
private IWaveProvider GetMp3WaveProvider(IWaveProvider stream, WaveFormat waveFormat)
{
var restrictions = waveFormat.GetMP3RestrictionCode();
if (restrictions.Contains(WaveFormatMP3Restriction.Channel))
{
stream = GetWaveProviderMP3ChannelReducer(stream);
}
if (restrictions.Contains(WaveFormatMP3Restriction.SampleRate))
{
stream = GetWaveProviderMP3SamplerReducer(stream);
}
return stream;
}
private void DeleteTempFile()
{
if (!_fileSystem.File.Exists(_tempFile)) return;
try { _fileSystem.File.Delete(_tempFile); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
private void ForceStopRecording()
{
_form.UpdateIconSpotify(true, false);
Running = false;
_waveIn.DataAvailable -= WaveIn_DataAvailable;
_waveIn.RecordingStopped -= WaveIn_RecordingStopped;
_waveIn.StopRecording();
_waveIn.Dispose();
_tempWaveWriter.Close();
_tempWaveWriter.Dispose();
DeleteTempFile();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
if (_waveIn != null)
{
_waveIn.DataAvailable -= WaveIn_DataAvailable;
_waveIn.RecordingStopped -= WaveIn_RecordingStopped;
_waveIn.StopRecording();
_waveIn.Dispose();
}
if (_tempWaveWriter != null)
{
_tempWaveWriter.Close();
_tempWaveWriter.Dispose();
}
if (_fileWriter != null)
{
_fileWriter.Dispose();
}
DeleteTempFile();
if (_currentOutputFile == null || !_fileSystem.File.Exists(_currentOutputFile.ToPendingFileString())) return;
try { _fileSystem.File.Delete(_currentOutputFile.ToPendingFileString()); }
catch (Exception ex) { Console.WriteLine($"test {ex.Message}"); }
}
_disposed = true;
}
}
}