]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/__init__.py
Add music-path option to the command line
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / __init__.py
1 # -*- coding: utf-8 -*-
2 import argparse
3 import sys
4 import os
5 import math
6 import sounddevice as sd
7 import logging
8
9 class Config:
10 pass
11
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
19 def parse_args():
20 argv = sys.argv[1 :]
21 sys.argv = sys.argv[: 1]
22 if "--" in argv:
23 index = argv.index("--")
24 kivy_args = argv[index+1 :]
25 argv = argv[: index]
26
27 sys.argv.extend(kivy_args)
28
29 parser = argparse.ArgumentParser(
30 description="A Music Sampler application.",
31 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
32 parser.add_argument("-c", "--config",
33 default="config.yml",
34 required=False,
35 help="Config file to load")
36 parser.add_argument("-p", "--music-path",
37 default=".",
38 required=False,
39 help="Folder in which to find the music files")
40 parser.add_argument("-d", "--debug",
41 nargs=0,
42 action=DebugModeAction,
43 help="Print messages in console")
44 parser.add_argument("-m", "--builtin-mixing",
45 action="store_true",
46 help="Make the mixing of sounds manually\
47 (do it if the system cannot handle it correctly)")
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,
56 help="Blocksize: If not 0, the number of frames to take\
57 at each step for the mixer")
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)")
73 parser.add_argument("-V", "--version",
74 action="version",
75 help="Displays the current version and exits. Only use\
76 in bundled package",
77 version=show_version())
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 )
87 parser.add_argument('--',
88 dest="args",
89 help="Kivy arguments. All arguments after this are interpreted\
90 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
91
92 from kivy.logger import Logger
93 Logger.setLevel(logging.WARN)
94
95 args = parser.parse_args(argv)
96
97 Config.yml_file = args.config
98
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
104 Config.builtin_mixing = args.builtin_mixing
105 if args.music_path.endswith("/"):
106 Config.music_path = args.music_path
107 else:
108 Config.music_path = args.music_path + "/"
109
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
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
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"
131
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)
139
140 def gain(volume, old_volume=None):
141 if old_volume is None:
142 return 20 * math.log10(max(volume, 0.1) / 100)
143 else:
144 return [
145 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)),
146 max(volume, 0)]
147
148 def debug_print(message, with_trace=False):
149 from kivy.logger import Logger
150 Logger.debug('MusicSampler: ' + message, exc_info=with_trace)
151
152 def error_print(message, with_trace=False):
153 from kivy.logger import Logger
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)