File: format.py

package info (click to toggle)
idjc 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 9,628 kB
  • sloc: python: 20,415; ansic: 15,941; sh: 4,869; makefile: 189; sed: 16
file content (1441 lines) | stat: -rw-r--r-- 62,154 bytes parent folder | download
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
#   format.py: format selection user interface widgets
#   Copyright (C) 2012 Stephen Fairchild (s-fairchild@users.sourceforge.net)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program in the file entitled COPYING.
#   If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import json
import gettext
import ctypes
from abc import ABCMeta, abstractmethod, abstractproperty

import gtk
import gobject

from idjc import FGlobs
from .gtkstuff import LEDDict
from .tooltips import set_tip


_ = gettext.translation(FGlobs.package_name, FGlobs.localedir,
                                                        fallback=True).gettext


class TestEncoder(object):
    __metaclass__ = ABCMeta

    @abstractproperty
    def default_bitrate_stereo(self):
        """A guaranteed good bitrate when encoding 2 channels."""

        return int()

    @abstractproperty
    def default_bitrate_mono(self):
        """A guaranteed good bitrate when encoding 1 channel.
        
        Ideally the same value as for stereo if possible."""

        return int()
        
    @abstractproperty
    def default_samplerate(self):
        """Typically 44100Hz unless the encoder won't support this."""
        
        return int()

    @abstractproperty
    def suggested_samplerates(self):
        """Useful samplerates for user interface dropdown selection.
        
        These values are for EncoderRange to verify.
        """
        
        return tuple()

    @abstractproperty
    def suggested_bitrates(self):
        """Useful bitrates for user interface dropdown selection.
        
        These values are for EncoderRange to verify.
        """
        
        return tuple()

    @abstractmethod
    def test(self, channels, samplerate, bitrate):
        return bool()


class VorbisTestEncoder(TestEncoder):
    class _VORBIS_INFO(ctypes.Structure):
        _fields_ = [("version", ctypes.c_int), 
                    ("channels", ctypes.c_int),
                    ("rate", ctypes.c_long),
                    ("bitrate_upper", ctypes.c_long),
                    ("bitrate_nominal", ctypes.c_long),
                    ("bitrate_lower", ctypes.c_long),
                    ("bitrate_window", ctypes.c_long),
                    ("codec_setup", ctypes.c_void_p)]


    _lv = ctypes.CDLL("libvorbis.so.0")
    _lve = ctypes.CDLL("libvorbisenc.so.2")

    def __init__(self):
        self._vi = self._VORBIS_INFO()

    @property
    def default_bitrate_stereo(self):
        return 128000

    @property
    def default_bitrate_mono(self):
        return 128000

    @property
    def default_samplerate(self):
        return 44100

    @property
    def suggested_samplerates(self):
        return (48000, 44100, 32000, 22050, 11025)

    @property
    def suggested_bitrates(self):
        return (192000, 160000, 128000, 112000, 96000, 80000, 64000, 48000, 45000, 32000)

    def test(self, channels, samplerate, bitrate):
        """Test run these encoder settings with a vorbis encoder.
        
        A return value of True indicates that encoding would work for the
        provided settings.
        """

        vi = self._vi

        self._lv.vorbis_info_init(ctypes.byref(vi))
        error_code = self._lve.vorbis_encode_init(ctypes.byref(vi),
                ctypes.c_long(channels), ctypes.c_long(samplerate),
                ctypes.c_long(-1), ctypes.c_long(bitrate), ctypes.c_long(-1))
        self._lv.vorbis_info_clear(ctypes.byref(vi))

        return error_code == 0


class EncoderRange(object):
    """Test out the limits of an encoder's settings."""

    def __init__(self, encoder):
        """An instance of EncoderRange can probe one type of encoder.
        
        @encoder: an instance of TestEncoder
        """
        
        self._encoder = encoder
        self._test = encoder.test
        self._working_bitrate = {1: encoder.default_bitrate_mono,
                                 2: encoder.default_bitrate_stereo}

    def _boundary_search(self, variable_span, test):
        """Encoder working boundary value finder.
        
        @variable_span is a list of two integers that form the search range.
        The algorithm will find the lowest limit value if the first value is
        smaller and the highest limit value if the first value is bigger.
        
        @test is a function that takes one value, the variable under test and
        returns boolean true indicating success.
        """

        span_1 = variable_span
        found = None

        while 1:
            val1 = None
            span_2 = []
            for val2 in span_1:
                if val1 is None:
                    if abs(span_1[0] - span_1[-1]) + 1 == len(span_1):
                        return found
                    val1 = val2
                    continue

                span_2.append(val1)
                mid = abs(val1 - val2) // 2 + min(val1, val2)
                if min(val1, val2) < mid < max(val1, val2):
                    span_2.append(mid)
                    if test(mid):
                        found = mid
                        span_1 = [val1, mid]
                        val1 = None
                        break
                
                val1 = val2
            else:
                span_1 = [span_2[-1], val1] if found is not None \
                                                        else span_2 + [val1]

    def lowest_bitrate(self, channels, samplerate):
        """Calculate the lowest working bitrate."""

        return self._boundary_search([8000, 1000000],
            lambda bitrate: self._test(channels, samplerate, bitrate))

    def highest_bitrate(self, channels, samplerate):
        """Calculate the highest working bitrate."""

        return self._boundary_search([1000000, 8000],
            lambda bitrate: self._test(channels, samplerate, bitrate))

    def lowest_samplerate(self, channels, bitrate):
        """Calculate the lowest working samplerate."""

        return self._boundary_search([4000, 200000],
            lambda samplerate: self._test(channels, samplerate, bitrate))

    def highest_samplerate(self, channels, bitrate):
        """Calculate the highest working samplerate."""

        return self._boundary_search([200000, 4000],
            lambda samplerate: self._test(channels, samplerate, bitrate))

    def bitrate_bounds(self, channels, samplerate):
        """Lowest and highest working bitrate as a 2 tuple."""

        return self.lowest_bitrate(channels, samplerate), \
                                self.highest_bitrate(channels, samplerate)

    def samplerate_bounds(self, channels, bitrate):
        """Lowest and highest working samplerate as a 2 tuple."""
        
        return self.lowest_samplerate(channels, bitrate), \
                                self.highest_samplerate(channels, bitrate)

    def bounds(self, channels):
        """Find the absolute lowest and highest encoder supported settings.
        
        Return value: dictionary containing 2 tuples for samplerate and bitrate
        Inputs:
        @channels: 1 for mono, 2 for stereo.
        """
        
        srb = self.samplerate_bounds(channels, self._working_bitrate[channels])
        oldbrb = brb = oldsrb = None, None
        
        while brb != oldbrb or oldsrb != srb:
            oldbrb = brb
            oldsrb = srb
     
            brb = (self.bitrate_bounds(channels, srb[0])[0],
                                self.bitrate_bounds(channels, srb[1])[1])
            srb = (self.samplerate_bounds(channels, brb[0])[0],
                                self.samplerate_bounds(channels, brb[1])[1])
        
        return {"samplerate_bounds": srb, "bitrate_bounds": brb}

    def good_samplerates(self, channels, bitrate=None):
        """Returns a tuple of standard suggestion values."""
        
        if bitrate is None:
            lower, upper = self.bounds(channels)["samplerate_bounds"]
        else:
            lower, upper = self.samplerate_bounds(channels, bitrate)
        
        return tuple(x for x in self._encoder.suggested_samplerates if
                                                        lower <= x <= upper)

    def good_bitrates(self, channels, samplerate=None):
        """Returns a tuple of standard suggestion values."""

        if samplerate is None:
            lower, upper = self.bounds(channels)["bitrate_bounds"]
        else:
            lower, upper = self.bitrate_bounds(channels, samplerate)

        return tuple(x for x in self._encoder.suggested_bitrates if
                                                        lower <= x <= upper)


