Guest User

auduino with key select

a guest
Nov 1st, 2012
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. // Auduino, the Lo-Fi granular synthesiser
  2. //
  3. // by Peter Knight, Tinker.it http://tinker.it
  4. //
  5. // key select functionality by Patrick Enright
  6. //
  7. // Help: http://code.google.com/p/tinkerit/wiki/Auduino
  8. // More help: http://groups.google.com/group/auduino
  9. //
  10. // Analog in 0: Grain 1 pitch
  11. // Analog in 1: Grain 2 decay
  12. // Analog in 2: Grain 1 decay
  13. // Analog in 3: Grain 2 pitch
  14. // Analog in 4: Grain repetition frequency
  15. //
  16. // Digital 3: Audio out (Digital 11 on ATmega8)
  17. //
  18. // Changelog:
  19. // 19 Nov 2008: Added support for ATmega8 boards
  20. // 21 Mar 2009: Added support for ATmega328 boards
  21. // 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
  22. // 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
  23.  
  24. #include <avr/io.h>
  25. #include <avr/interrupt.h>
  26.  
  27. int scalePin =5;
  28. int armButton =7;
  29. int armed =0;
  30.  
  31. // declaring note values for mapping
  32. // high and low octaves are commented out
  33. // due to Arduino memory limitations
  34. // and to enhance playability.
  35.  
  36. /*
  37. int c =17;
  38. int cs =18;
  39. int d =19;
  40. int ds =20;
  41. int e =22;
  42. int f =23;
  43. int fs =24;
  44. int g =26;
  45. int gs =27;
  46. int a =29;
  47. int as =31;
  48. int b =32;
  49. int c0 =34;
  50. int cs0 =36;
  51. int d0 =38;
  52. int ds0 =41;
  53. int e0 =43;
  54. int f0 =46;
  55. int fs0 =48;
  56. int g0 =51;
  57. int gs0 =54;
  58. int a0 =58;
  59. int as0 =61;
  60. int b0 =65;
  61. int c1 =69;
  62. int cs1 =73;
  63. int d1 =77;
  64. int ds1 =82;
  65. int e1 =86;
  66. int f1 =92;
  67. int fs1 =97;
  68. int g1 =103;
  69. int gs1 =109;
  70. int a1 =115;
  71. int as1 =122;
  72. int b1 =129;
  73. int c2 =137;
  74. int cs2 =145;
  75. int d2 =154;
  76. int ds2 =163;
  77. int e2 =173;
  78. int f2 =183;
  79. int fs2 =194;
  80. int g2 =206;
  81. int gs2 =218;
  82. int a2 =231;
  83. int as2 =244;
  84. int b2 =259;
  85. */
  86. int c3 =274;
  87. int cs3 =291;
  88. int d3 =308;
  89. int ds3 =326;
  90. int e3 =346;
  91. int f3 =366;
  92. int fs3 =388;
  93. int g3 =411;
  94. int gs3 =435;
  95. int a3 =461;
  96. int as3 =489;
  97. int b3 =518;
  98. int c4 =549;
  99. int cs4 =581;
  100. int d4 =616;
  101. int ds4 =652;
  102. int e4 =691;
  103. int f4 =732;
  104. int fs4 =776;
  105. int g4 =822;
  106. int gs4 =871;
  107. int a4 =923;
  108. int as4 =978;
  109. int b4 =1036;
  110. int c5 =1097;
  111. int cs5 =1163;
  112. int d5 =1232;
  113. int ds5 =1305;
  114. int e5 =1383;
  115. int f5 =1465;
  116. int fs5 =1552;
  117. int g5 =1644;
  118. int gs5 =1742;
  119. int a5 =1845;
  120. int as5 =1955;
  121. int b5 =2071;
  122. int c6 =2195;
  123. int cs6 =2325;
  124. int d6 =2463;
  125. int ds6 =2610;
  126. int e6 =2765;
  127. int f6 =2930;
  128. int fs6 =3104;
  129. int g6 =3288;
  130. int gs6 =3484;
  131. int a6 =3691;
  132. int as6 =3910;
  133. int b6 =4143;
  134. int c7 =4389;
  135. int cs7 =4650;
  136. int d7 =4927;
  137. int ds7 =5220;
  138. int e7 =5530;
  139. int f7 =5859;
  140. int fs7 =6207;
  141. int g7 =6577;
  142. int gs7 =6968;
  143. int a7 =7382;
  144. int as7 =7821;
  145. int b7 =8286;
  146. /*
  147. int c8 =8779;
  148. int cs8 =9301;
  149. int d8 =9854;
  150. int ds8 =10440;
  151. int e8 =11060;
  152. int f8 =11718;
  153. int fs8 =12415;
  154. int g8 =13153;
  155. int gs8 =13935;
  156. int a8 =14764;
  157. int as8 =15642;
  158. int b8 =16572;
  159. int c9 =17557;
  160. int cs9 =18601;
  161. int d9 =19708;
  162. int ds9 =20879;
  163. int e9 =22121;
  164. int f9 =23436;
  165. int fs9 =24830;
  166. int g9 =26306;
  167. */
  168.  
  169. uint16_t syncPhaseAcc;
  170. uint16_t syncPhaseInc;
  171. uint16_t grainPhaseAcc;
  172. uint16_t grainPhaseInc;
  173. uint16_t grainAmp;
  174. uint8_t grainDecay;
  175. uint16_t grain2PhaseAcc;
  176. uint16_t grain2PhaseInc;
  177. uint16_t grain2Amp;
  178. uint8_t grain2Decay;
  179.  
  180. // Map Analogue channels
  181. #define SYNC_CONTROL (4)
  182. #define GRAIN_FREQ_CONTROL (0)
  183. #define GRAIN_DECAY_CONTROL (2)
  184. #define GRAIN2_FREQ_CONTROL (3)
  185. #define GRAIN2_DECAY_CONTROL (1)
  186.  
  187.  
  188. // Changing these will also requires rewriting audioOn()
  189.  
  190. #if defined(__AVR_ATmega8__)
  191. //
  192. // On old ATmega8 boards.
  193. // Output is on pin 11
  194. //
  195. #define LED_PIN 13
  196. #define LED_PORT PORTB
  197. #define LED_BIT 5
  198. #define PWM_PIN 11
  199. #define PWM_VALUE OCR2
  200. #define PWM_INTERRUPT TIMER2_OVF_vect
  201. #elif defined(__AVR_ATmega1280__)
  202. //
  203. // On the Arduino Mega
  204. // Output is on pin 3
  205. //
  206. #define LED_PIN 13
  207. #define LED_PORT PORTB
  208. #define LED_BIT 7
  209. #define PWM_PIN 3
  210. #define PWM_VALUE OCR3C
  211. #define PWM_INTERRUPT TIMER3_OVF_vect
  212. #else
  213. //
  214. // For modern ATmega168 and ATmega328 boards
  215. // Output is on pin 3
  216. //
  217. #define PWM_PIN 3
  218. #define PWM_VALUE OCR2B
  219. #define LED_PIN 13
  220. #define LED_PORT PORTB
  221. #define LED_BIT 5
  222. #define PWM_INTERRUPT TIMER2_OVF_vect
  223. #endif
  224.  
  225.  
  226. // Smooth logarithmic mapping
  227. //
  228. uint16_t antilogTable[] = {
  229. 64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
  230. 54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
  231. 45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
  232. 38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
  233. };
  234. uint16_t mapPhaseInc(uint16_t input) {
  235. return (antilogTable[input & 0x3f]) >> (input >> 6);
  236. }
  237.  
  238. // Major keys
  239. // Thanks to Goatboy who's code I started this beast with
  240.  
  241. // Key of C ........C D E F G A B
  242. uint16_t majorC [29] = {
  243. c3, d3, e3, f3, g3, a3, b3,
  244. c4, d4, e4, f4, g4, a4, b4,
  245. c5, d5, e5, f5, g5, a5, b5,
  246. c6, d6, e6, f6, g6, a6, b6, c7
  247. };
  248.  
  249. uint16_t mapMajorC(uint16_t input) {
  250. uint8_t value = (1023-input) / (1024/29);
  251. return (majorC[value]);
  252. }
  253.  
  254. // Key of G ........G A B C D E F#
  255. uint16_t majorG [29] = {
  256. g3, a3, b3, c3, d3, e3, fs3,
  257. g4, a4, b4, c5, d5, e5, fs5,
  258. g5, a5, b5, c6, d6, e6, fs6,
  259. g6, a6, b6, c7, d7, e7, fs7, g7,
  260. };
  261.  
  262. uint16_t mapMajorG(uint16_t input) {
  263. uint8_t value = (1023-input) / (1024/29);
  264. return (majorG[value]);
  265. }
  266.  
  267. // Key of D ........D E F# G A B C#
  268. uint16_t majorD [29] = {
  269. d3, e3, fs3, g3, a3, b3, cs4,
  270. d4, e4, fs4, g4, a4, b4, cs5,
  271. d5, e5, fs5, g5, a5, b5, cs6,
  272. d6, e6, fs6, g6, a6, b6, cs7, d7,
  273. };
  274.  
  275. uint16_t mapMajorD(uint16_t input) {
  276. uint8_t value = (1023-input) / (1024/29);
  277. return (majorD[value]);
  278. }
  279.  
  280. // Key of A ........A B C# D E F# G#
  281. uint16_t majorA [29] = {
  282. a3, b3, cs4, d4, e4, fs4, gs4,
  283. a4, b4, cs5, d5, e5, fs5, gs5,
  284. a5, b5, cs6, d6, e6, fs6, gs6,
  285. a6, b6, cs7, d7, e7, fs7, gs7, a7,
  286. };
  287.  
  288. uint16_t mapMajorA(uint16_t input) {
  289. uint8_t value = (1023-input) / (1024/29);
  290. return (majorA[value]);
  291. }
  292.  
  293. // Key of E ........E F# G# A B C# D#
  294. uint16_t majorE [29] = {
  295. e3, fs3, gs3, a3, b3, cs4, ds4,
  296. e4, fs4, gs4, a4, b4, cs5, ds5,
  297. e5, fs5, gs5, a5, b5, cs6, ds6,
  298. e6, fs6, gs6, a6, b6, cs7, ds7, e7
  299. };
  300.  
  301. uint16_t mapMajorE(uint16_t input) {
  302. uint8_t value = (1023-input) / (1024/29);
  303. return (majorE[value]);
  304. }
  305.  
  306. // Key of B ........B C# D# E F# G# A#
  307. uint16_t majorB [29] = {
  308. b3, cs4, ds4, e4, fs4, gs4, as4,
  309. b4, cs5, ds5, e5, fs5, gs5, as5,
  310. b5, cs6, ds6, e6, fs6, gs6, as6,
  311. b6, cs7, ds7, e7, fs7, gs7, as7, b7
  312. };
  313.  
  314. uint16_t mapMajorB(uint16_t input) {
  315. uint8_t value = (1023-input) / (1024/29);
  316. return (majorB[value]);
  317. }
  318.  
  319. // Key of F# ........F# G# A# B C# D# E#
  320. uint16_t majorFs [29] = {
  321. f3, fs3, gs3, as3, b3, cs4, ds4,
  322. f4, fs4, gs4, as4, b4, cs5, ds5,
  323. f5, fs5, gs5, as5, b5, cs6, ds6,
  324. f6, fs6, gs6, as6, b6, cs7, ds7, f7,
  325. };
  326.  
  327. uint16_t mapMajorFs(uint16_t input) {
  328. uint8_t value = (1023-input) / (1024/29);
  329. return (majorFs[value]);
  330. }
  331.  
  332. // Key of Db ........C# D# F F# G# A# C
  333. uint16_t majorDb [29] = {
  334. cs3, ds3, f3, fs3, gs3, as3, c3,
  335. cs4, ds4, f4, fs4, gs4, as4, c4,
  336. cs5, ds5, f5, fs5, gs5, as5, c5,
  337. cs6, ds6, f6, fs6, gs6, as6, c6, cs7
  338.  
  339. };
  340.  
  341. uint16_t mapMajorDb(uint16_t input) {
  342. uint8_t value = (1023-input) / (1024/29);
  343. return (majorDb[value]);
  344. }
  345.  
  346. // Key of Ab ........G# A# C C# D# F G
  347. uint16_t majorAb [29] = {
  348. gs3, as3, c3, cs4, ds4, f4, g4,
  349. gs4, as4, c4, cs5, ds5, f5, g5,
  350. gs5, as5, c5, cs6, ds6, f6, g6,
  351. gs6, as6, c6, cs7, ds7, f7, g7, gs7
  352. };
  353.  
  354. uint16_t mapMajorAb(uint16_t input) {
  355. uint8_t value = (1023-input) / (1024/29);
  356. return (majorAb[value]);
  357. }
  358.  
  359. // Key of Eb ........D# F G G# A# C D
  360. uint16_t majorEb [29] = {
  361. ds3, f3, g3, gs3, as3, c3, d4,
  362. ds4, f4, g4, gs4, as4, c4, d5,
  363. ds5, f5, g5, gs5, as5, c5, d6,
  364. ds6, f6, g6, gs6, as6, c6, d7, ds7
  365. };
  366.  
  367. uint16_t mapMajorEb(uint16_t input) {
  368. uint8_t value = (1023-input) / (1024/29);
  369. return (majorEb[value]);
  370. }
  371.  
  372. // Key of Bb ........A# C D D# F G A
  373. uint16_t majorBb [29] = {
  374. as3, c3, d4, ds4, f4, g4, a4,
  375. as4, c4, d5, ds5, f5, g5, a5,
  376. as5, c5, d6, ds6, f6, g6, a6,
  377. as6, c6, d7, ds7, f7, g7, a7, as7
  378. };
  379.  
  380. uint16_t mapMajorBb(uint16_t input) {
  381. uint8_t value = (1023-input) / (1024/29);
  382. return (majorBb[value]);
  383. }
  384.  
  385. // Key of F ........F G A A# C D E
  386. uint16_t majorF [29] = {
  387. f3, g3, a3, as3, c3, d4, e4,
  388. f4, g4, a4, as4, c4, d5, e5,
  389. f5, g5, a5, as5, c5, d6, e6,
  390. f6, g6, a6, as6, c6, d7, e7, f7
  391. };
  392.  
  393. uint16_t mapMajorF(uint16_t input) {
  394. uint8_t value = (1023-input) / (1024/29);
  395. return (majorF[value]);
  396. }
  397.  
  398. void audioOn() {
  399. #if defined(__AVR_ATmega8__)
  400. // ATmega8 has different registers
  401. TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
  402. TIMSK = _BV(TOIE2);
  403. #elif defined(__AVR_ATmega1280__)
  404. TCCR3A = _BV(COM3C1) | _BV(WGM30);
  405. TCCR3B = _BV(CS30);
  406. TIMSK3 = _BV(TOIE3);
  407. #else
  408. // Set up PWM to 31.25kHz, phase accurate
  409. TCCR2A = _BV(COM2B1) | _BV(WGM20);
  410. TCCR2B = _BV(CS20);
  411. TIMSK2 = _BV(TOIE2);
  412. #endif
  413. }
  414.  
  415.  
  416. void setup() {
  417. pinMode(PWM_PIN,OUTPUT);
  418. audioOn();
  419. pinMode(LED_PIN,OUTPUT);
  420. pinMode(scalePin,INPUT);
  421. pinMode(armButton,INPUT);
  422. }
  423.  
  424. void loop() {
  425. // The loop is pretty simple - it just updates the parameters for the oscillators.
  426. //
  427. // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
  428. // They will cause clicks and poops in the audio.
  429.  
  430. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  431. // reads the scale select pin, converts input to 12 choices +
  432. // reads the converted input to select a scale from the +
  433. // corresponding case. +
  434. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  435.  
  436. int scale = analogRead(scalePin);
  437. scale = constrain(scale, 0, 1023);
  438. int scaleSelect = map(scale, 0, 1024, 0, 12);
  439.  
  440. switch (scaleSelect) {
  441. case 0:
  442. syncPhaseInc = mapMajorC(analogRead(SYNC_CONTROL));
  443. break;
  444. case 1:
  445. syncPhaseInc = mapMajorG(analogRead(SYNC_CONTROL));
  446. break;
  447. case 2:
  448. syncPhaseInc = mapMajorD(analogRead(SYNC_CONTROL));
  449. break;
  450. case 3:
  451. syncPhaseInc = mapMajorA(analogRead(SYNC_CONTROL));
  452. break;
  453. case 4:
  454. syncPhaseInc = mapMajorE(analogRead(SYNC_CONTROL));
  455. break;
  456. case 5:
  457. syncPhaseInc = mapMajorB(analogRead(SYNC_CONTROL));
  458. break;
  459. case 6:
  460. syncPhaseInc = mapMajorFs(analogRead(SYNC_CONTROL));
  461. break;
  462. case 7:
  463. syncPhaseInc = mapMajorDb(analogRead(SYNC_CONTROL));
  464. break;
  465. case 8:
  466. syncPhaseInc = mapMajorAb(analogRead(SYNC_CONTROL));
  467. break;
  468. case 9:
  469. syncPhaseInc = mapMajorEb(analogRead(SYNC_CONTROL));
  470. break;
  471. case 10:
  472. syncPhaseInc = mapMajorBb(analogRead(SYNC_CONTROL));
  473. break;
  474. case 11:
  475. syncPhaseInc = mapMajorF(analogRead(SYNC_CONTROL));
  476. break;
  477. }
  478.  
  479.  
  480. grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
  481. grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
  482. grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
  483. grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;
  484. }
  485.  
  486. SIGNAL(PWM_INTERRUPT)
  487. {
  488. uint8_t value;
  489. uint16_t output;
  490.  
  491. syncPhaseAcc += syncPhaseInc;
  492. if (syncPhaseAcc < syncPhaseInc) {
  493. // Time to start the next grain
  494. grainPhaseAcc = 0;
  495. grainAmp = 0x7fff;
  496. grain2PhaseAcc = 0;
  497. grain2Amp = 0x7fff;
  498. LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
  499. }
  500.  
  501. // Increment the phase of the grain oscillators
  502. grainPhaseAcc += grainPhaseInc;
  503. grain2PhaseAcc += grain2PhaseInc;
  504.  
  505. // Convert phase into a triangle wave
  506. value = (grainPhaseAcc >> 7) & 0xff;
  507. if (grainPhaseAcc & 0x8000) value = ~value;
  508. // Multiply by current grain amplitude to get sample
  509. output = value * (grainAmp >> 8);
  510.  
  511. // Repeat for second grain
  512. value = (grain2PhaseAcc >> 7) & 0xff;
  513. if (grain2PhaseAcc & 0x8000) value = ~value;
  514. output += value * (grain2Amp >> 8);
  515.  
  516. // Make the grain amplitudes decay by a factor every sample (exponential decay)
  517. grainAmp -= (grainAmp >> 8) * grainDecay;
  518. grain2Amp -= (grain2Amp >> 8) * grain2Decay;
  519.  
  520. // Scale output to the available range, clipping if necessary
  521. output >>= 9;
  522. if (output > 255) output = 255;
  523.  
  524. // Output to PWM (this is faster than using analogWrite)
  525. PWM_VALUE = output;
  526. }
Advertisement
Add Comment
Please, Sign In to add comment