]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/__init__.py
Invert no_mixing flag
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / __init__.py
CommitLineData
1df30f07 1# -*- coding: utf-8 -*-
71715c04 2import argparse
bb69f62e
IB
3import sys
4import os
1b4b78f5 5import math
29ce1fde 6import sounddevice as sd
bb69f62e 7
9b9dd12a 8class Config:
75d6cdba 9 pass
9b9dd12a 10
bb69f62e
IB
11def path():
12 if getattr(sys, 'frozen', False):
13 return sys._MEIPASS + "/"
14 else:
15 path = os.path.dirname(os.path.realpath(__file__))
16 return path + "/../"
17
cc008de4 18def parse_args():
71715c04
IB
19 argv = sys.argv[1:]
20 sys.argv = sys.argv[:1]
21 if "--" in argv:
22 index = argv.index("--")
23 kivy_args = argv[index+1:]
24 argv = argv[:index]
25
26 sys.argv.extend(kivy_args)
27
75d6cdba
IB
28 parser = argparse.ArgumentParser(
29 description="A Music Sampler application.",
30 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
71715c04
IB
31 parser.add_argument("-c", "--config",
32 default="config.yml",
33 required=False,
34 help="Config file to load")
d6290f14 35 parser.add_argument("-m", "--builtin-mixing",
af27d782 36 action="store_true",
d6290f14 37 help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)")
75d6cdba
IB
38 parser.add_argument("-l", "--latency",
39 default="high",
40 required=False,
41 help="Latency: low, high or number of seconds")
42 parser.add_argument("-b", "--blocksize",
43 default=0,
44 type=int,
45 required=False,
46 help="Blocksize: If not 0, the numbe of frames to take at each step for the mixer")
47 parser.add_argument("-f", "--frame-rate",
48 default=44100,
49 type=int,
50 required=False,
51 help="Frame rate to play the musics")
52 parser.add_argument("-x", "--channels",
53 default=2,
54 type=int,
55 required=False,
56 help="Number of channels to use")
57 parser.add_argument("-s", "--sample-width",
58 default=2,
59 type=int,
60 required=False,
61 help="Sample width (number of bytes for each frame)")
71715c04
IB
62 parser.add_argument("-V", "--version",
63 action="version",
64 help="Displays the current version and exits. Only use in bundled package",
65 version=show_version())
29ce1fde
IB
66 parser.add_argument("--device",
67 action=SelectDeviceAction,
68 help="Select this sound device"
69 )
70 parser.add_argument("--list-devices",
71 nargs=0,
72 action=ListDevicesAction,
73 help="List available sound devices"
74 )
71715c04
IB
75 parser.add_argument('--',
76 dest="args",
77 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.")
78 args = parser.parse_args(argv)
79
75d6cdba
IB
80 Config.yml_file = args.config
81 Config.latency = args.latency
82 Config.blocksize = args.blocksize
83 Config.frame_rate = args.frame_rate
84 Config.channels = args.channels
85 Config.sample_width = args.sample_width
d6290f14 86 Config.builtin_mixing = args.builtin_mixing
71715c04 87
29ce1fde
IB
88class SelectDeviceAction(argparse.Action):
89 def __call__(self, parser, namespace, values, option_string=None):
90 sd.default.device = values
91
92class ListDevicesAction(argparse.Action):
93 nargs = 0
94 def __call__(self, parser, namespace, values, option_string=None):
95 print(sd.query_devices())
96 sys.exit()
97
71715c04
IB
98def show_version():
99 if getattr(sys, 'frozen', False):
100 with open(path() + ".pyinstaller_commit", "r") as f:
101 return f.read()
102 else:
103 return "option '-v' can only be used in bundled package"
cc008de4 104
98ff4305
IB
105def duration_to_min_sec(duration):
106 minutes = int(duration / 60)
107 seconds = int(duration) % 60
108 if minutes < 100:
109 return "{:2}:{:0>2}".format(minutes, seconds)
110 else:
111 return "{}:{:0>2}".format(minutes, seconds)
1b4b78f5
IB
112
113def gain(volume, old_volume = None):
114 if old_volume is None:
115 return 20 * math.log10(volume / 100)
116 else:
117 return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)]
118