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
- // PROGMEM additions by Duane B http://rcarduino.blogspot.com
- // Additional scales, customizations, and tweaks by Patrick Enright
- //
- // 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>
- int scalePin =5;
- uint16_t syncPhaseAcc;
- volatile uint16_t syncPhaseInc;
- uint16_t grainPhaseAcc;
- volatile uint16_t grainPhaseInc;
- uint16_t grainAmp;
- volatile uint8_t grainDecay;
- uint16_t grain2PhaseAcc;
- volatile uint16_t grain2PhaseInc;
- uint16_t grain2Amp;
- volatile 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
- #ifndef _NOTETABLES_
- #define _NOTETABLES_
- #include "avr/pgmspace.h"
- #define MIDI_NOTES 128
- // used to convert midi note numbers into the increments required to generate the note in the ISR
- PROGMEM unsigned int midiNoteToWavePhaseIncrement[MIDI_NOTES] =
- {
- 66 // C 0, 8.18,66.98,66 C
- ,70 // C# 1, 8.66,70.96,70
- ,75 // D 2, 9.18,75.18,75
- ,79 // D# 3, 9.72,79.65,79
- ,84 // E 4, 10.30,84.38,84
- ,89 // F 5, 10.91,89.40,89
- ,94 // F# 6, 11.56,94.72,94
- ,100 // G 7, 12.25,100.35,100
- ,106 // G# 8, 12.98,106.32,106
- ,112 // A 9, 13.75,112.64,112
- ,119 // A# 10, 14.57,119.34,119
- ,126 // B 11, 15.43,126.43,126
- ,133 // C0 12, 16.35,133.95,133 C0
- ,141 // C#0 13, 17.32,141.92,141
- ,150 // D0 14, 18.35,150.35,150
- ,159 // D#0 15, 19.45,159.29,159
- ,168 // E0 16, 20.60,168.77,168
- ,178 // F0 17, 21.83,178.80,178
- ,189 // F#0 18, 23.12,189.43,189
- ,200 // G0 19, 24.50,200.70,200
- ,212 // G#0 20, 25.96,212.63,212
- ,225 // A0 21, 27.50,225.28,225
- ,238 // A#0 22, 29.14,238.67,238
- ,252 // B0 23, 30.87,252.86,252
- ,267 // C1 24, 32.70,267.90,267 C1
- ,283 // C#1 25, 34.65,283.83,283
- ,300 // D1 26, 36.71,300.71,300
- ,318 // D#1 27, 38.89,318.59,318
- ,337 // E1 28, 41.20,337.53,337
- ,357 // F1 29, 43.65,357.60,357
- ,378 // F#1 30, 46.25,378.87,378
- ,401 // G1 31, 49.00,401.40,401
- ,425 // G#1 32, 51.91,425.27,425
- ,450 // A1 33, 55.00,450.55,450
- ,477 // A#1 34, 58.27,477.34,477
- ,505 // B1 35, 61.74,505.73,505
- ,535 // C2 36, 65.41,535.80,535 C2
- ,567 // C#2 37, 69.30,567.66,567
- ,601 // D2 38, 73.42,601.42,601
- ,637 // D#2 39, 77.78,637.18,637
- ,675 // E2 40, 82.41,675.07,675
- ,715 // F2 41, 87.31,715.21,715
- ,757 // F#2 42, 92.50,757.74,757
- ,802 // G2 43, 98.00,802.79,802
- ,850 // G#2 44, 103.83,850.53,850
- ,901 // A2 45, 110.00,901.11,901
- ,954 // A#2 46, 116.54,954.69,954
- ,1011 // B2 47, 123.47,1011.46,1011
- ,1071 // C3 48, 130.81,1071.60,1071 C3
- ,1135 // C#3 49, 138.59,1135.32,1135
- ,1202 // D3 50, 146.83,1202.83,1202
- ,1274 // D#3 51, 155.56,1274.36,1274
- ,1350 // E3 52, 164.81,1350.13,1350
- ,1430 // F3 53, 174.61,1430.42,1430
- ,1515 // F#3 54, 185.00,1515.47,1515
- ,1605 // G3 55, 196.00,1605.59,1605
- ,1701 // G#3 56, 207.65,1701.06,1701
- ,1802 // A3 57, 220.00,1802.21,1802
- ,1909 // A#3 58, 233.08,1909.38,1909
- ,2022 // B3 59, 246.94,2022.92,2022
- ,2143 // C4 60, 261.63,2143.20,2143 C4
- ,2270 // C#4 61, 277.18,2270.64,2270
- ,2405 // D4 62, 293.66,2405.66,2405
- ,2548 // D#4 63, 311.13,2548.71,2548
- ,2700 // E4 64, 329.63,2700.27,2700
- ,2860 // F4 65, 349.23,2860.83,2860
- ,3030 // F#4 66, 369.99,3030.95,3030
- ,3211 // G4 67, 392.00,3211.18,3211
- ,3402 // G#4 68, 415.30,3402.12,3402
- ,3604 // A4 69, 440.00,3604.42,3604
- ,3818 // A#4 70, 466.16,3818.75,3818
- ,4045 // B4 71, 493.88,4045.83,4045
- ,4286 // C5 72, 523.25,4286.41,4286 C5
- ,4541 // C#5 73, 554.37,4541.29,4541
- ,4811 // D5 74, 587.33,4811.33,4811
- ,5097 // D#5 75, 622.25,5097.42,5097
- ,5400 // E5 76, 659.26,5400.53,5400
- ,5721 // F5 77, 698.46,5721.67,5721
- ,6061 // F#5 78, 739.99,6061.89,6061
- ,6422 // G5 79, 783.99,6422.36,6422
- ,6804 // G#5 80, 830.61,6804.25,6804
- ,7208 // A5 81, 880.00,7208.85,7208
- ,7637 // A#5 82, 932.33,7637.51,7637
- ,8091 // B5 83, 987.77,8091.66,8091
- ,8572 // C6 84, 1046.50,8572.82,8572 C6
- ,9082 // C#6 85, 1108.73,9082.58,9082
- ,9622 // D6 86, 1174.66,9622.66,9622
- ,10194 // D#6 87, 1244.51,10194.85,10194
- ,10801 // E6 88, 1318.51,10801.07,10801
- ,11443 // F6 89, 1396.91,11443.33,11443
- ,12123 // F#6 90, 1479.98,12123.79,12123
- ,12844 // G6 91, 1567.98,12844.71,12844
- ,13608 // G#6 92, 1661.22,13608.50,13608
- ,14417 // A6 93, 1760.00,14417.70,14417
- ,15275 // A#6 94, 1864.65,15275.02,15275
- ,16183 // B6 95, 1975.53,16183.31,16183
- ,17145 // C7 96, 2093.00,17145.63,17145 C7
- ,18165 // C#7 97, 2217.46,18165.16,18165
- ,19245 // D7 98, 2349.32,19245.31,19245
- ,20389 // D#7 99, 2489.01,20389.70,20389
- ,21602 // E7 100, 2637.02,21602.14,21602
- ,22886 // F7 101, 2793.83,22886.67,22886
- ,24247 // F#7 102, 2959.95,24247.58,24247
- ,25689 // G7 103, 3135.96,25689.42,25689
- ,27216 // G#7 104, 3322.44,27216.99,27216
- ,28835 // A7 105, 3520.00,28835.39,28835
- ,30550 // A#7 106, 3729.31,30550.04,30550
- ,32366 // B7 107, 3951.06,32366.63,32366
- ,34291 // C8 108, 4186.01,34291.26,34291 C8
- ,36330 // C#8 109, 4434.92,36330.32,36330
- ,38490 // D8 110, 4698.64,38490.65,38490
- ,40779 // D#8 111, 4978.03,40779.41,40779
- ,43204 // E8 112, 5274.04,43204.25,43204
- ,45773 // F8 113, 5587.65,45773.32,45773
- ,48495 // F#8 114, 5919.91,48495.14,48495
- ,51378 // G8 115, 6271.92,51378.79,51378
- ,54433 // G#8 116, 6644.87,54433.96,54433
- ,57670 // A8 117, 7040.00,57670.76,57670
- ,61100 // A#8 118, 7458.62,61100.07,61100
- ,64733 // B8 119, 7902.13,64733.26,64733
- ,3046 // C9 120, 8372.02,68582.53,3046 C9
- ,7124 // C#9 121, 8869.84,72660.64,7124
- ,11445 // D9 122, 9397.27,76981.30,11445
- ,16022 // D#9 123, 9956.06,81558.77,16022
- ,20872 // E9 124, 10548.07,86408.50,20872
- ,26010 // F9 125, 11175.30,91546.65,26010
- ,31454 // F#9 126, 11839.81,96990.28,31454
- ,31454 // G9 127, 11839.81,96990.28,31454 // this is wrong, need to calculate correct value, even though at 8Khz its wrapping around on every tick
- };
- unsigned int getMidiNotePhaseIncrement(unsigned char sNote)
- {
- return pgm_read_word(midiNoteToWavePhaseIncrement + (sNote));
- }
- // Major keys
- // Key of C ........C D E F G A B
- PROGMEM unsigned char majorC[29] = {
- 48, 50, 52, 53, 55, 57, 59,
- 60, 62, 64, 65, 67, 69, 71,
- 72, 74, 76, 77, 79, 81, 83,
- 84, 86, 88, 89, 91, 93, 95, 96
- };
- unsigned int getMajorC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of G ........G A B C D E F#
- PROGMEM unsigned char majorG[29] = {
- 55, 57, 59, 48, 50, 52, 54,
- 67, 69, 71, 72, 74, 76, 78,
- 79, 81, 83, 84, 86, 88, 90,
- 91, 93, 95, 96, 98, 100, 102, 103,
- };
- unsigned int getMajorG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of D ........D E F# G A B C#
- PROGMEM unsigned char majorD[29] = {
- 50, 52, 54, 55, 57, 59, 61,
- 62, 64, 66, 67, 69, 71, 73,
- 74, 76, 78, 79, 81, 83, 85,
- 86, 88, 90, 91, 93, 95, 97, 98,
- };
- unsigned int getMajorD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of A ........A B C# D E F# G#
- PROGMEM unsigned char majorA[29] = {
- 57, 59, 61, 62, 64, 66, 68,
- 69, 71, 73, 74, 76, 78, 80,
- 81, 83, 85, 86, 88, 90, 92,
- 93, 95, 97, 98, 100, 102, 104, 105,
- };
- unsigned int getMajorA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of E ........E F# G# A B C# D#
- PROGMEM unsigned char majorE[29] = {
- 52, 54, 56, 57, 59, 61, 63,
- 64, 66, 68, 69, 71, 73, 75,
- 76, 78, 80, 81, 83, 85, 87,
- 88, 90, 92, 93, 95, 97, 99, 100
- };
- unsigned int getMajorE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of B ........B C# D# E F# G# A#
- PROGMEM unsigned char majorB[29] = {
- 59, 61, 63, 64, 66, 68, 70,
- 71, 73, 75, 76, 78, 80, 82,
- 83, 85, 87, 88, 90, 92, 94,
- 95, 97, 99, 100, 102, 104, 106, 107
- };
- unsigned int getMajorB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F# ........F# G# A# B C# D# E#
- PROGMEM unsigned char majorFs[29] = {
- 54, 56, 58, 59, 61, 63, 65,
- 66, 68, 70, 71, 73, 75, 77,
- 78, 80, 82, 83, 85, 87, 89,
- 90, 92, 94, 95, 97, 99, 101, 102,
- };
- unsigned int getMajorFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Db ........C# D# F F# G# A# C
- PROGMEM unsigned char majorDb[29] = {
- 49, 51, 53, 54, 56, 58, 48,
- 61, 63, 65, 66, 68, 70, 60,
- 73, 75, 77, 78, 80, 82, 72,
- 85, 87, 89, 90, 92, 94, 84, 97
- };
- unsigned int getMajorDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Ab ........G# A# C C# D# F G
- PROGMEM unsigned char majorAb[29] = {
- 56, 58, 48, 61, 63, 65, 67,
- 68, 70, 60, 73, 75, 77, 79,
- 80, 82, 72, 85, 87, 89, 91,
- 92, 94, 84, 97, 99, 101, 103, 104
- };
- unsigned int getMajorAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Eb ........D# F G G# A# C D
- PROGMEM unsigned char majorEb[29] = {
- 51, 53, 55, 56, 58, 48, 62,
- 63, 65, 67, 68, 70, 60, 74,
- 75, 77, 79, 80, 82, 72, 86,
- 87, 89, 91, 92, 94, 84, 98, 99
- };
- unsigned int getMajorEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Bb ........A# C D D# F G A
- PROGMEM unsigned char majorBb[29] = {
- 58, 48, 62, 63, 65, 67, 69,
- 70, 60, 74, 75, 77, 79, 81,
- 82, 72, 86, 87, 89, 91, 93,
- 94, 84, 98, 99, 101, 103, 105, 106
- };
- unsigned int getMajorBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F ........F G A A# C D E
- PROGMEM unsigned char majorF[29] = {
- 53, 55, 57, 58, 48, 62, 64,
- 65, 67, 69, 70, 60, 74, 76,
- 77, 79, 81, 82, 72, 86, 88,
- 89, 91, 93, 94, 84, 98, 100, 101
- };
- unsigned int getMajorF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(majorF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Half Whole Diminished keys
- // Key of C ........C D E F G A B
- PROGMEM unsigned char dimC[29] = {
- 48, 50, 52, 53, 55, 57, 59,
- 60, 62, 64, 65, 67, 69, 71,
- 72, 74, 76, 77, 79, 81, 83,
- 84, 86, 88, 89, 91, 93, 95, 96
- };
- unsigned int getDimC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of G ........G A B C D E F#
- PROGMEM unsigned char dimG[29] = {
- 55, 57, 59, 48, 50, 52, 54,
- 67, 69, 71, 72, 74, 76, 78,
- 79, 81, 83, 84, 86, 88, 90,
- 91, 93, 95, 96, 98, 100, 102, 103,
- };
- unsigned int getDimG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of D ........D E F# G A B C#
- PROGMEM unsigned char dimD[29] = {
- 50, 52, 54, 55, 57, 59, 61,
- 62, 64, 66, 67, 69, 71, 73,
- 74, 76, 78, 79, 81, 83, 85,
- 86, 88, 90, 91, 93, 95, 97, 98,
- };
- unsigned int getDimD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of A ........A B C# D E F# G#
- PROGMEM unsigned char dimA[29] = {
- 57, 59, 61, 62, 64, 66, 68,
- 69, 71, 73, 74, 76, 78, 80,
- 81, 83, 85, 86, 88, 90, 92,
- 93, 95, 97, 98, 100, 102, 104, 105,
- };
- unsigned int getDimA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of E ........E F# G# A B C# D#
- PROGMEM unsigned char dimE[29] = {
- 52, 54, 56, 57, 59, 61, 63,
- 64, 66, 68, 69, 71, 73, 75,
- 76, 78, 80, 81, 83, 85, 87,
- 88, 90, 92, 93, 95, 97, 99, 100
- };
- unsigned int getDimE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of B ........B C# D# E F# G# A#
- PROGMEM unsigned char dimB[29] = {
- 59, 61, 63, 64, 66, 68, 70,
- 71, 73, 75, 76, 78, 80, 82,
- 83, 85, 87, 88, 90, 92, 94,
- 95, 97, 99, 100, 102, 104, 106, 107
- };
- unsigned int getDimB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F# ........F# G# A# B C# D# E#
- PROGMEM unsigned char dimFs[29] = {
- 54, 56, 58, 59, 61, 63, 65,
- 66, 68, 70, 71, 73, 75, 77,
- 78, 80, 82, 83, 85, 87, 89,
- 90, 92, 94, 95, 97, 99, 101, 102,
- };
- unsigned int getDimFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Db ........C# D# F F# G# A# C
- PROGMEM unsigned char dimDb[29] = {
- 49, 51, 53, 54, 56, 58, 48,
- 61, 63, 65, 66, 68, 70, 60,
- 73, 75, 77, 78, 80, 82, 72,
- 85, 87, 89, 90, 92, 94, 84, 97
- };
- unsigned int getDimDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Ab ........G# A# C C# D# F G
- PROGMEM unsigned char dimAb[29] = {
- 56, 58, 48, 61, 63, 65, 67,
- 68, 70, 60, 73, 75, 77, 79,
- 80, 82, 72, 85, 87, 89, 91,
- 92, 94, 84, 97, 99, 101, 103, 104
- };
- unsigned int getDimAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Eb ........D# F G G# A# C D
- PROGMEM unsigned char dimEb[29] = {
- 51, 53, 55, 56, 58, 48, 62,
- 63, 65, 67, 68, 70, 60, 74,
- 75, 77, 79, 80, 82, 72, 86,
- 87, 89, 91, 92, 94, 84, 98, 99
- };
- unsigned int getDimEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Bb ........A# C D D# F G A
- PROGMEM unsigned char dimBb[29] = {
- 58, 48, 62, 63, 65, 67, 69,
- 70, 60, 74, 75, 77, 79, 81,
- 82, 72, 86, 87, 89, 91, 93,
- 94, 84, 98, 99, 101, 103, 105, 106
- };
- unsigned int getDimBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F ........F G A A# C D E
- PROGMEM unsigned char dimF[29] = {
- 53, 55, 57, 58, 48, 62, 64,
- 65, 67, 69, 70, 60, 74, 76,
- 77, 79, 81, 82, 72, 86, 88,
- 89, 91, 93, 94, 84, 98, 100, 101
- };
- unsigned int getDimF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(dimF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Minor Pentatonic Keys
- // Key of C ........C D E F G A B
- PROGMEM unsigned char pentC[29] = {
- 48, 50, 52, 53, 55, 57, 59,
- 60, 62, 64, 65, 67, 69, 71,
- 72, 74, 76, 77, 79, 81, 83,
- 84, 86, 88, 89, 91, 93, 95, 96
- };
- unsigned int getPentC(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentC + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of G ........G A B C D E F#
- PROGMEM unsigned char pentG[29] = {
- 55, 57, 59, 48, 50, 52, 54,
- 67, 69, 71, 72, 74, 76, 78,
- 79, 81, 83, 84, 86, 88, 90,
- 91, 93, 95, 96, 98, 100, 102, 103,
- };
- unsigned int getPentG(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentG + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of D ........D E F# G A B C#
- PROGMEM unsigned char pentD[29] = {
- 50, 52, 54, 55, 57, 59, 61,
- 62, 64, 66, 67, 69, 71, 73,
- 74, 76, 78, 79, 81, 83, 85,
- 86, 88, 90, 91, 93, 95, 97, 98,
- };
- unsigned int getPentD(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentD + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of A ........A B C# D E F# G#
- PROGMEM unsigned char pentA[29] = {
- 57, 59, 61, 62, 64, 66, 68,
- 69, 71, 73, 74, 76, 78, 80,
- 81, 83, 85, 86, 88, 90, 92,
- 93, 95, 97, 98, 100, 102, 104, 105,
- };
- unsigned int getPentA(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentA + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of E ........E F# G# A B C# D#
- PROGMEM unsigned char pentE[29] = {
- 52, 54, 56, 57, 59, 61, 63,
- 64, 66, 68, 69, 71, 73, 75,
- 76, 78, 80, 81, 83, 85, 87,
- 88, 90, 92, 93, 95, 97, 99, 100
- };
- unsigned int getPentE(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentE + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of B ........B C# D# E F# G# A#
- PROGMEM unsigned char pentB[29] = {
- 59, 61, 63, 64, 66, 68, 70,
- 71, 73, 75, 76, 78, 80, 82,
- 83, 85, 87, 88, 90, 92, 94,
- 95, 97, 99, 100, 102, 104, 106, 107
- };
- unsigned int getPentB(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentB + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F# ........F# G# A# B C# D# E#
- PROGMEM unsigned char pentFs[29] = {
- 54, 56, 58, 59, 61, 63, 65,
- 66, 68, 70, 71, 73, 75, 77,
- 78, 80, 82, 83, 85, 87, 89,
- 90, 92, 94, 95, 97, 99, 101, 102,
- };
- unsigned int getPentFs(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentFs + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Db ........C# D# F F# G# A# C
- PROGMEM unsigned char pentDb[29] = {
- 49, 51, 53, 54, 56, 58, 48,
- 61, 63, 65, 66, 68, 70, 60,
- 73, 75, 77, 78, 80, 82, 72,
- 85, 87, 89, 90, 92, 94, 84, 97
- };
- unsigned int getPentDb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentDb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Ab ........G# A# C C# D# F G
- PROGMEM unsigned char pentAb[29] = {
- 56, 58, 48, 61, 63, 65, 67,
- 68, 70, 60, 73, 75, 77, 79,
- 80, 82, 72, 85, 87, 89, 91,
- 92, 94, 84, 97, 99, 101, 103, 104
- };
- unsigned int getPentAb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentAb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Eb ........D# F G G# A# C D
- PROGMEM unsigned char pentEb[29] = {
- 51, 53, 55, 56, 58, 48, 62,
- 63, 65, 67, 68, 70, 60, 74,
- 75, 77, 79, 80, 82, 72, 86,
- 87, 89, 91, 92, 94, 84, 98, 99
- };
- unsigned int getPentEb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentEb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of Bb ........A# C D D# F G A
- PROGMEM unsigned char pentBb[29] = {
- 58, 48, 62, 63, 65, 67, 69,
- 70, 60, 74, 75, 77, 79, 81,
- 82, 72, 86, 87, 89, 91, 93,
- 94, 84, 98, 99, 101, 103, 105, 106
- };
- unsigned int getPentBb(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentBb + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- // Key of F ........F G A A# C D E
- PROGMEM unsigned char pentF[29] = {
- 53, 55, 57, 58, 48, 62, 64,
- 65, 67, 69, 70, 60, 74, 76,
- 77, 79, 81, 82, 72, 86, 88,
- 89, 91, 93, 94, 84, 98, 100, 101
- };
- unsigned int getPentF(unsigned int nAnalogInput)
- {
- uint8_t sPentatonicNote = map(nAnalogInput,0,1024,0,29);
- uint8_t sMidiIndex = pgm_read_byte(pentF + sPentatonicNote);
- return pgm_read_word(midiNoteToWavePhaseIncrement + sMidiIndex);
- }
- #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);
- }
- 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);
- }
- 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.
- // Smooth frequency mapping
- //syncPhaseInc = mapPhaseInc(analogRead(SYNC_CONTROL)) / 4;
- // Stepped mapping to MIDI notes: C, Db, D, Eb, E, F...
- //syncPhaseInc = mapMidi(analogRead(SYNC_CONTROL));
- // Stepped pentatonic mapping: D, E, G, A, B
- //syncPhaseInc = getPentatonicPhaseIncrement(analogRead(SYNC_CONTROL));
- // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- // reads the scale select pin, converts input to 36 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, 35);
- switch (scaleSelect) {
- case 0:
- syncPhaseInc = getMajorC(analogRead(SYNC_CONTROL));
- break;
- case 1:
- syncPhaseInc = getMajorG(analogRead(SYNC_CONTROL));
- break;
- case 2:
- syncPhaseInc = getMajorD(analogRead(SYNC_CONTROL));
- break;
- case 3:
- syncPhaseInc = getMajorA(analogRead(SYNC_CONTROL));
- break;
- case 4:
- syncPhaseInc = getMajorE(analogRead(SYNC_CONTROL));
- break;
- case 5:
- syncPhaseInc = getMajorB(analogRead(SYNC_CONTROL));
- break;
- case 6:
- syncPhaseInc = getMajorFs(analogRead(SYNC_CONTROL));
- break;
- case 7:
- syncPhaseInc = getMajorDb(analogRead(SYNC_CONTROL));
- break;
- case 8:
- syncPhaseInc = getMajorAb(analogRead(SYNC_CONTROL));
- break;
- case 9:
- syncPhaseInc = getMajorEb(analogRead(SYNC_CONTROL));
- break;
- case 10:
- syncPhaseInc = getMajorBb(analogRead(SYNC_CONTROL));
- break;
- case 11:
- syncPhaseInc = getMajorF(analogRead(SYNC_CONTROL));
- break;
- case 12:
- syncPhaseInc = getMajorC(analogRead(SYNC_CONTROL));
- break;
- case 13:
- syncPhaseInc = getMajorG(analogRead(SYNC_CONTROL));
- break;
- case 14:
- syncPhaseInc = getMajorD(analogRead(SYNC_CONTROL));
- break;
- case 15:
- syncPhaseInc = getMajorA(analogRead(SYNC_CONTROL));
- break;
- case 16:
- syncPhaseInc = getMajorE(analogRead(SYNC_CONTROL));
- break;
- case 17:
- syncPhaseInc = getMajorB(analogRead(SYNC_CONTROL));
- break;
- case 18:
- syncPhaseInc = getMajorFs(analogRead(SYNC_CONTROL));
- break;
- case 19:
- syncPhaseInc = getMajorDb(analogRead(SYNC_CONTROL));
- break;
- case 20:
- syncPhaseInc = getMajorAb(analogRead(SYNC_CONTROL));
- break;
- case 21:
- syncPhaseInc = getMajorEb(analogRead(SYNC_CONTROL));
- break;
- case 22:
- syncPhaseInc = getMajorBb(analogRead(SYNC_CONTROL));
- break;
- case 23:
- syncPhaseInc = getMajorF(analogRead(SYNC_CONTROL));
- break;
- case 24:
- syncPhaseInc = getMajorC(analogRead(SYNC_CONTROL));
- break;
- case 25:
- syncPhaseInc = getMajorG(analogRead(SYNC_CONTROL));
- break;
- case 26:
- syncPhaseInc = getMajorD(analogRead(SYNC_CONTROL));
- break;
- case 27:
- syncPhaseInc = getMajorA(analogRead(SYNC_CONTROL));
- break;
- case 28:
- syncPhaseInc = getMajorE(analogRead(SYNC_CONTROL));
- break;
- case 29:
- syncPhaseInc = getMajorB(analogRead(SYNC_CONTROL));
- break;
- case 30:
- syncPhaseInc = getMajorFs(analogRead(SYNC_CONTROL));
- break;
- case 31:
- syncPhaseInc = getMajorDb(analogRead(SYNC_CONTROL));
- break;
- case 32:
- syncPhaseInc = getMajorAb(analogRead(SYNC_CONTROL));
- break;
- case 33:
- syncPhaseInc = getMajorEb(analogRead(SYNC_CONTROL));
- break;
- case 34:
- syncPhaseInc = getMajorBb(analogRead(SYNC_CONTROL));
- break;
- case 35:
- syncPhaseInc = getMajorF(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