def format_collate(specifier):
    """Takes a FormatDropdown or FormatSpin object, obtains the settings."""
    
    d = {}
    if specifier.prev_object is not None:
        d.update(format_collate(specifier.prev_object))
    
    d[specifier.ident] = specifier.value
    if not specifier.applied:
        d["__unapplied__"] = specifier.ident
    return d


class FormatDropdown(gtk.VBox):
    def __init__(self, prev_object, title, ident, elements, row, tooltip=None):
        """Parameter 'elements' is a tuple of dictionaries.
        
        @title: appears above the widget
        @name: is the official name of the control element
        @elements: is tuple of dictionary objects mandatory keys of which are
            'display_text' and 'value'.
        """
        
        self.prev_object = prev_object
        self._ident = ident
        self._row = row
        gtk.VBox.__init__(self)
        frame = gtk.Frame(" %s " % title)
        frame.set_label_align(0.5, 0.5)
        if tooltip is not None:
            set_tip(frame, tooltip)
        self.pack_start(frame, fill=False)
        size_group = gtk.SizeGroup(gtk.SIZE_GROUP_VERTICAL)
        vbox = gtk.VBox()
        vbox.set_border_width(3)
        frame.add(vbox)
        
        model = gtk.ListStore(gobject.TYPE_PYOBJECT)
        default = 0
        for index, each in enumerate(elements):
            if "default" in each and each["default"]:
                default = index
            model.append(((each),))
        cell_text = gtk.CellRendererText()
        self._combo_box = gtk.ComboBox(model)
        size_group.add_widget(self._combo_box)
        self._combo_box.pack_start(cell_text)
        self._combo_box.set_cell_data_func(cell_text, self._cell_data_func)
        vbox.pack_start(self._combo_box, False)
        self._fixed = gtk.Label()
        size_group.add_widget(self._fixed)
        vbox.pack_start(self._fixed, False)
        self._fixed.set_no_show_all(True)

        self._combo_box.connect("changed", self._on_changed)
        self._combo_box.set_active(default)
        
        self.show_all()

    def _cell_data_func(self, cell_layout, cell, model, iter):
        dict_ = model.get_value(iter, 0)
        cell.props.text = dict_["display_text"]
        cell.props.sensitive = dict_.get("sensitive", True)

    def _on_changed(self, combo_box):
        text = combo_box.props.model[combo_box.props.active][0]["display_text"]
        self._fixed.set_text(text)

    @property
    def next_element_name(self):
        cbp = self._combo_box.props
        try:
            return cbp.model[cbp.active][0]["chain"]
        except KeyError:
            return None

    @property
    def applied(self):
        return self._fixed.props.visible
        
    @property
    def row(self):
        return self._row

    @property
    def ident(self):
        return self._ident

    def apply(self):
        cbp = self._combo_box.props
        if cbp.model[cbp.active][0].get("sensitive", True):
            self._combo_box.hide()
            self._fixed.show()
            return True
        else:
            return False
        
    def unapply(self):
        self._combo_box.show()
        self._fixed.hide()

    @property
    def value(self):
        cbp = self._combo_box.props
        return cbp.model[cbp.active][0]["value"]

    @value.setter
    def value(self, data):
        if not self.applied:
            cbp = self._combo_box.props
            for i, each in enumerate(cbp.model):
                if each[0]["value"] == data:
                    self._combo_box.set_active(i)
                    break


class FormatSpin(gtk.VBox):
    def __init__(self, prev_object, title, ident, elements, row, unit, next_element_name, suggested_values, tooltip=None):
        """Parameter 'elements' is a tuple of dictionaries.
        
        @title: appears above the widget
        @name: is the official name of the control element
        @elements: the values of the gtk.Adjustment as integers
        @unit: e.g. " Hz"
        @suggested_values: sequence of standard values
        """
        
        self.prev_object = prev_object
        self._ident = ident
        self._row = row
        self._unit = unit
        self._next_element_name = next_element_name
        gtk.VBox.__init__(self)
        frame = gtk.Frame(" %s " % title)
        frame.set_label_align(0.5, 0.5)
        if tooltip is not None:
            set_tip(frame, tooltip)
        self.pack_start(frame, fill=False)
        vbox = gtk.VBox()
        vbox.set_border_width(3)
        frame.add(vbox)
        
        adjustment = gtk.Adjustment(*(float(x) for x in elements))
        self._spin_button = gtk.SpinButton(adjustment)
        if suggested_values is not None:
            self._spin_button.connect("populate_popup", self._on_populate_popup, suggested_values)
            if tooltip is None:
                set_tip(self._spin_button, _('Right click for suggested values.'))
        vbox.pack_start(self._spin_button, False)
        self._fixed = gtk.Label()
        self._fixed.set_alignment(0.5, 0.5)
        vbox.pack_start(self._fixed)
        self._fixed.set_no_show_all(True)

        self._spin_button.connect("value-changed", self._on_changed)
        self._spin_button.emit("value-changed")
        
        self.show_all()
        size_group = gtk.SizeGroup(gtk.SIZE_GROUP_VERTICAL)
        size_group.add_widget(prev_object.get_children()[0])
        size_group.add_widget(frame)
        self.scale = 1

    def _on_changed(self, spin_button):
        self._fixed.set_text(str(int(spin_button.props.value)) + self._unit)

    def _on_populate_popup(self, spin, menu, values):
        mi = gtk.MenuItem(_('Suggested Values'))
        menu.append(mi)
        mi.show()
        submenu = gtk.Menu()
        mi.set_submenu(submenu)
        submenu.show()
        for each in values:
            mi = gtk.MenuItem(str(each))
            mi.connect("activate", self._on_popup_activate, spin, each)
            submenu.append(mi)
            mi.show()

    def _on_popup_activate(self, menuitem, spin, value):
        spin.set_value(value)

    @property
    def next_element_name(self):
        return self._next_element_name

    @property
    def applied(self):
        return self._fixed.props.visible

    @property
    def row(self):
        return self._row

    @property
    def ident(self):
        return self._ident

    def apply(self):
        self._spin_button.hide()
        self._fixed.show()
        return True
        
    def unapply(self):
        self._spin_button.show()
        self._fixed.hide()
        
    @property
    def value(self):
        return str(int(self._spin_button.props.value * self.scale))

    @value.setter
    def value(self, value):
        if not self.applied:
            self._spin_button.props.value = int(value) // self.scale
            
    @property
    def scale(self):
        return self._scale

    @scale.setter
    def scale(self, value):
        self._scale = value


