1 # -*- coding: utf-8 -*-
6 import sounddevice
as sd
9 gettext
.install('music_sampler')
10 Logger
= logging
.getLogger("kivy")
17 def find_font(name
, style
=sysfont
.STYLE_NONE
):
18 if getattr(sys
, 'frozen', False):
19 font
= sys
._MEIPASS
+ "/fonts/{}_{}.ttf".format(name
, style
)
21 font
= sysfont
.get_font(name
, style
=style
)
27 from kivy
.core
.text
import LabelBase
29 ubuntu_regular
= find_font("Ubuntu", style
=sysfont
.STYLE_NORMAL
)
30 ubuntu_bold
= find_font("Ubuntu", style
=sysfont
.STYLE_BOLD
)
31 symbola
= find_font("Symbola")
33 if ubuntu_regular
is None:
34 error_print("Font Ubuntu regular could not be found, "
35 "please install it.", exit
=True)
37 error_print("Font Symbola could not be found, please install it.",
39 if ubuntu_bold
is None:
40 warn_print("Font Ubuntu Bold could not be found.")
42 LabelBase
.register(name
="Ubuntu",
43 fn_regular
=ubuntu_regular
,
45 LabelBase
.register(name
="Symbola",
50 if getattr(sys
, 'frozen', False):
51 return sys
._MEIPASS
+ "/"
53 return os
.path
.dirname(os
.path
.realpath(__file__
))
57 sys
.argv
= sys
.argv
[: 1]
59 index
= argv
.index("--")
60 kivy_args
= argv
[index
+1 :]
63 sys
.argv
.extend(kivy_args
)
65 os
.environ
["KIVY_NO_CONFIG"] = 'true'
66 sys
.argv
.extend(["-c", "kivy:log_level:warning"])
67 sys
.argv
.extend(["-c", "kivy:log_dir:/tmp"])
68 sys
.argv
.extend(["-c", "kivy:log_name:/tmp/music_sampler_%_.txt"])
70 parser
= argparse
.ArgumentParser(
71 description
=_("A Music Sampler application."),
72 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
73 parser
.add_argument("-c", "--config",
76 help=_("Config file to load"))
77 parser
.add_argument("-p", "--music-path",
80 help=_("Folder in which to find the music files"))
81 parser
.add_argument("-d", "--debug",
83 action
=DebugModeAction
,
84 help=_("Print messages in console"))
85 parser
.add_argument("-m", "--builtin-mixing",
87 help=_("Make the mixing of sounds manually\
88 (do it if the system cannot handle it correctly)"))
89 parser
.add_argument("-l", "--latency",
92 help=_("Latency: low, high or number of seconds"))
93 parser
.add_argument("-b", "--blocksize",
97 help=_("Blocksize: If not 0, the number of frames to take\
98 at each step for the mixer"))
99 parser
.add_argument("-f", "--frame-rate",
103 help=_("Frame rate to play the musics"))
104 parser
.add_argument("-x", "--channels",
108 help=_("Number of channels to use"))
109 parser
.add_argument("-s", "--sample-width",
113 help=_("Sample width (number of bytes for each frame)"))
114 parser
.add_argument("-V", "--version",
116 help=_("Displays the current version and exits. Only use\
117 in bundled package"),
118 version
=show_version())
119 parser
.add_argument("--device",
120 action
=SelectDeviceAction
,
121 help=_("Select this sound device")
123 parser
.add_argument("--list-devices",
125 action
=ListDevicesAction
,
126 help=_("List available sound devices")
128 parser
.add_argument("--no-focus-warning",
130 help=_("Don't show warning when focus is lost")
132 parser
.add_argument("-L", "--language",
135 help=_("Select another language")
137 parser
.add_argument('--',
139 help=_("Kivy arguments. All arguments after this are interpreted\
140 by Kivy. Pass \"-- --help\" to get Kivy's usage."))
142 args
= parser
.parse_args(argv
)
144 Config
.yml_file
= args
.config
146 Config
.latency
= args
.latency
147 Config
.blocksize
= args
.blocksize
148 Config
.frame_rate
= args
.frame_rate
149 Config
.channels
= args
.channels
150 Config
.sample_width
= args
.sample_width
151 Config
.builtin_mixing
= args
.builtin_mixing
152 Config
.no_focus_warning
= args
.no_focus_warning
153 if args
.language
!= 'en':
154 gettext
.translation("music_sampler",
155 localedir
=path() + '/locales',
156 languages
=[args
.language
]).install()
157 if args
.music_path
.endswith("/"):
158 Config
.music_path
= args
.music_path
160 Config
.music_path
= args
.music_path
+ "/"
162 class DebugModeAction(argparse
.Action
):
163 def __call__(self
, parser
, namespace
, values
, option_string
=None):
164 sys
.argv
.extend(["-c", "kivy:log_level:debug"])
166 class SelectDeviceAction(argparse
.Action
):
167 def __call__(self
, parser
, namespace
, values
, option_string
=None):
168 sd
.default
.device
= values
170 class ListDevicesAction(argparse
.Action
):
172 def __call__(self
, parser
, namespace
, values
, option_string
=None):
173 print(sd
.query_devices())
177 if getattr(sys
, 'frozen', False):
178 with open(path() + ".pyinstaller_commit", "r") as f
:
181 return _("option '-V' can only be used in bundled package")
183 def duration_to_min_sec(duration
):
184 minutes
= int(duration
/ 60)
185 seconds
= int(duration
) % 60
187 return "{:2}:{:0>2}".format(minutes
, seconds
)
189 return "{}:{:0>2}".format(minutes
, seconds
)
191 def gain(volume
, old_volume
=None):
192 if old_volume
is None:
193 return 20 * math
.log10(max(volume
, 0.1) / 100)
196 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
199 def debug_print(message
, with_trace
=None):
200 if with_trace
is None:
201 with_trace
= (Logger
.getEffectiveLevel() < logging
.WARN
)
202 with_trace
&= (sys
.exc_info()[0] is not None)
204 Logger
.debug('MusicSampler: ' + message
, exc_info
=with_trace
)
206 def error_print(message
, exit
=False, with_trace
=None):
207 if with_trace
is None:
208 with_trace
= (Logger
.getEffectiveLevel() < logging
.WARN
)
209 with_trace
&= (sys
.exc_info()[0] is not None)
211 # FIXME: handle it correctly when in a thread
213 Logger
.critical('MusicSampler: ' + message
, exc_info
=with_trace
)
216 Logger
.error('MusicSampler: ' + message
, exc_info
=with_trace
)
218 def warn_print(message
, with_trace
=None):
219 if with_trace
is None:
220 with_trace
= (Logger
.getEffectiveLevel() < logging
.WARN
)
221 with_trace
&= (sys
.exc_info()[0] is not None)
223 Logger
.warn('MusicSampler: ' + message
, exc_info
=with_trace
)