Mozzi  alpha 0.01.1o
sound synthesis library for Arduino
 All Classes Functions
utils.cpp
00001 #include "utils.h"
00002 
00013 // code from AF_precision_synthesis sketch, copyright 2009, Adrian Freed.
00014 float mtof(float midival)
00015 {
00016                 return 8.1757989156 * pow(2.0, midival/12.0);
00017 }
00018 
00019 static float ucmtof(unsigned char midival)
00020 {
00021                 return 8.1757989156 * pow(2.0, (float)midival/12.0);
00022 }
00023 
00024 // static const float __attribute__(()) fmidiToFreq[128] =
00025  // {
00026          // ucmtof(0), ucmtof(1), ucmtof(2),ucmtof(3), ucmtof(4), ucmtof(5), ucmtof(6), ucmtof(7),
00027          // ucmtof(8),ucmtof(9), ucmtof(10), ucmtof(11), ucmtof(12), ucmtof(13), ucmtof(14), ucmtof(15),
00028          // ucmtof(16), ucmtof(17), ucmtof(18), ucmtof(19), ucmtof(20), ucmtof(21), ucmtof(22), ucmtof(23),
00029          // ucmtof(24), ucmtof(25), ucmtof(26), ucmtof(27), ucmtof(28), ucmtof(29), ucmtof(30), ucmtof(31),
00030          // ucmtof(32), ucmtof(33), ucmtof(34), ucmtof(35), ucmtof(36), ucmtof(37), ucmtof(38), ucmtof(39),
00031          // ucmtof(40), ucmtof(41), ucmtof(42), ucmtof(43), ucmtof(44), ucmtof(45), ucmtof(46), ucmtof(47),
00032          // ucmtof(48), ucmtof(49), ucmtof(50), ucmtof(51), ucmtof(52), ucmtof(53), ucmtof(54), ucmtof(55),
00033          // ucmtof(56), ucmtof(57), ucmtof(58), ucmtof(59), ucmtof(60), ucmtof(61), ucmtof(62), ucmtof(63),
00034          // ucmtof(64), ucmtof(65), ucmtof(66), ucmtof(67), ucmtof(68), ucmtof(69), ucmtof(70), ucmtof(71),
00035          // ucmtof(72), ucmtof(73), ucmtof(74), ucmtof(75), ucmtof(76), ucmtof(77), ucmtof(78), ucmtof(79),
00036          // ucmtof(80), ucmtof(81), ucmtof(82), ucmtof(83), ucmtof(84), ucmtof(85), ucmtof(86), ucmtof(87),
00037          // ucmtof(88), ucmtof(89), ucmtof(90), ucmtof(91), ucmtof(92), ucmtof(93), ucmtof(94), ucmtof(95),
00038          // ucmtof(96),ucmtof(97), ucmtof(98), ucmtof(99), ucmtof(100), ucmtof(101), ucmtof(102), ucmtof(103),
00039          // ucmtof(104),ucmtof(105), ucmtof(106), ucmtof(107), ucmtof(108), ucmtof(109), ucmtof(110), ucmtof(111),
00040          // ucmtof(112),ucmtof(113), ucmtof(114), ucmtof(115), ucmtof(116), ucmtof(117), ucmtof(118), ucmtof(119),
00041          // ucmtof(120), ucmtof(121), ucmtof(122), ucmtof(123), ucmtof(124), ucmtof(125), ucmtof(126), ucmtof(127)
00042  // };
00043 // 
00044  // /** @ingroup util
00045 // Converts midi note number to frequency.  Fast, but only accepts whole note values, no fractions.
00046 // @param midival a midi note number.  Unlike the mtof object in Pd, midi values can have fractions.  Use mtof() or 
00047 // Q16n16_mtof() if you want to convert fractional midi values.
00048 // @return the frequency represented by the input midi note number.
00049 // @todo hard-code fmidiToFreq table instead of generating at startup, and add an interpolating m2f which returns a float.
00050 // */
00051 // float m2f(unsigned char midival)
00052 // {
00053                 // return (float) pgm_read_float(fmidiToFreq+midival);
00054 // }
00055 
00056 // static Q16n16 Q16n16_m2f(float midival)
00057 // {
00058 //              return float_to_Q16n16(mtof(midival));
00059 // }
00060 
00061 //static const Q16n16 __attribute__((progmem)) midiToFreq[128] =
00062 // {
00063 //         Q16n16_m2f(0), Q16n16_m2f(1), Q16n16_m2f(2),Q16n16_m2f(3), Q16n16_m2f(4), Q16n16_m2f(5), Q16n16_m2f(6), Q16n16_m2f(7),
00064 //         Q16n16_m2f(8),Q16n16_m2f(9), Q16n16_m2f(10), Q16n16_m2f(11), Q16n16_m2f(12), Q16n16_m2f(13), Q16n16_m2f(14), Q16n16_m2f(15),
00065 //         Q16n16_m2f(16), Q16n16_m2f(17), Q16n16_m2f(18), Q16n16_m2f(19), Q16n16_m2f(20), Q16n16_m2f(21), Q16n16_m2f(22), Q16n16_m2f(23),
00066 //         Q16n16_m2f(24), Q16n16_m2f(25), Q16n16_m2f(26), Q16n16_m2f(27), Q16n16_m2f(28), Q16n16_m2f(29), Q16n16_m2f(30), Q16n16_m2f(31),
00067 //         Q16n16_m2f(32), Q16n16_m2f(33), Q16n16_m2f(34), Q16n16_m2f(35), Q16n16_m2f(36), Q16n16_m2f(37), Q16n16_m2f(38), Q16n16_m2f(39),
00068 //         Q16n16_m2f(40), Q16n16_m2f(41), Q16n16_m2f(42), Q16n16_m2f(43), Q16n16_m2f(44), Q16n16_m2f(45), Q16n16_m2f(46), Q16n16_m2f(47),
00069 //         Q16n16_m2f(48), Q16n16_m2f(49), Q16n16_m2f(50), Q16n16_m2f(51), Q16n16_m2f(52), Q16n16_m2f(53), Q16n16_m2f(54), Q16n16_m2f(55),
00070 //         Q16n16_m2f(56), Q16n16_m2f(57), Q16n16_m2f(58), Q16n16_m2f(59), Q16n16_m2f(60), Q16n16_m2f(61), Q16n16_m2f(62), Q16n16_m2f(63),
00071 //         Q16n16_m2f(64), Q16n16_m2f(65), Q16n16_m2f(66), Q16n16_m2f(67), Q16n16_m2f(68), Q16n16_m2f(69), Q16n16_m2f(70), Q16n16_m2f(71),
00072 //         Q16n16_m2f(72), Q16n16_m2f(73), Q16n16_m2f(74), Q16n16_m2f(75), Q16n16_m2f(76), Q16n16_m2f(77), Q16n16_m2f(78), Q16n16_m2f(79),
00073 //         Q16n16_m2f(80), Q16n16_m2f(81), Q16n16_m2f(82), Q16n16_m2f(83), Q16n16_m2f(84), Q16n16_m2f(85), Q16n16_m2f(86), Q16n16_m2f(87),
00074 //         Q16n16_m2f(88), Q16n16_m2f(89), Q16n16_m2f(90), Q16n16_m2f(91), Q16n16_m2f(92), Q16n16_m2f(93), Q16n16_m2f(94), Q16n16_m2f(95),
00075 //         Q16n16_m2f(96),Q16n16_m2f(97), Q16n16_m2f(98), Q16n16_m2f(99), Q16n16_m2f(100), Q16n16_m2f(101), Q16n16_m2f(102), Q16n16_m2f(103),
00076 //         Q16n16_m2f(104),Q16n16_m2f(105), Q16n16_m2f(106), Q16n16_m2f(107), Q16n16_m2f(108), Q16n16_m2f(109), Q16n16_m2f(110), Q16n16_m2f(111),
00077 //         Q16n16_m2f(112),Q16n16_m2f(113), Q16n16_m2f(114), Q16n16_m2f(115), Q16n16_m2f(116), Q16n16_m2f(117), Q16n16_m2f(118), Q16n16_m2f(119),
00078 //         Q16n16_m2f(120), Q16n16_m2f(121), Q16n16_m2f(122), Q16n16_m2f(123), Q16n16_m2f(124), Q16n16_m2f(125), Q16n16_m2f(126), Q16n16_m2f(127)
00079 // };
00080 
00081 
00082 static const uint32_t __attribute__((progmem)) midiToFreq[128] =
00083         {
00084                 535809, 567670, 601425, 637188, 675077, 715219, 757748, 802806, 850544, 901120,
00085                 954703, 1011473, 1071618, 1135340, 1202851, 1274376, 1350154, 1430438, 1515497,
00086                 1605613, 1701088, 1802240, 1909406, 2022946, 2143236, 2270680, 2405702, 2548752,
00087                 2700309, 2860877, 3030994, 3211226, 3402176, 3604479, 3818813, 4045892, 4286472,
00088                 4541359, 4811404, 5097504, 5400618, 5721756, 6061988, 6422452, 6804352, 7208959,
00089                 7637627, 8091785, 8572945, 9082719, 9622808, 10195009, 10801235, 11443507,
00090                 12123974, 12844905, 13608704, 14417917, 15275252, 16183563, 17145888, 18165438,
00091                 19245616, 20390018, 21602470, 22887014, 24247948, 25689810, 27217408, 28835834,
00092                 30550514, 32367136, 34291776, 36330876, 38491212, 40780036, 43204940, 45774028,
00093                 48495912, 51379620, 54434816, 57671668, 61101028, 64734272, 68583552, 72661752,
00094                 76982424, 81560072, 86409880, 91548056, 96991792, 102759240, 108869632,
00095                 115343336, 122202056, 129468544, 137167104, 145323504, 153964848, 163120144,
00096                 172819760, 183096224, 193983648, 205518336, 217739200, 230686576, 244403840,
00097                 258937008, 274334112, 290647008, 307929696, 326240288, 345639520, 366192448,
00098                 387967040, 411036672, 435478400, 461373152, 488807680, 517874016, 548668224,
00099                 581294016, 615859392, 652480576, 691279040, 732384896, 775934592, 822073344
00100         };
00101 
00114 Q16n16  Q16n16_mtof(Q16n16 midival_fractional)
00115 {
00116                 Q16n16 diff_fraction;
00117                 unsigned char index = midival_fractional >> 16;
00118                 unsigned int fraction = (unsigned int) midival_fractional; // keeps low word
00119                 Q16n16 freq1 = (Q16n16) pgm_read_dword(midiToFreq + index);
00120                 Q16n16 freq2 = (Q16n16) pgm_read_dword(midiToFreq + (index+1));
00121                 Q16n16 difference = freq2 - freq1;
00122                 if (difference>=65536)
00123                 {
00124                                 diff_fraction = ((difference>>8) * fraction) >> 8;
00125                 }
00126                 else
00127                 {
00128                                 diff_fraction = (difference * fraction) >> 16;
00129                 }
00130                 return (Q16n16) (freq1+ diff_fraction);
00131 }
00132 
00133 
00134 
00139 unsigned char randomMidi()
00140 {
00141                 return lowByte(xorshift96())>>1;
00142 }
00143 
00144 
00145 // moved these out of xorshift96() so xorshift96() can be reseeded manually
00146 static unsigned long x=132456789, y=362436069, z=521288629;
00147 // static unsigned long x= analogRead(A0)+123456789;
00148 // static unsigned long y= analogRead(A1)+362436069;
00149 // static unsigned long z= analogRead(A2)+521288629;
00150 
00155 void randSeed(unsigned long seed)
00156 {
00157                 x=seed;
00158 }
00159 
00160 
00168 void xorshiftSeed(unsigned long seed)
00169 {
00170                 x=seed;
00171 }
00172 
00173 
00180 // Based on Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/xorshift.pdf
00181 unsigned long xorshift96()
00182 {          //period 2^96-1
00183                 // static unsigned long x=123456789, y=362436069, z=521288629;
00184                 unsigned long t;
00185 
00186                 x ^= x << 16;
00187                 x ^= x >> 5;
00188                 x ^= x << 1;
00189 
00190                 t = x;
00191                 x = y;
00192                 y = z;
00193                 z = t ^ x ^ y;
00194 
00195                 return z;
00196 }
00197 
00198 
00205 char rand(char minval, char maxval)
00206 {
00207                 return (char) ((((int) (lowByte(xorshift96()))) * (maxval-minval))/256) + minval;
00208 }
00209 
00210 
00217 unsigned char rand(unsigned char minval, unsigned char maxval)
00218 {
00219                 return (unsigned char) ((((unsigned int) (lowByte(xorshift96()))) * (maxval-minval))/256) + minval;
00220 }
00221 
00222 
00229 int rand( int minval,  int maxval)
00230 {
00231                 return (int) ((((xorshift96()>>16) * (maxval-minval))>>16) + minval);
00232 }
00233 
00234 
00241 unsigned int rand(unsigned int minval, unsigned int maxval)
00242 {
00243                 return (unsigned int) ((((xorshift96()>>16) * (maxval-minval))>>16) + minval);
00244 }
00245 
00246 
00252 char rand(char maxval)
00253 {
00254                 return (char) ((((int) (lowByte(xorshift96()))) * maxval)/256);
00255 }
00256 
00257 
00263 unsigned char rand(unsigned char maxval)
00264 {
00265                 return (unsigned char) ((((unsigned int) (lowByte(xorshift96()))) * maxval)/256);
00266 }
00267 
00268 
00274 int rand(int maxval)
00275 {
00276                 return (int) (((xorshift96()>>16) * maxval)>>16);
00277 }
00278 
00279 
00285 unsigned int rand(unsigned int maxval)
00286 {
00287                 return (unsigned int) (((xorshift96()>>16) * maxval)>>16);
00288 }
00289 
00290 
00291 
00300 void setupFastAnalogRead()
00301 {
00302                 // fastest predivided rate (divide by 16, giving 1Mhz) for which behaviour is defined (~16us per sample)
00303                 sbi(ADCSRA,ADPS2);
00304                 cbi(ADCSRA,ADPS1);
00305                 cbi(ADCSRA,ADPS0);
00306 }
00307 
00308 
00309 
00325 void setAnalogInChannel(byte channel_num){
00326                 DIDR0 |= 1<<channel_num;
00327 }
00328 
00329 
00330 
00336 void unSetAnalogInChannel(byte channel_num){
00337                 DIDR0 &= ~(1<<channel_num);
00338 }
00339 
00340 
00341 static unsigned char analog_reference = DEFAULT;
00342 
00356 // basically analogRead() chopped in half so the ADC conversion
00357 // can be started in one function and received in another.
00358 void startAnalogRead(unsigned char pin)
00359 {
00360 
00361 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
00362                 if (pin >= 54)
00363                                 pin -= 54; // allow for channel or pin numbers
00364 #elif defined(__AVR_ATmega32U4__)
00365 
00366                 if (pin >= 18)
00367                                 pin -= 18; // allow for channel or pin numbers
00368 #elif defined(__AVR_ATmega1284__)
00369 
00370                 if (pin >= 24)
00371                                 pin -= 24; // allow for channel or pin numbers
00372 #else
00373 
00374                 if (pin >= 14)
00375                                 pin -= 14; // allow for channel or pin numbers
00376 #endif
00377 
00378 #if defined(__AVR_ATmega32U4__)
00379 
00380                 pin = analogPinToChannel(pin);
00381                 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
00382 #elif defined(ADCSRB) && defined(MUX5)
00383                 // the MUX5 bit of ADCSRB selects whether we're reading from channels
00384                 // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
00385                 ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
00386 #endif
00387 
00388                 // set the analog reference (high two bits of ADMUX) and select the
00389                 // channel (low 4 bits).  this also sets ADLAR (left-adjust result)
00390                 // to 0 (the default).
00391 #if defined(ADMUX)
00392 
00393                 ADMUX = (analog_reference << 6) | (pin & 0x07);
00394 #endif
00395 
00396                 // without a delay, we seem to read from the wrong channel
00397                 //delay(1);
00398 
00399 #if defined(ADCSRA) && defined(ADCL)
00400                 // start the conversion
00401                 sbi(ADCSRA, ADSC);
00402 #endif
00403 }
00404 
00414 int receiveAnalogRead()
00415 {
00416                 unsigned char low, high;
00417 #if defined(ADCSRA) && defined(ADCL)
00418                 // ADSC is cleared when the conversion finishes
00419                 while (bit_is_set(ADCSRA, ADSC))
00420                                 ;
00421 
00422                 // we have to read ADCL first; doing so locks both ADCL
00423                 // and ADCH until ADCH is read.  reading ADCL second would
00424                 // cause the results of each conversion to be discarded,
00425                 // as ADCL and ADCH would be locked when it completed.
00426                 low  = ADCL;
00427                 high = ADCH;
00428 #else
00429                 // we dont have an ADC, return 0
00430                 low  = 0;
00431                 high = 0;
00432 #endif
00433 
00434                 // combine the two bytes
00435                 return (high << 8) | low;
00436 }
00437