]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/__init__.py
Add debugger
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / __init__.py
CommitLineData
1df30f07 1# -*- coding: utf-8 -*-
71715c04 2import argparse
bb69f62e
IB
3import sys
4import os
1b4b78f5 5import math
29ce1fde 6import sounddevice as sd
a24c34bc 7import logging
bb69f62e 8
9b9dd12a 9class Config:
75d6cdba 10 pass
9b9dd12a 11
bb69f62e
IB
12def 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 19def parse_args():
71715c04
IB
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
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")
a24c34bc
IB
36 parser.add_argument("-d", "--debug",
37 nargs=0,
38 action=DebugModeAction,
39 help="Print messages in console")
d6290f14 40 parser.add_argument("-m", "--builtin-mixing",
af27d782 41 action="store_true",
d6290f14 42 help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)")
75d6cdba
IB
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)")
71715c04
IB
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())
29ce1fde
IB
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 )
71715c04
IB
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.")
a24c34bc
IB
83
84 from kivy.logger import Logger
85 Logger.setLevel(logging.ERROR)
86
71715c04
IB
87 args = parser.parse_args(argv)
88
75d6cdba 89 Config.yml_file = args.config
a24c34bc 90
75d6cdba
IB
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
d6290f14 96 Config.builtin_mixing = args.builtin_mixing
71715c04 97
a24c34bc
IB
98class 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
29ce1fde
IB
103class SelectDeviceAction(argparse.Action):
104 def __call__(self, parser, namespace, values, option_string=None):
105 sd.default.device = values
106
107class 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
71715c04
IB
113def 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"
cc008de4 119
98ff4305
IB
120def 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)
1b4b78f5
IB
127
128def 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
a24c34bc
IB
134def debug_print(message):
135 from kivy.logger import Logger
136 Logger.debug('MusicSampler: ' + message)
137
138def error_print(message):
139 from kivy.logger import Logger
140 Logger.error('MusicSampler: ' + message)