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("-d", "--debug",
38 action
=DebugModeAction
,
39 help="Print messages in console")
40 parser
.add_argument("-m", "--builtin-mixing",
42 help="Make the mixing of sounds manually\
43 (do it if the system cannot handle it correctly)")
44 parser
.add_argument("-l", "--latency",
47 help="Latency: low, high or number of seconds")
48 parser
.add_argument("-b", "--blocksize",
52 help="Blocksize: If not 0, the number of frames to take\
53 at each step for the mixer")
54 parser
.add_argument("-f", "--frame-rate",
58 help="Frame rate to play the musics")
59 parser
.add_argument("-x", "--channels",
63 help="Number of channels to use")
64 parser
.add_argument("-s", "--sample-width",
68 help="Sample width (number of bytes for each frame)")
69 parser
.add_argument("-V", "--version",
71 help="Displays the current version and exits. Only use\
73 version
=show_version())
74 parser
.add_argument("--device",
75 action
=SelectDeviceAction
,
76 help="Select this sound device"
78 parser
.add_argument("--list-devices",
80 action
=ListDevicesAction
,
81 help="List available sound devices"
83 parser
.add_argument('--',
85 help="Kivy arguments. All arguments after this are interpreted\
86 by Kivy. Pass \"-- --help\" to get Kivy's usage.")
88 from kivy
.logger
import Logger
89 Logger
.setLevel(logging
.ERROR
)
91 args
= parser
.parse_args(argv
)
93 Config
.yml_file
= args
.config
95 Config
.latency
= args
.latency
96 Config
.blocksize
= args
.blocksize
97 Config
.frame_rate
= args
.frame_rate
98 Config
.channels
= args
.channels
99 Config
.sample_width
= args
.sample_width
100 Config
.builtin_mixing
= args
.builtin_mixing
102 class DebugModeAction(argparse
.Action
):
103 def __call__(self
, parser
, namespace
, values
, option_string
=None):
104 from kivy
.logger
import Logger
105 Logger
.setLevel(logging
.DEBUG
)
107 class SelectDeviceAction(argparse
.Action
):
108 def __call__(self
, parser
, namespace
, values
, option_string
=None):
109 sd
.default
.device
= values
111 class ListDevicesAction(argparse
.Action
):
113 def __call__(self
, parser
, namespace
, values
, option_string
=None):
114 print(sd
.query_devices())
118 if getattr(sys
, 'frozen', False):
119 with open(path() + ".pyinstaller_commit", "r") as f
:
122 return "option '-v' can only be used in bundled package"
124 def duration_to_min_sec(duration
):
125 minutes
= int(duration
/ 60)
126 seconds
= int(duration
) % 60
128 return "{:2}:{:0>2}".format(minutes
, seconds
)
130 return "{}:{:0>2}".format(minutes
, seconds
)
132 def gain(volume
, old_volume
=None):
133 if old_volume
is None:
134 return 20 * math
.log10(max(volume
, 0.1) / 100)
137 20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)),
140 def debug_print(message
):
141 from kivy
.logger
import Logger
142 Logger
.debug('MusicSampler: ' + message
)
144 def error_print(message
):
145 from kivy
.logger
import Logger
146 Logger
.error('MusicSampler: ' + message
)