class FormatPregain(FormatDropdown):
    """Level adjustment for audio before hitting the codec."""
    
    def __init__(self, prev_object):
        codec = format_collate(prev_object)["codec"]
        
        FormatDropdown.__init__(self, prev_object, _('Pregain'), "pregain", (
            dict(display_text=_('0 dB'), value="1.0"),
            dict(display_text=_('-0.5 dB'), value="0.944"),
            dict(display_text=_('-1.0 dB'), value="0.891", default=(codec in ("aac", "aacpv2"))),
            dict(display_text=_('-1.5 dB'), value="0.841"),
            dict(display_text=_('-2.0 dB'), value="0.794", default=(codec in ("mp2", "mp3"))),
            dict(display_text=_('-2.5 dB'), value="0.750"),
            dict(display_text=_('-3.0 dB'), value="0.708", default=(codec == "speex"))), 1,
            _("A blanket gain reduction to promote audio quality when using long established lossy audio codecs like mp3 with loud audio material.\n\n"
            "ReplayGain makes this feature generally unnecessary and the correct setting in that case is 0 dB."))


class FormatResampleQuality(FormatDropdown):
    """Resample quality."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Resample Quality'), "resample_quality", (
            dict(display_text=_('Highest'), value="highest", chain="FormatPregain"),
            dict(display_text=_('Medium'), value="medium", chain="FormatPregain", default=True),
            dict(display_text=_('Lowest'), value="lowest", chain="FormatPregain")), 1,
            _("All of these settings will provide adequate audio quality. The highest setting will preserve more of the original audio bandwidth at the expense of many CPU cycles."))


class FormatMetadataChoice(FormatDropdown):
    """User can select the metadata encoding format."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Metadata'), "metadata_mode", (
            dict(display_text=_('Suppressed'), value="suppressed", chain="FormatResampleQuality"),
            dict(display_text=_('UTF-8'), value="utf-8", chain="FormatResampleQuality"),
            dict(display_text=_('Latin1 *'), value="latin1", chain="FormatResampleQuality", default=True)), 1,
            _("This affects the stream metadata only. Recordings will use UTF-8 for their metadata."))


class FormatMetadataNone(FormatDropdown):
    """User can not select any metadata."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Metadata'), "metadata_mode", (
            dict(display_text=_('Unsupported'), value="suppressed", chain="FormatResampleQuality"),), 1,
            _("Choose whether the stream will carry dynamic metadata. In the case of Ogg streams this is important as a great many players can't handle chained Ogg streams which result from the metadata updates."))


class FormatMetadataUTF8(FormatDropdown):
    """User can select whether to have metadata."""
    
    def __init__(self, prev_object):
        discourage = format_collate(prev_object)["family"] == "webm"
        
        FormatDropdown.__init__(self, prev_object, _('Metadata'), "metadata_mode", (
            dict(display_text=_('Suppressed'), value="suppressed", chain="FormatResampleQuality", default=discourage),
            dict(display_text=_('UTF-8'), value="utf-8", chain="FormatResampleQuality", default=not discourage)), 1,
            _("Choose whether the stream will carry dynamic metadata. In the case of Ogg streams this is important as a great many players can't handle chained Ogg streams which result from the metadata updates."))


class FormatMetadataLatin1(FormatDropdown):
    """User can select whether to have metadata."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Metadata'), "metadata_mode", (
            dict(display_text=_('Suppressed'), value="suppressed", chain="FormatResampleQuality"),
            dict(display_text=_('Latin1'), value="latin1", chain="FormatResampleQuality", default=True)), 1,
            _("Choose whether to send metadata. Recordings will use UTF-8 metadata."))


class FormatCodecMPEGMP2Mode(FormatDropdown):
    """MP2 modes."""
    
    def __init__(self, prev_object):

        bitrate = int(format_collate(prev_object)["bitrate"])
        if bitrate < 80:
            defmode = "mono"
        elif bitrate < 192:
            defmode = "jointstereo"
        else:
            defmode = "stereo"

        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", default=(defmode == "mono"), chain="FormatMetadataChoice"),
            dict(display_text=_("Stereo"), value="stereo", default=(defmode == "stereo"), chain="FormatMetadataChoice"),
            dict(display_text=_("Joint Stereo"), value="jointstereo", default=(defmode=="jointstereo"), chain="FormatMetadataChoice")), 0,
            _('Joint Stereo is a good choice on streams with low bitrates.'))


class FormatCodecMPEGMP2ModeStereo(FormatDropdown):
    """MP2 modes. No mono option due to previous bitrate choice."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", sensitive=False, chain="FormatMetadataLatin1"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatMetadataLatin1"),
            dict(display_text=_("Joint Stereo"), value="jointstereo", chain="FormatMetadataLatin1")), 0,
            _('Due to the high bitrate selected, this codec will only support stereo.'))


class FormatCodecMPEGMP2ModeSingle(FormatDropdown):
    """MP2 modes. Only mono available due to previous bitrate choice."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatMetadataLatin1"),
            dict(display_text=_("Stereo"), value="stereo", sensitive=False, chain="FormatMetadataLatin1"),
            dict(display_text=_("Joint Stereo"), value="jointstereo", sensitive=False, chain="FormatMetadataLatin1")), 0,
            _('Due to the low bitrate selected, this codec will only support mono.'))


