Debian Codegraph @

components/encoder/coreaudioconnect/config.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2020 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include "config.h"
14
15 const String BoCA::ConfigureCoreAudio::ConfigID = "CoreAudio";
16
17 BoCA::ConfigureCoreAudio::ConfigureCoreAudio(const CoreAudioCommCodecs &iFormats)
18 {
19 const Config *config = Config::Get();
20
21 formats = iFormats;
22
23 bitrate = config->GetIntValue(ConfigID, "Bitrate", 64);
24 allowID3 = config->GetIntValue(ConfigID, "AllowID3v2", False);
25 fileFormat = config->GetIntValue(ConfigID, "MP4Container", True);
26 fileExtension = config->GetIntValue(ConfigID, "MP4FileExtension", 0);
27
28 I18n *i18n = I18n::Get();
29
30 tabwidget = new TabWidget(Point(7, 7), Size(500, 208));
31
32 i18n->SetContext("Encoders::AAC::Format");
33
34 layer_format = new Layer(i18n->TranslateString("Format"));
35
36 group_id3v2 = new GroupBox(i18n->TranslateString("Tags"), Point(7, 88), Size(279, 90));
37
38 check_id3v2 = new CheckBox(i18n->TranslateString("Allow ID3v2 tags in AAC files"), Point(10, 13), Size(200, 0), &allowID3);
39 check_id3v2->SetWidth(check_id3v2->GetUnscaledTextWidth() + 20);
40
41 text_note = new Text(i18n->AddColon(i18n->TranslateString("Note")), Point(10, 38));
42 text_id3v2 = new Text(i18n->TranslateString("Some players may have problems playing AAC\nfiles with ID3 tags attached. Please use this option only\nif you are sure that your player can handle these tags."), Point(text_note->GetUnscaledTextWidth() + 12, 38));
43
44 group_id3v2->SetSize(Size(Math::Max(240, text_note->GetUnscaledTextWidth() + text_id3v2->GetUnscaledTextWidth() + 22), Math::Max(text_note->GetUnscaledTextHeight(), text_id3v2->GetUnscaledTextHeight()) + 48));
45
46 group_id3v2->Add(check_id3v2);
47 group_id3v2->Add(text_note);
48 group_id3v2->Add(text_id3v2);
49
50 group_mp4 = new GroupBox(i18n->TranslateString("File format"), Point(7, 11), Size(group_id3v2->GetWidth() / 2 - 4, 65));
51
52 option_mp4 = new OptionBox("MP4", Point(10, 13), Size(group_mp4->GetWidth() - 20, 0), &fileFormat, 1);
53 option_mp4->onAction.Connect(&ConfigureCoreAudio::SetFileFormat, this);
54
55 option_aac = new OptionBox("AAC", Point(10, 38), Size(group_mp4->GetWidth() - 20, 0), &fileFormat, 0);
56 option_aac->onAction.Connect(&ConfigureCoreAudio::SetFileFormat, this);
57
58 group_mp4->Add(option_mp4);
59 group_mp4->Add(option_aac);
60
61 group_extension = new GroupBox(i18n->TranslateString("File extension"), Point(group_mp4->GetWidth() + 15 + (group_id3v2->GetWidth() % 2), 11), Size(group_id3v2->GetWidth() / 2 - 4, 65));
62
63 option_extension_m4a = new OptionBox(".m4a", Point(10, 13), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 0);
64 option_extension_m4b = new OptionBox(".m4b", Point(10, 38), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 1);
65 option_extension_m4r = new OptionBox(".m4r", Point(group_extension->GetWidth() / 2 + 4, 13), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 2);
66 option_extension_mp4 = new OptionBox(".mp4", Point(group_extension->GetWidth() / 2 + 4, 38), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 3);
67
68 group_extension->Add(option_extension_m4a);
69 group_extension->Add(option_extension_m4b);
70 group_extension->Add(option_extension_m4r);
71 group_extension->Add(option_extension_mp4);
72
73 i18n->SetContext("Encoders::AAC::Codec");
74
75 layer_quality = new Layer(i18n->TranslateString("Codec"));
76
77 group_codec = new GroupBox(i18n->TranslateString("Audio codec"), Point(7, 11), Size(group_id3v2->GetWidth(), 43));
78
79 text_codec = new Text(i18n->AddColon(i18n->TranslateString("Audio codec")), Point(10, 15));
80
81 combo_codec = new ComboBox(Point(text_codec->GetUnscaledTextSize().cx + 17, 12), Size(group_codec->GetWidth() - text_codec->GetUnscaledTextSize().cx - 27, 0));
82
83 /* Query supported formats from Core Audio
84 * and add them to the combo box list.
85 */
86 for (Int i = 0; i < 32 && formats.codecs[i] != 0; i++)
87 {
88 if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC) combo_codec->AddEntry("MPEG4 AAC Low Complexity");
89 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_HE) combo_codec->AddEntry("MPEG4 AAC High Efficiency");
90 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_HE_V2) combo_codec->AddEntry("MPEG4 AAC High Efficiency v2");
91 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_LD) combo_codec->AddEntry("MPEG4 AAC Low Delay");
92 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_ELD) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay");
93 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_ELD_SBR) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay SBR");
94 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_ELD_V2) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay v2");
95 else if (formats.codecs[i] == CA::kAudioFormatMPEG4AAC_Spatial) combo_codec->AddEntry("MPEG4 AAC Spatial");
96 else if (formats.codecs[i] == CA::kAudioFormatAppleLossless) combo_codec->AddEntry("Apple Lossless Audio Codec");
97 else continue;
98
99 codecs.Add(formats.codecs[i]);
100
101 if ((UnsignedInt) config->GetIntValue(ConfigID, "Codec", CA::kAudioFormatMPEG4AAC) == formats.codecs[i]) combo_codec->SelectNthEntry(combo_codec->Length() - 1);
102 }
103
104 combo_codec->onSelectEntry.Connect(&ConfigureCoreAudio::SetCodec, this);
105
106 group_codec->Add(text_codec);
107 group_codec->Add(combo_codec);
108
109 i18n->SetContext("Encoders::AAC::Quality");
110
111 group_bitrate = new GroupBox(i18n->TranslateString("Bitrate"), Point(7, 66), Size(group_id3v2->GetWidth(), 43));
112
113 text_bitrate = new Text(i18n->AddColon(i18n->TranslateString("Bitrate per channel")), Point(10, 15));
114
115 text_bitrate_kbps = new Text(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", NIL).Trim(), Point(35, 15));
116 text_bitrate_kbps->SetX(text_bitrate_kbps->GetUnscaledTextWidth() + 10);
117 text_bitrate_kbps->SetOrientation(OR_UPPERRIGHT);
118
119 edit_bitrate = new EditBox(String::FromInt(bitrate), Point(text_bitrate_kbps->GetX() + 32, 12), Size(25, 0), 3);
120 edit_bitrate->SetFlags(EDB_NUMERIC);
121 edit_bitrate->SetOrientation(OR_UPPERRIGHT);
122 edit_bitrate->onInput.Connect(&ConfigureCoreAudio::SetBitrateByEditBox, this);
123
124 slider_bitrate = new Slider(Point(text_bitrate->GetUnscaledTextSize().cx + 17, 13), Size(group_bitrate->GetWidth() - edit_bitrate->GetX() - 25 - text_bitrate->GetUnscaledTextSize().cx, 0), OR_HORZ, &bitrate, 1, 256);
125 slider_bitrate->onValueChange.Connect(&ConfigureCoreAudio::SetBitrate, this);
126
127 group_bitrate->Add(text_bitrate);
128 group_bitrate->Add(slider_bitrate);
129 group_bitrate->Add(edit_bitrate);
130 group_bitrate->Add(text_bitrate_kbps);
131
132 SetCodec();
133
134 tabwidget->SetSize(group_id3v2->GetSize() + Size(18, 118));
135
136 Add(tabwidget);
137
138 tabwidget->Add(layer_quality);
139 tabwidget->Add(layer_format);
140
141 layer_format->Add(group_mp4);
142 layer_format->Add(group_extension);
143 layer_format->Add(group_id3v2);
144
145 layer_quality->Add(group_codec);
146 layer_quality->Add(group_bitrate);
147
148 SetSize(group_id3v2->GetSize() + Size(32, 132));
149 }
150
151 BoCA::ConfigureCoreAudio::~ConfigureCoreAudio()
152 {
153 DeleteObject(tabwidget);
154 DeleteObject(layer_format);
155 DeleteObject(layer_quality);
156
157 DeleteObject(group_mp4);
158 DeleteObject(option_mp4);
159 DeleteObject(option_aac);
160 DeleteObject(group_extension);
161 DeleteObject(option_extension_m4a);
162 DeleteObject(option_extension_m4b);
163 DeleteObject(option_extension_m4r);
164 DeleteObject(option_extension_mp4);
165 DeleteObject(group_id3v2);
166 DeleteObject(check_id3v2);
167 DeleteObject(text_note);
168 DeleteObject(text_id3v2);
169
170 DeleteObject(group_codec);
171 DeleteObject(text_codec);
172 DeleteObject(combo_codec);
173 DeleteObject(group_bitrate);
174 DeleteObject(text_bitrate);
175 DeleteObject(slider_bitrate);
176 DeleteObject(edit_bitrate);
177 DeleteObject(text_bitrate_kbps);
178 }
179
180 Int BoCA::ConfigureCoreAudio::SaveSettings()
181 {
182 Config *config = Config::Get();
183
184 config->SetIntValue(ConfigID, "Codec", codecs.GetNth(combo_codec->GetSelectedEntryNumber()));
185
186 if (bitrates.Length() == 2)
187 {
188 if (bitrate < bitrates.GetNth(0)) bitrate = bitrates.GetNth(0);
189 if (bitrate > bitrates.GetNth(1)) bitrate = bitrates.GetNth(1);
190 }
191
192 if (bitrates.Length() == 2) config->SetIntValue(ConfigID, "Bitrate", bitrate);
193 else if (bitrates.Length() > 2) config->SetIntValue(ConfigID, "Bitrate", bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1));
194
195 config->SetIntValue(ConfigID, "MP4Container", fileFormat);
196 config->SetIntValue(ConfigID, "MP4FileExtension", fileExtension);
197 config->SetIntValue(ConfigID, "AllowID3v2", allowID3);
198
199 return Success();
200 }
201
202 Void BoCA::ConfigureCoreAudio::SetCodec()
203 {
204 UnsignedInt format = codecs.GetNth(combo_codec->GetSelectedEntryNumber());
205
206 for (Int i = 0; i < 32 && formats.codecs[i] != 0; i++)
207 {
208 if (formats.codecs[i] != format) continue;
209
210 Int numBitrates = 0;
211
212 for (Int j = 0; j < 64; j++) if (formats.bitrates[i][2 * j + 1] != 0) numBitrates++;
213
214 if (numBitrates > 0 && bitrates.Length() > 2) bitrate = bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1);
215
216 bitrates.RemoveAll();
217
218 for (Int j = 0; j < 64 && formats.bitrates[i][2 * j + 1] != 0; j++)
219 {
220 bitrates.Add(formats.bitrates[i][2 * j ] / 1000);
221 bitrates.Add(formats.bitrates[i][2 * j + 1] / 1000);
222
223 if (bitrate == bitrates.GetNth(j * 2 + 1)) bitrate = -numBitrates + j;
224 }
225
226 break;
227 }
228
229 if (bitrates.Length() > 0) group_bitrate->Activate();
230 else group_bitrate->Deactivate();
231
232 if (bitrates.Length() == 2) { edit_bitrate->Activate(); slider_bitrate->SetRange(bitrates.GetNth(0), bitrates.GetNth(1)); }
233 else if (bitrates.Length() > 2) { edit_bitrate->Deactivate(); slider_bitrate->SetRange(-bitrates.Length() / 2, -1); }
234
235 if (codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatAppleLossless)
236 {
237 group_mp4->Deactivate();
238
239 fileFormat = 1;
240
241 option_extension_m4r->Deactivate();
242
243 if (fileExtension == 2) fileExtension = 0;
244 }
245 else if (codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC ||
246 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_HE ||
247 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_HE_V2 ||
248 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_LD ||
249 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_ELD ||
250 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_ELD_SBR ||
251 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_ELD_V2 ||
252 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_Spatial)
253 {
254 group_mp4->Activate();
255
256 option_extension_m4r->Activate();
257 }
258
259 SetBitrate();
260 SetFileFormat();
261 }
262
263 Void BoCA::ConfigureCoreAudio::SetBitrate()
264 {
265 if (bitrates.Length() == 0) return;
266
267 if (!edit_bitrate->IsFocussed())
268 {
269 if (bitrates.Length() == 2) edit_bitrate->SetText(String::FromInt(bitrate));
270 else edit_bitrate->SetText(String::FromInt(bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1)));
271 }
272 }
273
274 Void BoCA::ConfigureCoreAudio::SetBitrateByEditBox()
275 {
276 slider_bitrate->SetValue(edit_bitrate->GetText().ToInt());
277 }
278
279 Void BoCA::ConfigureCoreAudio::SetFileFormat()
280 {
281 if (fileFormat == 1) // MP4 container
282 {
283 group_id3v2->Deactivate();
284
285 group_extension->Activate();
286 }
287 else // raw AAC file format
288 {
289 group_id3v2->Activate();
290
291 group_extension->Deactivate();
292 }
293 }