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 return os
.path
.dirname(os
.path
.realpath(__file__
))
54 sys
.argv
= sys
.argv
[: 1]
56 index
= argv
.index("--")
57 kivy_args
= argv
[index
+1 :]
60 sys
.argv
.extend(kivy_args
)
62 parser
= argparse
.ArgumentParser(
63 description
="A Music Sampler application.",
64 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
65 parser
.add_argument("-c", "--config",
68 help="Config file to load")
69 parser
.add_argument("-p", "--music-path",
72 help="Folder in which to find the music files")
73 parser
.add_argument("-d", "--debug",
75 action
=DebugModeAction
,
76 help="Print messages in console")
77 parser
.add_argument("-m", "--builtin-mixing",
79 help="Make the mixing of sounds manually\
80 (do it if the system cannot handle it correctly)")
81 parser
.add_argument("-l", "--latency",
84 help="Latency: low, high or number of seconds")
85 parser
.add_argument("-b", "--blocksize",
89 help="Blocksize: If not 0, the number of frames to take\
90 at each step for the mixer")
91 parser
.add_argument("-f", "--frame-rate",
95 help="Frame rate to play the musics")
96 parser
.add_argument("-x", "--channels",
100 help="Number of channels to use")
101 parser
.add_argument("-s", "--sample-width",
105 help="Sample width (number of bytes for each frame)")
106 parser
.add_argument("-V", "--version",
108 help="Displays the current version and exits. Only use\
110 version
=show_version())
111 parser
.add_argument("--device",
112 action
=SelectDeviceAction
,
113 help="Select this sound device"
115 parser
.add_argument("--list-devices",
117 action
=ListDevicesAction
,
118 help="List available sound devices"
120 parser
.add_argument('--',
122 help="Kivy arguments. All arguments after this are interpreted\
123 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
125 from kivy
.logger
import Logger
126 Logger
.setLevel(logging
.WARN
)
128 args
= parser
.parse_args(argv
)
130 Config
.yml_file
= args
.config
132 Config
.latency
= args
.latency
133 Config
.blocksize
= args
.blocksize
134 Config
.frame_rate
= args
.frame_rate
135 Config
.channels
= args
.channels
136 Config
.sample_width
= args
.sample_width
137 Config
.builtin_mixing
= args
.builtin_mixing
138 if args
.music_path
.endswith("/"):
139 Config
.music_path
= args
.music_path
141 Config
.music_path
= args
.music_path
+ "/"
143 class DebugModeAction(argparse
.Action
):
144 def __call__(self
, parser
, namespace
, values
, option_string
=None):
145 from kivy
.logger
import Logger
146 Logger
.setLevel(logging
.DEBUG
)
148 class SelectDeviceAction(argparse
.Action
):
149 def __call__(self
, parser
, namespace
, values
, option_string
=None):
150 sd
.default
.device
= values
152 class ListDevicesAction(argparse
.Action
):
154 def __call__(self
, parser
, namespace
, values
, option_string
=None):
155 print(sd
.query_devices())
159 if getattr(sys
, 'frozen', False):
160 with open(path() + ".pyinstaller_commit", "r") as f
:
163 return "option '-v' can only be used in bundled package"
165 def duration_to_min_sec(duration
):
166 minutes
= int(duration
/ 60)
167 seconds
= int(duration
) % 60
169 return "{:2}:{:0>2}".format(minutes
, seconds
)
171 return "{}:{:0>2}".format(minutes
, seconds
)
173 def gain(volume
, old_volume
=None):
174 if old_volume
is None:
175 return 20 * math
.log10(max(volume
, 0.1) / 100)
178 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
181 def debug_print(message
, with_trace
=False):
182 from kivy
.logger
import Logger
183 Logger
.debug('MusicSampler: ' + message
, exc_info
=with_trace
)
185 def error_print(message
, with_trace
=False):
186 from kivy
.logger
import Logger
187 Logger
.error('MusicSampler: ' + message
, exc_info
=with_trace
)
189 def warn_print(message
, with_trace
=False):
190 from kivy
.logger
import Logger
191 Logger
.warn('MusicSampler: ' + message
, exc_info
=with_trace
)