class FormatCodecMPEGMP2V1BitRates(FormatDropdown):
    """MP2 MPEG1 bit rates. Some channel modes are restricted by bitrate."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Bitrate'), "bitrate", (
            dict(display_text="384 kbps", value="384", chain="FormatCodecMPEGMP2ModeStereo"),
            dict(display_text="320 kbps", value="320", chain="FormatCodecMPEGMP2ModeStereo"),
            dict(display_text="256 kbps", value="256", chain="FormatCodecMPEGMP2ModeStereo"),
            dict(display_text="224 kbps", value="224", chain="FormatCodecMPEGMP2ModeStereo"),
            dict(display_text="192 kbps", value="192", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="160 kbps", value="160", chain="FormatCodecMPEGMP2Mode", default=True),
            dict(display_text="128 kbps", value="128", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="112 kbps", value="112", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="96 kbps", value="96", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="80 kbps", value="80", chain="FormatCodecMPEGMP2ModeSingle"),
            dict(display_text="64 kbps", value="64", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="56 kbps", value="56", chain="FormatCodecMPEGMP2ModeSingle"),
            dict(display_text="48 kbps", value="48", chain="FormatCodecMPEGMP2ModeSingle"),
            dict(display_text="32 kbps", value="32", chain="FormatCodecMPEGMP2ModeSingle")), 0)


class FormatCodecMPEGMP2V2BitRates(FormatDropdown):
    """MP2 MPEG2 bit rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Bitrate'), "bitrate", (
            dict(display_text="160 kbps", value="160", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="144 kbps", value="144", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="128 kbps", value="128", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="112 kbps", value="112", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="96 kbps", value="96", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="80 kbps", value="80", chain="FormatCodecMPEGMP2Mode", default=True),
            dict(display_text="64 kbps", value="64", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="56 kbps", value="56", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="48 kbps", value="48", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="40 kbps", value="40", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="32 kbps", value="32", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="24 kbps", value="24", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="16 kbps", value="16", chain="FormatCodecMPEGMP2Mode"),
            dict(display_text="8 kbps", value="8", chain="FormatCodecMPEGMP2Mode")), 0)


class FormatCodecMPEGMP2V1SampleRates(FormatDropdown):
    """MP2 MPEG1 sample rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text="48000 Hz", value="48000", chain="FormatCodecMPEGMP2V1BitRates"),
            dict(display_text="44100 Hz", value="44100", chain="FormatCodecMPEGMP2V1BitRates", default=True),
            dict(display_text="32000 Hz", value="32000", chain="FormatCodecMPEGMP2V1BitRates")), 0)


class FormatCodecMPEGMP2V2SampleRates(FormatDropdown):
    """MP2 MPEG2 sample rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text="24000 Hz", value="24000", chain="FormatCodecMPEGMP2V2BitRates"),
            dict(display_text="22050 Hz", value="22050", chain="FormatCodecMPEGMP2V2BitRates", default=True),
            dict(display_text="16000 Hz", value="16000", chain="FormatCodecMPEGMP2V2BitRates")), 0)


class FormatCodecMPEGMP2(FormatDropdown):
    """MP2 standard selection."""
    
    def __init__(self, prev_object):
        # TC: Abbreviation of the word, standard.
        FormatDropdown.__init__(self, prev_object, _('Std.'), "standard", (
            # TC: v stands for version.
            dict(display_text=_("V 1"), value="1", chain="FormatCodecMPEGMP2V1SampleRates"),
            # TC: v stands for version.
            dict(display_text=_("V 2"), value="2", chain="FormatCodecMPEGMP2V2SampleRates")), 0,
            _('MPEG2 introduced lower samplerate options and corresponding lower bitrates. Choose 2 if those are required.'))


class FormatCodecMPEGMP3Quality(FormatDropdown):
    """MP3 quality."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Quality'), "quality", (
            dict(display_text=_('0 most'), value="0", chain="FormatMetadataChoice"),
            dict(display_text="1", value="1", chain="FormatMetadataChoice"),
            # TC: * means is the recommended setting.
            dict(display_text=_("2 *"), value="2", chain="FormatMetadataChoice", default=True)) + tuple(
            dict(display_text=str(x), value=str(x), chain="FormatMetadataChoice") for x in range(3, 10)), 0,
            _('Higher quality costs more in terms of CPU cycles.'))


class FormatCodecMPEGMP3Mode(FormatDropdown):
    """MP3 modes."""
    
    def __init__(self, prev_object):
        bitrate = int(format_collate(prev_object)["bitrate"])
        if bitrate < 64:
            defmode = "mono"
        elif bitrate < 160:
            defmode = "jointstereo"
        else:
            defmode = "stereo"    

        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", default=(defmode == "mono"), chain="FormatCodecMPEGMP3Quality"),
            dict(display_text=_("Stereo"), value="stereo", default=(defmode == "stereo"), chain="FormatCodecMPEGMP3Quality"),
            dict(display_text=_("Joint Stereo"), value="jointstereo", default=(defmode == "jointstereo"), chain="FormatCodecMPEGMP3Quality")), 0,
            _('Joint Stereo is a good choice on streams with low bitrates'))


class FormatCodecMPEGMP3V1BitRates(FormatDropdown):
    """MP3 MPEG1 bit rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Bitrate'), "bitrate", (
            dict(display_text="320 kbps", value="320", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="256 kbps", value="256", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="224 kbps", value="224", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="192 kbps", value="192", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="160 kbps", value="160", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="128 kbps", value="128", chain="FormatCodecMPEGMP3Mode", default=True),
            dict(display_text="112 kbps", value="112", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="96 kbps", value="96", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="80 kbps", value="80", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="64 kbps", value="64", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="56 kbps", value="56", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="48 kbps", value="48", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="40 kbps", value="40", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="32 kbps", value="32", chain="FormatCodecMPEGMP3Mode")), 0)


class FormatCodecMPEGMP3V2BitRates(FormatDropdown):
    """MP3 MPEG2 and 2.5 bit rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Bitrate'), "bitrate", (
            dict(display_text="160 kbps", value="160", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="144 kbps", value="144", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="128 kbps", value="128", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="112 kbps", value="112", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="96 kbps", value="96", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="80 kbps", value="80", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="64 kbps", value="64", chain="FormatCodecMPEGMP3Mode", default=True),
            dict(display_text="56 kbps", value="56", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="48 kbps", value="48", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="40 kbps", value="40", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="32 kbps", value="32", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="24 kbps", value="24", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="16 kbps", value="16", chain="FormatCodecMPEGMP3Mode"),
            dict(display_text="8 kbps", value="8", chain="FormatCodecMPEGMP3Mode")), 0)


class FormatCodecMPEGMP3V1SampleRates(FormatDropdown):
    """MP3 MPEG1 sample rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text="48000 Hz", value="48000", chain="FormatCodecMPEGMP3V1BitRates"),
            dict(display_text="44100 Hz", value="44100", chain="FormatCodecMPEGMP3V1BitRates", default=True),
            dict(display_text="32000 Hz", value="32000", chain="FormatCodecMPEGMP3V1BitRates")), 0)


