aboutsummaryrefslogtreecommitdiff
path: root/music_sampler/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'music_sampler/__init__.py')
-rw-r--r--music_sampler/__init__.py193
1 files changed, 1 insertions, 192 deletions
diff --git a/music_sampler/__init__.py b/music_sampler/__init__.py
index 4827e6c..aa551fb 100644
--- a/music_sampler/__init__.py
+++ b/music_sampler/__init__.py
@@ -1,192 +1 @@
1# -*- coding: utf-8 -*- from . import app
2import argparse
3import sys
4import os
5import math
6import sounddevice as sd
7import logging
8
9from . import sysfont
10
11class Config:
12 pass
13
14def find_font(name, style=sysfont.STYLE_NONE):
15 if getattr(sys, 'frozen', False):
16 font = sys._MEIPASS + "/fonts/{}_{}.ttf".format(name, style)
17 else:
18 font = sysfont.get_font(name, style=style)
19 if font is not None:
20 font = font[4]
21 return font
22
23def register_fonts():
24 from kivy.core.text import LabelBase
25
26 ubuntu_regular = find_font("Ubuntu", style=sysfont.STYLE_NORMAL)
27 ubuntu_bold = find_font("Ubuntu", style=sysfont.STYLE_BOLD)
28 symbola = find_font("Symbola")
29
30 if ubuntu_regular is None:
31 error_print("Font Ubuntu regular could not be found, please install it.")
32 sys.exit()
33 if symbola is None:
34 error_print("Font Symbola could not be found, please install it.")
35 sys.exit()
36 if ubuntu_bold is None:
37 warn_print("Font Ubuntu Bold could not be found.")
38
39 LabelBase.register(name="Ubuntu",
40 fn_regular=ubuntu_regular,
41 fn_bold=ubuntu_bold)
42 LabelBase.register(name="Symbola",
43 fn_regular=symbola)
44
45
46def path():
47 if getattr(sys, 'frozen', False):
48 return sys._MEIPASS + "/"
49 else:
50 path = os.path.dirname(os.path.realpath(__file__))
51 return path + "/../"
52
53def parse_args():
54 argv = sys.argv[1 :]
55 sys.argv = sys.argv[: 1]
56 if "--" in argv:
57 index = argv.index("--")
58 kivy_args = argv[index+1 :]
59 argv = argv[: index]
60
61 sys.argv.extend(kivy_args)
62
63 parser = argparse.ArgumentParser(
64 description="A Music Sampler application.",
65 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
66 parser.add_argument("-c", "--config",
67 default="config.yml",
68 required=False,
69 help="Config file to load")
70 parser.add_argument("-p", "--music-path",
71 default=".",
72 required=False,
73 help="Folder in which to find the music files")
74 parser.add_argument("-d", "--debug",
75 nargs=0,
76 action=DebugModeAction,
77 help="Print messages in console")
78 parser.add_argument("-m", "--builtin-mixing",
79 action="store_true",
80 help="Make the mixing of sounds manually\
81 (do it if the system cannot handle it correctly)")
82 parser.add_argument("-l", "--latency",
83 default="high",
84 required=False,
85 help="Latency: low, high or number of seconds")
86 parser.add_argument("-b", "--blocksize",
87 default=0,
88 type=int,
89 required=False,
90 help="Blocksize: If not 0, the number of frames to take\
91 at each step for the mixer")
92 parser.add_argument("-f", "--frame-rate",
93 default=44100,
94 type=int,
95 required=False,
96 help="Frame rate to play the musics")
97 parser.add_argument("-x", "--channels",
98 default=2,
99 type=int,
100 required=False,
101 help="Number of channels to use")
102 parser.add_argument("-s", "--sample-width",
103 default=2,
104 type=int,
105 required=False,
106 help="Sample width (number of bytes for each frame)")
107 parser.add_argument("-V", "--version",
108 action="version",
109 help="Displays the current version and exits. Only use\
110 in bundled package",
111 version=show_version())
112 parser.add_argument("--device",
113 action=SelectDeviceAction,
114 help="Select this sound device"
115 )
116 parser.add_argument("--list-devices",
117 nargs=0,
118 action=ListDevicesAction,
119 help="List available sound devices"
120 )
121 parser.add_argument('--',
122 dest="args",
123 help="Kivy arguments. All arguments after this are interpreted\
124 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
125
126 from kivy.logger import Logger
127 Logger.setLevel(logging.WARN)
128
129 args = parser.parse_args(argv)
130
131 Config.yml_file = args.config
132
133 Config.latency = args.latency
134 Config.blocksize = args.blocksize
135 Config.frame_rate = args.frame_rate
136 Config.channels = args.channels
137 Config.sample_width = args.sample_width
138 Config.builtin_mixing = args.builtin_mixing
139 if args.music_path.endswith("/"):
140 Config.music_path = args.music_path
141 else:
142 Config.music_path = args.music_path + "/"
143
144class DebugModeAction(argparse.Action):
145 def __call__(self, parser, namespace, values, option_string=None):
146 from kivy.logger import Logger
147 Logger.setLevel(logging.DEBUG)
148
149class SelectDeviceAction(argparse.Action):
150 def __call__(self, parser, namespace, values, option_string=None):
151 sd.default.device = values
152
153class ListDevicesAction(argparse.Action):
154 nargs = 0
155 def __call__(self, parser, namespace, values, option_string=None):
156 print(sd.query_devices())
157 sys.exit()
158
159def show_version():
160 if getattr(sys, 'frozen', False):
161 with open(path() + ".pyinstaller_commit", "r") as f:
162 return f.read()
163 else:
164 return "option '-v' can only be used in bundled package"
165
166def duration_to_min_sec(duration):
167 minutes = int(duration / 60)
168 seconds = int(duration) % 60
169 if minutes < 100:
170 return "{:2}:{:0>2}".format(minutes, seconds)
171 else:
172 return "{}:{:0>2}".format(minutes, seconds)
173
174def gain(volume, old_volume=None):
175 if old_volume is None:
176 return 20 * math.log10(max(volume, 0.1) / 100)
177 else:
178 return [
179 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)),
180 max(volume, 0)]
181
182def debug_print(message, with_trace=False):
183 from kivy.logger import Logger
184 Logger.debug('MusicSampler: ' + message, exc_info=with_trace)
185
186def error_print(message, with_trace=False):
187 from kivy.logger import Logger
188 Logger.error('MusicSampler: ' + message, exc_info=with_trace)
189
190def warn_print(message, with_trace=False):
191 from kivy.logger import Logger
192 Logger.warn('MusicSampler: ' + message, exc_info=with_trace)