1 # -*- coding: utf-8 -*-
6 import sounddevice
as sd
8 Logger
= logging
.getLogger("kivy")
15 def find_font(name
, style
=sysfont
.STYLE_NONE
):
16 if getattr(sys
, 'frozen', False):
17 font
= sys
._MEIPASS
+ "/fonts/{}_{}.ttf".format(name
, style
)
19 font
= sysfont
.get_font(name
, style
=style
)
25 from kivy
.core
.text
import LabelBase
27 ubuntu_regular
= find_font("Ubuntu", style
=sysfont
.STYLE_NORMAL
)
28 ubuntu_bold
= find_font("Ubuntu", style
=sysfont
.STYLE_BOLD
)
29 symbola
= find_font("Symbola")
31 if ubuntu_regular
is None:
32 error_print("Font Ubuntu regular could not be found, please install it.")
35 error_print("Font Symbola could not be found, please install it.")
37 if ubuntu_bold
is None:
38 warn_print("Font Ubuntu Bold could not be found.")
40 LabelBase
.register(name
="Ubuntu",
41 fn_regular
=ubuntu_regular
,
43 LabelBase
.register(name
="Symbola",
48 if getattr(sys
, 'frozen', False):
49 return sys
._MEIPASS
+ "/"
51 return 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 os
.environ
["KIVY_NO_CONFIG"] = 'true'
64 sys
.argv
.extend(["-c", "kivy:log_level:warning"])
65 sys
.argv
.extend(["-c", "kivy:log_dir:/tmp"])
66 sys
.argv
.extend(["-c", "kivy:log_name:/tmp/music_sampler_%_.txt"])
68 parser
= argparse
.ArgumentParser(
69 description
="A Music Sampler application.",
70 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
71 parser
.add_argument("-c", "--config",
74 help="Config file to load")
75 parser
.add_argument("-p", "--music-path",
78 help="Folder in which to find the music files")
79 parser
.add_argument("-d", "--debug",
81 action
=DebugModeAction
,
82 help="Print messages in console")
83 parser
.add_argument("-m", "--builtin-mixing",
85 help="Make the mixing of sounds manually\
86 (do it if the system cannot handle it correctly)")
87 parser
.add_argument("-l", "--latency",
90 help="Latency: low, high or number of seconds")
91 parser
.add_argument("-b", "--blocksize",
95 help="Blocksize: If not 0, the number of frames to take\
96 at each step for the mixer")
97 parser
.add_argument("-f", "--frame-rate",
101 help="Frame rate to play the musics")
102 parser
.add_argument("-x", "--channels",
106 help="Number of channels to use")
107 parser
.add_argument("-s", "--sample-width",
111 help="Sample width (number of bytes for each frame)")
112 parser
.add_argument("-V", "--version",
114 help="Displays the current version and exits. Only use\
116 version
=show_version())
117 parser
.add_argument("--device",
118 action
=SelectDeviceAction
,
119 help="Select this sound device"
121 parser
.add_argument("--list-devices",
123 action
=ListDevicesAction
,
124 help="List available sound devices"
126 parser
.add_argument('--',
128 help="Kivy arguments. All arguments after this are interpreted\
129 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
131 args
= parser
.parse_args(argv
)
133 Config
.yml_file
= args
.config
135 Config
.latency
= args
.latency
136 Config
.blocksize
= args
.blocksize
137 Config
.frame_rate
= args
.frame_rate
138 Config
.channels
= args
.channels
139 Config
.sample_width
= args
.sample_width
140 Config
.builtin_mixing
= args
.builtin_mixing
141 if args
.music_path
.endswith("/"):
142 Config
.music_path
= args
.music_path
144 Config
.music_path
= args
.music_path
+ "/"
146 class DebugModeAction(argparse
.Action
):
147 def __call__(self
, parser
, namespace
, values
, option_string
=None):
148 sys
.argv
.extend(["-c", "kivy:log_level:debug"])
150 class SelectDeviceAction(argparse
.Action
):
151 def __call__(self
, parser
, namespace
, values
, option_string
=None):
152 sd
.default
.device
= values
154 class ListDevicesAction(argparse
.Action
):
156 def __call__(self
, parser
, namespace
, values
, option_string
=None):
157 print(sd
.query_devices())
161 if getattr(sys
, 'frozen', False):
162 with open(path() + ".pyinstaller_commit", "r") as f
:
165 return "option '-v' can only be used in bundled package"
167 def duration_to_min_sec(duration
):
168 minutes
= int(duration
/ 60)
169 seconds
= int(duration
) % 60
171 return "{:2}:{:0>2}".format(minutes
, seconds
)
173 return "{}:{:0>2}".format(minutes
, seconds
)
175 def gain(volume
, old_volume
=None):
176 if old_volume
is None:
177 return 20 * math
.log10(max(volume
, 0.1) / 100)
180 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
183 def debug_print(message
, with_trace
=False):
184 Logger
.debug('MusicSampler: ' + message
, exc_info
=with_trace
)
186 def error_print(message
, with_trace
=False):
187 Logger
.error('MusicSampler: ' + message
, exc_info
=with_trace
)
189 def warn_print(message
, with_trace
=False):
190 Logger
.warn('MusicSampler: ' + message
, exc_info
=with_trace
)