class FormatCodecMPEGMP3V2SampleRates(FormatDropdown):
    """MP3 MPEG2 sample rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text="24000 Hz", value="24000", chain="FormatCodecMPEGMP3V2BitRates"),
            dict(display_text="22050 Hz", value="22050", chain="FormatCodecMPEGMP3V2BitRates", default=True),
            dict(display_text="16000 Hz", value="16000", chain="FormatCodecMPEGMP3V2BitRates")), 0)


class FormatCodecMPEGMP3V2_5SampleRates(FormatDropdown):
    """MP3 MPEG2.5 non standard sample rates."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text="12000 Hz", value="12000", chain="FormatCodecMPEGMP3V2BitRates"),
            dict(display_text="11025 Hz", value="11025", chain="FormatCodecMPEGMP3V2BitRates", default=True),
            dict(display_text="8000 Hz", value="8000", chain="FormatCodecMPEGMP3V2BitRates")), 0)


class FormatCodecMPEGMP3(FormatDropdown):
    """MP3 standard selection."""
    
    def __init__(self, prev_object):
        # TC: Abbreviation of the word, standard.
        FormatDropdown.__init__(self, prev_object, _('Std.'), "standard", (
            # TC: v stands for version.
            dict(display_text=_("V 1"), value="1", chain="FormatCodecMPEGMP3V1SampleRates"),
            # TC: v stands for version.
            dict(display_text=_("V 2"), value="2", chain="FormatCodecMPEGMP3V2SampleRates"),
            # TC: v stands for version.
            dict(display_text=_("V 2.5"), value="2.5", chain="FormatCodecMPEGMP3V2_5SampleRates")), 0)


class FormatCodecSpeexComplexity(FormatDropdown):
    """Speex cpu usage selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Complexity'), "complexity", 
            tuple(dict(display_text=str(x), value=str(x), chain="FormatMetadataUTF8", default=(x==5))
                                                            for x in range(9, -1, -1)), 0,
            _('A quality setting that affects how heavily the CPU is used.'))


class FormatCodecSpeexQuality(FormatDropdown):
    """Speex quality selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Quality'), "quality", 
            tuple(dict(display_text=str(x), value=str(x), default=(x==8), chain="FormatCodecSpeexComplexity")
                                                            for x in range(9, -1, -1)), 0,
            _('The higher this setting, the higher the bitrate.'))


class FormatCodecSpeexBandwidth(FormatDropdown):
    """Speex bandwidth selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Bandwidth'), "samplerate", (
            dict(display_text=_("Ultrawide"), value="32000", chain="FormatCodecSpeexQuality"),
            dict(display_text=_("Wide"), value="16000", chain="FormatCodecSpeexQuality"),
            dict(display_text=_("Narrow"), value="8000", chain="FormatCodecSpeexQuality")), 0,
            _('Essentially a samplerate setting.'))


class FormatCodecSpeexMode(FormatDropdown):
    """Speex mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecSpeexBandwidth"),
            dict(display_text=_("Stereo"), value="stereo", chain="FormatCodecSpeexBandwidth")), 0)


class FormatCodecFLACBits(FormatDropdown):
    """FLAC bit width selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Width'), "bitwidth", (
            dict(display_text=_("24 bits"), value="24", chain="FormatMetadataUTF8"),
            dict(display_text=_("20 bits"), value="20", chain="FormatMetadataUTF8"),
            dict(display_text=_("16 bits"), value="16", chain="FormatMetadataUTF8")), 0,
            _('24 bit records with the highest level of detail. If file size is a concern maybe FLAC is not the right codec.'))


class FormatCodecVorbisVariability(FormatDropdown):
    """Vorbis bit rate variability."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Variability'), "variability", (
            dict(display_text=_("Constant"), value="0", chain="FormatMetadataUTF8"),
            dict(display_text=_(u"\u00B110%"), value="10", chain="FormatMetadataUTF8"),
            dict(display_text=_(u"\u00B120%"), value="20", chain="FormatMetadataUTF8"),
            dict(display_text=_(u"\u00B130%"), value="30", chain="FormatMetadataUTF8"),
            dict(display_text=_(u"\u00B140%"), value="40", chain="FormatMetadataUTF8"),
            dict(display_text=_(u"\u00B150%"), value="50", chain="FormatMetadataUTF8")), 0,
            _('This control is for enabling variable bitrate on Vorbis streams.'))


class FormatCodecVorbisBitRate(FormatSpin):
    """Vorbis bit rate selection."""
    
    def __init__(self, prev_object):
        dict_ = format_collate(prev_object)
        channels = 1 if dict_["mode"] == "mono" else 2
        er = EncoderRange(VorbisTestEncoder())
        sr = int(dict_["samplerate"])
        bounds = er.bitrate_bounds(channels, sr)
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            (128000,) + bounds + (1, 10), 0, " bps", "FormatCodecVorbisVariability",
            er.good_bitrates(channels, sr))


class FormatCodecVorbisSampleRate(FormatSpin):
    """Vorbis sample rate selection."""
    
    def __init__(self, prev_object):
        channels = 1 if format_collate(prev_object)["mode"] == "mono" else 2
        er = EncoderRange(VorbisTestEncoder())
        bounds = er.bounds(channels)["samplerate_bounds"]
        FormatSpin.__init__(self, prev_object, _('Samplerate'), "samplerate",
            (44100,) + bounds + (1, 10), 0, " Hz", "FormatCodecVorbisBitRate",
            er.good_samplerates(channels))


class FormatCodecFLACSampleRate(FormatSpin):
    """FLAC sample rate selection."""
    
    
    def __init__(self, prev_object):
        FormatSpin.__init__(self, prev_object, _('Samplerate'), "samplerate",
            (44100, 1, 655350, 1, 10), 0, " Hz", "FormatCodecFLACBits",
            (96000, 88200, 48000, 44100))


class FormatCodecFLACMode(FormatDropdown):
    """Speex mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecFLACSampleRate"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatCodecFLACSampleRate")), 0)


class FormatCodecVorbisMode(FormatDropdown):
    """Vorbis mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecVorbisSampleRate"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatCodecVorbisSampleRate")), 0)


