forked from micknoise/Maximilian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximilian.h
executable file
·365 lines (294 loc) · 8.72 KB
/
maximilian.h
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* maximilian.h
* platform independent synthesis library using portaudio or rtaudio
*
* Created by Mick Grierson on 29/12/2009.
* Copyright 2009 Mick Grierson & Strangeloop Limited. All rights reserved.
* Thanks to the Goldsmiths Creative Computing Team.
* Special thanks to Arturo Castro for the PortAudio implementation.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef MAXIMILIAN_H
#define MAXIMILIAN_H
//#define MAXIMILIAN_PORTAUDIO
#define MAXIMILIAN_RT_AUDIO
//#include "ofMain.h"
//#include "ofUtils.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
#include "math.h"
using namespace std;
#define TWOPI (3.14159*2)
class maxiSettings {
public:
static int sampleRate;
static int channels;
static int bufferSize;
static void setup(int initSampleRate, int initChannels, int initBufferSize) {
maxiSettings::sampleRate = initSampleRate;
maxiSettings::channels = initChannels;
maxiSettings::bufferSize = initBufferSize;
}
};
class maxiOsc {
double frequency;
double phase;
double startphase;
double endphase;
double output;
double tri;
public:
maxiOsc();
double sinewave(double frequency);
double coswave(double frequency);
double phasor(double frequency);
double phasor(double frequency, double startphase, double endphase);
double saw(double frequency);
double triangle(double frequency);
double square(double frequency);
double pulse(double frequency, double duty);
double noise();
double sinebuf(double frequency);
double sinebuf4(double frequency);
void phaseReset(double phaseIn);
};
class maxiEnvelope {
double period;
double output;
double startval;
double currentval;
double nextval;
int isPlaying;
public:
double line(int numberofsegments,double segments[100]);
void trigger(int index,double amp);
int valindex;
double amplitude;
};
class maxiDelayline {
double frequency;
int phase;
double startphase;
double endphase;
double output;
double memory[88200];
public:
maxiDelayline();
double dl(double input, int size, double feedback);
double dl(double input, int size, double feedback, int position);
};
class maxiFilter {
double gain;
double input;
double output;
double inputs[10];
double outputs[10];
double cutoff1;
double x;//speed
double y;//pos
double z;//pole
double c;//filter coefficient
public:
maxiFilter():x(0.0), y(0.0), z(0.0), c(0.0){};
double cutoff;
double resonance;
double lores(double input,double cutoff1, double resonance);
double hires(double input,double cutoff1, double resonance);
double bandpass(double input,double cutoff1, double resonance);
double lopass(double input,double cutoff);
double hipass(double input,double cutoff);
};
class maxiMix {
double input;
double two[2];
double four[4];
double eight[8];
public:
double x;
double y;
double z;
double *stereo(double input,double two[2],double x);
double *quad(double input,double four[4], double x,double y);
double *ambisonic(double input,double eight[8],double x,double y, double z);
};
class maxiSample {
private:
string myPath;
int myChunkSize;
int mySubChunk1Size;
int readChannel;
short myFormat;
int myByteRate;
short myBlockAlign;
short myBitsPerSample;
double position;
double speed;
double output;
public:
int myDataSize;
short myChannels;
int mySampleRate;
long length;
void getLength();
char* myData;
short* temp;
// get/set for the Path property
~maxiSample()
{
if (myData) delete[] myData;
if (temp) delete[] temp;
}
maxiSample():myData(NULL){};
bool load(string fileName, int channel=0);
bool loadOgg(char *filename,int channel=0);
void trigger();
// read a wav file into this class
bool read();
//read an ogg file into this class using stb_vorbis
bool readOgg();
double play();
double playOnce();
double playOnce(double speed);
double play(double speed);
double play(double frequency, double start, double end, double &pos);
double play(double frequency, double start, double end);
double play4(double frequency, double start, double end);
double bufferPlay(unsigned char &bufferin,long length);
double bufferPlay(unsigned char &bufferin,double speed,long length);
double bufferPlay(unsigned char &bufferin,double frequency, double start, double end);
double bufferPlay4(unsigned char &bufferin,double frequency, double start, double end);
//int open(char *filename) {
// // open the wav file
// char *path = new char[50];
// strcpy(path, filename);
// sample *myWav = new sample(path);
// return 0;
// }
// // print a summary of the wav file
// int info() {
// char *summary = myWav->getSummary();
// printf("Summary:\n%s", summary);
// return 0;
// }
// int savefile() {
// // write the summary back out
// strcpy(path, "testout.wav");
// myWav->setPath(path);
// myWav->save();
// return 0:
// }
// int close() {
// // collect the garbage
// delete summary;
// delete path;
// delete myWav;
//
// return 0;
// }
// write out the wav file
bool save()
{
fstream myFile (myPath.c_str(), ios::out | ios::binary);
// write the wav file per the wav file format
myFile.seekp (0, ios::beg);
myFile.write ("RIFF", 4);
myFile.write ((char*) &myChunkSize, 4);
myFile.write ("WAVE", 4);
myFile.write ("fmt ", 4);
myFile.write ((char*) &mySubChunk1Size, 4);
myFile.write ((char*) &myFormat, 2);
myFile.write ((char*) &myChannels, 2);
myFile.write ((char*) &mySampleRate, 4);
myFile.write ((char*) &myByteRate, 4);
myFile.write ((char*) &myBlockAlign, 2);
myFile.write ((char*) &myBitsPerSample, 2);
myFile.write ("data", 4);
myFile.write ((char*) &myDataSize, 4);
myFile.write (myData, myDataSize);
return true;
}
// return a printable summary of the wav file
char *getSummary()
{
char *summary = new char[250];
sprintf(summary, " Format: %d\n Channels: %d\n SampleRate: %d\n ByteRate: %d\n BlockAlign: %d\n BitsPerSample: %d\n DataSize: %d\n", myFormat, myChannels, mySampleRate, myByteRate, myBlockAlign, myBitsPerSample, myDataSize);
std::cout << myDataSize;
return summary;
}
};
class maxiMap {
public:
static double inline linlin(double val, double inMin, double inMax, double outMin, double outMax) {
return ((val - inMin) / (inMax - inMin) * (outMax - outMin)) + outMin;
}
static double inline linexp(double val, double inMin, double inMax, double outMin, double outMax) {
//clipping
val = max(min(val, inMax), inMin);
return pow((outMax / outMin), (val - inMin) / (inMax - inMin)) * outMin;
}
static double inline explin(double val, double inMin, double inMax, double outMin, double outMax) {
//clipping
val = max(min(val, inMax), inMin);
return (log(val/inMin) / log(inMax/inMin) * (outMax - outMin)) + outMin;
}
};
class maxiDyn {
public:
double gate(double input, double threshold=0.9, long holdtime=1, double attack=1, double release=0.9995);
double compressor(double input, double ratio, double threshold=0.9, double attack=1, double release=0.9995);
double input;
double ratio;
double currentRatio;
double threshold;
double output;
double attack;
double release;
double amplitude;
long holdtime;
long holdcount;
int attackphase,holdphase,releasephase;
};
class maxiEnv {
public:
double ar(double input, double attack=1, double release=0.9, long holdtime=1, int trigger=0);
double adsr(double input, double attack=1, double decay=0.99, double sustain=0.125, double release=0.9, long holdtime=1, int trigger=0);
double input;
double output;
double attack;
double decay;
double sustain;
double release;
double amplitude;
int trigger;
long holdtime;
long holdcount;
int attackphase,decayphase,sustainphase,holdphase,releasephase;
};
class convert {
public:
double mtof(int midinote);
};
#endif