-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidisampler.py
More file actions
executable file
·265 lines (213 loc) · 7.94 KB
/
Copy pathmidisampler.py
File metadata and controls
executable file
·265 lines (213 loc) · 7.94 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# python3 -m pip install python-rtmidi
# python3 -m pip install setuptools --user
# python3 -m pip install cffi --user
# python3 -m pip install sounddevice --user
# python3 -m pip install pydub --user
# libav
# brew install libav
#
# ffmpeg
# brew install ffmpeg
#
# python3 -m pip install scipy
# python3 -m pip install wavio
# https://realpython.com/playing-and-recording-sound-python/#python-sounddevice_1
# https://python-sounddevice.readthedocs.io/en/0.3.3/
# https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies
# https://tutswiki.com/read-write-config-files-in-python/
# Nfrom __future__ import print_functionNN
from configparser import ConfigParser
from pydub import AudioSegment
from pydub.silence import detect_leading_silence
from pydub.silence import detect_nonsilent
import rtmidi
from rtmidi.midiconstants import NOTE_OFF, NOTE_ON
from rtmidi.midiutil import open_midioutput
import logging
import os
import sys
import time
import sounddevice as sd
from scipy.io.wavfile import write
from pathlib import Path
import wavio as wv
import numpy
assert numpy
#######################
def MainInit():
# Logging
# https://realpython.com/python-logging/
log = logging.getLogger('midiout')
logging.basicConfig(level=logging.DEBUG)
print("*****************************************")
print("Midisampler")
print()
print("2023 by Christian Gehring (Build 2912.23)")
print("*****************************************")
config_file = Path("config.ini")
if config_file.is_file():
print(str(config_file) + " found")
# Read config.ini file
config_object = ConfigParser()
config_object.read("config.ini")
# Get MIDI Config
MIDI = config_object["MIDI"]
AUDIO = config_object["AUDIO"]
PATCH = config_object["PATCH"]
MISC = config_object["MISC"]
global velo_start
global note_start
global note_stop
global note_step
global velo_stop
global velo_step
global note_length
global midiport
global freq
global Audiochannels
global folder
global Vendor
global Device
global Patchname
global workdir
global AudioDevice
note_start = int(MIDI["note_start"])
note_stop = int(MIDI["note_stop"])
note_length = int(MIDI["note_length"])
note_step = int(MIDI["note_step"])
velo_start = int(MIDI["velo_start"])
velo_stop = int(MIDI["velo_stop"])
velo_step = int(MIDI["velo_step"])
midiport = int(MIDI["midiport"])
freq = int(AUDIO["Samplerate"])
Audiochannels = int(AUDIO["Channels"])
AudioDevice = AUDIO["Device"]
folder = MISC["Folder"]
Vendor = PATCH["Vendor"]
Device = PATCH["Device"]
Patchname = PATCH["Patchname"]
# Creating Directories for Samples
workdir = "Samples/" + Vendor + "/" + Device + "/" + Patchname
os.makedirs(workdir, exist_ok=True)
else:
print("config.ini not found. Please run app inside directory.")
sys.exit(1)
return
#######################
def get_midiport():
# Prompts user for MIDI input port, unless a valid port number or name
# is given as the first argument on the command line.
# API backend defaults to ALSA on Linux.
port = sys.argv[1] if len(sys.argv) > 1 else None
try:
midiout, port_name = open_midioutput(port)
except (EOFError, KeyboardInterrupt):
sys.exit()
note_on = [NOTE_ON, 60, 112] # channel 1, middle C, velocity 112
note_off = [NOTE_OFF, 60, 0]
with midiout:
print("Sending NoteOn event.")
midiout.send_message(note_on)
time.sleep(1)
print("Sending NoteOff event.")
midiout.send_message(note_off)
time.sleep(0.1)
del midiout
return
#######################
def get_soundcard():
print(sd.query_devices())
sd.default.samplerate = 44100
print(sd.DeviceList())
sd.default.device = AudioDevice
return
#######################
def trimsample():
# Load your audio file
sound = AudioSegment.from_wav("temp.wav")
# Detect non-silent parts
nonsilent_parts = detect_nonsilent(
sound,
min_silence_len=500, # Minimum length of silence to be considered
silence_thresh=sound.dBFS-14 # Silence threshold
)
# Get start and end of first nonsilent part
start = nonsilent_parts[0][0] if nonsilent_parts else 0
end = nonsilent_parts[-1][1] if nonsilent_parts else len(sound)
# Extract non-silent part
trimmed_sound = sound[start:end]
# Export the result
trimmed_sound.export("trimmed_output.wav", format="wav")
return
#######################
def get_samplefrommidi():
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
print("Checking MIDI Ports.....")
print(available_ports)
if available_ports:
midiout.open_port(midiport)
else:
midiout.open_virtual_port("My virtual output")
with midiout:
current_note = note_start
while current_note <= note_stop:
current_vel = velo_start
while current_vel <= velo_stop:
note_on = [NOTE_ON, current_note, current_vel]
note_off = [NOTE_OFF, current_note, 0]
wavfilename = Patchname + "_" + \
str(current_note) + "_" + str(current_vel) + ".wav"
########### Start MIDI Sampling ###############
duration = note_length + 0.2 # 0.2s buffer
recording = sd.rec(int(duration * freq),
samplerate=freq, channels=Audiochannels)
# Send Midi and Velocity to Midi Port
time.sleep(0.2) # Short pause
print("Sending NoteOn event " +
str(current_note) + " " + str(current_vel))
midiout.send_message(note_on)
time.sleep(note_length)
print("Sending NoteOff event.")
midiout.send_message(note_off)
time.sleep(1) # Short pause
# Wait for Audiodevice and save Audiofile
sd.wait()
write(workdir + "/" + wavfilename, freq, recording)
current_vel = current_vel + velo_step
#########################################
# Play Note with last velo (unsauber)
#########################################
if current_vel > velo_stop:
last_vel = velo_stop
note_on = [NOTE_ON, current_note, last_vel]
note_off = [NOTE_OFF, current_note, 0]
wavfilename = Patchname + "_" + \
str(current_note) + "_" + str(last_vel) + ".wav"
########### Start MIDI Sampling ###############
duration = note_length + 0.2 # 0.2s buffer
recording = sd.rec(int(duration * freq),
samplerate=freq, channels=Audiochannels)
# Send Midi and Velocity to Midi Port
time.sleep(0.2) # Short pause
print("Sending NoteOn event " +
str(current_note) + " " + str(last_vel))
midiout.send_message(note_on)
time.sleep(note_length)
print("Sending NoteOff event.")
midiout.send_message(note_off)
time.sleep(1) # Short pause
# Wait for Audiodevice and save Audiofile
sd.wait()
write(workdir + "/" + wavfilename, freq, recording)
###############################################
current_note = current_note + note_step
del midiout
######################
# MAIN
######################
MainInit()
# get_midiport()
get_soundcard()
get_samplefrommidi()
# trimsample()