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