]>
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 IB |
8 | class Config: |
9 | def __init__(self, **kwargs): | |
10 | for arg in kwargs: | |
11 | setattr(self, arg, kwargs[arg]) | |
12 | ||
13 | config = Config(yml_file="config.yml") | |
14 | ||
bb69f62e IB |
15 | def path(): |
16 | if getattr(sys, 'frozen', False): | |
17 | return sys._MEIPASS + "/" | |
18 | else: | |
19 | path = os.path.dirname(os.path.realpath(__file__)) | |
20 | return path + "/../" | |
21 | ||
cc008de4 | 22 | def parse_args(): |
71715c04 IB |
23 | argv = sys.argv[1:] |
24 | sys.argv = sys.argv[:1] | |
25 | if "--" in argv: | |
26 | index = argv.index("--") | |
27 | kivy_args = argv[index+1:] | |
28 | argv = argv[:index] | |
29 | ||
30 | sys.argv.extend(kivy_args) | |
31 | ||
32 | parser = argparse.ArgumentParser(description="A Music Sampler application.") | |
33 | parser.add_argument("-c", "--config", | |
34 | default="config.yml", | |
35 | required=False, | |
36 | help="Config file to load") | |
37 | parser.add_argument("-V", "--version", | |
38 | action="version", | |
39 | help="Displays the current version and exits. Only use in bundled package", | |
40 | version=show_version()) | |
29ce1fde IB |
41 | parser.add_argument("--device", |
42 | action=SelectDeviceAction, | |
43 | help="Select this sound device" | |
44 | ) | |
45 | parser.add_argument("--list-devices", | |
46 | nargs=0, | |
47 | action=ListDevicesAction, | |
48 | help="List available sound devices" | |
49 | ) | |
71715c04 IB |
50 | parser.add_argument('--', |
51 | dest="args", | |
52 | help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.") | |
53 | args = parser.parse_args(argv) | |
54 | ||
55 | config.yml_file = args.config | |
56 | ||
29ce1fde IB |
57 | class SelectDeviceAction(argparse.Action): |
58 | def __call__(self, parser, namespace, values, option_string=None): | |
59 | sd.default.device = values | |
60 | ||
61 | class ListDevicesAction(argparse.Action): | |
62 | nargs = 0 | |
63 | def __call__(self, parser, namespace, values, option_string=None): | |
64 | print(sd.query_devices()) | |
65 | sys.exit() | |
66 | ||
71715c04 IB |
67 | def show_version(): |
68 | if getattr(sys, 'frozen', False): | |
69 | with open(path() + ".pyinstaller_commit", "r") as f: | |
70 | return f.read() | |
71 | else: | |
72 | return "option '-v' can only be used in bundled package" | |
cc008de4 | 73 | |
9b9dd12a IB |
74 | def yml_file(): |
75 | return config.yml_file | |
76 | ||
98ff4305 IB |
77 | def duration_to_min_sec(duration): |
78 | minutes = int(duration / 60) | |
79 | seconds = int(duration) % 60 | |
80 | if minutes < 100: | |
81 | return "{:2}:{:0>2}".format(minutes, seconds) | |
82 | else: | |
83 | return "{}:{:0>2}".format(minutes, seconds) | |
1b4b78f5 IB |
84 | |
85 | def gain(volume, old_volume = None): | |
86 | if old_volume is None: | |
87 | return 20 * math.log10(volume / 100) | |
88 | else: | |
89 | return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] | |
90 |