1 # -*- coding: utf-8 -*-
6 import sounddevice
as sd
13 if getattr(sys
, 'frozen', False):
14 return sys
._MEIPASS
+ "/"
16 path
= os
.path
.dirname(os
.path
.realpath(__file__
))
21 sys
.argv
= sys
.argv
[: 1]
23 index
= argv
.index("--")
24 kivy_args
= argv
[index
+1 :]
27 sys
.argv
.extend(kivy_args
)
29 parser
= argparse
.ArgumentParser(
30 description
="A Music Sampler application.",
31 formatter_class
=argparse
.ArgumentDefaultsHelpFormatter
)
32 parser
.add_argument("-c", "--config",
35 help="Config file to load")
36 parser
.add_argument("-p", "--music-path",
39 help="Folder in which to find the music files")
40 parser
.add_argument("-d", "--debug",
42 action
=DebugModeAction
,
43 help="Print messages in console")
44 parser
.add_argument("-m", "--builtin-mixing",
46 help="Make the mixing of sounds manually\
47 (do it if the system cannot handle it correctly)")
48 parser
.add_argument("-l", "--latency",
51 help="Latency: low, high or number of seconds")
52 parser
.add_argument("-b", "--blocksize",
56 help="Blocksize: If not 0, the number of frames to take\
57 at each step for the mixer")
58 parser
.add_argument("-f", "--frame-rate",
62 help="Frame rate to play the musics")
63 parser
.add_argument("-x", "--channels",
67 help="Number of channels to use")
68 parser
.add_argument("-s", "--sample-width",
72 help="Sample width (number of bytes for each frame)")
73 parser
.add_argument("-V", "--version",
75 help="Displays the current version and exits. Only use\
77 version
=show_version())
78 parser
.add_argument("--device",
79 action
=SelectDeviceAction
,
80 help="Select this sound device"
82 parser
.add_argument("--list-devices",
84 action
=ListDevicesAction
,
85 help="List available sound devices"
87 parser
.add_argument('--',
89 help="Kivy arguments. All arguments after this are interpreted\
90 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
92 from kivy
.logger
import Logger
93 Logger
.setLevel(logging
.WARN
)
95 args
= parser
.parse_args(argv
)
97 Config
.yml_file
= args
.config
99 Config
.latency
= args
.latency
100 Config
.blocksize
= args
.blocksize
101 Config
.frame_rate
= args
.frame_rate
102 Config
.channels
= args
.channels
103 Config
.sample_width
= args
.sample_width
104 Config
.builtin_mixing
= args
.builtin_mixing
105 if args
.music_path
.endswith("/"):
106 Config
.music_path
= args
.music_path
108 Config
.music_path
= args
.music_path
+ "/"
110 class DebugModeAction(argparse
.Action
):
111 def __call__(self
, parser
, namespace
, values
, option_string
=None):
112 from kivy
.logger
import Logger
113 Logger
.setLevel(logging
.DEBUG
)
115 class SelectDeviceAction(argparse
.Action
):
116 def __call__(self
, parser
, namespace
, values
, option_string
=None):
117 sd
.default
.device
= values
119 class ListDevicesAction(argparse
.Action
):
121 def __call__(self
, parser
, namespace
, values
, option_string
=None):
122 print(sd
.query_devices())
126 if getattr(sys
, 'frozen', False):
127 with open(path() + ".pyinstaller_commit", "r") as f
:
130 return "option '-v' can only be used in bundled package"
132 def duration_to_min_sec(duration
):
133 minutes
= int(duration
/ 60)
134 seconds
= int(duration
) % 60
136 return "{:2}:{:0>2}".format(minutes
, seconds
)
138 return "{}:{:0>2}".format(minutes
, seconds
)
140 def gain(volume
, old_volume
=None):
141 if old_volume
is None:
142 return 20 * math
.log10(max(volume
, 0.1) / 100)
145 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
148 def debug_print(message
, with_trace
=False):
149 from kivy
.logger
import Logger
150 Logger
.debug('MusicSampler: ' + message
, exc_info
=with_trace
)
152 def error_print(message
, with_trace
=False):
153 from kivy
.logger
import Logger
154 Logger
.error('MusicSampler: ' + message
, exc_info
=with_trace
)
156 def warn_print(message
, with_trace
=False):
157 from kivy
.logger
import Logger
158 Logger
.warn('MusicSampler: ' + message
, exc_info
=with_trace
)