1 # -*- coding: utf-8 -*-
6 import sounddevice
as sd
14 def find_font(name
, style
=sysfont
.STYLE_NONE
):
15 if getattr(sys
, 'frozen', False):
16 font
= sys
._MEIPASS
+ "/fonts/{}_{}.ttf".format(name
, style
)
18 font
= sysfont
.get_font(name
, style
=style
)
24 from kivy
.core
.text
import LabelBase
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")
30 if ubuntu_regular
is None:
31 error_print("Font Ubuntu regular could not be found, please install it.")
34 error_print("Font Symbola could not be found, please install it.")
36 if ubuntu_bold
is None:
37 warn_print("Font Ubuntu Bold could not be found.")
39 LabelBase
.register(name
="Ubuntu",
40 fn_regular
=ubuntu_regular
,
42 LabelBase
.register(name
="Symbola",
47 if getattr(sys
, 'frozen', False):
48 return sys
._MEIPASS
+ "/"
50 path
= os
.path
.dirname(os
.path
.realpath(__file__
))
55 sys
.argv
= sys
.argv
[: 1]
57 index
= argv
.index("--")
58 kivy_args
= argv
[index
+1 :]
61 sys
.argv
.extend(kivy_args
)
63 parser
= argparse
.ArgumentParser(
64 description
="A Music Sampler application.",
65 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
66 parser
.add_argument("-c", "--config",
69 help="Config file to load")
70 parser
.add_argument("-p", "--music-path",
73 help="Folder in which to find the music files")
74 parser
.add_argument("-d", "--debug",
76 action
=DebugModeAction
,
77 help="Print messages in console")
78 parser
.add_argument("-m", "--builtin-mixing",
80 help="Make the mixing of sounds manually\
81 (do it if the system cannot handle it correctly)")
82 parser
.add_argument("-l", "--latency",
85 help="Latency: low, high or number of seconds")
86 parser
.add_argument("-b", "--blocksize",
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",
96 help="Frame rate to play the musics")
97 parser
.add_argument("-x", "--channels",
101 help="Number of channels to use")
102 parser
.add_argument("-s", "--sample-width",
106 help="Sample width (number of bytes for each frame)")
107 parser
.add_argument("-V", "--version",
109 help="Displays the current version and exits. Only use\
111 version
=show_version())
112 parser
.add_argument("--device",
113 action
=SelectDeviceAction
,
114 help="Select this sound device"
116 parser
.add_argument("--list-devices",
118 action
=ListDevicesAction
,
119 help="List available sound devices"
121 parser
.add_argument('--',
123 help="Kivy arguments. All arguments after this are interpreted\
124 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
126 from kivy
.logger
import Logger
127 Logger
.setLevel(logging
.WARN
)
129 args
= parser
.parse_args(argv
)
131 Config
.yml_file
= args
.config
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
142 Config
.music_path
= args
.music_path
+ "/"
144 class DebugModeAction(argparse
.Action
):
145 def __call__(self
, parser
, namespace
, values
, option_string
=None):
146 from kivy
.logger
import Logger
147 Logger
.setLevel(logging
.DEBUG
)
149 class SelectDeviceAction(argparse
.Action
):
150 def __call__(self
, parser
, namespace
, values
, option_string
=None):
151 sd
.default
.device
= values
153 class ListDevicesAction(argparse
.Action
):
155 def __call__(self
, parser
, namespace
, values
, option_string
=None):
156 print(sd
.query_devices())
160 if getattr(sys
, 'frozen', False):
161 with open(path() + ".pyinstaller_commit", "r") as f
:
164 return "option '-v' can only be used in bundled package"
166 def duration_to_min_sec(duration
):
167 minutes
= int(duration
/ 60)
168 seconds
= int(duration
) % 60
170 return "{:2}:{:0>2}".format(minutes
, seconds
)
172 return "{}:{:0>2}".format(minutes
, seconds
)
174 def gain(volume
, old_volume
=None):
175 if old_volume
is None:
176 return 20 * math
.log10(max(volume
, 0.1) / 100)
179 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
182 def debug_print(message
, with_trace
=False):
183 from kivy
.logger
import Logger
184 Logger
.debug('MusicSampler: ' + message
, exc_info
=with_trace
)
186 def error_print(message
, with_trace
=False):
187 from kivy
.logger
import Logger
188 Logger
.error('MusicSampler: ' + message
, exc_info
=with_trace
)
190 def warn_print(message
, with_trace
=False):
191 from kivy
.logger
import Logger
192 Logger
.warn('MusicSampler: ' + message
, exc_info
=with_trace
)