]>
Commit | Line | Data |
---|---|---|
1df30f07 | 1 | # -*- coding: utf-8 -*- |
71715c04 | 2 | import argparse |
bb69f62e IB |
3 | import sys |
4 | import os | |
1b4b78f5 | 5 | import math |
29ce1fde | 6 | import sounddevice as sd |
bb69f62e | 7 | |
9b9dd12a | 8 | class Config: |
75d6cdba | 9 | pass |
9b9dd12a | 10 | |
bb69f62e IB |
11 | def path(): |
12 | if getattr(sys, 'frozen', False): | |
13 | return sys._MEIPASS + "/" | |
14 | else: | |
15 | path = os.path.dirname(os.path.realpath(__file__)) | |
16 | return path + "/../" | |
17 | ||
cc008de4 | 18 | def parse_args(): |
71715c04 IB |
19 | argv = sys.argv[1:] |
20 | sys.argv = sys.argv[:1] | |
21 | if "--" in argv: | |
22 | index = argv.index("--") | |
23 | kivy_args = argv[index+1:] | |
24 | argv = argv[:index] | |
25 | ||
26 | sys.argv.extend(kivy_args) | |
27 | ||
75d6cdba IB |
28 | parser = argparse.ArgumentParser( |
29 | description="A Music Sampler application.", | |
30 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
71715c04 IB |
31 | parser.add_argument("-c", "--config", |
32 | default="config.yml", | |
33 | required=False, | |
34 | help="Config file to load") | |
75d6cdba IB |
35 | parser.add_argument("-l", "--latency", |
36 | default="high", | |
37 | required=False, | |
38 | help="Latency: low, high or number of seconds") | |
39 | parser.add_argument("-b", "--blocksize", | |
40 | default=0, | |
41 | type=int, | |
42 | required=False, | |
43 | help="Blocksize: If not 0, the numbe of frames to take at each step for the mixer") | |
44 | parser.add_argument("-f", "--frame-rate", | |
45 | default=44100, | |
46 | type=int, | |
47 | required=False, | |
48 | help="Frame rate to play the musics") | |
49 | parser.add_argument("-x", "--channels", | |
50 | default=2, | |
51 | type=int, | |
52 | required=False, | |
53 | help="Number of channels to use") | |
54 | parser.add_argument("-s", "--sample-width", | |
55 | default=2, | |
56 | type=int, | |
57 | required=False, | |
58 | help="Sample width (number of bytes for each frame)") | |
71715c04 IB |
59 | parser.add_argument("-V", "--version", |
60 | action="version", | |
61 | help="Displays the current version and exits. Only use in bundled package", | |
62 | version=show_version()) | |
29ce1fde IB |
63 | parser.add_argument("--device", |
64 | action=SelectDeviceAction, | |
65 | help="Select this sound device" | |
66 | ) | |
67 | parser.add_argument("--list-devices", | |
68 | nargs=0, | |
69 | action=ListDevicesAction, | |
70 | help="List available sound devices" | |
71 | ) | |
71715c04 IB |
72 | parser.add_argument('--', |
73 | dest="args", | |
74 | help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.") | |
75 | args = parser.parse_args(argv) | |
76 | ||
75d6cdba IB |
77 | Config.yml_file = args.config |
78 | Config.latency = args.latency | |
79 | Config.blocksize = args.blocksize | |
80 | Config.frame_rate = args.frame_rate | |
81 | Config.channels = args.channels | |
82 | Config.sample_width = args.sample_width | |
71715c04 | 83 | |
29ce1fde IB |
84 | class SelectDeviceAction(argparse.Action): |
85 | def __call__(self, parser, namespace, values, option_string=None): | |
86 | sd.default.device = values | |
87 | ||
88 | class ListDevicesAction(argparse.Action): | |
89 | nargs = 0 | |
90 | def __call__(self, parser, namespace, values, option_string=None): | |
91 | print(sd.query_devices()) | |
92 | sys.exit() | |
93 | ||
71715c04 IB |
94 | def show_version(): |
95 | if getattr(sys, 'frozen', False): | |
96 | with open(path() + ".pyinstaller_commit", "r") as f: | |
97 | return f.read() | |
98 | else: | |
99 | return "option '-v' can only be used in bundled package" | |
cc008de4 | 100 | |
98ff4305 IB |
101 | def duration_to_min_sec(duration): |
102 | minutes = int(duration / 60) | |
103 | seconds = int(duration) % 60 | |
104 | if minutes < 100: | |
105 | return "{:2}:{:0>2}".format(minutes, seconds) | |
106 | else: | |
107 | return "{}:{:0>2}".format(minutes, seconds) | |
1b4b78f5 IB |
108 | |
109 | def gain(volume, old_volume = None): | |
110 | if old_volume is None: | |
111 | return 20 * math.log10(volume / 100) | |
112 | else: | |
113 | return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] | |
114 |