aboutsummaryrefslogtreecommitdiffstats
path: root/maximilian.cpp
diff options
context:
space:
mode:
authorMick Grierson <mickgrierson@gmail.com>2012-04-26 13:09:55 +0100
committerMick Grierson <mickgrierson@gmail.com>2012-04-26 13:09:55 +0100
commit8c23fae3b55609bececab93003d7633f58ece083 (patch)
treea981f9acef71c9cd453b8eb495f3401e149ccc7a /maximilian.cpp
parent32f1a9a098249e68490faef837bd3a7370760db5 (diff)
some commenting
Diffstat (limited to 'maximilian.cpp')
-rw-r--r--maximilian.cpp34
1 files changed, 22 insertions, 12 deletions
diff --git a/maximilian.cpp b/maximilian.cpp
index fc00f25..62cf1a7 100644
--- a/maximilian.cpp
+++ b/maximilian.cpp
@@ -45,18 +45,19 @@
#include "stb_vorbis.h"
#endif
-//int channels=2;
-//int samplerate=44100;
-//int buffersize=1024;
+//This used to be important for dealing with multichannel playback
float chandiv= 1;
int maxiSettings::sampleRate = 44100;
int maxiSettings::channels = 2;
int maxiSettings::bufferSize = 1024;
+
+//this is a 514-point sinewave table that has many uses.
double sineBuffer[514]={0,0.012268,0.024536,0.036804,0.049042,0.06131,0.073547,0.085785,0.097992,0.1102,0.12241,0.13455,0.1467,0.15884,0.17093,0.18301,0.19507,0.20709,0.21909,0.23105,0.24295,0.25485,0.26669,0.2785,0.29025,0.30197,0.31366,0.32529,0.33685,0.34839,0.35986,0.37128,0.38266,0.39395,0.40521,0.41641,0.42752,0.4386,0.44958,0.46051,0.47137,0.48215,0.49286,0.50351,0.51407,0.52457,0.53497,0.54529,0.55554,0.5657,0.57578,0.58575,0.59567,0.60547,0.6152,0.62482,0.63437,0.6438,0.65314,0.66238,0.67151,0.68057,0.68951,0.69833,0.70706,0.7157,0.72421,0.7326,0.74091,0.74908,0.75717,0.76514,0.77298,0.7807,0.7883,0.79581,0.80316,0.81042,0.81754,0.82455,0.83142,0.8382,0.84482,0.85132,0.8577,0.86392,0.87006,0.87604,0.88187,0.8876,0.89319,0.89862,0.90396,0.90912,0.91415,0.91907,0.92383,0.92847,0.93295,0.93729,0.9415,0.94556,0.94949,0.95325,0.95691,0.96039,0.96375,0.96692,0.97,0.9729,0.97565,0.97827,0.98074,0.98306,0.98523,0.98724,0.98914,0.99084,0.99243,0.99387,0.99515,0.99628,0.99725,0.99808,0.99875,0.99927,0.99966,0.99988,0.99997,0.99988,0.99966,0.99927,0.99875,0.99808,0.99725,0.99628,0.99515,0.99387,0.99243,0.99084,0.98914,0.98724,0.98523,0.98306,0.98074,0.97827,0.97565,0.9729,0.97,0.96692,0.96375,0.96039,0.95691,0.95325,0.94949,0.94556,0.9415,0.93729,0.93295,0.92847,0.92383,0.91907,0.91415,0.90912,0.90396,0.89862,0.89319,0.8876,0.88187,0.87604,0.87006,0.86392,0.8577,0.85132,0.84482,0.8382,0.83142,0.82455,0.81754,0.81042,0.80316,0.79581,0.7883,0.7807,0.77298,0.76514,0.75717,0.74908,0.74091,0.7326,0.72421,0.7157,0.70706,0.69833,0.68951,0.68057,0.67151,0.66238,0.65314,0.6438,0.63437,0.62482,0.6152,0.60547,0.59567,0.58575,0.57578,0.5657,0.55554,0.54529,0.53497,0.52457,0.51407,0.50351,0.49286,0.48215,0.47137,0.46051,0.44958,0.4386,0.42752,0.41641,0.40521,0.39395,0.38266,0.37128,0.35986,0.34839,0.33685,0.32529,0.31366,0.30197,0.29025,0.2785,0.26669,0.25485,0.24295,0.23105,0.21909,0.20709,0.19507,0.18301,0.17093,0.15884,0.1467,0.13455,0.12241,0.1102,0.097992,0.085785,0.073547,0.06131,0.049042,0.036804,0.024536,0.012268,0,-0.012268,-0.024536,-0.036804,-0.049042,-0.06131,-0.073547,-0.085785,-0.097992,-0.1102,-0.12241,-0.13455,-0.1467,-0.15884,-0.17093,-0.18301,-0.19507,-0.20709,-0.21909,-0.23105,-0.24295,-0.25485,-0.26669,-0.2785,-0.29025,-0.30197,-0.31366,-0.32529,-0.33685,-0.34839,-0.35986,-0.37128,-0.38266,-0.39395,-0.40521,-0.41641,-0.42752,-0.4386,-0.44958,-0.46051,-0.47137,-0.48215,-0.49286,-0.50351,-0.51407,-0.52457,-0.53497,-0.54529,-0.55554,-0.5657,-0.57578,-0.58575,-0.59567,-0.60547,-0.6152,-0.62482,-0.63437,-0.6438,-0.65314,-0.66238,-0.67151,-0.68057,-0.68951,-0.69833,-0.70706,-0.7157,-0.72421,-0.7326,-0.74091,-0.74908,-0.75717,-0.76514,-0.77298,-0.7807,-0.7883,-0.79581,-0.80316,-0.81042,-0.81754,-0.82455,-0.83142,-0.8382,-0.84482,-0.85132,-0.8577,-0.86392,-0.87006,-0.87604,-0.88187,-0.8876,-0.89319,-0.89862,-0.90396,-0.90912,-0.91415,-0.91907,-0.92383,-0.92847,-0.93295,-0.93729,-0.9415,-0.94556,-0.94949,-0.95325,-0.95691,-0.96039,-0.96375,-0.96692,-0.97,-0.9729,-0.97565,-0.97827,-0.98074,-0.98306,-0.98523,-0.98724,-0.98914,-0.99084,-0.99243,-0.99387,-0.99515,-0.99628,-0.99725,-0.99808,-0.99875,-0.99927,-0.99966,-0.99988,-0.99997,-0.99988,-0.99966,-0.99927,-0.99875,-0.99808,-0.99725,-0.99628,-0.99515,-0.99387,-0.99243,-0.99084,-0.98914,-0.98724,-0.98523,-0.98306,-0.98074,-0.97827,-0.97565,-0.9729,-0.97,-0.96692,-0.96375,-0.96039,-0.95691,-0.95325,-0.94949,-0.94556,-0.9415,-0.93729,-0.93295,-0.92847,-0.92383,-0.91907,-0.91415,-0.90912,-0.90396,-0.89862,-0.89319,-0.8876,-0.88187,-0.87604,-0.87006,-0.86392,-0.8577,-0.85132,-0.84482,-0.8382,-0.83142,-0.82455,-0.81754,-0.81042,-0.80316,-0.79581,-0.7883,-0.7807,-0.77298,-0.76514,-0.75717,-0.74908,-0.74091,-0.7326,-0.72421,-0.7157,-0.70706,-0.69833,-0.68951,-0.68057,-0.67151,-0.66238,-0.65314,-0.6438,-0.63437,-0.62482,-0.6152,-0.60547,-0.59567,-0.58575,-0.57578,-0.5657,-0.55554,-0.54529,-0.53497,-0.52457,-0.51407,-0.50351,-0.49286,-0.48215,-0.47137,-0.46051,-0.44958,-0.4386,-0.42752,-0.41641,-0.40521,-0.39395,-0.38266,-0.37128,-0.35986,-0.34839,-0.33685,-0.32529,-0.31366,-0.30197,-0.29025,-0.2785,-0.26669,-0.25485,-0.24295,-0.23105,-0.21909,-0.20709,-0.19507,-0.18301,-0.17093,-0.15884,-0.1467,-0.13455,-0.12241,-0.1102,-0.097992,-0.085785,-0.073547,-0.06131,-0.049042,-0.036804,-0.024536,-0.012268,0,0.012268
};
+//This is a lookup table for converting midi to frequency
double mtofarray[129]={0, 8.661957, 9.177024, 9.722718, 10.3, 10.913383, 11.562325, 12.25, 12.978271, 13.75, 14.567617, 15.433853, 16.351599, 17.323914, 18.354048, 19.445436, 20.601723, 21.826765, 23.124651, 24.5, 25.956543, 27.5, 29.135235, 30.867706, 32.703197, 34.647827, 36.708096, 38.890873, 41.203445, 43.65353, 46.249302, 49., 51.913086, 55., 58.27047, 61.735413, 65.406395, 69.295654, 73.416191, 77.781746, 82.406891, 87.30706, 92.498604, 97.998856, 103.826172, 110., 116.540939, 123.470825, 130.81279, 138.591309, 146.832382, 155.563492, 164.813782, 174.61412, 184.997208, 195.997711, 207.652344, 220., 233.081879, 246.94165, 261.62558, 277.182617,293.664764, 311.126984, 329.627563, 349.228241, 369.994415, 391.995422, 415.304688, 440., 466.163757, 493.883301, 523.25116, 554.365234, 587.329529, 622.253967, 659.255127, 698.456482, 739.988831, 783.990845, 830.609375, 880., 932.327515, 987.766602, 1046.502319, 1108.730469, 1174.659058, 1244.507935, 1318.510254, 1396.912964, 1479.977661, 1567.981689, 1661.21875, 1760., 1864.655029, 1975.533203, 2093.004639, 2217.460938, 2349.318115, 2489.015869, 2637.020508, 2793.825928, 2959.955322, 3135.963379, 3322.4375, 3520., 3729.31, 3951.066406, 4186.009277, 4434.921875, 4698.63623, 4978.031738, 5274.041016, 5587.651855, 5919.910645, 6271.926758, 6644.875, 7040., 7458.620117, 7902.132812, 8372.018555, 8869.84375, 9397.272461, 9956.063477, 10548.082031, 11175.303711, 11839.821289, 12543.853516, 13289.75};
void setup();//use this to do any initialisation if you want.
@@ -64,12 +65,12 @@ void setup();//use this to do any initialisation if you want.
void play(double *channels);//run dac!
maxiOsc::maxiOsc(){
+ //When you create an oscillator, the constructor sets the phase of the oscillator to 0.
phase = 0.0;
-// memset(phases,0,500);
-// memset(freqs,0,500);
}
double maxiOsc::noise() {
+ //White Noise
//always the same unless you seed it.
float r = rand()/(float)RAND_MAX;
output=r*2-1;
@@ -77,11 +78,13 @@ double maxiOsc::noise() {
}
void maxiOsc::phaseReset(double phaseIn) {
+ //This allows you to set the phase of the oscillator to anything you like.
phase=phaseIn;
}
double maxiOsc::sinewave(double frequency) {
+ //This is a sinewave oscillator
output=sin (phase*(TWOPI));
if ( phase >= 1.0 ) phase -= 1.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
@@ -90,6 +93,7 @@ double maxiOsc::sinewave(double frequency) {
}
double maxiOsc::sinebuf4(double frequency) {
+ //This is a sinewave oscillator that uses 4 point interpolation on a 514 point buffer
double remainder;
double a,b,c,d,a1,a2,a3;
phase += 512./(maxiSettings::sampleRate/(frequency));
@@ -117,7 +121,8 @@ double maxiOsc::sinebuf4(double frequency) {
return(output);
}
-double maxiOsc::sinebuf(double frequency) {
+double maxiOsc::sinebuf(double frequency) { //specify the frequency of the oscillator in Hz / cps etc.
+ //This is a sinewave oscillator that uses linear interpolation on a 514 point buffer
double remainder;
phase += 512./(maxiSettings::sampleRate/(frequency*chandiv));
if ( phase >= 511 ) phase -=512;
@@ -127,6 +132,7 @@ double maxiOsc::sinebuf(double frequency) {
}
double maxiOsc::coswave(double frequency) {
+ //This is a cosine oscillator
output=cos (phase*(TWOPI));
if ( phase >= 1.0 ) phase -= 1.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
@@ -135,6 +141,7 @@ double maxiOsc::coswave(double frequency) {
}
double maxiOsc::phasor(double frequency) {
+ //This produces a floating point linear ramp between 0 and 1 at the desired frequency
output=phase;
if ( phase >= 1.0 ) phase -= 1.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
@@ -142,6 +149,7 @@ double maxiOsc::phasor(double frequency) {
}
double maxiOsc::square(double frequency) {
+ //This is a square wave
if (phase<0.5) output=-1;
if (phase>0.5) output=1;
if ( phase >= 1.0 ) phase -= 1.0;
@@ -150,6 +158,7 @@ double maxiOsc::square(double frequency) {
}
double maxiOsc::pulse(double frequency, double duty) {
+ //This is a pulse generator that creates a signal between -1 and 1.
if (duty<0.) duty=0;
if (duty>1.) duty=1;
if ( phase >= 1.0 ) phase -= 1.0;
@@ -160,6 +169,7 @@ double maxiOsc::pulse(double frequency, double duty) {
}
double maxiOsc::phasor(double frequency, double startphase, double endphase) {
+ //This is a phasor that takes a value for the start and end of the ramp.
output=phase;
if (phase<startphase) {
phase=startphase;
@@ -171,7 +181,7 @@ double maxiOsc::phasor(double frequency, double startphase, double endphase) {
double maxiOsc::saw(double frequency) {
-
+ //Sawtooth generator. This is like a phasor but goes between -1 and 1
output=phase;
if ( phase >= 1.0 ) phase -= 2.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
@@ -180,6 +190,7 @@ double maxiOsc::saw(double frequency) {
}
double maxiOsc::triangle(double frequency) {
+ //This is a triangle wave.
if ( phase >= 1.0 ) phase -= 1.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
if (phase <= 0.5 ) {
@@ -191,10 +202,10 @@ double maxiOsc::triangle(double frequency) {
}
-//I like this.
double maxiEnvelope::line(int numberofsegments,double segments[1000]) {
- if (isPlaying==1) {//only make a sound once you've been triggered
-
+ //This is a basic multi-segment ramp generator that you can use for more or less anything.
+ //However, it's not that intuitive.
+ if (isPlaying==1) {//only make a sound once you've been triggered
period=2./(segments[valindex+1]*0.004);
nextval=segments[valindex+2];
currentval=segments[valindex];
@@ -226,8 +237,7 @@ void maxiEnvelope::trigger(int index, double amp) {
}
-//and this
-
+//Delay with feedback
maxiDelayline::maxiDelayline() {
memset( memory, 0, 88200*sizeof (double) );
}