Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Auduino, the Lo-Fi granular synthesiser
- //
- // by Peter Knight, Tinker.it http://tinker.it
- //
- // Help: http://code.google.com/p/tinkerit/wiki/Auduino
- // More help: http://groups.google.com/group/auduino
- //
- // Analog in 0: Grain 1 pitch
- // Analog in 1: Grain 2 decay
- // Analog in 2: Grain 1 decay
- // Analog in 3: Grain 2 pitch
- // Analog in 4: Grain repetition frequency
- //
- // Digital 3: Audio out (Digital 11 on ATmega8)
- //
- // Changelog:
- // 19 Nov 2008: Added support for ATmega8 boards
- // 21 Mar 2009: Added support for ATmega328 boards
- // 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
- // 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- int scalePin =5;
- int armButton =7;
- int armed =0;
- // declaring note values for mapping
- // commented out due to memory limitations
- // left behind for reference
- /*
- int c =17;
- int cs =18;
- int d =19;
- int ds =20;
- int e =22;
- int f =23;
- int fs =24;
- int g =26;
- int gs =27;
- int a =29;
- int as =31;
- int b =32;
- int c0 =34;
- int cs0 =36;
- int d0 =38;
- int ds0 =41;
- int e0 =43;
- int f0 =46;
- int fs0 =48;
- int g0 =51;
- int gs0 =54;
- int a0 =58;
- int as0 =61;
- int b0 =65;
- int c1 =69;
- int cs1 =73;
- int d1 =77;
- int ds1 =82;
- int e1 =86;
- int f1 =92;
- int fs1 =97;
- int g1 =103;
- int gs1 =109;
- int a1 =115;
- int as1 =122;
- int b1 =129;
- int c2 =137;
- int cs2 =145;
- int d2 =154;
- int ds2 =163;
- int e2 =173;
- int f2 =183;
- int fs2 =194;
- int g2 =206;
- int gs2 =218;
- int a2 =231;
- int as2 =244;
- int b2 =259;
- int c3 =274;
- int cs3 =291;
- int d3 =308;
- int ds3 =326;
- int e3 =346;
- int f3 =366;
- int fs3 =388;
- int g3 =411;
- int gs3 =435;
- int a3 =461;
- int as3 =489;
- int b3 =518;
- int c4 =549;
- int cs4 =581;
- int d4 =616;
- int ds4 =652;
- int e4 =691;
- int f4 =732;
- int fs4 =776;
- int g4 =822;
- int gs4 =871;
- int a4 =923;
- int as4 =978;
- int b4 =1036;
- int c5 =1097;
- int cs5 =1163;
- int d5 =1232;
- int ds5 =1305;
- int e5 =1383;
- int f5 =1465;
- int fs5 =1552;
- int g5 =1644;
- int gs5 =1742;
- int a5 =1845;
- int as5 =1955;
- int b5 =2071;
- int c6 =2195;
- int cs6 =2325;
- int d6 =2463;
- int ds6 =2610;
- int e6 =2765;
- int f6 =2930;
- int fs6 =3104;
- int g6 =3288;
- int gs6 =3484;
- int a6 =3691;
- int as6 =3910;
- int b6 =4143;
- int c7 =4389;
- int cs7 =4650;
- int d7 =4927;
- int ds7 =5220;
- int e7 =5530;
- int f7 =5859;
- int fs7 =6207;
- int g7 =6577;
- int gs7 =6968;
- int a7 =7382;
- int as7 =7821;
- int b7 =8286;
- int c8 =8779;
- int cs8 =9301;
- int d8 =9854;
- int ds8 =10440;
- int e8 =11060;
- int f8 =11718;
- int fs8 =12415;
- int g8 =13153;
- int gs8 =13935;
- int a8 =14764;
- int as8 =15642;
- int b8 =16572;
- int c9 =17557;
- int cs9 =18601;
- int d9 =19708;
- int ds9 =20879;
- int e9 =22121;
- int f9 =23436;
- int fs9 =24830;
- int g9 =26306;
- */
- uint16_t syncPhaseAcc;
- uint16_t syncPhaseInc;
- uint16_t grainPhaseAcc;
- uint16_t grainPhaseInc;
- uint16_t grainAmp;
- uint8_t grainDecay;
- uint16_t grain2PhaseAcc;
- uint16_t grain2PhaseInc;
- uint16_t grain2Amp;
- uint8_t grain2Decay;
- // Map Analogue channels
- #define SYNC_CONTROL (4)
- #define GRAIN_FREQ_CONTROL (0)
- #define GRAIN_DECAY_CONTROL (2)
- #define GRAIN2_FREQ_CONTROL (3)
- #define GRAIN2_DECAY_CONTROL (1)
- // Changing these will also requires rewriting audioOn()
- #if defined(__AVR_ATmega8__)
- //
- // On old ATmega8 boards.
- // Output is on pin 11
- //
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 5
- #define PWM_PIN 11
- #define PWM_VALUE OCR2
- #define PWM_INTERRUPT TIMER2_OVF_vect
- #elif defined(__AVR_ATmega1280__)
- //
- // On the Arduino Mega
- // Output is on pin 3
- //
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 7
- #define PWM_PIN 3
- #define PWM_VALUE OCR3C
- #define PWM_INTERRUPT TIMER3_OVF_vect
- #else
- //
- // For modern ATmega168 and ATmega328 boards
- // Output is on pin 3
- //
- #define PWM_PIN 3
- #define PWM_VALUE OCR2B
- #define LED_PIN 13
- #define LED_PORT PORTB
- #define LED_BIT 5
- #define PWM_INTERRUPT TIMER2_OVF_vect
- #endif
- // Smooth logarithmic mapping
- //
- uint16_t antilogTable[] = {
- 64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
- 54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
- 45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
- 38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
- };
- uint16_t mapPhaseInc(uint16_t input) {
- return (antilogTable[input & 0x3f]) >> (input >> 6);
- }
- // Major keys
- // Thanks to Goatboy who's code I started this beast with
- // Key of C ........C D E F G A B
- uint16_t majorC [29] = {
- 274, 308, 346, 366, 411, 461, 518,
- 549, 616, 691, 732, 822, 923, 1036,
- 1097, 1232, 1383, 1465, 1644, 1845, 2071,
- 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
- };
- uint16_t mapMajorC(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorC[value]);
- }
- // Key of G ........G A B C D E F#
- uint16_t majorG [29] = {
- 411, 461, 518, 274, 308, 346, 388,
- 822, 923, 1036, 1097, 1232, 1383, 1552,
- 1644, 1845, 2071, 2195, 2463, 2765, 3104,
- 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
- };
- uint16_t mapMajorG(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorG[value]);
- }
- // Key of D ........D E F# G A B C#
- uint16_t majorD [29] = {
- 308, 346, 388, 411, 461, 518, 581,
- 616, 691, 776, 822, 923, 1036, 1163,
- 1232, 1383, 1552, 1644, 1845, 2071, 2325,
- 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
- };
- uint16_t mapMajorD(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorD[value]);
- }
- // Key of A ........A B C# D E F# G#
- uint16_t majorA [29] = {
- 461, 518, 581, 616, 691, 776, 871,
- 923, 1036, 1163, 1232, 1383, 1552, 1742,
- 1845, 2071, 2325, 2463, 2765, 3104, 3484,
- 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
- };
- uint16_t mapMajorA(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorA[value]);
- }
- // Key of E ........E F# G# A B C# D#
- uint16_t majorE [29] = {
- 346, 388, 435, 461, 518, 581, 652,
- 691, 776, 871, 923, 1036, 1163, 1305,
- 1383, 1552, 1742, 1845, 2071, 2325, 2610,
- 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
- };
- uint16_t mapMajorE(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorE[value]);
- }
- // Key of B ........B C# D# E F# G# A#
- uint16_t majorB [29] = {
- 518, 581, 652, 691, 776, 871, 978,
- 1036, 1163, 1305, 1383, 1552, 1742, 1955,
- 2071, 2325, 2610, 2765, 3104, 3484, 3910,
- 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
- };
- uint16_t mapMajorB(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorB[value]);
- }
- // Key of F# ........F# G# A# B C# D# E#
- uint16_t majorFs [29] = {
- 366, 388, 435, 489, 518, 581, 652,
- 732, 776, 871, 978, 1036, 1163, 1305,
- 1465, 1552, 1742, 1955, 2071, 2325, 2610,
- 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
- };
- uint16_t mapMajorFs(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorFs[value]);
- }
- // Key of Db ........C# D# F F# G# A# C
- uint16_t majorDb [29] = {
- 291, 326, 366, 388, 435, 489, 274,
- 581, 652, 732, 776, 871, 978, 549,
- 1163, 1305, 1465, 1552, 1742, 1955, 1097,
- 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
- };
- uint16_t mapMajorDb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorDb[value]);
- }
- // Key of Ab ........G# A# C C# D# F G
- uint16_t majorAb [29] = {
- 435, 489, 274, 581, 652, 732, 822,
- 871, 978, 549, 1163, 1305, 1465, 1644,
- 1742, 1955, 1097, 2325, 2610, 2930, 3288,
- 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
- };
- uint16_t mapMajorAb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorAb[value]);
- }
- // Key of Eb ........D# F G G# A# C D
- uint16_t majorEb [29] = {
- 326, 366, 411, 435, 489, 274, 616,
- 652, 732, 822, 871, 978, 549, 1232,
- 1305, 1465, 1644, 1742, 1955, 1097, 2463,
- 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
- };
- uint16_t mapMajorEb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorEb[value]);
- }
- // Key of Bb ........A# C D D# F G A
- uint16_t majorBb [29] = {
- 489, 274, 616, 652, 732, 822, 923,
- 978, 549, 1232, 1305, 1465, 1644, 1845,
- 1955, 1097, 2463, 2610, 2930, 3288, 3691,
- 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
- };
- uint16_t mapMajorBb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorBb[value]);
- }
- // Key of F ........F G A A# C D E
- uint16_t majorF [29] = {
- 366, 411, 461, 489, 274, 616, 691,
- 732, 822, 923, 978, 549, 1232, 1383,
- 1465, 1644, 1845, 1955, 1097, 2463, 2765,
- 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
- };
- uint16_t mapMajorF(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (majorF[value]);
- }
- // Half Whole Diminished keys
- // Key of C ........C D E F G A B
- uint16_t dimC [29] = {
- 274, 308, 346, 366, 411, 461, 518,
- 549, 616, 691, 732, 822, 923, 1036,
- 1097, 1232, 1383, 1465, 1644, 1845, 2071,
- 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
- };
- uint16_t mapDimC(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimC[value]);
- }
- // Key of G ........G A B C D E F#
- uint16_t dimG [29] = {
- 411, 461, 518, 274, 308, 346, 388,
- 822, 923, 1036, 1097, 1232, 1383, 1552,
- 1644, 1845, 2071, 2195, 2463, 2765, 3104,
- 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
- };
- uint16_t mapDimG(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimG[value]);
- }
- // Key of D ........D E F# G A B C#
- uint16_t dimD [29] = {
- 308, 346, 388, 411, 461, 518, 581,
- 616, 691, 776, 822, 923, 1036, 1163,
- 1232, 1383, 1552, 1644, 1845, 2071, 2325,
- 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
- };
- uint16_t mapDimD(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimD[value]);
- }
- // Key of A ........A B C# D E F# G#
- uint16_t dimA [29] = {
- 461, 518, 581, 616, 691, 776, 871,
- 923, 1036, 1163, 1232, 1383, 1552, 1742,
- 1845, 2071, 2325, 2463, 2765, 3104, 3484,
- 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
- };
- uint16_t mapDimA(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimA[value]);
- }
- // Key of E ........E F# G# A B C# D#
- uint16_t dimE [29] = {
- 346, 388, 435, 461, 518, 581, 652,
- 691, 776, 871, 923, 1036, 1163, 1305,
- 1383, 1552, 1742, 1845, 2071, 2325, 2610,
- 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
- };
- uint16_t mapDimE(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimE[value]);
- }
- // Key of B ........B C# D# E F# G# A#
- uint16_t dimB [29] = {
- 518, 581, 652, 691, 776, 871, 978,
- 1036, 1163, 1305, 1383, 1552, 1742, 1955,
- 2071, 2325, 2610, 2765, 3104, 3484, 3910,
- 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
- };
- uint16_t mapDimB(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimB[value]);
- }
- // Key of F# ........F# G# A# B C# D# E#
- uint16_t dimFs [29] = {
- 366, 388, 435, 489, 518, 581, 652,
- 732, 776, 871, 978, 1036, 1163, 1305,
- 1465, 1552, 1742, 1955, 2071, 2325, 2610,
- 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
- };
- uint16_t mapDimFs(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimFs[value]);
- }
- // Key of Db ........C# D# F F# G# A# C
- uint16_t dimDb [29] = {
- 291, 326, 366, 388, 435, 489, 274,
- 581, 652, 732, 776, 871, 978, 549,
- 1163, 1305, 1465, 1552, 1742, 1955, 1097,
- 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
- };
- uint16_t mapDimDb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimDb[value]);
- }
- // Key of Ab ........G# A# C C# D# F G
- uint16_t dimAb [29] = {
- 435, 489, 274, 581, 652, 732, 822,
- 871, 978, 549, 1163, 1305, 1465, 1644,
- 1742, 1955, 1097, 2325, 2610, 2930, 3288,
- 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
- };
- uint16_t mapDimAb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimAb[value]);
- }
- // Key of Eb ........D# F G G# A# C D
- uint16_t dimEb [29] = {
- 326, 366, 411, 435, 489, 274, 616,
- 652, 732, 822, 871, 978, 549, 1232,
- 1305, 1465, 1644, 1742, 1955, 1097, 2463,
- 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
- };
- uint16_t mapDimEb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimEb[value]);
- }
- // Key of Bb ........A# C D D# F G A
- uint16_t dimBb [29] = {
- 489, 274, 616, 652, 732, 822, 923,
- 978, 549, 1232, 1305, 1465, 1644, 1845,
- 1955, 1097, 2463, 2610, 2930, 3288, 3691,
- 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
- };
- uint16_t mapDimBb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimBb[value]);
- }
- // Key of F ........F G A A# C D E
- uint16_t dimF [29] = {
- 366, 411, 461, 489, 274, 616, 691,
- 732, 822, 923, 978, 549, 1232, 1383,
- 1465, 1644, 1845, 1955, 1097, 2463, 2765,
- 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
- };
- uint16_t mapDimF(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (dimF[value]);
- }
- // Minor Pentatonic keys
- // Key of C ........C D E F G A B
- uint16_t pentC [29] = {
- 274, 308, 346, 366, 411, 461, 518,
- 549, 616, 691, 732, 822, 923, 1036,
- 1097, 1232, 1383, 1465, 1644, 1845, 2071,
- 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
- };
- uint16_t mapPentC(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentC[value]);
- }
- // Key of G ........G A B C D E F#
- uint16_t pentG [29] = {
- 411, 461, 518, 274, 308, 346, 388,
- 822, 923, 1036, 1097, 1232, 1383, 1552,
- 1644, 1845, 2071, 2195, 2463, 2765, 3104,
- 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
- };
- uint16_t mapPentG(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentG[value]);
- }
- // Key of D ........D E F# G A B C#
- uint16_t pentD [29] = {
- 308, 346, 388, 411, 461, 518, 581,
- 616, 691, 776, 822, 923, 1036, 1163,
- 1232, 1383, 1552, 1644, 1845, 2071, 2325,
- 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
- };
- uint16_t mapPentD(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentD[value]);
- }
- // Key of A ........A B C# D E F# G#
- uint16_t pentA [29] = {
- 461, 518, 581, 616, 691, 776, 871,
- 923, 1036, 1163, 1232, 1383, 1552, 1742,
- 1845, 2071, 2325, 2463, 2765, 3104, 3484,
- 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
- };
- uint16_t mapPentA(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentA[value]);
- }
- // Key of E ........E F# G# A B C# D#
- uint16_t pentE [29] = {
- 346, 388, 435, 461, 518, 581, 652,
- 691, 776, 871, 923, 1036, 1163, 1305,
- 1383, 1552, 1742, 1845, 2071, 2325, 2610,
- 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
- };
- uint16_t mapPentE(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentE[value]);
- }
- // Key of B ........B C# D# E F# G# A#
- uint16_t pentB [29] = {
- 518, 581, 652, 691, 776, 871, 978,
- 1036, 1163, 1305, 1383, 1552, 1742, 1955,
- 2071, 2325, 2610, 2765, 3104, 3484, 3910,
- 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
- };
- uint16_t mapPentB(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentB[value]);
- }
- // Key of F# ........F# G# A# B C# D# E#
- uint16_t pentFs [29] = {
- 366, 388, 435, 489, 518, 581, 652,
- 732, 776, 871, 978, 1036, 1163, 1305,
- 1465, 1552, 1742, 1955, 2071, 2325, 2610,
- 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
- };
- uint16_t mapPentFs(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentFs[value]);
- }
- /*
- // Key of Db ........C# D# F F# G# A# C
- uint16_t pentDb [29] = {
- 291, 326, 366, 388, 435, 489, 274,
- 581, 652, 732, 776, 871, 978, 549,
- 1163, 1305, 1465, 1552, 1742, 1955, 1097,
- 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
- };
- uint16_t mapPentDb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentDb[value]);
- }
- // Key of Ab ........G# A# C C# D# F G
- uint16_t pentAb [29] = {
- 435, 489, 274, 581, 652, 732, 822,
- 871, 978, 549, 1163, 1305, 1465, 1644,
- 1742, 1955, 1097, 2325, 2610, 2930, 3288,
- 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
- };
- uint16_t mapPentAb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentAb[value]);
- }
- // Key of Eb ........D# F G G# A# C D
- uint16_t pentEb [29] = {
- 326, 366, 411, 435, 489, 274, 616,
- 652, 732, 822, 871, 978, 549, 1232,
- 1305, 1465, 1644, 1742, 1955, 1097, 2463,
- 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
- };
- uint16_t mapPentEb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentEb[value]);
- }
- // Key of Bb ........A# C D D# F G A
- uint16_t pentBb [29] = {
- 489, 274, 616, 652, 732, 822, 923,
- 978, 549, 1232, 1305, 1465, 1644, 1845,
- 1955, 1097, 2463, 2610, 2930, 3288, 3691,
- 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
- };
- uint16_t mapPentBb(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentBb[value]);
- }
- // Key of F ........F G A A# C D E
- uint16_t pentF [29] = {
- 366, 411, 461, 489, 274, 616, 691,
- 732, 822, 923, 978, 549, 1232, 1383,
- 1465, 1644, 1845, 1955, 1097, 2463, 2765,
- 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
- };
- uint16_t mapPentF(uint16_t input) {
- PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
- return (pentF[value]);
- }
- */
- void audioOn() {
- #if defined(__AVR_ATmega8__)
- // ATmega8 has different registers
- TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
- TIMSK = _BV(TOIE2);
- #elif defined(__AVR_ATmega1280__)
- TCCR3A = _BV(COM3C1) | _BV(WGM30);
- TCCR3B = _BV(CS30);
- TIMSK3 = _BV(TOIE3);
- #else
- // Set up PWM to 31.25kHz, phase accurate
- TCCR2A = _BV(COM2B1) | _BV(WGM20);
- TCCR2B = _BV(CS20);
- TIMSK2 = _BV(TOIE2);
- #endif
- }
- void setup() {
- pinMode(PWM_PIN,OUTPUT);
- audioOn();
- pinMode(LED_PIN,OUTPUT);
- pinMode(scalePin,INPUT);
- pinMode(armButton,INPUT);
- }
- void loop() {
- // The loop is pretty simple - it just updates the parameters for the oscillators.
- //
- // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
- // They will cause clicks and poops in the audio.
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // reads the scale select pin, converts input to 3 choices +
- // reads the converted input to select a scale from the +
- // corresponding case. +
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- int scale = analogRead(scalePin);
- scale = constrain(scale, 0, 1023);
- int scaleSelect = map(scale, 0, 1024, 0, 30);
- switch (scaleSelect) {
- case 0:
- syncPhaseInc = mapMajorC(analogRead(SYNC_CONTROL));
- break;
- case 1:
- syncPhaseInc = mapMajorG(analogRead(SYNC_CONTROL));
- break;
- case 2:
- syncPhaseInc = mapMajorD(analogRead(SYNC_CONTROL));
- break;
- case 3:
- syncPhaseInc = mapMajorA(analogRead(SYNC_CONTROL));
- break;
- case 4:
- syncPhaseInc = mapMajorE(analogRead(SYNC_CONTROL));
- break;
- case 5:
- syncPhaseInc = mapMajorB(analogRead(SYNC_CONTROL));
- break;
- case 6:
- syncPhaseInc = mapMajorFs(analogRead(SYNC_CONTROL));
- break;
- case 7:
- syncPhaseInc = mapMajorDb(analogRead(SYNC_CONTROL));
- break;
- case 8:
- syncPhaseInc = mapMajorAb(analogRead(SYNC_CONTROL));
- break;
- case 9:
- syncPhaseInc = mapMajorEb(analogRead(SYNC_CONTROL));
- break;
- case 10:
- syncPhaseInc = mapMajorBb(analogRead(SYNC_CONTROL));
- break;
- case 11:
- syncPhaseInc = mapMajorF(analogRead(SYNC_CONTROL));
- break;
- case 12:
- syncPhaseInc = mapDimC(analogRead(SYNC_CONTROL));
- break;
- case 13:
- syncPhaseInc = mapDimG(analogRead(SYNC_CONTROL));
- break;
- case 14:
- syncPhaseInc = mapDimD(analogRead(SYNC_CONTROL));
- break;
- case 15:
- syncPhaseInc = mapDimA(analogRead(SYNC_CONTROL));
- break;
- case 16:
- syncPhaseInc = mapDimE(analogRead(SYNC_CONTROL));
- break;
- case 17:
- syncPhaseInc = mapDimB(analogRead(SYNC_CONTROL));
- break;
- case 18:
- syncPhaseInc = mapDimFs(analogRead(SYNC_CONTROL));
- break;
- case 19:
- syncPhaseInc = mapDimDb(analogRead(SYNC_CONTROL));
- break;
- case 20:
- syncPhaseInc = mapDimAb(analogRead(SYNC_CONTROL));
- break;
- case 21:
- syncPhaseInc = mapDimEb(analogRead(SYNC_CONTROL));
- break;
- case 22:
- syncPhaseInc = mapDimBb(analogRead(SYNC_CONTROL));
- break;
- case 23:
- syncPhaseInc = mapDimF(analogRead(SYNC_CONTROL));
- break;
- case 24:
- syncPhaseInc = mapPentC(analogRead(SYNC_CONTROL));
- break;
- case 25:
- syncPhaseInc = mapPentG(analogRead(SYNC_CONTROL));
- break;
- case 26:
- syncPhaseInc = mapPentD(analogRead(SYNC_CONTROL));
- break;
- case 27:
- syncPhaseInc = mapPentA(analogRead(SYNC_CONTROL));
- break;
- case 28:
- syncPhaseInc = mapPentE(analogRead(SYNC_CONTROL));
- break;
- case 29:
- syncPhaseInc = mapPentB(analogRead(SYNC_CONTROL));
- break;
- case 30:
- syncPhaseInc = mapPentFs(analogRead(SYNC_CONTROL));
- break;
- /*
- case 31:
- syncPhaseInc = mapPentDb(analogRead(SYNC_CONTROL));
- break;
- case 32:
- syncPhaseInc = mapPentAb(analogRead(SYNC_CONTROL));
- break;
- case 33:
- syncPhaseInc = mapPentEb(analogRead(SYNC_CONTROL));
- break;
- case 34:
- syncPhaseInc = mapPentBb(analogRead(SYNC_CONTROL));
- break;
- case 35:
- syncPhaseInc = mapPentF(analogRead(SYNC_CONTROL));
- break;
- */
- }
- grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
- grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
- grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
- grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;
- }
- SIGNAL(PWM_INTERRUPT)
- {
- uint8_t value;
- uint16_t output;
- syncPhaseAcc += syncPhaseInc;
- if (syncPhaseAcc < syncPhaseInc) {
- // Time to start the next grain
- grainPhaseAcc = 0;
- grainAmp = 0x7fff;
- grain2PhaseAcc = 0;
- grain2Amp = 0x7fff;
- LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
- }
- // Increment the phase of the grain oscillators
- grainPhaseAcc += grainPhaseInc;
- grain2PhaseAcc += grain2PhaseInc;
- // Convert phase into a triangle wave
- value = (grainPhaseAcc >> 7) & 0xff;
- if (grainPhaseAcc & 0x8000) value = ~value;
- // Multiply by current grain amplitude to get sample
- output = value * (grainAmp >> 8);
- // Repeat for second grain
- value = (grain2PhaseAcc >> 7) & 0xff;
- if (grain2PhaseAcc & 0x8000) value = ~value;
- output += value * (grain2Amp >> 8);
- // Make the grain amplitudes decay by a factor every sample (exponential decay)
- grainAmp -= (grainAmp >> 8) * grainDecay;
- grain2Amp -= (grain2Amp >> 8) * grain2Decay;
- // Scale output to the available range, clipping if necessary
- output >>= 9;
- if (output > 255) output = 255;
- // Output to PWM (this is faster than using analogWrite)
- PWM_VALUE = output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement