]>
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 | |
35bde798 IB |
9 | from . import sysfont |
10 | ||
9b9dd12a | 11 | class Config: |
75d6cdba | 12 | pass |
9b9dd12a | 13 | |
35bde798 IB |
14 | def find_font(name, style=sysfont.STYLE_NONE): |
15 | if getattr(sys, 'frozen', False): | |
16 | font = sys._MEIPASS + "/fonts/{}_{}.ttf".format(name, style) | |
17 | else: | |
18 | font = sysfont.get_font(name, style=style) | |
19 | if font is not None: | |
20 | font = font[4] | |
21 | return font | |
22 | ||
23 | def register_fonts(): | |
24 | from kivy.core.text import LabelBase | |
25 | ||
26 | ubuntu_regular = find_font("Ubuntu", style=sysfont.STYLE_NORMAL) | |
27 | ubuntu_bold = find_font("Ubuntu", style=sysfont.STYLE_BOLD) | |
28 | symbola = find_font("Symbola") | |
29 | ||
30 | if ubuntu_regular is None: | |
31 | error_print("Font Ubuntu regular could not be found, please install it.") | |
32 | sys.exit() | |
33 | if symbola is None: | |
34 | error_print("Font Symbola could not be found, please install it.") | |
35 | sys.exit() | |
36 | if ubuntu_bold is None: | |
37 | warn_print("Font Ubuntu Bold could not be found.") | |
38 | ||
39 | LabelBase.register(name="Ubuntu", | |
40 | fn_regular=ubuntu_regular, | |
41 | fn_bold=ubuntu_bold) | |
42 | LabelBase.register(name="Symbola", | |
43 | fn_regular=symbola) | |
44 | ||
45 | ||
bb69f62e IB |
46 | def path(): |
47 | if getattr(sys, 'frozen', False): | |
48 | return sys._MEIPASS + "/" | |
49 | else: | |
50 | path = os.path.dirname(os.path.realpath(__file__)) | |
51 | return path + "/../" | |
52 | ||
cc008de4 | 53 | def parse_args(): |
2e404903 IB |
54 | argv = sys.argv[1 :] |
55 | sys.argv = sys.argv[: 1] | |
71715c04 IB |
56 | if "--" in argv: |
57 | index = argv.index("--") | |
2e404903 IB |
58 | kivy_args = argv[index+1 :] |
59 | argv = argv[: index] | |
71715c04 IB |
60 | |
61 | sys.argv.extend(kivy_args) | |
62 | ||
75d6cdba IB |
63 | parser = argparse.ArgumentParser( |
64 | description="A Music Sampler application.", | |
65 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
71715c04 IB |
66 | parser.add_argument("-c", "--config", |
67 | default="config.yml", | |
68 | required=False, | |
69 | help="Config file to load") | |
940bb6ad IB |
70 | parser.add_argument("-p", "--music-path", |
71 | default=".", | |
72 | required=False, | |
73 | help="Folder in which to find the music files") | |
a24c34bc IB |
74 | parser.add_argument("-d", "--debug", |
75 | nargs=0, | |
76 | action=DebugModeAction, | |
77 | help="Print messages in console") | |
d6290f14 | 78 | parser.add_argument("-m", "--builtin-mixing", |
af27d782 | 79 | action="store_true", |
2e404903 IB |
80 | help="Make the mixing of sounds manually\ |
81 | (do it if the system cannot handle it correctly)") | |
75d6cdba IB |
82 | parser.add_argument("-l", "--latency", |
83 | default="high", | |
84 | required=False, | |
85 | help="Latency: low, high or number of seconds") | |
86 | parser.add_argument("-b", "--blocksize", | |
87 | default=0, | |
88 | type=int, | |
89 | required=False, | |
2e404903 IB |
90 | help="Blocksize: If not 0, the number of frames to take\ |
91 | at each step for the mixer") | |
75d6cdba IB |
92 | parser.add_argument("-f", "--frame-rate", |
93 | default=44100, | |
94 | type=int, | |
95 | required=False, | |
96 | help="Frame rate to play the musics") | |
97 | parser.add_argument("-x", "--channels", | |
98 | default=2, | |
99 | type=int, | |
100 | required=False, | |
101 | help="Number of channels to use") | |
102 | parser.add_argument("-s", "--sample-width", | |
103 | default=2, | |
104 | type=int, | |
105 | required=False, | |
106 | help="Sample width (number of bytes for each frame)") | |
71715c04 IB |
107 | parser.add_argument("-V", "--version", |
108 | action="version", | |
2e404903 IB |
109 | help="Displays the current version and exits. Only use\ |
110 | in bundled package", | |
71715c04 | 111 | version=show_version()) |
29ce1fde IB |
112 | parser.add_argument("--device", |
113 | action=SelectDeviceAction, | |
114 | help="Select this sound device" | |
115 | ) | |
116 | parser.add_argument("--list-devices", | |
117 | nargs=0, | |
118 | action=ListDevicesAction, | |
119 | help="List available sound devices" | |
120 | ) | |
71715c04 IB |
121 | parser.add_argument('--', |
122 | dest="args", | |
2e404903 IB |
123 | help="Kivy arguments. All arguments after this are interpreted\ |
124 | by Kivy. Pass \"-- --help\" to get Kivy's usage.") | |
a24c34bc IB |
125 | |
126 | from kivy.logger import Logger | |
05d0d2ed | 127 | Logger.setLevel(logging.WARN) |
a24c34bc | 128 | |
71715c04 IB |
129 | args = parser.parse_args(argv) |
130 | ||
75d6cdba | 131 | Config.yml_file = args.config |
a24c34bc | 132 | |
75d6cdba IB |
133 | Config.latency = args.latency |
134 | Config.blocksize = args.blocksize | |
135 | Config.frame_rate = args.frame_rate | |
136 | Config.channels = args.channels | |
137 | Config.sample_width = args.sample_width | |
d6290f14 | 138 | Config.builtin_mixing = args.builtin_mixing |
940bb6ad IB |
139 | if args.music_path.endswith("/"): |
140 | Config.music_path = args.music_path | |
141 | else: | |
142 | Config.music_path = args.music_path + "/" | |
71715c04 | 143 | |
a24c34bc IB |
144 | class DebugModeAction(argparse.Action): |
145 | def __call__(self, parser, namespace, values, option_string=None): | |
146 | from kivy.logger import Logger | |
147 | Logger.setLevel(logging.DEBUG) | |
148 | ||
29ce1fde IB |
149 | class SelectDeviceAction(argparse.Action): |
150 | def __call__(self, parser, namespace, values, option_string=None): | |
151 | sd.default.device = values | |
152 | ||
153 | class ListDevicesAction(argparse.Action): | |
154 | nargs = 0 | |
155 | def __call__(self, parser, namespace, values, option_string=None): | |
156 | print(sd.query_devices()) | |
157 | sys.exit() | |
158 | ||
71715c04 IB |
159 | def show_version(): |
160 | if getattr(sys, 'frozen', False): | |
161 | with open(path() + ".pyinstaller_commit", "r") as f: | |
162 | return f.read() | |
163 | else: | |
164 | return "option '-v' can only be used in bundled package" | |
cc008de4 | 165 | |
98ff4305 IB |
166 | def duration_to_min_sec(duration): |
167 | minutes = int(duration / 60) | |
168 | seconds = int(duration) % 60 | |
169 | if minutes < 100: | |
170 | return "{:2}:{:0>2}".format(minutes, seconds) | |
171 | else: | |
172 | return "{}:{:0>2}".format(minutes, seconds) | |
1b4b78f5 | 173 | |
2e404903 | 174 | def gain(volume, old_volume=None): |
1b4b78f5 | 175 | if old_volume is None: |
aee1334c | 176 | return 20 * math.log10(max(volume, 0.1) / 100) |
1b4b78f5 | 177 | else: |
2e404903 IB |
178 | return [ |
179 | 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), | |
180 | max(volume, 0)] | |
1b4b78f5 | 181 | |
05d0d2ed | 182 | def debug_print(message, with_trace=False): |
a24c34bc | 183 | from kivy.logger import Logger |
05d0d2ed | 184 | Logger.debug('MusicSampler: ' + message, exc_info=with_trace) |
a24c34bc | 185 | |
05d0d2ed | 186 | def error_print(message, with_trace=False): |
a24c34bc | 187 | from kivy.logger import Logger |
05d0d2ed IB |
188 | Logger.error('MusicSampler: ' + message, exc_info=with_trace) |
189 | ||
190 | def warn_print(message, with_trace=False): | |
191 | from kivy.logger import Logger | |
192 | Logger.warn('MusicSampler: ' + message, exc_info=with_trace) |