-
Notifications
You must be signed in to change notification settings - Fork 5
/
frmMergeAndChapter.cs
executable file
·374 lines (311 loc) · 13 KB
/
frmMergeAndChapter.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
using AAClib;
using MarkAble2.Properties;
namespace MarkAble2
{
public partial class frmMergeAndChapter : Form
{
public frmMergeAndChapter()
{
InitializeComponent();
}
//this is set to a ridiculous length unless adjusted
private TimeSpan MaxDurationForSampleRate = new TimeSpan(50,0,0,0); //50 days
private bool DoneIt;
private void frmMergeAndChapter_Activated(object sender, EventArgs e)
{
if (!DoneIt)
{
DoneIt = true;
//Global.ResizeForDPI(this);
if (Global.Options.Encoder == Global.EncodeToTypes.AAC && Global.RippedCDFiles.Count > 0)
{
//read first AAC file to check sample rate
var frec = Global.RippedCDFiles[0];
if (frec.FileType == Global.FileTypes.AAC)
{
var aacname = frec.FilePath;
AACfile aac = new AACfile();
aac.ReportError += AAC_ReportError;
aac.ProcessingProgress += AAC_ProcessingProgress;
aac.ReportMessage += AAC_ReportMessage;
aac.ReadFile(aacname, false);
if (aac.LoadedOK)
{
Global.SampleRate = aac.GetAudioTrackTimeUnitsPerSecond();
double maxsecs = (double)Int32.MaxValue / (double)Global.SampleRate;
long maxticks = Convert.ToInt64(TimeSpan.TicksPerSecond*maxsecs);
MaxDurationForSampleRate = new TimeSpan(maxticks);
}
aac.Dispose();
}
}
nudFiles.Maximum = Global.RippedCDFiles.Count;
int numFiles = 1;
while((CheckFileSize(numFiles) != FileSizeChecks.OK) && (numFiles < Global.RippedCDFiles.Count))
{
Application.DoEvents();
numFiles++;
}
nudFiles.Value = numFiles;
ShowFileSize();
}
}
private void AAC_ReportMessage(string A_0, string A_1)
{
Application.DoEvents();
}
private void AAC_ProcessingProgress(double A_0, AACfile.ProcessingPhases A_1)
{
Application.DoEvents();
}
private void AAC_ReportError(string A_0)
{
Application.DoEvents();
}
private void nudFiles_ValueChanged(object sender, EventArgs e)
{
ShowFileSize();
}
private void ShowFileSize()
{
if ((Global.RippedCDFiles != null) && (Global.RippedCDFiles.Count > 0))
{
double totsize = Global.RippedCDFiles.TotalSize() / 1E6; //this should give MB
TimeSpan totduration = Global.RippedCDFiles.TotalDuration();
if (nudFiles.Value == 1)
{
var filebreakdown = new StringBuilder(Resources.File_will_be_totsize_MB_in_size_duration_hours);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0.0"));
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
else
{
totsize = totsize/(double)nudFiles.Value;
double newticks = (double) totduration.Ticks/(double) nudFiles.Value;
long longticks = Convert.ToInt64(newticks);
totduration = new TimeSpan(longticks);
if (totduration > TimeSpan.Zero)
{
var filebreakdown =
new StringBuilder(Resources.Each_file_will_be_approx_totsize_MB_in_size_duration_duration);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0"));
filebreakdown.Replace("<duration>", totduration.TotalHours.ToString("#,##0.0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
else
{
var filebreakdown = new StringBuilder(Resources.Each_file_will_be_approx_totsize_MB_in_size);
filebreakdown.Replace("<totsize>", totsize.ToString("#,##0"));
labFileBreakdown.Text = filebreakdown.ToString();
}
}
}
}
private void butStart_Click(object sender, EventArgs e)
{
Global.ProcessMode = Global.ProcessModes.CDs;
Global.RippedCDFiles.NumParts = (int) nudFiles.Value;
if (cboTrackInterval.SelectedItem != null)
{
var ti = (TrackOrFileInterval)cboTrackInterval.SelectedItem;
Global.Options.TrackSpacing = ti.Interval;
Global.RippedCDFiles.TrackSpacing = ti.Interval;
}
Global.RippedCDFiles.RegularMins = (double)nudRegMins.Value;
if (String.IsNullOrEmpty(Global.RippedCDFiles.BookImage))
{
Global.RippedCDFiles.BookImage = Application.StartupPath + @"\" + "defaultimage.jpg";
}
if (radChapsByTime.Checked)
{
Global.RippedCDFiles.ChapterType = Global.ChapterTypes.ByTime;
}
if (radDiscOnly.Checked)
{
Global.RippedCDFiles.ChapterType = Global.ChapterTypes.ByTimeWithDisc;
}
if (radChapsByTrack.Checked)
{
Global.RippedCDFiles.ChapterType = Global.ChapterTypes.ByDiscAndTrack;
}
if (radChapsNone.Checked)
{
Global.RippedCDFiles.ChapterType = Global.ChapterTypes.None;
}
Global.Options.ChapterType = Global.RippedCDFiles.ChapterType;
Global.Options.Save();
Global.WriteRippedFilelist();
WindowState = FormWindowState.Normal;
DialogResult = DialogResult.OK;
}
private void frmMergeAndChapter_Load(object sender, EventArgs e)
{
if (File.Exists(Global.Options.DefaultImage))
{
picBookImage.Load(Global.Options.DefaultImage);
}
else
{
picBookImage.Load(Application.StartupPath + @"\" + "defaultimage.jpg");
Global.Options.DefaultImage = Application.StartupPath + @"\" + "defaultimage.jpg";
}
Global.RippedCDFiles.BookImage = Global.Options.DefaultImage;
if (Global.Options.DefaultInterval > 0)
nudRegMins.Value = Global.Options.DefaultInterval;
if (Global.Options.Encoder == Global.EncodeToTypes.MP3)
{
labchap7prompt.Text =
Resources.MP3_files_can_t_have_chapter_stops;
panel1.Hide();
}
else
{
labchap7prompt.Text =
Resources.Audiobooks_on_the_iPod_can_have_chapter_stops;
panel1.Show();
for (int n = 1; n <= 30; n++)
{
cboTrackInterval.Items.Add(new TrackOrFileInterval(n));
}
if (Global.Options.TrackSpacing >= 0 && Global.Options.TrackSpacing <= 30)
cboTrackInterval.SelectedIndex = Global.Options.TrackSpacing - 1;
switch (Global.Options.ChapterType)
{
case Global.ChapterTypes.None:
radChapsNone.Checked = true;
break;
case Global.ChapterTypes.ByTime:
radChapsByTime.Checked = true;
break;
case Global.ChapterTypes.ByDiscAndTrack:
case Global.ChapterTypes.BySourceFile:
radChapsByTrack.Checked = true;
break;
case Global.ChapterTypes.ByTimeWithDisc:
radDiscOnly.Checked = true;
break;
}
}
}
private void butChoosePic_Click(object sender, EventArgs e)
{
dlgOpenImage.FileName = "";
if (dlgOpenImage.ShowDialog() == DialogResult.OK)
{
try
{
Image imgsmall = Global.MakeSmallImage(dlgOpenImage.FileName);
Global.RippedCDFiles.BookImage = dlgOpenImage.FileName;
picBookImage.Image = imgsmall;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Resources.Unable_to_load_image_exclaim);
}
}
}
private void frmMergeAndChapter_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void frmMergeAndChapter_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 0)
return;
string graphicexts = ".gif.png.jpg.bmp";
try
{
string ext = Global.GetExtension(files[0]);
if (graphicexts.IndexOf(ext) == -1)
return; //not a graphic file
Image imgsmall = Global.MakeSmallImage(files[0]);
Global.RippedCDFiles.BookImage = files[0];
picBookImage.Image = imgsmall;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Resources.Unable_to_load_image_exclaim);
}
}
private void nudFiles_Validating(object sender, CancelEventArgs e)
{
FileSizeChecks check = CheckFileSize((int) nudFiles.Value);
if (check == FileSizeChecks.Critical)
{
DialogResult result = MessageBox.Show(Resources.Files_will_be_too_big_to_play_at_the_current_sampling_rate + "\r\n\r\n" + Resources.Ignore_this_query,
Resources.Critical_exclaim,
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result == DialogResult.No)
{
e.Cancel = true;
nudFiles.Focus();
}
return;
}
if (check != FileSizeChecks.OK)
{
DialogResult result = MessageBox.Show(
Resources.Files_will_be_larger_than_recommended_size_and_or_duration + "\r\n\r\n" + Resources.Ignore_this_query,
Resources.Warning_exclaim,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.No)
{
e.Cancel = true;
nudFiles.Focus();
}
}
}
private enum FileSizeChecks
{
OK,
BiggerThanOptionDuration,
BiggerThanOptionSize,
Critical
}
private FileSizeChecks CheckFileSize(int NumFiles)
{
if ((Global.RippedCDFiles != null) && (Global.RippedCDFiles.Count > 0))
{
double totsize = Global.RippedCDFiles.TotalSize()/1E6; //this should give MB
TimeSpan totduration = Global.RippedCDFiles.TotalDuration();
totsize = totsize/(double) NumFiles;
double newticks = (double) totduration.Ticks/(double) NumFiles;
long longticks = Convert.ToInt64(newticks);
totduration = new TimeSpan(longticks);
if (totduration > MaxDurationForSampleRate)
{
return FileSizeChecks.Critical;
}
if (totduration.TotalHours > (double) Global.Options.MaxHours)
{
return FileSizeChecks.BiggerThanOptionDuration;
}
if
(totsize > (double) Global.Options.MaxSize)
{
return FileSizeChecks.BiggerThanOptionSize;
}
return FileSizeChecks.OK;
}
return FileSizeChecks.OK;
}
private void cboTrackInterval_SelectedIndexChanged(object sender, EventArgs e)
{
radChapsByTrack.Checked = true;
}
}
}