]>
Commit | Line | Data |
---|---|---|
1 | # -*- coding: utf-8 -*- | |
2 | import argparse | |
3 | import sys | |
4 | import os | |
5 | import math | |
6 | import sounddevice as sd | |
7 | ||
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 | ||
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 | ||
22 | def parse_args(): | |
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()) | |
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 | ) | |
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 | ||
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 | ||
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" | |
73 | ||
74 | def yml_file(): | |
75 | return config.yml_file | |
76 | ||
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) | |
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 |