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