Advertisement
Guest User

Auduino with key select (WIP)

a guest
Nov 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.42 KB | None | 0 0
  1. // Auduino, the Lo-Fi granular synthesiser
  2. //
  3. // by Peter Knight, Tinker.it http://tinker.it
  4. //
  5. // Help: http://code.google.com/p/tinkerit/wiki/Auduino
  6. // More help: http://groups.google.com/group/auduino
  7. //
  8. // Analog in 0: Grain 1 pitch
  9. // Analog in 1: Grain 2 decay
  10. // Analog in 2: Grain 1 decay
  11. // Analog in 3: Grain 2 pitch
  12. // Analog in 4: Grain repetition frequency
  13. //
  14. // Digital 3: Audio out (Digital 11 on ATmega8)
  15. //
  16. // Changelog:
  17. // 19 Nov 2008: Added support for ATmega8 boards
  18. // 21 Mar 2009: Added support for ATmega328 boards
  19. // 7 Apr 2009: Fixed interrupt vector for ATmega328 boards
  20. // 8 Apr 2009: Added support for ATmega1280 boards (Arduino Mega)
  21.  
  22. #include <avr/io.h>
  23. #include <avr/interrupt.h>
  24. #include <avr/pgmspace.h>
  25.  
  26. int scalePin =5;
  27. int armButton =7;
  28. int armed =0;
  29.  
  30. // declaring note values for mapping
  31. // commented out due to memory limitations
  32. // left behind for reference
  33.  
  34. /*
  35. int c =17;
  36. int cs =18;
  37. int d =19;
  38. int ds =20;
  39. int e =22;
  40. int f =23;
  41. int fs =24;
  42. int g =26;
  43. int gs =27;
  44. int a =29;
  45. int as =31;
  46. int b =32;
  47. int c0 =34;
  48. int cs0 =36;
  49. int d0 =38;
  50. int ds0 =41;
  51. int e0 =43;
  52. int f0 =46;
  53. int fs0 =48;
  54. int g0 =51;
  55. int gs0 =54;
  56. int a0 =58;
  57. int as0 =61;
  58. int b0 =65;
  59. int c1 =69;
  60. int cs1 =73;
  61. int d1 =77;
  62. int ds1 =82;
  63. int e1 =86;
  64. int f1 =92;
  65. int fs1 =97;
  66. int g1 =103;
  67. int gs1 =109;
  68. int a1 =115;
  69. int as1 =122;
  70. int b1 =129;
  71. int c2 =137;
  72. int cs2 =145;
  73. int d2 =154;
  74. int ds2 =163;
  75. int e2 =173;
  76. int f2 =183;
  77. int fs2 =194;
  78. int g2 =206;
  79. int gs2 =218;
  80. int a2 =231;
  81. int as2 =244;
  82. int b2 =259;
  83. int c3 =274;
  84. int cs3 =291;
  85. int d3 =308;
  86. int ds3 =326;
  87. int e3 =346;
  88. int f3 =366;
  89. int fs3 =388;
  90. int g3 =411;
  91. int gs3 =435;
  92. int a3 =461;
  93. int as3 =489;
  94. int b3 =518;
  95. int c4 =549;
  96. int cs4 =581;
  97. int d4 =616;
  98. int ds4 =652;
  99. int e4 =691;
  100. int f4 =732;
  101. int fs4 =776;
  102. int g4 =822;
  103. int gs4 =871;
  104. int a4 =923;
  105. int as4 =978;
  106. int b4 =1036;
  107. int c5 =1097;
  108. int cs5 =1163;
  109. int d5 =1232;
  110. int ds5 =1305;
  111. int e5 =1383;
  112. int f5 =1465;
  113. int fs5 =1552;
  114. int g5 =1644;
  115. int gs5 =1742;
  116. int a5 =1845;
  117. int as5 =1955;
  118. int b5 =2071;
  119. int c6 =2195;
  120. int cs6 =2325;
  121. int d6 =2463;
  122. int ds6 =2610;
  123. int e6 =2765;
  124. int f6 =2930;
  125. int fs6 =3104;
  126. int g6 =3288;
  127. int gs6 =3484;
  128. int a6 =3691;
  129. int as6 =3910;
  130. int b6 =4143;
  131. int c7 =4389;
  132. int cs7 =4650;
  133. int d7 =4927;
  134. int ds7 =5220;
  135. int e7 =5530;
  136. int f7 =5859;
  137. int fs7 =6207;
  138. int g7 =6577;
  139. int gs7 =6968;
  140. int a7 =7382;
  141. int as7 =7821;
  142. int b7 =8286;
  143. int c8 =8779;
  144. int cs8 =9301;
  145. int d8 =9854;
  146. int ds8 =10440;
  147. int e8 =11060;
  148. int f8 =11718;
  149. int fs8 =12415;
  150. int g8 =13153;
  151. int gs8 =13935;
  152. int a8 =14764;
  153. int as8 =15642;
  154. int b8 =16572;
  155. int c9 =17557;
  156. int cs9 =18601;
  157. int d9 =19708;
  158. int ds9 =20879;
  159. int e9 =22121;
  160. int f9 =23436;
  161. int fs9 =24830;
  162. int g9 =26306;
  163. */
  164.  
  165. uint16_t syncPhaseAcc;
  166. uint16_t syncPhaseInc;
  167. uint16_t grainPhaseAcc;
  168. uint16_t grainPhaseInc;
  169. uint16_t grainAmp;
  170. uint8_t grainDecay;
  171. uint16_t grain2PhaseAcc;
  172. uint16_t grain2PhaseInc;
  173. uint16_t grain2Amp;
  174. uint8_t grain2Decay;
  175.  
  176. // Map Analogue channels
  177. #define SYNC_CONTROL (4)
  178. #define GRAIN_FREQ_CONTROL (0)
  179. #define GRAIN_DECAY_CONTROL (2)
  180. #define GRAIN2_FREQ_CONTROL (3)
  181. #define GRAIN2_DECAY_CONTROL (1)
  182.  
  183.  
  184. // Changing these will also requires rewriting audioOn()
  185.  
  186. #if defined(__AVR_ATmega8__)
  187. //
  188. // On old ATmega8 boards.
  189. // Output is on pin 11
  190. //
  191. #define LED_PIN 13
  192. #define LED_PORT PORTB
  193. #define LED_BIT 5
  194. #define PWM_PIN 11
  195. #define PWM_VALUE OCR2
  196. #define PWM_INTERRUPT TIMER2_OVF_vect
  197. #elif defined(__AVR_ATmega1280__)
  198. //
  199. // On the Arduino Mega
  200. // Output is on pin 3
  201. //
  202. #define LED_PIN 13
  203. #define LED_PORT PORTB
  204. #define LED_BIT 7
  205. #define PWM_PIN 3
  206. #define PWM_VALUE OCR3C
  207. #define PWM_INTERRUPT TIMER3_OVF_vect
  208. #else
  209. //
  210. // For modern ATmega168 and ATmega328 boards
  211. // Output is on pin 3
  212. //
  213. #define PWM_PIN 3
  214. #define PWM_VALUE OCR2B
  215. #define LED_PIN 13
  216. #define LED_PORT PORTB
  217. #define LED_BIT 5
  218. #define PWM_INTERRUPT TIMER2_OVF_vect
  219. #endif
  220.  
  221.  
  222. // Smooth logarithmic mapping
  223. //
  224. uint16_t antilogTable[] = {
  225. 64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
  226. 54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
  227. 45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
  228. 38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
  229. };
  230. uint16_t mapPhaseInc(uint16_t input) {
  231. return (antilogTable[input & 0x3f]) >> (input >> 6);
  232. }
  233.  
  234. // Major keys
  235. // Thanks to Goatboy who's code I started this beast with
  236.  
  237. // Key of C ........C D E F G A B
  238. uint16_t majorC [29] = {
  239. 274, 308, 346, 366, 411, 461, 518,
  240. 549, 616, 691, 732, 822, 923, 1036,
  241. 1097, 1232, 1383, 1465, 1644, 1845, 2071,
  242. 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
  243. };
  244.  
  245. uint16_t mapMajorC(uint16_t input) {
  246. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  247. return (majorC[value]);
  248. }
  249.  
  250. // Key of G ........G A B C D E F#
  251. uint16_t majorG [29] = {
  252. 411, 461, 518, 274, 308, 346, 388,
  253. 822, 923, 1036, 1097, 1232, 1383, 1552,
  254. 1644, 1845, 2071, 2195, 2463, 2765, 3104,
  255. 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
  256. };
  257.  
  258. uint16_t mapMajorG(uint16_t input) {
  259. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  260. return (majorG[value]);
  261. }
  262.  
  263. // Key of D ........D E F# G A B C#
  264. uint16_t majorD [29] = {
  265. 308, 346, 388, 411, 461, 518, 581,
  266. 616, 691, 776, 822, 923, 1036, 1163,
  267. 1232, 1383, 1552, 1644, 1845, 2071, 2325,
  268. 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
  269. };
  270.  
  271. uint16_t mapMajorD(uint16_t input) {
  272. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  273. return (majorD[value]);
  274. }
  275.  
  276. // Key of A ........A B C# D E F# G#
  277. uint16_t majorA [29] = {
  278. 461, 518, 581, 616, 691, 776, 871,
  279. 923, 1036, 1163, 1232, 1383, 1552, 1742,
  280. 1845, 2071, 2325, 2463, 2765, 3104, 3484,
  281. 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
  282. };
  283.  
  284. uint16_t mapMajorA(uint16_t input) {
  285. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  286. return (majorA[value]);
  287. }
  288.  
  289. // Key of E ........E F# G# A B C# D#
  290. uint16_t majorE [29] = {
  291. 346, 388, 435, 461, 518, 581, 652,
  292. 691, 776, 871, 923, 1036, 1163, 1305,
  293. 1383, 1552, 1742, 1845, 2071, 2325, 2610,
  294. 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
  295. };
  296.  
  297. uint16_t mapMajorE(uint16_t input) {
  298. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  299. return (majorE[value]);
  300. }
  301.  
  302. // Key of B ........B C# D# E F# G# A#
  303. uint16_t majorB [29] = {
  304. 518, 581, 652, 691, 776, 871, 978,
  305. 1036, 1163, 1305, 1383, 1552, 1742, 1955,
  306. 2071, 2325, 2610, 2765, 3104, 3484, 3910,
  307. 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
  308. };
  309.  
  310. uint16_t mapMajorB(uint16_t input) {
  311. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  312. return (majorB[value]);
  313. }
  314.  
  315. // Key of F# ........F# G# A# B C# D# E#
  316. uint16_t majorFs [29] = {
  317. 366, 388, 435, 489, 518, 581, 652,
  318. 732, 776, 871, 978, 1036, 1163, 1305,
  319. 1465, 1552, 1742, 1955, 2071, 2325, 2610,
  320. 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
  321. };
  322.  
  323. uint16_t mapMajorFs(uint16_t input) {
  324. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  325. return (majorFs[value]);
  326. }
  327.  
  328. // Key of Db ........C# D# F F# G# A# C
  329. uint16_t majorDb [29] = {
  330. 291, 326, 366, 388, 435, 489, 274,
  331. 581, 652, 732, 776, 871, 978, 549,
  332. 1163, 1305, 1465, 1552, 1742, 1955, 1097,
  333. 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
  334.  
  335. };
  336.  
  337. uint16_t mapMajorDb(uint16_t input) {
  338. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  339. return (majorDb[value]);
  340. }
  341.  
  342. // Key of Ab ........G# A# C C# D# F G
  343. uint16_t majorAb [29] = {
  344. 435, 489, 274, 581, 652, 732, 822,
  345. 871, 978, 549, 1163, 1305, 1465, 1644,
  346. 1742, 1955, 1097, 2325, 2610, 2930, 3288,
  347. 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
  348. };
  349.  
  350. uint16_t mapMajorAb(uint16_t input) {
  351. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  352. return (majorAb[value]);
  353. }
  354.  
  355. // Key of Eb ........D# F G G# A# C D
  356. uint16_t majorEb [29] = {
  357. 326, 366, 411, 435, 489, 274, 616,
  358. 652, 732, 822, 871, 978, 549, 1232,
  359. 1305, 1465, 1644, 1742, 1955, 1097, 2463,
  360. 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
  361. };
  362.  
  363. uint16_t mapMajorEb(uint16_t input) {
  364. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  365. return (majorEb[value]);
  366. }
  367.  
  368. // Key of Bb ........A# C D D# F G A
  369. uint16_t majorBb [29] = {
  370. 489, 274, 616, 652, 732, 822, 923,
  371. 978, 549, 1232, 1305, 1465, 1644, 1845,
  372. 1955, 1097, 2463, 2610, 2930, 3288, 3691,
  373. 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
  374. };
  375.  
  376. uint16_t mapMajorBb(uint16_t input) {
  377. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  378. return (majorBb[value]);
  379. }
  380.  
  381. // Key of F ........F G A A# C D E
  382. uint16_t majorF [29] = {
  383. 366, 411, 461, 489, 274, 616, 691,
  384. 732, 822, 923, 978, 549, 1232, 1383,
  385. 1465, 1644, 1845, 1955, 1097, 2463, 2765,
  386. 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
  387. };
  388.  
  389. uint16_t mapMajorF(uint16_t input) {
  390. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  391. return (majorF[value]);
  392. }
  393.  
  394. // Half Whole Diminished keys
  395. // Key of C ........C D E F G A B
  396. uint16_t dimC [29] = {
  397. 274, 308, 346, 366, 411, 461, 518,
  398. 549, 616, 691, 732, 822, 923, 1036,
  399. 1097, 1232, 1383, 1465, 1644, 1845, 2071,
  400. 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
  401. };
  402.  
  403. uint16_t mapDimC(uint16_t input) {
  404. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  405. return (dimC[value]);
  406. }
  407.  
  408. // Key of G ........G A B C D E F#
  409. uint16_t dimG [29] = {
  410. 411, 461, 518, 274, 308, 346, 388,
  411. 822, 923, 1036, 1097, 1232, 1383, 1552,
  412. 1644, 1845, 2071, 2195, 2463, 2765, 3104,
  413. 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
  414. };
  415.  
  416. uint16_t mapDimG(uint16_t input) {
  417. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  418. return (dimG[value]);
  419. }
  420.  
  421. // Key of D ........D E F# G A B C#
  422. uint16_t dimD [29] = {
  423. 308, 346, 388, 411, 461, 518, 581,
  424. 616, 691, 776, 822, 923, 1036, 1163,
  425. 1232, 1383, 1552, 1644, 1845, 2071, 2325,
  426. 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
  427. };
  428.  
  429. uint16_t mapDimD(uint16_t input) {
  430. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  431. return (dimD[value]);
  432. }
  433.  
  434. // Key of A ........A B C# D E F# G#
  435. uint16_t dimA [29] = {
  436. 461, 518, 581, 616, 691, 776, 871,
  437. 923, 1036, 1163, 1232, 1383, 1552, 1742,
  438. 1845, 2071, 2325, 2463, 2765, 3104, 3484,
  439. 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
  440. };
  441.  
  442. uint16_t mapDimA(uint16_t input) {
  443. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  444. return (dimA[value]);
  445. }
  446.  
  447. // Key of E ........E F# G# A B C# D#
  448. uint16_t dimE [29] = {
  449. 346, 388, 435, 461, 518, 581, 652,
  450. 691, 776, 871, 923, 1036, 1163, 1305,
  451. 1383, 1552, 1742, 1845, 2071, 2325, 2610,
  452. 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
  453. };
  454.  
  455. uint16_t mapDimE(uint16_t input) {
  456. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  457. return (dimE[value]);
  458. }
  459.  
  460. // Key of B ........B C# D# E F# G# A#
  461. uint16_t dimB [29] = {
  462. 518, 581, 652, 691, 776, 871, 978,
  463. 1036, 1163, 1305, 1383, 1552, 1742, 1955,
  464. 2071, 2325, 2610, 2765, 3104, 3484, 3910,
  465. 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
  466. };
  467.  
  468. uint16_t mapDimB(uint16_t input) {
  469. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  470. return (dimB[value]);
  471. }
  472.  
  473. // Key of F# ........F# G# A# B C# D# E#
  474. uint16_t dimFs [29] = {
  475. 366, 388, 435, 489, 518, 581, 652,
  476. 732, 776, 871, 978, 1036, 1163, 1305,
  477. 1465, 1552, 1742, 1955, 2071, 2325, 2610,
  478. 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
  479. };
  480.  
  481. uint16_t mapDimFs(uint16_t input) {
  482. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  483. return (dimFs[value]);
  484. }
  485.  
  486. // Key of Db ........C# D# F F# G# A# C
  487. uint16_t dimDb [29] = {
  488. 291, 326, 366, 388, 435, 489, 274,
  489. 581, 652, 732, 776, 871, 978, 549,
  490. 1163, 1305, 1465, 1552, 1742, 1955, 1097,
  491. 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
  492.  
  493. };
  494.  
  495. uint16_t mapDimDb(uint16_t input) {
  496. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  497. return (dimDb[value]);
  498. }
  499.  
  500. // Key of Ab ........G# A# C C# D# F G
  501. uint16_t dimAb [29] = {
  502. 435, 489, 274, 581, 652, 732, 822,
  503. 871, 978, 549, 1163, 1305, 1465, 1644,
  504. 1742, 1955, 1097, 2325, 2610, 2930, 3288,
  505. 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
  506. };
  507.  
  508. uint16_t mapDimAb(uint16_t input) {
  509. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  510. return (dimAb[value]);
  511. }
  512.  
  513. // Key of Eb ........D# F G G# A# C D
  514. uint16_t dimEb [29] = {
  515. 326, 366, 411, 435, 489, 274, 616,
  516. 652, 732, 822, 871, 978, 549, 1232,
  517. 1305, 1465, 1644, 1742, 1955, 1097, 2463,
  518. 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
  519. };
  520.  
  521. uint16_t mapDimEb(uint16_t input) {
  522. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  523. return (dimEb[value]);
  524. }
  525.  
  526. // Key of Bb ........A# C D D# F G A
  527. uint16_t dimBb [29] = {
  528. 489, 274, 616, 652, 732, 822, 923,
  529. 978, 549, 1232, 1305, 1465, 1644, 1845,
  530. 1955, 1097, 2463, 2610, 2930, 3288, 3691,
  531. 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
  532. };
  533.  
  534. uint16_t mapDimBb(uint16_t input) {
  535. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  536. return (dimBb[value]);
  537. }
  538.  
  539. // Key of F ........F G A A# C D E
  540. uint16_t dimF [29] = {
  541. 366, 411, 461, 489, 274, 616, 691,
  542. 732, 822, 923, 978, 549, 1232, 1383,
  543. 1465, 1644, 1845, 1955, 1097, 2463, 2765,
  544. 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
  545. };
  546.  
  547. uint16_t mapDimF(uint16_t input) {
  548. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  549. return (dimF[value]);
  550. }
  551.  
  552. // Minor Pentatonic keys
  553. // Key of C ........C D E F G A B
  554. uint16_t pentC [29] = {
  555. 274, 308, 346, 366, 411, 461, 518,
  556. 549, 616, 691, 732, 822, 923, 1036,
  557. 1097, 1232, 1383, 1465, 1644, 1845, 2071,
  558. 2195, 2463, 2765, 2930, 3288, 3691, 4143, 4389
  559. };
  560.  
  561. uint16_t mapPentC(uint16_t input) {
  562. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  563. return (pentC[value]);
  564. }
  565.  
  566. // Key of G ........G A B C D E F#
  567. uint16_t pentG [29] = {
  568. 411, 461, 518, 274, 308, 346, 388,
  569. 822, 923, 1036, 1097, 1232, 1383, 1552,
  570. 1644, 1845, 2071, 2195, 2463, 2765, 3104,
  571. 3288, 3691, 4143, 4389, 4927, 5530, 6207, 6577,
  572. };
  573.  
  574. uint16_t mapPentG(uint16_t input) {
  575. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  576. return (pentG[value]);
  577. }
  578.  
  579. // Key of D ........D E F# G A B C#
  580. uint16_t pentD [29] = {
  581. 308, 346, 388, 411, 461, 518, 581,
  582. 616, 691, 776, 822, 923, 1036, 1163,
  583. 1232, 1383, 1552, 1644, 1845, 2071, 2325,
  584. 2463, 2765, 3104, 3288, 3691, 4143, 4650, 4927,
  585. };
  586.  
  587. uint16_t mapPentD(uint16_t input) {
  588. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  589. return (pentD[value]);
  590. }
  591.  
  592. // Key of A ........A B C# D E F# G#
  593. uint16_t pentA [29] = {
  594. 461, 518, 581, 616, 691, 776, 871,
  595. 923, 1036, 1163, 1232, 1383, 1552, 1742,
  596. 1845, 2071, 2325, 2463, 2765, 3104, 3484,
  597. 3691, 4143, 4650, 4927, 5530, 6207, 6968, 7382,
  598. };
  599.  
  600. uint16_t mapPentA(uint16_t input) {
  601. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  602. return (pentA[value]);
  603. }
  604.  
  605. // Key of E ........E F# G# A B C# D#
  606. uint16_t pentE [29] = {
  607. 346, 388, 435, 461, 518, 581, 652,
  608. 691, 776, 871, 923, 1036, 1163, 1305,
  609. 1383, 1552, 1742, 1845, 2071, 2325, 2610,
  610. 2765, 3104, 3484, 3691, 4143, 4650, 5220, 5530
  611. };
  612.  
  613. uint16_t mapPentE(uint16_t input) {
  614. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  615. return (pentE[value]);
  616. }
  617.  
  618. // Key of B ........B C# D# E F# G# A#
  619. uint16_t pentB [29] = {
  620. 518, 581, 652, 691, 776, 871, 978,
  621. 1036, 1163, 1305, 1383, 1552, 1742, 1955,
  622. 2071, 2325, 2610, 2765, 3104, 3484, 3910,
  623. 4143, 4650, 5220, 5530, 6207, 6968, 7821, 8286
  624. };
  625.  
  626. uint16_t mapPentB(uint16_t input) {
  627. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  628. return (pentB[value]);
  629. }
  630. // Key of F# ........F# G# A# B C# D# E#
  631. uint16_t pentFs [29] = {
  632. 366, 388, 435, 489, 518, 581, 652,
  633. 732, 776, 871, 978, 1036, 1163, 1305,
  634. 1465, 1552, 1742, 1955, 2071, 2325, 2610,
  635. 2930, 3104, 3484, 3910, 4143, 4650, 5220, 5859,
  636. };
  637.  
  638. uint16_t mapPentFs(uint16_t input) {
  639. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  640. return (pentFs[value]);
  641. }
  642. /*
  643. // Key of Db ........C# D# F F# G# A# C
  644. uint16_t pentDb [29] = {
  645. 291, 326, 366, 388, 435, 489, 274,
  646. 581, 652, 732, 776, 871, 978, 549,
  647. 1163, 1305, 1465, 1552, 1742, 1955, 1097,
  648. 2325, 2610, 2930, 3104, 3484, 3910, 2195, 4650
  649.  
  650. };
  651.  
  652. uint16_t mapPentDb(uint16_t input) {
  653. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  654. return (pentDb[value]);
  655. }
  656. // Key of Ab ........G# A# C C# D# F G
  657. uint16_t pentAb [29] = {
  658. 435, 489, 274, 581, 652, 732, 822,
  659. 871, 978, 549, 1163, 1305, 1465, 1644,
  660. 1742, 1955, 1097, 2325, 2610, 2930, 3288,
  661. 3484, 3910, 2195, 4650, 5220, 5859, 6577, 6968
  662. };
  663.  
  664. uint16_t mapPentAb(uint16_t input) {
  665. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  666. return (pentAb[value]);
  667. }
  668. // Key of Eb ........D# F G G# A# C D
  669. uint16_t pentEb [29] = {
  670. 326, 366, 411, 435, 489, 274, 616,
  671. 652, 732, 822, 871, 978, 549, 1232,
  672. 1305, 1465, 1644, 1742, 1955, 1097, 2463,
  673. 2610, 2930, 3288, 3484, 3910, 2195, 4927, 5220
  674. };
  675.  
  676. uint16_t mapPentEb(uint16_t input) {
  677. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  678. return (pentEb[value]);
  679. }
  680.  
  681. // Key of Bb ........A# C D D# F G A
  682. uint16_t pentBb [29] = {
  683. 489, 274, 616, 652, 732, 822, 923,
  684. 978, 549, 1232, 1305, 1465, 1644, 1845,
  685. 1955, 1097, 2463, 2610, 2930, 3288, 3691,
  686. 3910, 2195, 4927, 5220, 5859, 6577, 7382, 7821
  687. };
  688.  
  689. uint16_t mapPentBb(uint16_t input) {
  690. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  691. return (pentBb[value]);
  692. }
  693.  
  694. // Key of F ........F G A A# C D E
  695. uint16_t pentF [29] = {
  696. 366, 411, 461, 489, 274, 616, 691,
  697. 732, 822, 923, 978, 549, 1232, 1383,
  698. 1465, 1644, 1845, 1955, 1097, 2463, 2765,
  699. 2930, 3288, 3691, 3910, 2195, 4927, 5530, 5859
  700. };
  701.  
  702. uint16_t mapPentF(uint16_t input) {
  703. PROGMEM prog_uint8_t value = (1023-input) / (1024/29);
  704. return (pentF[value]);
  705. }
  706. */
  707.  
  708. void audioOn() {
  709. #if defined(__AVR_ATmega8__)
  710. // ATmega8 has different registers
  711. TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
  712. TIMSK = _BV(TOIE2);
  713. #elif defined(__AVR_ATmega1280__)
  714. TCCR3A = _BV(COM3C1) | _BV(WGM30);
  715. TCCR3B = _BV(CS30);
  716. TIMSK3 = _BV(TOIE3);
  717. #else
  718. // Set up PWM to 31.25kHz, phase accurate
  719. TCCR2A = _BV(COM2B1) | _BV(WGM20);
  720. TCCR2B = _BV(CS20);
  721. TIMSK2 = _BV(TOIE2);
  722. #endif
  723. }
  724.  
  725.  
  726. void setup() {
  727. pinMode(PWM_PIN,OUTPUT);
  728. audioOn();
  729. pinMode(LED_PIN,OUTPUT);
  730. pinMode(scalePin,INPUT);
  731. pinMode(armButton,INPUT);
  732. }
  733.  
  734. void loop() {
  735. // The loop is pretty simple - it just updates the parameters for the oscillators.
  736. //
  737. // Avoid using any functions that make extensive use of interrupts, or turn interrupts off.
  738. // They will cause clicks and poops in the audio.
  739.  
  740. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  741. // reads the scale select pin, converts input to 3 choices +
  742. // reads the converted input to select a scale from the +
  743. // corresponding case. +
  744. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  745.  
  746. int scale = analogRead(scalePin);
  747. scale = constrain(scale, 0, 1023);
  748. int scaleSelect = map(scale, 0, 1024, 0, 30);
  749.  
  750. switch (scaleSelect) {
  751. case 0:
  752. syncPhaseInc = mapMajorC(analogRead(SYNC_CONTROL));
  753. break;
  754. case 1:
  755. syncPhaseInc = mapMajorG(analogRead(SYNC_CONTROL));
  756. break;
  757. case 2:
  758. syncPhaseInc = mapMajorD(analogRead(SYNC_CONTROL));
  759. break;
  760. case 3:
  761. syncPhaseInc = mapMajorA(analogRead(SYNC_CONTROL));
  762. break;
  763. case 4:
  764. syncPhaseInc = mapMajorE(analogRead(SYNC_CONTROL));
  765. break;
  766. case 5:
  767. syncPhaseInc = mapMajorB(analogRead(SYNC_CONTROL));
  768. break;
  769. case 6:
  770. syncPhaseInc = mapMajorFs(analogRead(SYNC_CONTROL));
  771. break;
  772. case 7:
  773. syncPhaseInc = mapMajorDb(analogRead(SYNC_CONTROL));
  774. break;
  775. case 8:
  776. syncPhaseInc = mapMajorAb(analogRead(SYNC_CONTROL));
  777. break;
  778. case 9:
  779. syncPhaseInc = mapMajorEb(analogRead(SYNC_CONTROL));
  780. break;
  781. case 10:
  782. syncPhaseInc = mapMajorBb(analogRead(SYNC_CONTROL));
  783. break;
  784. case 11:
  785. syncPhaseInc = mapMajorF(analogRead(SYNC_CONTROL));
  786. break;
  787. case 12:
  788. syncPhaseInc = mapDimC(analogRead(SYNC_CONTROL));
  789. break;
  790. case 13:
  791. syncPhaseInc = mapDimG(analogRead(SYNC_CONTROL));
  792. break;
  793. case 14:
  794. syncPhaseInc = mapDimD(analogRead(SYNC_CONTROL));
  795. break;
  796. case 15:
  797. syncPhaseInc = mapDimA(analogRead(SYNC_CONTROL));
  798. break;
  799. case 16:
  800. syncPhaseInc = mapDimE(analogRead(SYNC_CONTROL));
  801. break;
  802. case 17:
  803. syncPhaseInc = mapDimB(analogRead(SYNC_CONTROL));
  804. break;
  805. case 18:
  806. syncPhaseInc = mapDimFs(analogRead(SYNC_CONTROL));
  807. break;
  808. case 19:
  809. syncPhaseInc = mapDimDb(analogRead(SYNC_CONTROL));
  810. break;
  811. case 20:
  812. syncPhaseInc = mapDimAb(analogRead(SYNC_CONTROL));
  813. break;
  814. case 21:
  815. syncPhaseInc = mapDimEb(analogRead(SYNC_CONTROL));
  816. break;
  817. case 22:
  818. syncPhaseInc = mapDimBb(analogRead(SYNC_CONTROL));
  819. break;
  820. case 23:
  821. syncPhaseInc = mapDimF(analogRead(SYNC_CONTROL));
  822. break;
  823. case 24:
  824. syncPhaseInc = mapPentC(analogRead(SYNC_CONTROL));
  825. break;
  826. case 25:
  827. syncPhaseInc = mapPentG(analogRead(SYNC_CONTROL));
  828. break;
  829. case 26:
  830. syncPhaseInc = mapPentD(analogRead(SYNC_CONTROL));
  831. break;
  832. case 27:
  833. syncPhaseInc = mapPentA(analogRead(SYNC_CONTROL));
  834. break;
  835. case 28:
  836. syncPhaseInc = mapPentE(analogRead(SYNC_CONTROL));
  837. break;
  838. case 29:
  839. syncPhaseInc = mapPentB(analogRead(SYNC_CONTROL));
  840. break;
  841. case 30:
  842. syncPhaseInc = mapPentFs(analogRead(SYNC_CONTROL));
  843. break;
  844. /*
  845. case 31:
  846. syncPhaseInc = mapPentDb(analogRead(SYNC_CONTROL));
  847. break;
  848. case 32:
  849. syncPhaseInc = mapPentAb(analogRead(SYNC_CONTROL));
  850. break;
  851. case 33:
  852. syncPhaseInc = mapPentEb(analogRead(SYNC_CONTROL));
  853. break;
  854. case 34:
  855. syncPhaseInc = mapPentBb(analogRead(SYNC_CONTROL));
  856. break;
  857. case 35:
  858. syncPhaseInc = mapPentF(analogRead(SYNC_CONTROL));
  859. break;
  860. */
  861. }
  862.  
  863.  
  864. grainPhaseInc = mapPhaseInc(analogRead(GRAIN_FREQ_CONTROL)) / 2;
  865. grainDecay = analogRead(GRAIN_DECAY_CONTROL) / 8;
  866. grain2PhaseInc = mapPhaseInc(analogRead(GRAIN2_FREQ_CONTROL)) / 2;
  867. grain2Decay = analogRead(GRAIN2_DECAY_CONTROL) / 4;
  868. }
  869.  
  870. SIGNAL(PWM_INTERRUPT)
  871. {
  872. uint8_t value;
  873. uint16_t output;
  874.  
  875. syncPhaseAcc += syncPhaseInc;
  876. if (syncPhaseAcc < syncPhaseInc) {
  877. // Time to start the next grain
  878. grainPhaseAcc = 0;
  879. grainAmp = 0x7fff;
  880. grain2PhaseAcc = 0;
  881. grain2Amp = 0x7fff;
  882. LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
  883. }
  884.  
  885. // Increment the phase of the grain oscillators
  886. grainPhaseAcc += grainPhaseInc;
  887. grain2PhaseAcc += grain2PhaseInc;
  888.  
  889. // Convert phase into a triangle wave
  890. value = (grainPhaseAcc >> 7) & 0xff;
  891. if (grainPhaseAcc & 0x8000) value = ~value;
  892. // Multiply by current grain amplitude to get sample
  893. output = value * (grainAmp >> 8);
  894.  
  895. // Repeat for second grain
  896. value = (grain2PhaseAcc >> 7) & 0xff;
  897. if (grain2PhaseAcc & 0x8000) value = ~value;
  898. output += value * (grain2Amp >> 8);
  899.  
  900. // Make the grain amplitudes decay by a factor every sample (exponential decay)
  901. grainAmp -= (grainAmp >> 8) * grainDecay;
  902. grain2Amp -= (grain2Amp >> 8) * grain2Decay;
  903.  
  904. // Scale output to the available range, clipping if necessary
  905. output >>= 9;
  906. if (output > 255) output = 255;
  907.  
  908. // Output to PWM (this is faster than using analogWrite)
  909. PWM_VALUE = output;
  910. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement