Advertisement
Guest User

Auduino with key select (WIP)

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