]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/__init__.py
Invert no_mixing flag
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / __init__.py
1 # -*- coding: utf-8 -*-
2 import argparse
3 import sys
4 import os
5 import math
6 import sounddevice as sd
7
8 class Config:
9 pass
10
11 def 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
18 def parse_args():
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
28 parser = argparse.ArgumentParser(
29 description="A Music Sampler application.",
30 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
31 parser.add_argument("-c", "--config",
32 default="config.yml",
33 required=False,
34 help="Config file to load")
35 parser.add_argument("-m", "--builtin-mixing",
36 action="store_true",
37 help="Make the mixing of sounds manually (do it if the system cannot handle it correctly)")
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)")
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())
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 )
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
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
86 Config.builtin_mixing = args.builtin_mixing
87
88 class SelectDeviceAction(argparse.Action):
89 def __call__(self, parser, namespace, values, option_string=None):
90 sd.default.device = values
91
92 class 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
98 def 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"
104
105 def 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)
112
113 def 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