class FormatCodecOpusPostGain(FormatDropdown):
    """Level adjustment for audio before hitting the codec."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Postgain'), "postgain", (
            dict(display_text=_('3.0 dB'), value="768", chain="FormatMetadataUTF8"),
            dict(display_text=_('2.5 dB'), value="640", chain="FormatMetadataUTF8"),
            dict(display_text=_('2.0 dB'), value="512", chain="FormatMetadataUTF8"),
            dict(display_text=_('1.5 dB'), value="384", chain="FormatMetadataUTF8"),
            dict(display_text=_('1.0 dB'), value="256", chain="FormatMetadataUTF8"),
            dict(display_text=_('0.5 dB'), value="128", chain="FormatMetadataUTF8"),
            dict(display_text=_('0 dB'), value="0", default=True, chain="FormatMetadataUTF8"),
            dict(display_text=_('-0.5 dB'), value="-128", chain="FormatMetadataUTF8"),
            dict(display_text=_('-1.0 dB'), value="-256", chain="FormatMetadataUTF8"),
            dict(display_text=_('-1.5 dB'), value="-384", chain="FormatMetadataUTF8"),
            dict(display_text=_('-2.0 dB'), value="-512", chain="FormatMetadataUTF8"),
            dict(display_text=_('-2.5 dB'), value="-640", chain="FormatMetadataUTF8"),
            dict(display_text=_('-3.0 dB'), value="-768", chain="FormatMetadataUTF8")), 1,
            _("A gain adjustment for the player to apply."))


class FormatCodecOpusVariability(FormatDropdown):
    """Set VBR, CBR, etc."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Variability'), "variability", (
            dict(display_text=_('CBR *'), value="cbr", default=True, chain="FormatCodecOpusPostGain"),
            dict(display_text=_('CVBR'), value="cvbr", chain="FormatCodecOpusPostGain"),
            dict(display_text=_('VBR'), value="vbr", chain="FormatCodecOpusPostGain")), 0,
            _("Bitrate variability. Actual VBR operation may require a higher frame size."))


class FormatCodecOpusFrameSize(FormatDropdown):
    """Size of Opus frames in milliseconds."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Frame Size'), "framesize", (
            dict(display_text=_('60 ms'), value="60", chain="FormatCodecOpusVariability"),
            dict(display_text=_('40 ms'), value="40", chain="FormatCodecOpusVariability"),
            dict(display_text=_('20 ms'), value="20", default=True, chain="FormatCodecOpusVariability")), 0,
            _("A higher frame size may sound better on very low bitrates."))


class FormatCodecOpusComplexity(FormatDropdown):
    """Opus cpu usage selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('CPU'), "complexity", 
            tuple(dict(display_text=str(x), value=str(x), chain="FormatCodecOpusFrameSize", default=(x==10))
                                                            for x in range(10, -1, -1)), 0,
            _('The encoder complexity setting which affects CPU load.'))


class FormatCodecOpusBitRate(FormatSpin):
    """Opus bit rate selection."""
    
    def __init__(self, prev_object):
        dict_ = format_collate(prev_object)
        channels = 1 if dict_["mode"] == "mono" else 2
        bounds = (6 * channels, 256 * channels)
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            ((64, 96)[channels - 1],) + bounds + (1, 10), 0, " kbps", "FormatCodecOpusComplexity",
            (256 * channels, 128 * channels, 64 * channels, 48 * channels, 32 * channels, 16 * channels))


class FormatCodecOpusMode(FormatDropdown):
    """Opus mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecOpusBitRate"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatCodecOpusBitRate")), 0)


class FormatCodecXiphOgg(FormatDropdown):
    """Ogg codec selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Codec'), "codec", (
            dict(display_text=_('Vorbis'), value="vorbis", chain="FormatCodecVorbisMode"),
            dict(display_text=_('FLAC'), value="flac", chain="FormatCodecFLACMode", sensitive=FGlobs.oggflacenabled),
            dict(display_text=_('Speex'), value="speex", chain="FormatCodecSpeexMode", sensitive=FGlobs.speexenabled),
            dict(display_text=_('Opus'), value="opus", chain="FormatCodecOpusMode", sensitive=FGlobs.opusenabled),
            
            ), 0,
            _('Codecs of the Ogg container.'))


class FormatCodecMPEGAACMode(FormatDropdown):
    """AAC mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatMetadataUTF8"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatMetadataUTF8")), 0)


class FormatCodecMPEGAACModeStereo(FormatDropdown):
    """AAC+ v2 mode selection which is always stereo."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", sensitive=False),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatMetadataUTF8")), 0)


class FormatCodecMPEGAACBitrate(FormatSpin):
    """AAC bit rate selection."""
    
    def __init__(self, prev_object):
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            (128000, 32000, 320000, 1, 10), 0, " bps", "FormatCodecMPEGAACMode",
            (192000, 160000, 128000, 112000, 96000, 80000, 64000, 48000, 45000, 32000))



class FormatCodecMPEGAACPlusV2Bitrate(FormatSpin):
    """AAC bit rate selection."""
    
    def __init__(self, prev_object):
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            (64000, 16000, 72000, 1, 10), 0, " bps", "FormatCodecMPEGAACModeStereo",
            (72000, 64000, 48000, 45000, 32000, 24000, 16000))



class FormatCodecMPEGAACSamplerate(FormatDropdown):
    """Sample rates as allowed when using the ADTS header."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text=_('96000 Hz'), value="96000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('88200 Hz'), value="88200", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('64000 Hz'), value="64000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('48000 Hz'), value="48000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('44100 Hz'), value="44100", chain="FormatCodecMPEGAACBitrate", default=True),
            dict(display_text=_('32000 Hz'), value="32000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('24000 Hz'), value="24000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('22050 Hz'), value="22050", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('16000 Hz'), value="16000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('12000 Hz'), value="12000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('11025 Hz'), value="11025", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('8000 Hz'), value="8000", chain="FormatCodecMPEGAACBitrate"),
            dict(display_text=_('7350 Hz'), value="7350", chain="FormatCodecMPEGAACBitrate")), 0)


class FormatCodecMPEGAACPlusV2Samplerate(FormatDropdown):
    """Sample rates as allowed when using the ADTS header."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Samplerate'), "samplerate", (
            dict(display_text=_('48000 Hz'), value="48000", chain="FormatCodecMPEGAACPlusV2Bitrate"),
            dict(display_text=_('44100 Hz'), value="44100", chain="FormatCodecMPEGAACPlusV2Bitrate", default=True)), 0)


