-
Notifications
You must be signed in to change notification settings - Fork 0
/
midisend.py
159 lines (127 loc) · 4.77 KB
/
midisend.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
import mido
import time
import numpy as np
import matplotlib.pyplot as plt
import random
import pyaudio
import wave
import os
import subprocess
import asyncio
import sys
from scipy.io.wavfile import read
import process
outs = mido.get_output_names()
print(outs)
port = mido.open_output(outs[5])
# CHANNEL 0
Osc1 = [0,1,2] # Range, Waveform, Vol
Osc2 = [3,4,5,6] # Range, Tuning, Waveform, Vol
Osc3 = [7,8,9,10] # Range, Tuning, Waveform, Vol
# Noise = [11,12] # Volume, Type(bool)
Filter = [13,14,15,16] # Type, Cutoff, Res, Contour
Filter_Env = [17,18,19,20] # A, D, S/R(bool), val
Env = [21,22,23,24] # A, D, S/R(bool), val
Pitch = [25] # Global tuning
Mod = [26,27] # Wheel, Mix
# param_list = [0,1,2,3,4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,22,23,24,25] # all oscillators
param_list = [0,1,14,15,16,17,18,19,20,21,22,23,24,25] # single oscillator
# [0,1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13]
print(len(param_list))
param_six = [0,1,3,5,7,9] # ind for six-pos params
param_two = [19,23] # ind for binary params
vals_six = [0,26,51,77,102,127] # vals for six-pos params
vals_two = [0,127] # vals for binary params
class Create_Dataset():
def __init__(self, t, N):
self.t = t # recording time
self.N = N # number of samples
self.fs = 44100
self.p1 = 0.7 # probability that the tuning params are close to 0
self.p2 = 0.7 # probability that the noise volume is close to 0
self.create_dataset()
# self.rand_params()
self.summ = 0
self.numm = 0
def rec_synth(self,filename):
p = pyaudio.PyAudio()
val = 60
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = self.fs
RECORD_SECONDS = self.t
WAVE_OUTPUT_FILENAME = '../data/1osc/bigset/audiodata/' + filename + '.wav'
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=2,
frames_per_buffer=CHUNK)
frames = []
on = mido.Message('note_on', channel=0, note=val)
off = mido.Message('note_off', channel=0, note=val)
# port.send(on)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
# print(i)
if i==20:
port.send(on)
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
port.send(off)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def rand_params(self):
params = np.zeros(26)
for i in param_list:
r = random.random()
" balance the tuning and noise parameters "
if i==4 or i==8: # either tuning control
p = np.random.normal(0,0.05) # sample from normal distribution
val = int(127*np.abs((p+0.5)))
# elif i==11: # noise volume
# p = np.random.normal(0,0.05) # sample from normal distribution
# val = int(127*np.abs(p))
elif i in param_six:
val_ind = int(np.round(5*r))
val = vals_six[val_ind]
elif i in param_two:
val_ind = int(np.round(r))
val = vals_two[val_ind]
else:
val = int(127*r)
params[i] = val
msg = mido.Message('control_change', channel=0, control=i, value=val)
port.send(msg)
# time.sleep(1)
return params
def create_dataset(self):
for i in range(self.N):
print('Recording Audio {}'.format(i))
params = self.rand_params()
self.rec_synth('audio{}'.format(i))
np.save('../data/1osc/bigset/paramsdata/'+'params{}'.format(i),params)
time.sleep(0.1)
def learn_cc(ch, ctrl):
msg = mido.Message('control_change', channel=ch, control=ctrl, value=0)
port.send(msg)
msg = mido.Message('control_change', channel=ch, control=ctrl, value=10)
port.send(msg)
def send_cc(ch, ctrl, val):
msg = mido.Message('control_change', channel=ch, control=ctrl, value=val)
port.send(msg)
# rand_params()
if __name__ == "__main__":
t = 4 # length of each recording (s)
N = 10000 # size of data set
CD = Create_Dataset(t,N)
# process.convert_data('../data/audiodata','../data/TRAIN')
# learn_cc(0,25)
# send_cc(0,19,63)