-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioConversion.xaml.cs
386 lines (308 loc) · 13.4 KB
/
AudioConversion.xaml.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSCore.Codecs;
using KiriMusicHelper.Annotations;
using KiriMusicHelper.Waveform;
using Ookii.Dialogs.Wpf;
using Binding = System.Windows.Data.Binding;
using Path = System.IO.Path;
using TextBox = System.Windows.Controls.TextBox;
using UserControl = System.Windows.Controls.UserControl;
namespace KiriMusicHelper {
//https://stackoverflow.com/questions/15309008/binding-converterparameter
[MarkupExtensionReturnType(typeof(BindingExpression))]
[ContentProperty(nameof(Binding))]
public class ConverterBindableParameter : MarkupExtension
{
#region Public Properties
public Binding Binding { get; set; }
public BindingMode Mode { get; set; }
public IValueConverter Converter { get; set; }
public Binding ConverterParameter { get; set; }
#endregion
public ConverterBindableParameter()
{ }
public ConverterBindableParameter(string path)
{
Binding = new Binding(path);
}
public ConverterBindableParameter(Binding binding)
{
Binding = binding;
}
#region Overridden Methods
public override object ProvideValue(IServiceProvider serviceProvider)
{
var multiBinding = new MultiBinding();
Binding.Mode = Mode;
multiBinding.Bindings.Add(Binding);
if (ConverterParameter != null)
{
ConverterParameter.Mode = BindingMode.OneWay;
multiBinding.Bindings.Add(ConverterParameter);
}
var adapter = new MultiValueConverterAdapter
{
Converter = Converter
};
multiBinding.Converter = adapter;
return multiBinding.ProvideValue(serviceProvider);
}
#endregion
[ContentProperty(nameof(Converter))]
private class MultiValueConverterAdapter : IMultiValueConverter
{
public IValueConverter Converter { get; set; }
private object lastParameter;
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (Converter == null) return values[0]; // Required for VS design-time
if (values.Length > 1) lastParameter = values[1];
return Converter.Convert(values[0], targetType, lastParameter, culture);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
if (Converter == null) return new object[] { value }; // Required for VS design-time
return new object[] { Converter.ConvertBack(value, targetTypes[0], lastParameter, culture) };
}
}
}
public class AudioConversionParameters : INotifyPropertyChanged
{
public string PboPrefix { get; set; } = "KiriMusic_A2";
public string MusicClass { get; set; } = "Action";
public event PropertyChangedEventHandler PropertyChanged;
}
public class AudioFile: INotifyPropertyChanged
{
public FileInfo InputFile;
public bool Enabled { get; set; } = true;
public string FileName { get; set; }
public long BitRate { get; set; }
public int SamplingRate { get; set; }
public double Duration { get; set; }
public double ConversionProgress { get; set; }
public string GetCfgSoundsClass(AudioConversionParameters param)
{
return $"class {FileName} {{\n" +
$" name=\"{FileName}\";\n" +
$" sound[] = {{\"{param.PboPrefix}\\{FileName}.ogg\", 1.0, 1.0}};\n" +
$" duration={Duration};\n" +
$" musicClass=\"{param.MusicClass}\"\n" +
$"}};";
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class AudioFilesToCfgMusicConverter : IValueConverter
{
//public AudioConversionParameters param;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is IEnumerable<AudioFile> audioFiles)) return "";
if (!(parameter is AudioConversionParameters param)) return "";
StringBuilder output = new StringBuilder();
foreach (var audioFile in audioFiles)
{
output.AppendLine(audioFile.GetCfgSoundsClass(param));
}
return output.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
public partial class AudioConversion : UserControl, INotifyPropertyChanged {
public AudioConversion() {
InitializeComponent();
var upax = ConfigText.GetBindingExpression(TextBox.TextProperty);
var _inputBinding = new Binding("Value")
{
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Path = new PropertyPath("AudioFiles"),
Source = this,
Converter = new AudioFilesToCfgMusicConverter(),
ConverterParameter = ConversionParameters
};
//_inputBinding.Converter = new AudioFilesToCfgMusicConverter {param = ConversionParameters};
ConfigText.SetBinding(TextBox.TextProperty, _inputBinding);
ConversionParameters.PropertyChanged += (sender, args) =>
{
var upx = ConfigText.GetBindingExpression(TextBox.TextProperty);
upx.UpdateTarget();
};
encoder.OnProgress += (sender, conversion, args) =>
{
var file = AudioFiles.FirstOrDefault((x => x.FileName == conversion.FileName));
if (file == null) return;
file.ConversionProgress = args.Percent;
};
}
public ObservableCollection<AudioFile> AudioFiles { get; set; } = new ObservableCollection<AudioFile>();
public AudioConversionParameters ConversionParameters { get; set; } = new AudioConversionParameters();
public int TargetBitrate { get; set; } = 192;
private AudioEncoder encoder = new AudioEncoder();
private bool FilterRackOpen { get; set; } = false;
private bool LivePreviewOpen { get; set; } = false;
private bool IsEncoding { get; set; } = false;
public bool CanOpenLivePreview => FilterRackOpen && !LivePreviewOpen && !IsEncoding;
public bool CanOpenFilterRack => !FilterRackOpen;
public bool CanEncode => !IsEncoding;
private async void LoadFolder_Click(object sender, RoutedEventArgs e)
{
VistaFolderBrowserDialog x = new VistaFolderBrowserDialog();
x.Description = "burp";
if (!x.ShowDialog().Value) return;
AudioFiles.Clear();
string [] fileEntries = Directory.GetFiles(x.SelectedPath);
foreach (string fileName in fileEntries)
{
var fileInfo = new FileInfo(fileName);
var audioInfo = await AudioEncoder.GetInfo(fileInfo.FullName);
if (audioInfo == null) continue;
var audioStream = audioInfo.AudioStreams.FirstOrDefault();
if (audioStream == null) continue;
AudioFiles.Add(new AudioFile
{
InputFile = fileInfo,
FileName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName),
SamplingRate = audioStream.SampleRate,
BitRate = audioStream.Bitrate,
Duration = audioStream.Duration.TotalSeconds
});
}
OnPropertyChanged("AudioFiles");
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private async void LoadFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog { Title = "Audiodatei auswählen", Multiselect = true};
if (openFileDialog.ShowDialog() != DialogResult.OK)
return;
AudioFiles.Clear();
foreach (string fileName in openFileDialog.FileNames)
{
var fileInfo = new FileInfo(fileName);
var audioInfo = await AudioEncoder.GetInfo(fileInfo.FullName);
var audioStream = audioInfo?.AudioStreams?.FirstOrDefault();
if (audioStream == null) continue;
AudioFiles.Add(new AudioFile
{
InputFile = fileInfo,
FileName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName),
SamplingRate = audioStream.SampleRate,
BitRate = audioStream.Bitrate,
Duration = audioStream.Duration.TotalSeconds
});
}
OnPropertyChanged("AudioFiles");
}
private async void OpenEffectsRack_Click(object sender, RoutedEventArgs e)
{
await AudioEffect.Instance.SetSource(AudioEffect.CreateSource(AudioFiles.FirstOrDefault()?.InputFile.FullName));
var p = new FilterRack.FilterRack(AudioEffect.Instance);
p.Show();
//AudioEffect.Instance.StartLivePlay();
FilterRackOpen = true;
}
private async void OpenLivePlay_Click(object sender, RoutedEventArgs e)
{
var p = new LivePlay();
await p.Init();
p.Show();
LivePreviewOpen = true;
}
private async void RunConversion_Click(object sender, RoutedEventArgs e)
{
var outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "out");
Directory.CreateDirectory(outputDirectory);
IsEncoding = true;
int threadCount = Math.Min(4, AudioFiles.Count);
Task[] workers = new Task[threadCount];
var queue = new ConcurrentQueue<AudioConversionItem>();
var effectQueue = new ConcurrentQueue<Tuple<AudioEffect, FileInfo>>();
if (AudioEffect.Instance.Filters.Count != 0)
{
foreach (var audioFile in AudioFiles)
{
var effect = await AudioEffect.Instance.CopyFor(audioFile.InputFile);
effectQueue.Enqueue(new Tuple<AudioEffect, FileInfo>(effect, audioFile.InputFile));
}
}
else
{
foreach (var audioFile in AudioFiles)
{
queue.Enqueue(new AudioConversionItem
{
Bitrate = TargetBitrate * 1000,
InputFile = audioFile.InputFile,
OutputPath = Path.Combine(outputDirectory, Path.ChangeExtension(audioFile.InputFile.Name, ".ogg")),
FileName = System.IO.Path.GetFileNameWithoutExtension(audioFile.InputFile.Name),
SampleRate = 44100
});
}
}
for (int i = 0; i < threadCount; ++i)
{
Task task = new Task(() =>
{
while (effectQueue.TryDequeue(out Tuple<AudioEffect, FileInfo> fileToConvert))
{
var tempFile = fileToConvert.Item1.ProcessIntoTempFile();
queue.Enqueue(new AudioConversionItem
{
Bitrate = TargetBitrate * 1000,
InputFile = new FileInfo(tempFile),
OutputPath = Path.Combine(outputDirectory, Path.ChangeExtension(fileToConvert.Item2.Name, ".ogg")),
FileName = System.IO.Path.GetFileNameWithoutExtension(fileToConvert.Item2.Name),
SampleRate = 44100
});
}
while (queue.TryDequeue(out AudioConversionItem fileToConvert))
{
var subTask = encoder.AudioConvert(fileToConvert, CancellationToken.None);
subTask.Wait();
}
});
workers[i] = task;
task.Start();
}
Task.WhenAll(workers).ContinueWith((x) =>
{
IsEncoding = false;
});
}
}
}