class FormatCodecMPEG(FormatDropdown):
    """MPEG codec selection."""

    def __init__(self, prev_object):
        have_mp2 = FGlobs.twolameenabled
        have_mp3 = FGlobs.have_libmp3lame
        have_aac = FGlobs.avenabled
        extra = []

        if not have_mp2:
            extra.append(_('The MP2 option requires IDJC be rebuilt against libtwolame.'))

        if not have_mp3:
            extra.append(_('Enable the MP3 option by installing libmp3lame.'))

        if not have_aac:
            extra.append(_('The AAC options require IDJC be rebuilt against libav libraries.'))
        else:
            if not FormatCodecMPEG.aac_enabled:
                extra.append(_('Specific AAC support not present in libavcodec. More info: idjc.sourceforge.net.'))
            if not FormatCodecMPEG.aacpv2_enabled:
                extra.append(_('Specific AAC+ support not present in libavcodec. More info: idjc.sourceforge.net.'))

        spc = "\n\n\n" if extra else ""
        tooltip = _('Codecs of the MPEG family.') + spc + "\n\n".join(extra)
        
        FormatDropdown.__init__(self, prev_object, _('Codec'), "codec", (
            dict(display_text=_('MP2'), value="mp2", chain="FormatCodecMPEGMP2", sensitive=have_mp2),
            dict(display_text=_('MP3'), value="mp3", chain="FormatCodecMPEGMP3", sensitive=have_mp3, default=have_mp3),
            dict(display_text=_('AAC'), value="aac", chain="FormatCodecMPEGAACSamplerate", sensitive=have_aac and FormatCodecMPEG.aac_enabled),
            dict(display_text=_('AAC+ v2'), value="aacpv2", chain="FormatCodecMPEGAACPlusV2Samplerate", sensitive=have_aac and FormatCodecMPEG.aacpv2_enabled)), 0,
            tooltip)


class FormatCodecWebMVorbisBitRate(FormatSpin):
    """Vorbis bit rate selection."""
    
    def __init__(self, prev_object):
        dict_ = format_collate(prev_object)
        channels = 1 if dict_["mode"] == "mono" else 2
        er = EncoderRange(VorbisTestEncoder())
        sr = int(dict_["samplerate"])
        bounds = er.bitrate_bounds(channels, sr)
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            (128000,) + bounds + (1, 10), 0, " bps", "FormatMetadataUTF8",
            er.good_bitrates(channels, sr))


class FormatCodecWebMVorbisSampleRate(FormatSpin):
    """Vorbis sample rate selection."""
    
    def __init__(self, prev_object):
        channels = 1 if format_collate(prev_object)["mode"] == "mono" else 2
        er = EncoderRange(VorbisTestEncoder())
        bounds = er.bounds(channels)["samplerate_bounds"]
        FormatSpin.__init__(self, prev_object, _('Samplerate'), "samplerate",
            (44100,) + bounds + (1, 10), 0, " Hz", "FormatCodecWebMVorbisBitRate",
            er.good_samplerates(channels))


