]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/__init__.py
Add a common mixer
[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
IB
8class Config:
9 def __init__(self, **kwargs):
10 for arg in kwargs:
11 setattr(self, arg, kwargs[arg])
12
13config = Config(yml_file="config.yml")
14
bb69f62e
IB
15def path():
16 if getattr(sys, 'frozen', False):
17 return sys._MEIPASS + "/"
18 else:
19 path = os.path.dirname(os.path.realpath(__file__))
20 return path + "/../"
21
cc008de4 22def parse_args():
71715c04
IB
23 argv = sys.argv[1:]
24 sys.argv = sys.argv[:1]
25 if "--" in argv:
26 index = argv.index("--")
27 kivy_args = argv[index+1:]
28 argv = argv[:index]
29
30 sys.argv.extend(kivy_args)
31
32 parser = argparse.ArgumentParser(description="A Music Sampler application.")
33 parser.add_argument("-c", "--config",
34 default="config.yml",
35 required=False,
36 help="Config file to load")
37 parser.add_argument("-V", "--version",
38 action="version",
39 help="Displays the current version and exits. Only use in bundled package",
40 version=show_version())
29ce1fde
IB
41 parser.add_argument("--device",
42 action=SelectDeviceAction,
43 help="Select this sound device"
44 )
45 parser.add_argument("--list-devices",
46 nargs=0,
47 action=ListDevicesAction,
48 help="List available sound devices"
49 )
71715c04
IB
50 parser.add_argument('--',
51 dest="args",
52 help="Kivy arguments. All arguments after this are interpreted by Kivy. Pass \"-- --help\" to get Kivy's usage.")
53 args = parser.parse_args(argv)
54
55 config.yml_file = args.config
56
29ce1fde
IB
57class SelectDeviceAction(argparse.Action):
58 def __call__(self, parser, namespace, values, option_string=None):
59 sd.default.device = values
60
61class ListDevicesAction(argparse.Action):
62 nargs = 0
63 def __call__(self, parser, namespace, values, option_string=None):
64 print(sd.query_devices())
65 sys.exit()
66
71715c04
IB
67def show_version():
68 if getattr(sys, 'frozen', False):
69 with open(path() + ".pyinstaller_commit", "r") as f:
70 return f.read()
71 else:
72 return "option '-v' can only be used in bundled package"
cc008de4 73
9b9dd12a
IB
74def yml_file():
75 return config.yml_file
76
98ff4305
IB
77def duration_to_min_sec(duration):
78 minutes = int(duration / 60)
79 seconds = int(duration) % 60
80 if minutes < 100:
81 return "{:2}:{:0>2}".format(minutes, seconds)
82 else:
83 return "{}:{:0>2}".format(minutes, seconds)
1b4b78f5
IB
84
85def gain(volume, old_volume = None):
86 if old_volume is None:
87 return 20 * math.log10(volume / 100)
88 else:
89 return [20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)]
90