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 (do it if the system cannot handle it correctly)")
43 parser
.add_argument("-l", "--latency",
46 help="Latency: low, high or number of seconds")
47 parser
.add_argument("-b", "--blocksize",
51 help="Blocksize: If not 0, the numbe of frames to take at each step for the mixer")
52 parser
.add_argument("-f", "--frame-rate",
56 help="Frame rate to play the musics")
57 parser
.add_argument("-x", "--channels",
61 help="Number of channels to use")
62 parser
.add_argument("-s", "--sample-width",
66 help="Sample width (number of bytes for each frame)")
67 parser
.add_argument("-V", "--version",
69 help="Displays the current version and exits. Only use in bundled package",
70 version
=show_version())
71 parser
.add_argument("--device",
72 action
=SelectDeviceAction
,
73 help="Select this sound device"
75 parser
.add_argument("--list-devices",
77 action
=ListDevicesAction
,
78 help="List available sound devices"
80 parser
.add_argument('--',
82 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.")
84 from kivy
.logger
import Logger
85 Logger
.setLevel(logging
.ERROR
)
87 args
= parser
.parse_args(argv
)
89 Config
.yml_file
= args
.config
91 Config
.latency
= args
.latency
92 Config
.blocksize
= args
.blocksize
93 Config
.frame_rate
= args
.frame_rate
94 Config
.channels
= args
.channels
95 Config
.sample_width
= args
.sample_width
96 Config
.builtin_mixing
= args
.builtin_mixing
98 class DebugModeAction(argparse
.Action
):
99 def __call__(self
, parser
, namespace
, values
, option_string
=None):
100 from kivy
.logger
import Logger
101 Logger
.setLevel(logging
.DEBUG
)
103 class SelectDeviceAction(argparse
.Action
):
104 def __call__(self
, parser
, namespace
, values
, option_string
=None):
105 sd
.default
.device
= values
107 class ListDevicesAction(argparse
.Action
):
109 def __call__(self
, parser
, namespace
, values
, option_string
=None):
110 print(sd
.query_devices())
114 if getattr(sys
, 'frozen', False):
115 with open(path() + ".pyinstaller_commit", "r") as f
:
118 return "option '-v' can only be used in bundled package"
120 def duration_to_min_sec(duration
):
121 minutes
= int(duration
/ 60)
122 seconds
= int(duration
) % 60
124 return "{:2}:{:0>2}".format(minutes
, seconds
)
126 return "{}:{:0>2}".format(minutes
, seconds
)
128 def gain(volume
, old_volume
= None):
129 if old_volume
is None:
130 return 20 * math
.log10(volume
/ 100)
132 return [20 * math
.log10(max(volume
, 0.1) / max(old_volume
, 0.1)), max(volume
, 0)]
134 def debug_print(message
):
135 from kivy
.logger
import Logger
136 Logger
.debug('MusicSampler: ' + message
)
138 def error_print(message
):
139 from kivy
.logger
import Logger
140 Logger
.error('MusicSampler: ' + message
)