class FormatCodecWebMVorbisMode(FormatDropdown):
    """Vorbis mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecWebMVorbisSampleRate"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatCodecWebMVorbisSampleRate")), 0)


class FormatCodecWebMOpusBitRate(FormatSpin):
    """Opus bit rate selection."""
    
    def __init__(self, prev_object):
        dict_ = format_collate(prev_object)
        channels = 1 if dict_["mode"] == "mono" else 2
        bounds = (6 * channels, 256 * channels)
        FormatSpin.__init__(self, prev_object, _('Bitrate'), "bitrate",
            ((64, 96)[channels - 1],) + bounds + (1, 10), 0, " kbps", "FormatMetadataUTF8",
            (256 * channels, 128 * channels, 64 * channels, 48 * channels, 32 * channels, 16 * channels))
        self.scale = 1000


class FormatCodecWebMOpusMode(FormatDropdown):
    """Opus mode selection."""
    
    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Mode'), "mode", (
            dict(display_text=_("Mono"), value="mono", chain="FormatCodecWebMOpusBitRate"),
            dict(display_text=_("Stereo"), value="stereo", default=True, chain="FormatCodecWebMOpusBitRate")), 0)


class FormatCodecWebM(FormatDropdown):
    """Audio only WebM codec selection."""

    def __init__(self, prev_object):
        FormatDropdown.__init__(self, prev_object, _('Codec'), "codec", (
            dict(display_text=_('Opus'), value="opus", chain="FormatCodecWebMOpusMode", default=True),
            dict(display_text=_('Vorbis'), value="vorbis", chain="FormatCodecWebMVorbisMode")), 0)


class FormatFamily(FormatDropdown):
    """Gives choice of codec family/container format e.g. Xiph/Ogg or MPEG.
    
    The format is modified by means of a dropdown box.
    """

    def __init__(self, prev_object):
        # TC: Codec family e.g. Xiph/Ogg, MPEG etc.
        FormatDropdown.__init__(self, prev_object, _('Family'), "family", (
            dict(display_text=_('WebM'), value="webm", chain="FormatCodecWebM", shoutcast=False, sensitive=FGlobs.avenabled),
            # TC: Xiph.org Ogg container format.
            dict(display_text=_('Xiph/Ogg'), value="ogg", chain="FormatCodecXiphOgg", shoutcast=False),
            dict(display_text=_('MPEG'), value="mpeg", chain="FormatCodecMPEG", default=True)), 0,
            _('Codecs have been grouped by standards body and or container format.'))


class FormatControl(gtk.VBox):
    __gproperties__ = {
        'cap-icecast': (gobject.TYPE_BOOLEAN, 'icecast capable',
                        'if true this format can stream to icecast',
                        0, gobject.PARAM_READABLE),
                        
        'cap-shoutcast': (gobject.TYPE_BOOLEAN, 'shoutcast capable',
                        'if true this format can stream to shoutcast',
                        0, gobject.PARAM_READABLE),
                        
        'cap-recordable': (gobject.TYPE_BOOLEAN, 'can be recorded',
                        'if true this format is compatible with the recording facility',
                        0, gobject.PARAM_READABLE)
    }
    
    def __init__(self, send, receive):
        gtk.VBox.__init__(self)
        self.set_border_width(6)
        self.set_spacing(4)
        elem_box = [gtk.HBox()]
        self.pack_start(elem_box[0])
        
        self.caps_frame = gtk.Frame(" %s " % _('Capabilities'))
        self.caps_frame.set_sensitive(False)
        caps_box = gtk.HBox()
        caps_box.set_border_width(6)
        self.caps_frame.add(caps_box)
        self.pack_start(self.caps_frame, fill=False)
        
        led = LEDDict(8)
        self.green = led["green"]
        self.clear = led["clear"]
        
        for name, label_text in zip("icecast shoutcast recordable".split(),
                            (_('Icecast'), _('Shoutcast'), _('Recordable'))):
            hbox = gtk.HBox()
            hbox.set_spacing(3)
            label = gtk.Label(label_text)
            hbox.pack_start(label, False)
            
            image = gtk.Image()
            image.set_from_pixbuf(self.clear)
            hbox.pack_start(image, False)
            
            caps_box.pack_start(hbox, fill=False)
            setattr(self, "_" + name + "_indicator", image)

        elem_box.append(gtk.HBox())
        self.pack_start(elem_box[-1])
        
        button_frame = gtk.Alignment(xalign=1.0, yscale=0.85)
        button_frame.props.top_padding = 6
        button_box = gtk.HBox()
        button_box.set_spacing(3)
        button_frame.add(button_box)
        image = gtk.image_new_from_stock(gtk.STOCK_GO_BACK, gtk.ICON_SIZE_MENU)
        back_button = self._back_button = gtk.Button()
        back_button.set_sensitive(False)
        back_button.add(image)
        button_box.add(back_button)
        image = gtk.image_new_from_stock(gtk.STOCK_GO_FORWARD, gtk.ICON_SIZE_MENU)
        apply_button = self.apply_button = gtk.Button()
        apply_button.add(image)
        button_box.add(apply_button)

        #test_button = gtk.ToggleButton("Test")
        #button_box.add(test_button)
        #test_button.connect("toggled", self._on_test)
        
        elem_box[-1].pack_end(button_frame, True)
        self.show_all()

        self._current = self._first = FormatFamily(prev_object=None)
        elem_box[self._first.row].pack_start(self._first, False)
        
        apply_button.connect("clicked", self._on_apply, back_button, elem_box)
        back_button.connect("clicked", self._on_back, apply_button)
        
        sizegroup = gtk.SizeGroup(gtk.SIZE_GROUP_VERTICAL)
        for each in elem_box:
            sizegroup.add_widget(each)
        
        self._send = send
        self._receive = receive
        self._reference_counter = 0
        self._cap_icecast = self._cap_shoutcast = self._cap_recordable = False

        # GTK closures seem to be weak references.
        self.__refs = (elem_box,)

    def _on_apply(self, apply_button, back_button, elem_box):
        if self._current.apply():
            next_element_name = self._current.next_element_name
            if next_element_name is None:
                apply_button.set_sensitive(False)
                self._update_capabilities()
            else:
                self._current = globals()[next_element_name](self._current)
                elem_box[self._current.row].pack_start(self._current, False)
            back_button.set_sensitive(True)
        
    def _on_back(self, back_button, apply_button):
        apply_button.set_sensitive(True)
        if self._current.applied:
            self._current.unapply()
            self._update_capabilities()
        else:
            current = self._current
            self._current = current.prev_object
            current.destroy()
            self._current.unapply()
        back_button.set_sensitive(self._current.prev_object is not None)

    def _on_test(self, widget):
        if widget.get_active():
            self.start_encoder_rc()
        else:
            self.stop_encoder_rc()
            
    _cap_table = {
            "webm":
            {
                "opus":
                    {"shoutcast": False, "icecast": True, "recordable": True},
                "vorbis":
                    {"shoutcast": False, "icecast": True, "recordable": True},
            },
            "ogg":
            {
                "vorbis":
                    {"shoutcast": False, "icecast": True, "recordable": True},
                "flac":
                    {"shoutcast": False, "icecast": True, "recordable": True},
                "speex":
                    {"shoutcast": False, "icecast": True, "recordable": True},
                "opus":
                    {"shoutcast": False, "icecast": True, "recordable": True},
            },
            "mpeg":
            {
                "mp2":
                    {"shoutcast": True, "icecast": True, "recordable": True},
                "mp3":
                    {"shoutcast": True, "icecast": True, "recordable": True},
                "aac":
                    {"shoutcast": True, "icecast": True, "recordable": True},
                "aacpv2":
                    {"shoutcast": True, "icecast": True, "recordable": True}
            }
    }

    def _update_capabilities(self):
        settings = format_collate(self._current)
        dict_ = self._cap_table[settings["family"]][settings["codec"]]
            
        for each in "shoutcast icecast recordable".split():
            can = dict_[each] and self._current.applied
            indicator = getattr(self, "_" + each + "_indicator")
            indicator.set_from_pixbuf(self.green if can else self.clear)
            name = "_cap_" + each
            if getattr(self, name) != can:
                setattr(self, name, can)
                self.notify("cap-" + each)
            
        self.caps_frame.set_sensitive(self._current.applied)

    def do_get_property(self, property):
        if property.name not in (
                            "cap-icecast", "cap-shoutcast", "cap-recordable"):
            raise AttributeError('unknown property %s' % property.name)
        return getattr(self, "_" + property.name.replace("-", "_"))

    def marshall(self):
        return json.dumps(format_collate(self._current))

    def unmarshall(self, data):
        dict_ = json.loads(data)
        unapplied = dict_.get("__unapplied__", None)
        oldcurr = None
        
        while 1:
            try:
                self._current.value = dict_[self._current.ident]
            except KeyError:
                print("key error", self._current.ident)
                break
            else:
                if self._current.applied or self._current.ident == unapplied or oldcurr == self._current:
                    break
                oldcurr = self._current
                self.apply_button.clicked()
                if oldcurr.next_element_name is None:
                    break

    def get_settings(self):
        return format_collate(self._current)

    def start_encoder_rc(self):
        """Start the encoder (with reference counter)."""
        
        if self._reference_counter:
            self._reference_counter += 1
        else:
            self._reference_counter = int(self._start_encoder())
        return self._reference_counter > 0

    def stop_encoder_rc(self):
        """Stop the encoder (with reference counter)."""

        if self._reference_counter:
            if self._reference_counter == 1:
                self._stop_encoder()
            self._reference_counter -= 1

    @property
    def running(self):
        """True if the encoder is currently running."""

        return self._reference_counter > 0
        
    @property
    def finalised(self):
        """Return whether the encoder settings list is complete."""

        return self._current.applied

    def _start_encoder(self):
        if self._current.applied:
            kvps = [] 
            for pairs in format_collate(self._current).iteritems():
                kvps.append("=".join(pairs))
            kvps.append("encode_source=jack\ncommand=encoder_start\n")
            kvps = "\n".join(kvps)

            self._send(kvps)
            
            ret = self._receive() != "failed"
            if ret:
                self._back_button.set_sensitive(False)
            return ret
        else:
            print("encoder settings are not finalised")
            return False

    def _stop_encoder(self):
        self._back_button.set_sensitive(True)
        self._send("command=encoder_stop\n")
        return self._receive() != "failed"