Debian Codegraph @

components/encoder/coreaudio/config.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2023 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 #include "dllinterface.h"
15
16 const String BoCA::ConfigureCoreAudio::ConfigID = "CoreAudio";
17
18 BoCA::ConfigureCoreAudio::ConfigureCoreAudio()
19 {
20 const Config *config = Config::Get();
21
22 bitrate = config->GetIntValue(ConfigID, "Bitrate", 64);
23 allowID3 = config->GetIntValue(ConfigID, "AllowID3v2", False);
24 fileFormat = config->GetIntValue(ConfigID, "MP4Container", True);
25 fileExtension = config->GetIntValue(ConfigID, "MP4FileExtension", 0);
26
27 I18n *i18n = I18n::Get();
28
29 tabwidget = new TabWidget(Point(7, 7), Size(500, 208));
30
31 i18n->SetContext("Encoders::AAC::Format");
32
33 layer_format = new Layer(i18n->TranslateString("Format"));
34
35 group_id3v2 = new GroupBox(i18n->TranslateString("Tags"), Point(7, 88), Size(279, 90));
36
37 check_id3v2 = new CheckBox(i18n->TranslateString("Allow ID3v2 tags in AAC files"), Point(10, 13), Size(200, 0), &allowID3);
38 check_id3v2->SetWidth(check_id3v2->GetUnscaledTextWidth() + 20);
39
40 text_note = new Text(i18n->AddColon(i18n->TranslateString("Note")), Point(10, 38));
41 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));
42
43 group_id3v2->SetSize(Size(Math::Max(240, text_note->GetUnscaledTextWidth() + text_id3v2->GetUnscaledTextWidth() + 22), Math::Max(text_note->GetUnscaledTextHeight(), text_id3v2->GetUnscaledTextHeight()) + 48));
44
45 group_id3v2->Add(check_id3v2);
46 group_id3v2->Add(text_note);
47 group_id3v2->Add(text_id3v2);
48
49 group_mp4 = new GroupBox(i18n->TranslateString("File format"), Point(7, 11), Size(group_id3v2->GetWidth() / 2 - 4, 65));
50
51 option_mp4 = new OptionBox("MP4", Point(10, 13), Size(group_mp4->GetWidth() - 20, 0), &fileFormat, 1);
52 option_mp4->onAction.Connect(&ConfigureCoreAudio::SetFileFormat, this);
53
54 option_aac = new OptionBox("AAC", Point(10, 38), Size(group_mp4->GetWidth() - 20, 0), &fileFormat, 0);
55 option_aac->onAction.Connect(&ConfigureCoreAudio::SetFileFormat, this);
56
57 group_mp4->Add(option_mp4);
58 group_mp4->Add(option_aac);
59
60 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));
61
62 option_extension_m4a = new OptionBox(".m4a", Point(10, 13), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 0);
63 option_extension_m4b = new OptionBox(".m4b", Point(10, 38), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 1);
64 option_extension_m4r = new OptionBox(".m4r", Point(group_extension->GetWidth() / 2 + 4, 13), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 2);
65 option_extension_mp4 = new OptionBox(".mp4", Point(group_extension->GetWidth() / 2 + 4, 38), Size(group_extension->GetWidth() / 2 - 14, 0), &fileExtension, 3);
66
67 group_extension->Add(option_extension_m4a);
68 group_extension->Add(option_extension_m4b);
69 group_extension->Add(option_extension_m4r);
70 group_extension->Add(option_extension_mp4);
71
72 i18n->SetContext("Encoders::AAC::Codec");
73
74 layer_quality = new Layer(i18n->TranslateString("Codec"));
75
76 group_codec = new GroupBox(i18n->TranslateString("Audio codec"), Point(7, 11), Size(group_id3v2->GetWidth(), 43));
77
78 text_codec = new Text(i18n->AddColon(i18n->TranslateString("Audio codec")), Point(10, 15));
79
80 combo_codec = new ComboBox(Point(text_codec->GetUnscaledTextSize().cx + 17, 12), Size(group_codec->GetWidth() - text_codec->GetUnscaledTextSize().cx - 27, 0));
81
82 /* Query supported formats from Core Audio
83 * and add them to the combo box list.
84 */
85 CA::UInt32 size = 0;
86
87 CA::AudioFormatGetPropertyInfo(CA::kAudioFormatProperty_EncodeFormatIDs, 0, NIL, &size);
88
89 CA::UInt32 *formats = new CA::UInt32 [size / sizeof(CA::UInt32)];
90
91 CA::AudioFormatGetProperty(CA::kAudioFormatProperty_EncodeFormatIDs, 0, NIL, &size, formats);
92
93 for (UnsignedInt i = 0; i < size / sizeof(CA::UInt32); i++)
94 {
95 if (formats[i] == CA::kAudioFormatMPEG4AAC) combo_codec->AddEntry("MPEG4 AAC Low Complexity");
96 else if (formats[i] == CA::kAudioFormatMPEG4AAC_HE) combo_codec->AddEntry("MPEG4 AAC High Efficiency");
97 else if (formats[i] == CA::kAudioFormatMPEG4AAC_HE_V2) combo_codec->AddEntry("MPEG4 AAC High Efficiency v2");
98 else if (formats[i] == CA::kAudioFormatMPEG4AAC_LD) combo_codec->AddEntry("MPEG4 AAC Low Delay");
99 else if (formats[i] == CA::kOptionalAudioFormatMPEG4AAC_ELD) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay");
100 else if (formats[i] == CA::kOptionalAudioFormatMPEG4AAC_ELD_SBR) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay SBR");
101 else if (formats[i] == CA::kOptionalAudioFormatMPEG4AAC_ELD_V2) combo_codec->AddEntry("MPEG4 AAC Enhanced Low Delay v2");
102 else if (formats[i] == CA::kAudioFormatMPEG4AAC_Spatial) combo_codec->AddEntry("MPEG4 AAC Spatial");
103 else if (formats[i] == CA::kAudioFormatAppleLossless) combo_codec->AddEntry("Apple Lossless Audio Codec");
104 else continue;
105
106 codecs.Add(formats[i]);
107
108 if ((UnsignedInt) config->GetIntValue(ConfigID, "Codec", CA::kAudioFormatMPEG4AAC) == formats[i]) combo_codec->SelectNthEntry(combo_codec->Length() - 1);
109 }
110
111 delete [] formats;
112
113 combo_codec->onSelectEntry.Connect(&ConfigureCoreAudio::SetCodec, this);
114
115 group_codec->Add(text_codec);
116 group_codec->Add(combo_codec);
117
118 i18n->SetContext("Encoders::AAC::Quality");
119
120 group_bitrate = new GroupBox(i18n->TranslateString("Bitrate"), Point(7, 66), Size(group_id3v2->GetWidth(), 43));
121
122 text_bitrate = new Text(i18n->AddColon(i18n->TranslateString("Bitrate per channel")), Point(10, 15));
123
124 text_bitrate_kbps = new Text(i18n->TranslateString("%1 kbps", "Technical").Replace("%1", NIL).Trim(), Point(35, 15));
125 text_bitrate_kbps->SetX(text_bitrate_kbps->GetUnscaledTextWidth() + 10);
126 text_bitrate_kbps->SetOrientation(OR_UPPERRIGHT);
127
128 edit_bitrate = new EditBox(String::FromInt(bitrate), Point(text_bitrate_kbps->GetX() + 32, 12), Size(25, 0), 3);
129 edit_bitrate->SetFlags(EDB_NUMERIC);
130 edit_bitrate->SetOrientation(OR_UPPERRIGHT);
131 edit_bitrate->onInput.Connect(&ConfigureCoreAudio::SetBitrateByEditBox, this);
132
133 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);
134 slider_bitrate->onValueChange.Connect(&ConfigureCoreAudio::SetBitrate, this);
135
136 group_bitrate->Add(text_bitrate);
137 group_bitrate->Add(slider_bitrate);
138 group_bitrate->Add(edit_bitrate);
139 group_bitrate->Add(text_bitrate_kbps);
140
141 SetCodec();
142
143 tabwidget->SetSize(group_id3v2->GetSize() + Size(18, 118));
144
145 Add(tabwidget);
146
147 tabwidget->Add(layer_quality);
148 tabwidget->Add(layer_format);
149
150 layer_format->Add(group_mp4);
151 layer_format->Add(group_extension);
152 layer_format->Add(group_id3v2);
153
154 layer_quality->Add(group_codec);
155 layer_quality->Add(group_bitrate);
156
157 SetSize(group_id3v2->GetSize() + Size(32, 132));
158 }
159
160 BoCA::ConfigureCoreAudio::~ConfigureCoreAudio()
161 {
162 DeleteObject(tabwidget);
163 DeleteObject(layer_format);
164 DeleteObject(layer_quality);
165
166 DeleteObject(group_mp4);
167 DeleteObject(option_mp4);
168 DeleteObject(option_aac);
169 DeleteObject(group_extension);
170 DeleteObject(option_extension_m4a);
171 DeleteObject(option_extension_m4b);
172 DeleteObject(option_extension_m4r);
173 DeleteObject(option_extension_mp4);
174 DeleteObject(group_id3v2);
175 DeleteObject(check_id3v2);
176 DeleteObject(text_note);
177 DeleteObject(text_id3v2);
178
179 DeleteObject(group_codec);
180 DeleteObject(text_codec);
181 DeleteObject(combo_codec);
182 DeleteObject(group_bitrate);
183 DeleteObject(text_bitrate);
184 DeleteObject(slider_bitrate);
185 DeleteObject(edit_bitrate);
186 DeleteObject(text_bitrate_kbps);
187 }
188
189 Int BoCA::ConfigureCoreAudio::SaveSettings()
190 {
191 Config *config = Config::Get();
192
193 config->SetIntValue(ConfigID, "Codec", codecs.GetNth(combo_codec->GetSelectedEntryNumber()));
194
195 if (bitrates.Length() == 2)
196 {
197 if (bitrate < bitrates.GetNth(0)) bitrate = bitrates.GetNth(0);
198 if (bitrate > bitrates.GetNth(1)) bitrate = bitrates.GetNth(1);
199 }
200
201 if (bitrates.Length() == 2) config->SetIntValue(ConfigID, "Bitrate", bitrate);
202 else if (bitrates.Length() > 2) config->SetIntValue(ConfigID, "Bitrate", bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1));
203
204 config->SetIntValue(ConfigID, "MP4Container", fileFormat);
205 config->SetIntValue(ConfigID, "MP4FileExtension", fileExtension);
206 config->SetIntValue(ConfigID, "AllowID3v2", allowID3);
207
208 return Success();
209 }
210
211 Void BoCA::ConfigureCoreAudio::SetCodec()
212 {
213 CA::UInt32 format = codecs.GetNth(combo_codec->GetSelectedEntryNumber());
214 CA::UInt32 size = 0;
215
216 CA::AudioFormatGetPropertyInfo(CA::kAudioFormatProperty_AvailableEncodeBitRates, sizeof(format), &format, &size);
217
218 CA::AudioValueRange *bitrateValues = new CA::AudioValueRange [size / sizeof(CA::AudioValueRange)];
219
220 CA::AudioFormatGetProperty(CA::kAudioFormatProperty_AvailableEncodeBitRates, sizeof(format), &format, &size, bitrateValues);
221
222 if (size > 0 && bitrates.Length() > 2) bitrate = bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1);
223
224 bitrates.RemoveAll();
225
226 for (UnsignedInt i = 0; i < size / sizeof(CA::AudioValueRange); i++)
227 {
228 if (bitrateValues[i].mMinimum / 1000 > 192 && bitrateValues[i].mMaximum / 1000 > 192) continue;
229 if ( bitrateValues[i].mMaximum / 1000 > 192) bitrateValues[i].mMaximum = 192 * 1000;
230
231 bitrates.Add(bitrateValues[i].mMinimum / 1000);
232 bitrates.Add(bitrateValues[i].mMaximum / 1000);
233 }
234
235 delete [] bitrateValues;
236
237 if (bitrates.Length() > 0) group_bitrate->Activate();
238 else group_bitrate->Deactivate();
239
240 for (Int i = 0; i < bitrates.Length() / 2; i++)
241 {
242 if (bitrate == bitrates.GetNth(i * 2 + 1)) bitrate = -(bitrates.Length() / 2) + i;
243 }
244
245 if (bitrates.Length() == 2) { edit_bitrate->Activate(); slider_bitrate->SetRange(bitrates.GetNth(0), bitrates.GetNth(1)); }
246 else if (bitrates.Length() > 2) { edit_bitrate->Deactivate(); slider_bitrate->SetRange(-bitrates.Length() / 2, -1); }
247
248 if (codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatAppleLossless)
249 {
250 group_mp4->Deactivate();
251
252 fileFormat = 1;
253
254 option_extension_m4r->Deactivate();
255
256 if (fileExtension == 2) fileExtension = 0;
257 }
258 else if (codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC ||
259 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_HE ||
260 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_HE_V2 ||
261 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_LD ||
262 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kOptionalAudioFormatMPEG4AAC_ELD ||
263 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kOptionalAudioFormatMPEG4AAC_ELD_SBR ||
264 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kOptionalAudioFormatMPEG4AAC_ELD_V2 ||
265 codecs.GetNth(combo_codec->GetSelectedEntryNumber()) == CA::kAudioFormatMPEG4AAC_Spatial)
266 {
267 group_mp4->Activate();
268
269 option_extension_m4r->Activate();
270 }
271
272 SetBitrate();
273 SetFileFormat();
274 }
275
276 Void BoCA::ConfigureCoreAudio::SetBitrate()
277 {
278 if (bitrates.Length() == 0) return;
279
280 if (!edit_bitrate->IsFocussed())
281 {
282 if (bitrates.Length() == 2) edit_bitrate->SetText(String::FromInt(bitrate));
283 else edit_bitrate->SetText(String::FromInt(bitrates.GetNth((bitrates.Length() / 2 + bitrate) * 2 + 1)));
284 }
285 }
286
287 Void BoCA::ConfigureCoreAudio::SetBitrateByEditBox()
288 {
289 slider_bitrate->SetValue(edit_bitrate->GetText().ToInt());
290 }
291
292 Void BoCA::ConfigureCoreAudio::SetFileFormat()
293 {
294 if (fileFormat == 1) // MP4 container
295 {
296 group_id3v2->Deactivate();
297
298 group_extension->Activate();
299 }
300 else // raw AAC file format
301 {
302 group_id3v2->Activate();
303
304 group_extension->Deactivate();
305 }
306 }