-
Notifications
You must be signed in to change notification settings - Fork 0
/
rip.py
executable file
·201 lines (152 loc) · 5.99 KB
/
rip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# coding: utf-8
# Midi
from mido import open_output, get_output_names
# Audio
#import sounddevice as audio.DEVICE
import soundfile as sf
from numpy import delete # audiolink fix
# Utils
from time import sleep
from sys import exit
from colorama import Fore, Style
from shutil import copyfile
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, RawDescriptionHelpFormatter
from pathlib import Path
from IPython import embed
# Midi Tools
#from midi import midi.DEVICE, midi.DEVICES_LIST, midi.DEVICES_NAMES, ACORDES, NOTAS # Globals
#from midi import set_mididevice, play_chord
import midi
# Audio Tools
#from audio import audio.DEVICE, audio.DEVICES_LIST
#from audio import rip_note, EMPTY_WAV
import audio
# Logging
import logger
# Config
import config
# EPILOG
EPILOG = """
-- SOUND DEVICES
%s
-- MIDI DEVICES
%s
""" % ("\n".join(audio.DEVICES_LIST), "\n".join(midi.DEVICES_NAMES))
PARSER = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, epilog=EPILOG)
# PARSER.add_argument('--asens', default=False) # Aftertouch Sensitive
PARSER.add_argument('--config-file', default="config.yaml")
PARSER.add_argument('--pitch-range', default="synth")
PARSER.add_argument('--dry', action='store_true')
PARSER.add_argument('--chord-mode', action='store_true')
PARSER.add_argument('--test-mode', action='store_true')
PARSER.add_argument('--test-volume', action='store_true')
PARSER.add_argument('--fix-audiolink', action='store_true')
PARSER.add_argument('--ignore-clip', action='store_true')
PARSER.add_argument('--continue-rip', action='store_true')
PARSER.add_argument('--instrument', default="unknown")
PARSER.add_argument('--patch-name', default="unknown")
ARGS = PARSER.parse_args()
print(ARGS)
# Load settings from config.CONFIG FILE
if not config.load(ARGS.config_file):
print("Couldn't load config file [%s]" % ARGS.config_file)
exit(1)
# Load logger
if not logger.load():
print("Couldn't create logger file")
exit(2)
# Set SOUND DEVICE
if audio.load():
logger.LOGGER.info("SOUND-DEVICE: {}".format(audio.DEVICE.default.device))
else:
logger.LOGGER.error(audio.DEVICES_LIST)
exit(1)
# Set MIDI DEVICE
if midi.load():
logger.LOGGER.info("MIDI DEVICE: {}".format(midi.DEVICE))
else:
logger.LOGGER.error(midi.DEVICES_LIST)
exit(1)
# Send PANIC to MIDI and wait for silence, JIC
logger.LOGGER.info("Waiting for MIDI silence at %s" % midi.DEVICE)
midi.DEVICE.panic()
midi.DEVICE.reset()
sleep(5) # Espero
#
MIDI_NOTES = range(
config.CONFIG['midi-device']['pitch-ranges'][ARGS.pitch_range]['lowest-pitch'],
config.CONFIG['midi-device']['pitch-ranges'][ARGS.pitch_range]['highest-pitch'],
config.CONFIG['midi-device']['pitch-step']
)
# TODO, is this really necessary?
RECORD_TIME = config.CONFIG['midi-device']['duration'] * config.CONFIG['sound']['sample-rate']
FAILED = 0
if ARGS.test_volume:
logger.LOGGER.info("Testing Recording Volume --")
for MIDI_NOTE in [MIDI_NOTES[i] for i in range(0, len(MIDI_NOTES))]:
for MIDI_VEL in config.CONFIG['midi-device']['velocities']:
print("Volume testing: %s (%s)" % (MIDI_NOTE, MIDI_VEL))
record = rip_note(audio.DEVICE, config.CONFIG['sound'],
midi.DEVICE, config.CONFIG['midi-device'],
MIDI_NOTE, MIDI_VEL)
# TODO: range should be in config file
if record.min() < -0.97 or record.max() > 0.97:
print(Fore.RED + "Recording might have clippins!" + Style.RESET_ALL)
if not ARGS.ignore_clip:
exit(1)
elif ARGS.chord_mode:
for n in range(ARGS.lowest_pitch, ARGS.highest_pitch):
for c in ACORDES:
FILENAME = "/tmp/midi-ripper/%s-%s-%s.wav" % (n, NOTAS[n % len(NOTAS)], c)
print("Grabando:", FILENAME)
if not ARGS.dry:
record = audio.DEVICE.rec(RECORD_TIME) # Begin recording
sleep(0.002)
play_chord(midi.DEVICE, n, ACORDES[c], ARGS.sustain_time)
sleep(ARGS.decay_time)
audio.DEVICE.wait()
if not ARGS.dry:
sf.write(FILENAME, record, config.CONFIG['sound']['sample-rate'])
else:
# Por cada nota midi
for MIDI_NOTE in range(0, 128, 1):
# Por cada velocidad
for MIDI_VEL in config.CONFIG['midi-device']['velocities']:
# TODO: :
logger.LOGGER.debug("Recording: note=%d velocity=%d" % (MIDI_NOTE, MIDI_VEL))
FILENAME = "{tmp}/n{note:02d}-v{vel:02d}.wav"\
.format(tmp=config.CONFIG['temp-dir'], note=MIDI_NOTE, vel=MIDI_VEL)
# Si ya existe el archivo, continuo
if ARGS.continue_rip and Path(FILENAME).exists():
logger.LOGGER.debug("{} already ripped.".format(FILENAME))
continue
# Las notas que no me pidieron grabar, las reemplazo por un wav vacio
if MIDI_NOTE not in MIDI_NOTES:
if not ARGS.dry:
copyfile(audio.EMPTY_WAV, FILENAME)
continue
record = audio.rip_note(audio.DEVICE, config.CONFIG['sound'],
midi.DEVICE, config.CONFIG['midi-device'],
MIDI_NOTE, MIDI_VEL)
if ARGS.fix_audiolink: # Drop left channel
record = delete(record, 0, axis=1)
if not ARGS.dry:
sf.write(FILENAME, record, config.CONFIG['sound']['sample-rate'])
# sleep(1)
if not audio.check_volume(record):
logger.LOGGER.error(Fore.RED + "Recording might have clippins!" + Style.RESET_ALL)
FAILED+=1
Path(FILENAME).unlink()
if not ARGS.ignore_clip:
exit(1)
logger.LOGGER.info("Recorded: {}, {}, {}".format(FILENAME, record.min(), record.max()))
if FAILED > 0:
logger.LOGGER.error("Recordings FAILED: {}/{}".format(FAILED,len(MIDI_NOTES)))
exit()
"""
midi.DEVICE.reset()
sleep(5)
sustain = 5
duration = 7
"""