-
Notifications
You must be signed in to change notification settings - Fork 1
/
pianotest.cpp
292 lines (253 loc) · 7.64 KB
/
pianotest.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <signal.h>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <RtWvOut.h>
#include <RtAudio.h>
#include <RtMidi.h>
#include <FileWvOut.h>
using namespace stk;
#include "piano.h"
#define VERSION "v1.0"
#define AUTHOR "Stephen Sinclair (with respect to the authors of SynthBuilder)"
// Controls
// Clavier notes have a MIDI velocity of 64
float velocity = 64.0 / 127.0;
// Default slider values
float detuning = 0.0;
float stiffness = 0.0;
float gain = 2.807882 / 3.0;
float resonance = 0.73913;
float brightness = (-0.050239 + 0.25) / 0.25;
int voices=4;
int midinote=0;
int mididev=-1;
int samplerate=48000;
int buffersize=512;
bool displaymidi=false;
///// Test program
bool quit = false;
extern "C" void sighandler(int sig)
{
quit = true;
}
void parseParams(int argc, char* argv[])
{
char name[256];
char value[256];
int pos, n;
bool equal;
for (int i=1; i<argc; i++) {
pos = n = 0;
equal = false;
while (argv[i][pos] && n<256) {
if (equal)
value[n++] = argv[i][pos++];
else {
if (argv[i][pos]=='=') {
pos++;
equal = true;
name[n]=0;
n = 0;
}
else
name[n++] = argv[i][pos++];
}
}
value[n] = 0;
if (strcmp(name, "velocity")==0) {
velocity = atof(value);
}
else if (strcmp(name, "detuning")==0) {
detuning = atof(value);
}
else if (strcmp(name, "stiffness")==0) {
stiffness = atof(value);
}
else if (strcmp(name, "gain")==0) {
gain = atof(value);
}
else if (strcmp(name, "resonance")==0) {
resonance = atof(value);
}
else if (strcmp(name, "brightness")==0) {
brightness = atof(value);
}
else if (strcmp(name, "voices")==0) {
voices = atoi(value);
}
else if (strcmp(name, "midinote")==0) {
midinote = atoi(value);
}
else if (strcmp(name, "mididev")==0) {
mididev = atoi(value);
}
else if (strcmp(name, "samplerate")==0) {
samplerate = atoi(value);
}
else if (strcmp(name, "buffersize")==0) {
buffersize = atoi(value);
}
else if (strcmp(name, "displaymidi")==0) {
if (strcmp(value, "false")==0)
displaymidi = false;
else
displaymidi = true;
}
else {
std::cout << "STK Piano demonstration " VERSION " " __DATE__ << std::endl;
std::cout << AUTHOR << std::endl << std::endl;
std::cout << "Usage: pianotest [option=value] ... " << std::endl;
std::cout << std::endl;
std::cout << "Where option is one of:" << std::endl;
std::cout << std::endl;
std::cout << "velocity [0.0-1.0] Velocity of automatically generated note." << std::endl;
std::cout << "detuning [0.0-1.0] Amount of desired detuning between the two" << std::endl;
std::cout << " piano strings." << std::endl;
std::cout << "stiffness [0.0-1.0] Amount of desired stiffness for the strings." << std::endl;
std::cout << "gain [0.0-1.0] Amount of overall gain." << std::endl;
std::cout << "resonance [0.0-1.0] Amount of pedal presence." << std::endl;
std::cout << "brightness [0.0-1.0] Amount of brightness (higher frequencies)" << std::endl;
std::cout << " allowed in the sound." << std::endl;
std::cout << "voices [1-10] Number of voices used for polyphony (default=4)" << std::endl;
std::cout << "midinote [1-107] Automatically play this note number instead of" << std::endl;
std::cout << " connecting to a MIDI device." << std::endl;
std::cout << "mididev [0, ...] Connect to this MIDI device number." << std::endl;
std::cout << "samplerate [44100, 48000, ...] Desired sample reate (default=48000)" << std::endl;
std::cout << "buffersize [512, 1024, ...] Amount of buffer latency (default=512)" << std::endl;
std::cout << "displaymidi [true, false] Whether or not to print incoming MIDI data." << std::endl;
std::cout << std::endl;
quit = true;
return;
}
}
}
int main(int argc, char* argv[])
{
int i;
parseParams(argc, argv);
if (quit) return -1;
Stk::setSampleRate(samplerate);
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGQUIT, &sighandler);
signal(SIGINT, &sighandler);
RtWvOut pcm(2, samplerate, 0, buffersize);
StkFrames frame(1,2);
RtMidiIn *midi=NULL;
if (midinote == 0) {
try {
midi = new RtMidiIn;
}
catch (RtMidiError &e) {
std::cout << "Error opening MIDI." << std::endl;
midi = NULL;
}
}
// Polyphony!
Piano *piano=new Piano[voices];
if (!piano) {
std::cout << "Error: Could not allocate " << voices << "piano"
<< ((voices>1)?"s":"") << std::endl;
return -1;
}
// Set controls
for (i=0; i<voices; i++) {
piano[i].setBrightnessFactor(brightness);
piano[i].setDetuningFactor(detuning);
piano[i].setStiffnessFactor(stiffness);
piano[i].setOverallGain(gain);
piano[i].setPedalPresenceFactor(resonance);
}
if (midi) {
int port=mididev;
try {
if (port==-1) {
std::cout << "Midi ports: " << midi->getPortCount() << std::endl;
unsigned int i;
for (i=0; i<midi->getPortCount(); i++) {
std::cout << i << ": " << midi->getPortName(i) << std::endl;
}
std::cout << "Connect to: ";
std::cin >> port;
}
midi->openPort(port);
std::cout << "Connected to " << midi->getPortName(port) << std::endl;
}
catch (RtMidiError &e) {
std::cout << "Error opening MIDI port " << port << std::endl;
delete midi;
midi = NULL;
}
}
if (midinote > 0) {
piano[0].noteOn(midinote, velocity);
}
std::vector<unsigned char> v_midi_msg;
v_midi_msg.clear();
// FileWvOut out("out.wav", 1);
int next = 0;
int count = 0;
try {
while(!quit) {
if (midi) {
midi->getMessage(&v_midi_msg);
if (v_midi_msg.size()>0 && displaymidi) {
std::cout << "(" << (int)v_midi_msg.size() << ") ";
for (i=0; i<(int)v_midi_msg.size(); i++)
std::cout << (int)v_midi_msg[i] << " ";
std::cout << std::endl;
}
if (v_midi_msg.size() > 2) {
int msg = v_midi_msg[0];
int note = v_midi_msg[1];
StkFloat amp = v_midi_msg[2] / 128.0;
if ( msg==0x90 && amp>0 )
{
for (i=0; i<voices; i++) {
if (piano[i].getNoteNumber()==note)
break;
}
if (i>=voices) {
i=next;
next = (next+1)%voices;
}
piano[i].noteOn( note, amp );
}
else if ( (msg==0x90 && amp==0)
|| msg==0x80 )
{
for (i=0; i<voices; i++) {
if (piano[i].getNoteNumber()==note)
{
piano[i].noteOff(0);
break;
}
}
}
}
}
StkFloat f=0;
for (i=0; i<voices; i++)
f += piano[i].tick();
frame[0] = frame[1] = f;
pcm.tick(frame);
// out.tick(f);
// exit automatically when playing a single note
if (midinote > 0) {
if (!piano[0].isActive())
quit = true;
if (count++ > samplerate/2) {
piano[0].noteOff(0);
count = 0;
}
}
}
} catch (...) {
}
std::cout << "Done." << std::endl;
if (piano) delete[] piano;
if (midi) delete midi;
return 0;
}