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