File: jack_helpers.py

package info (click to toggle)
jack 3.1.1+cvs20050801-31
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,316 kB
  • sloc: python: 6,358; ansic: 1,910; makefile: 6; sh: 5
file content (574 lines) | stat: -rw-r--r-- 18,261 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
# -*- coding: iso-8859-15 -*-
### jack_helpers: helper applications for
### jack - extract audio from a CD and encode it using 3rd party software
### Copyright (C) 1999-2004  Arne Zellentin <zarne@users.sf.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; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import string
import re

import jack_plugins

from jack_globals import *


helpers = {
    'builtin': {
        'type': "dummy",
        'status_blocksize': 160
        },

    'oggenc': { # based on a patch kindly provided by Bryan Larsen.
        'type': "encoder",
        'target': "ogg",
        'can_tag': 1,
        'vbr-cmd': "oggenc -o %o -t %t -a %a -N %n -l %l -G %g -d %y -q %q %i",
        'cmd':     "oggenc -o %o -t %t -a %a -N %n -l %l -G %g -d %y -b %r %i",
        'tags': {
            'ogg': {
                'track': "-t %s",
                'artist': "-a %s",
                'number': "-N %s",
                'album': "-l %s",
                'genre': "-G %s",
                'date': "-d %s",
            },
        },
        'status_blocksize': 64,
        'bitrate_factor': 1,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2:
    s = s[-2]
if len(s) == 1:
    s = s[0]
y0 = string.find(s, "[")
y1 = string.find(s, "%]")
if y0 != -1 and y1 != -1:
    try:
        percent = float(s[y0 + 1:y1])
    except ValueError:
        percent = 0
else:
    percent = 0
""",
    },

    'mp3enc': {
        'type': "encoder",
        'target': "mp3",
        'cmd': "mp3enc -v -qual 9 -br %r -if %i -of %o",
        'otf-cmd': "mp3enc -v -qual 9 -br %r -sti -be -of %o",
        'status_blocksize': 99,
        'bitrate_factor': 1000,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 4:
    s = s[-2]
    if string.find(s, "%") >= 0:
        y = string.split(s, " ", 3)
        percent = float(y[0]) / (i['track'][LEN] * CDDA_BLOCKSIZE / 2) * 100.0
    else:
        percent = 0
""",
    },

    'l3enc': {
        'type': "encoder",
        'target': "mp3",
        'cmd': "l3enc -hq -br %r %i %o",
        'status_blocksize': 99,
        'bitrate_factor': 1000,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2: s = s[-2]
if len(s) == 1: s = s[0]
if string.find(s, "%") >= 0:
    y = string.split(s, " / ")
    y0 = string.split(y[0])[-1]
    y1 = string.split(y[1])[0]
    percent=float(y0) / float(y1) * 100.0
else:
    percent = 0
""",
    },

    'lame': {
        'type': "encoder",
        'target': "mp3",
        'inverse-quality': 1,
        'cmd': "lame --preset cbr %r --strictly-enforce-ISO %i %o",
        'vbr-cmd': "lame --preset standard --vbr-new --nohist --strictly-enforce-ISO %i %o",
        'otf-cmd': "lame --preset cbr %r --strictly-enforce-ISO - %o",
        'vbr-otf-cmd': "lame --preset standard --vbr-new --nohist --strictly-enforce-ISO - %o",
        'status_blocksize': 160,
        'bitrate_factor': 1,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2: s=s[-2]
if len(s) == 1: s=s[0]
if string.find(s, "%") >= 0:       # status reporting starts here
    y = string.split(s, "/")
    y1 = string.split(y[1], "(")[0]
    percent = float(y[0]) / float(y1) * 100.0
elif string.find(s, "Frame:") >= 0:    # older versions, like 3.13
    y = string.split(s, "/")
    y0 = string.split(y[0], "[")[-1]
    y1 = string.split(y[1], "]")[0]
    percent = float(y0) / float(y1) * 100.0
else:
    percent = 0
""",
    },

    'gogo': { # Thanks to Jos� Antonio P�rez S�nchez for the vbr and otf presets
        'type': "encoder",
        'target': "mp3",
        'inverse-quality': 1,
        'cmd': "gogo %i %o -b %r",
        'vbr-cmd': "gogo %i %o -v %q",
        'otf-cmd': "gogo stdin %o -b %r",
        'vbr-otf-cmd': "gogo stdin %o -v %q",
        'status_blocksize': 160,
        'bitrate_factor': 1,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2: s=s[-2]
if len(s) == 1: s=s[0]
if string.find(s, "%") >= 0: # status reporting starts here
    s = replace(s, "\000", " ")
    y = string.split(s, "/")
    y0 = string.split(y[0], "{")[-1]
    y1 = string.split(y[1], "}")[0]
    percent = float(y0) / float(y1) * 100.0
else:
    percent = 0
""",
    },

    'bladeenc': {
        'type': "encoder",
        'target': "mp3",
        'cmd': "bladeenc %i %o -br %r",
        'status_blocksize': 180,
        'bitrate_factor': 1000,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2: s=s[-2]
if string.find(s, "Status:") != -1:
    y = string.split(s[8:])
    percent = float(string.split(y[0], '%')[0])
else:
    percent = 0
""",
    },

# xing  definitions kindly provided by Sebastian Weber
    'xing': {
        'type': "encoder",
        'target': "mp3",
        'cmd': "xingmp3enc -B %r %i %o",
        'vbr-cmd': "xingmp3enc -V 100 %i %o",
        'otf-cmd': "xingmp3enc -b %r -- %o",
        'vbr-otf-cmd': "xingmp3enc -V 100 -- %o",
        'status_blocksize': 160,
        'bitrate_factor': 1,
        'status_start': "%",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 2: s = s[-2]
if string.find(s, "ETA:") != -1:
    y = string.strip(string.split(s, '%')[0])
    if len(y) == 0:
        percent = 0
    else:
        percent = float(y)
else:
    percent = 0
""",
    },

    'flac': {
        'type': "encoder",
        'target': "flac",
        'vbr-cmd': "flac -o %o %i",
        'vbr-otf-cmd': "flac --channels 2 --bps 16 --sample-rate 44100 --force-raw-format --endian=big --sign=signed -o %o -",
        'status_blocksize': 160,
        'status_start': "%", 
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')    
if len (s) >= 2: s = s[-2]
if len (s) == 1: s = s[0]       
y0 = string.rfind(s, ": ")             
y1 = string.find (s, "%", y0)
if y0 != -1 and y1 != -1: 
    try:
        percent = float(s[y0 + 1:y1])
    except ValueError:
        percent = 0
else:                   
    percent = 0         
""",
    },

    'mppenc': {
        'type': "encoder",
        'target': "mpc",
        'can_tag': 0,
        'vbr-cmd': "mppenc --standard %i %o",
        #'vbr-otf-cmd': "mppenc --standard - %o", # doesn't work, needs WAVE
        'status_blocksize': 160,
        'status_start': "-.-",
        'percent_fkt': r"""
s = string.split(i['buf'], '\r')
if len(s) >= 3:
    s = s[-3]
    s = string.split(string.strip(s))
    if len(s) >= 3 and s[2] == "kbps" and s[0] != "-.-":
        percent = float(s[0])
    else:
        percent = 0
""",
    },

    'cdparanoia': {
        'filters': [[r'\n', r'\r'], [r'(\r)+', r'\r'], [r'(Done\.\r)+', r'Done.\r']],
        'type': "ripper",
        'cmd': "cdparanoia --abort-on-skip -d %d %n %o",
        'otf-cmd': "cdparanoia --abort-on-skip -e -d %d %n -R -",
        'status_blocksize': 500,
        'status_start': "%",
        'status_fkt': r"""
# (== PROGRESS == [                              | 013124 00 ] == :^D * ==)
# (== PROGRESS == [                       >      .| 011923 00 ] == :-) . ==)
tmp = string.split(i['buf'], "\r")
if len(tmp) >= 2:
    tmp = tmp[-2] + " "
    new_status = tmp[17:48] + tmp[49:69] # 68->69 because of newer version
else:
    new_status = "Cannot parse status"
""",
        'otf-status_fkt': r"""
buf = i['buf']
tmp = string.split(buf, "\n")
new_status = ""
if len(tmp) >= 2:
    tmp = string.split(tmp[-2], " @ ")
    if tmp[0] == "##: -2 [wrote]":
        percent = (float(tmp[1]) - (i['track'][START] * CDDA_BLOCKSIZE / 2.0)) / (i['track'][LEN] * CDDA_BLOCKSIZE / 2.0) * 100.0
        new_status = "[otf - reading, %2i%%]" % percent
""",
        'final_status_fkt': r"""
last_status="0123456789012345 [ -- error decoding status --  ]" # fallback
if 0 and cf['_debug']: # disabled for now
    import jack_version
    tmpf=open("%s.debug.%02d.txt" % (jack_version.prog_name, exited_proc['track'][NUM]), "w")
    tmpf.write(exited_proc['buf'])
    del tmpf
tmps = string.split(exited_proc['buf'], '\r')
tmps.reverse()
for tmp in tmps:
    if string.find(tmp, "PROGRESS") != -1:
        last_status = tmp
        break
final_status = ("%sx" % jack_functions.pprint_speed(speed)) + last_status[16:48] + "]"
""",
        'otf-final_status_fkt': r"""
final_status = "[otf - done]"
""",
        #'toc': 1,  # we can't generate correct freedb IDs with cdparanoia.
        'toc_cmd': "cdparanoia -d %d -Q 2>&1",
# The output from cdparanoia which we parse looks like this:

# cdparanoia III release 9.8 (March 23, 2001)
# (C) 2001 Monty <monty@xiph.org> and Xiphophorus
# ...
# track        length               begin        copy pre ch
# ===========================================================
#   1.    13584 [03:01.09]        0 [00:00.00]    no   no  2
#   2.    13769 [03:03.44]    13584 [03:01.09]    no   no  2
# ...
# TOTAL  121128 [26:55.03]    (audio only)

# That is, we look for a line only consisting of === signs as the start,
# for a line starting with "TOTAL" as the end, and take everything
# inbetween (to be precise: the first number on each line)
        'toc_fkt': r"""
for l in p.readlines():
    l = string.rstrip(l)
    if not l:
        continue
    if l.startswith("TOTAL"):
        start = 0
    elif l == ('=' * len(l)):
        start = 1
    elif start:
        l = l.split('.', 1)
        if l[0].lstrip().isdigit():
            num = int(l[0])
            l = l[1].split()
            erg.append([num, int(l[0]), int(l[2]), l[4] == 'OK', l[5] == 'yes', int(l[6]), 1, cf['_bitrate'], cf['_name'] % num])
        else:
            warning("Cannot parse cdrecord TOC line: " + ". ".join(l))
""",
    },

    'cdda2wav': {
        'type': "ripper",
        'cmd': "cdda2wav --no-infofile -H -v 1 -D %d -O wav -t %n %o",
        'status_blocksize': 200,
        'status_start': "percent_done:",
        'status_fkt': r"""
tmp = string.split(i['buf'], "\r")
if len(tmp) >= 2:
    tmp = tmp[-2].lstrip()
    pct = tmp.find("%")
    if pct == -1:
        new_status = "waiting..."
    else:
        # A normal line when it's ripping looks like this:
        #   7%
        # However, when an error occurs, it'll look something like this:
        #   0%cdda2wav: Operation not permitted. Cannot send SCSI cmd via ioctl
        info = tmp[:pct+1]
        error = info + "cdda2wav:"
        if tmp == info:
            new_status = "ripping: " + info
        elif tmp.startswith(error):
            new_status = "Error: " + tmp[len(error):].lstrip()
        else:
            new_status = "Cannot parse status"
else:
    new_status = "Cannot parse status"
""",
        'final_status_fkt': r"""
final_status = ("%s" % jack_functions.pprint_speed(speed)) + "x [ DAE done with cdda2wav       ]"
""",
        'toc': 1,
        'toc_cmd': "cdda2wav --no-infofile -D %d -J -v toc --gui 2>&1",
        'toc_fkt': r"""
while 1:
    l = p.readline()
    if not l:
        break
    if l[0] == "T" and l[1] in string.digits and l[2] in string.digits and l[3] == ":":
        num, start, length, type, pre, copy, ch, dummy = string.split(l)[:8]
        if type == "audio":
            num = int(num[1:3])
            start = int(start)
            length = string.replace(length,".", ":")
            length = timestrtoblocks(length)
            pre = pre == "pre-emphasized"
            copy = copy != "copydenied"
            ch = [ "none", "mono", "stereo", "three", "quad" ].index(ch)
            erg.append([num, length, start, copy, pre, ch, 1, cf['_bitrate'], cf['_name'] % (num + 1)])
""",
        'toc_fkt_old': r"""
new_c2w = 0
new_toc1 = 0
new_toc2 = 0
new_lengths = []
new_starts = []
while 1:
    l = p.readline()
    if not l:
        break
    l = string.strip(l)
    # new cdda2wav
    if starts_with(l, "Table of Contents: total tracks"):
        new_toc1 = 1
        continue

    if starts_with(l, "Table of Contents: starting sectors"):
        new_toc2 = 1
        new_toc1 = 0
        new_c2w = 1
        continue

    if new_toc2 and l and l[0] in string.digits:
        l = string.split(l, "(")[1:]
        for i in l:
            x = string.split(i, ")")[0]
            x = string.strip(x)
            try:
                new_starts.append(int(x))
            except:
                pass
        continue

    if new_toc1 and l and l[0] in string.digits:
        l = string.split(l, "(")[1:]
        for i in l:
            if string.find(i, ":") >= 0:
                x = string.split(i, ")")[0]
                x = replace(x, ".", ":")
                new_lengths.append(timestrtoblocks(x))
        continue

    # old cdda2wav
    if l and l[0:11] == "Album title":
        start = 1
    elif l and start:
        l = string.split(l)
        if l[0] == "Leadout:":
            start = 0
        else:
            num = int(l[0][1:3])
            if l[6] == "stereo":
                channels = 2
            elif l[6] == "mono":
                channels = 1
            else:
                channels = 0
            t_start = int(l[1])
            msf = string.split(l[2], ":")
            sf = string.split(msf[1], ".")
            t_length = int(sf[1]) + int(sf[0]) * 75 + int(msf[0]) * 60 * 75
            erg.append([num, t_length, t_start, l[5] == "copyallowed", l[4] != "linear", channels, 1, cf['_bitrate'], cf['_name'] % num])
if new_c2w and len(new_lengths) == len(new_starts) - 1:
    for i in range(min(len(new_lengths), len(new_starts))): # this provokes an error if the lists are of different length
        erg.append([i + 1, new_lengths[i], new_starts[i], 0, 0, 2, 1, cf['_bitrate'], cf['_name'] % (i + 1)])
""",
    },

    'dagrab': {
        'type': "ripper",
        'cmd': "dagrab -d %d -f %o %n",
        'status_blocksize': 100,
        'status_start': "total:",
        'status_fkt': r"""
tmp = string.split(i['buf'], "\r")
if len(tmp) >= 2:
    if string.find(tmp[-2], 'total:') != -1:
        new_status = string.strip(tmp[-2])
    else:
        new_status = "waiting..."
else:
    new_status = "Cannot parse status"
""",
        'final_status_fkt': r"""
final_status = ("%s" % jack_functions.pprint_speed(speed)) + "x [ DAE done with dagrab         ]"
""",
        'toc': 1,
        'toc_cmd': "dagrab -d %d -i 2>&1",
        'toc_fkt': r"""
while l:
    l = string.strip(l)
    if l and l[0:5] == "track":
        start = 1
    elif l and start:
        l = string.split(l)
        if l[3] == "leadout":
            start = 0
        else:
            num = int(l[0])
            channels = 2
            copy = 0
            pre = 0
            t_start = int(l[1]) - 150
            t_length = int(l[2])
            erg.append([num, t_length, t_start, copy, pre, channels, 1, cf['_bitrate'], cf['_name'] % num])
    l = p.readline()
""",
    },

    'tosha': {
        'type': "ripper",
        'cmd': "tosha -d %d -f wav -t %n -o %o",
        'status_blocksize': 100,
        'status_start': "total:",
        'status_fkt': r"""
x = string.split(i['buf'], '\r')[-2]
if string.find(x, 'total:') != -1:
    new_status = string.strip(string.split(i['buf'], '\r')[-2])
else:
    new_status = "waiting..."
""",
        'final_status_fkt': r"""
final_status = ("%s" % jack_functions.pprint_speed(speed)) + "x [ DAE done with tosha          ]"
""",
        'toc': 1,
        'toc_cmd': "tosha -d %d -iq 2>&1",
        'toc_fkt': r"""
while l:
    l = string.rstrip(l)
    if l:
        l = string.split(l)
        num = int(l[0])
        erg.append([num, 1 + int(l[3]) - int(l[2]), int(l[2]), 0, 0, 2, 1, cf['_bitrate'], cf['_name'] % num])
    l = p.readline()
""",
    },

    'CDDB.py': {
        'type': "toc-reader",
        'toc': 1,
        'toc_fkt': r"""
import cdrom
if not os.path.exists(cf['_cd_device']):
    error("Device %s does not exist!" % cf['_cd_device'])
if not os.access(cf['_cd_device'], os.R_OK):
    error("You don't have permission to access device %s!" % cf['_cd_device'])
if not stat.S_ISBLK(os.stat(cf['_cd_device'])[stat.ST_MODE]):
    error("Device %s is not a block device!" % cf['_cd_device'])
try:
    device = cdrom.open(cf['_cd_device'])
    (first, last) = cdrom.toc_header(device)
except cdrom.error, m:
    error("Access of CD device %s resulted in error: %s" % (cf['_cd_device'], m[1]))

toc = []
for i in range(first, last + 1):
    (min, sec, frame) = cdrom.toc_entry(device, i)
    toc.append(min * 60 * 75 + sec * 75 + frame)
(min, sec, frame) = cdrom.leadout(device)
device.close()
toc.append(min * 60 * 75 + sec * 75 + frame)
for i in range(first, last + 1):
    erg.append([i, toc[i - first + 1] - toc[i - first], toc[i - first] - MSF_OFFSET, 0, 0, 2, 1, cf['_bitrate'], cf['_name'] % i])
""",
    }
}

helpers['lame-user'] = helpers['lame'].copy()
helpers['lame-user'].update({'cmd': "lame --preset cbr %r --strictly-enforce-ISO %i %o",
        'vbr-cmd': "lame -V %q --vbr-new --nohist --strictly-enforce-ISO %i %o",
        'otf-cmd': "lame --preset cbr %r --strictly-enforce-ISO - %o",
        'vbr-otf-cmd': "lame -V %q --vbr-new --nohist --strictly-enforce-ISO - %o", })

def init():
    # import plugin
    jack_plugins.import_helpers()

    # compile exec strings
    for h in helpers.keys():
        for i in helpers[h].keys():
            if i[-4:] == "_fkt":
                helpers[h][i] = compile(helpers[h][i], '<string>', 'exec')

    # compile filters
    for h in helpers.keys():
        if helpers[h].has_key('filters'):
            newf = []
            for i in helpers[h]['filters']:
                newf.append([re.compile(i[0]), i[1]])
            helpers[h]['filters'] = newf