]>
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 |
a24c34bc | 7 | import logging |
bb69f62e | 8 | |
9b9dd12a | 9 | class Config: |
75d6cdba | 10 | pass |
9b9dd12a | 11 | |
bb69f62e IB |
12 | def path(): |
13 | if getattr(sys, 'frozen', False): | |
14 | return sys._MEIPASS + "/" | |
15 | else: | |
16 | path = os.path.dirname(os.path.realpath(__file__)) | |
17 | return path + "/../" | |
18 | ||
cc008de4 | 19 | def parse_args(): |
2e404903 IB |
20 | argv = sys.argv[1 :] |
21 | sys.argv = sys.argv[: 1] | |
71715c04 IB |
22 | if "--" in argv: |
23 | index = argv.index("--") | |
2e404903 IB |
24 | kivy_args = argv[index+1 :] |
25 | argv = argv[: index] | |
71715c04 IB |
26 | |
27 | sys.argv.extend(kivy_args) | |
28 | ||
75d6cdba IB |
29 | parser = argparse.ArgumentParser( |
30 | description="A Music Sampler application.", | |
31 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
71715c04 IB |
32 | parser.add_argument("-c", "--config", |
33 | default="config.yml", | |
34 | required=False, | |
35 | help="Config file to load") | |
a24c34bc IB |
36 | parser.add_argument("-d", "--debug", |
37 | nargs=0, | |
38 | action=DebugModeAction, | |
39 | help="Print messages in console") | |
d6290f14 | 40 | parser.add_argument("-m", "--builtin-mixing", |
af27d782 | 41 | action="store_true", |
2e404903 IB |
42 | help="Make the mixing of sounds manually\ |
43 | (do it if the system cannot handle it correctly)") | |
75d6cdba IB |
44 | parser.add_argument("-l", "--latency", |
45 | default="high", | |
46 | required=False, | |
47 | help="Latency: low, high or number of seconds") | |
48 | parser.add_argument("-b", "--blocksize", | |
49 | default=0, | |
50 | type=int, | |
51 | required=False, | |
2e404903 IB |
52 | help="Blocksize: If not 0, the number of frames to take\ |
53 | at each step for the mixer") | |
75d6cdba IB |
54 | parser.add_argument("-f", "--frame-rate", |
55 | default=44100, | |
56 | type=int, | |
57 | required=False, | |
58 | help="Frame rate to play the musics") | |
59 | parser.add_argument("-x", "--channels", | |
60 | default=2, | |
61 | type=int, | |
62 | required=False, | |
63 | help="Number of channels to use") | |
64 | parser.add_argument("-s", "--sample-width", | |
65 | default=2, | |
66 | type=int, | |
67 | required=False, | |
68 | help="Sample width (number of bytes for each frame)") | |
71715c04 IB |
69 | parser.add_argument("-V", "--version", |
70 | action="version", | |
2e404903 IB |
71 | help="Displays the current version and exits. Only use\ |
72 | in bundled package", | |
71715c04 | 73 | version=show_version()) |
29ce1fde IB |
74 | parser.add_argument("--device", |
75 | action=SelectDeviceAction, | |
76 | help="Select this sound device" | |
77 | ) | |
78 | parser.add_argument("--list-devices", | |
79 | nargs=0, | |
80 | action=ListDevicesAction, | |
81 | help="List available sound devices" | |
82 | ) | |
71715c04 IB |
83 | parser.add_argument('--', |
84 | dest="args", | |
2e404903 IB |
85 | help="Kivy arguments. All arguments after this are interpreted\ |
86 | by Kivy. Pass \"-- --help\" to get Kivy's usage.") | |
a24c34bc IB |
87 | |
88 | from kivy.logger import Logger | |
89 | Logger.setLevel(logging.ERROR) | |
90 | ||
71715c04 IB |
91 | args = parser.parse_args(argv) |
92 | ||
75d6cdba | 93 | Config.yml_file = args.config |
a24c34bc | 94 | |
75d6cdba IB |
95 | Config.latency = args.latency |
96 | Config.blocksize = args.blocksize | |
97 | Config.frame_rate = args.frame_rate | |
98 | Config.channels = args.channels | |
99 | Config.sample_width = args.sample_width | |
d6290f14 | 100 | Config.builtin_mixing = args.builtin_mixing |
71715c04 | 101 | |
a24c34bc IB |
102 | class DebugModeAction(argparse.Action): |
103 | def __call__(self, parser, namespace, values, option_string=None): | |
104 | from kivy.logger import Logger | |
105 | Logger.setLevel(logging.DEBUG) | |
106 | ||
29ce1fde IB |
107 | class SelectDeviceAction(argparse.Action): |
108 | def __call__(self, parser, namespace, values, option_string=None): | |
109 | sd.default.device = values | |
110 | ||
111 | class ListDevicesAction(argparse.Action): | |
112 | nargs = 0 | |
113 | def __call__(self, parser, namespace, values, option_string=None): | |
114 | print(sd.query_devices()) | |
115 | sys.exit() | |
116 | ||
71715c04 IB |
117 | def show_version(): |
118 | if getattr(sys, 'frozen', False): | |
119 | with open(path() + ".pyinstaller_commit", "r") as f: | |
120 | return f.read() | |
121 | else: | |
122 | return "option '-v' can only be used in bundled package" | |
cc008de4 | 123 | |
98ff4305 IB |
124 | def duration_to_min_sec(duration): |
125 | minutes = int(duration / 60) | |
126 | seconds = int(duration) % 60 | |
127 | if minutes < 100: | |
128 | return "{:2}:{:0>2}".format(minutes, seconds) | |
129 | else: | |
130 | return "{}:{:0>2}".format(minutes, seconds) | |
1b4b78f5 | 131 | |
2e404903 | 132 | def gain(volume, old_volume=None): |
1b4b78f5 | 133 | if old_volume is None: |
aee1334c | 134 | return 20 * math.log10(max(volume, 0.1) / 100) |
1b4b78f5 | 135 | else: |
2e404903 IB |
136 | return [ |
137 | 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), | |
138 | max(volume, 0)] | |
1b4b78f5 | 139 | |
a24c34bc IB |
140 | def debug_print(message): |
141 | from kivy.logger import Logger | |
142 | Logger.debug('MusicSampler: ' + message) | |
143 | ||
144 | def error_print(message): | |
145 | from kivy.logger import Logger | |
146 | Logger.error('MusicSampler: ' + message) |