Debian Codegraph @

decode.py

raw symbols SCIP
1 # This file is part of audioread.
2 # Copyright 2011, Adrian Sampson.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be
13 # included in all copies or substantial portions of the Software.
14
15 """Command-line tool to decode audio files to WAV files."""
16 import audioread
17 import sys
18 import os
19 import wave
20 import contextlib
21
22
23 def decode(filename):
24 filename = os.path.abspath(os.path.expanduser(filename))
25 if not os.path.exists(filename):
26 print("File not found.", file=sys.stderr)
27 sys.exit(1)
28
29 try:
30 with audioread.audio_open(filename) as f:
31 print('Input file: %i channels at %i Hz; %.1f seconds.' %
32 (f.channels, f.samplerate, f.duration),
33 file=sys.stderr)
34 print('Backend:', str(type(f).__module__).split('.')[1],
35 file=sys.stderr)
36
37 with contextlib.closing(wave.open(filename + '.wav', 'w')) as of:
38 of.setnchannels(f.channels)
39 of.setframerate(f.samplerate)
40 of.setsampwidth(2)
41
42 for buf in f:
43 of.writeframes(buf)
44
45 except audioread.DecodeError:
46 print("File could not be decoded.", file=sys.stderr)
47 sys.exit(1)
48
49
50 if __name__ == '__main__':
51 decode(sys.argv[1])