Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

277 linhas
13KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. // These audio waveforms have a period of 256 points, plus a 257th
  28. // point that is a duplicate of the first point. This duplicate
  29. // is needed because the waveform generator uses linear interpolation
  30. // between each point and the next point in the waveform.
  31. const int16_t AudioWaveformSine[257] = {
  32. 0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179,
  33. 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732,
  34. 15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403,
  35. 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790,
  36. 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571,
  37. 30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521,
  38. 32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521,
  39. 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571,
  40. 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790,
  41. 26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403,
  42. 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732,
  43. 14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179,
  44. 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804,
  45. -1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739,
  46. -9512,-10278,-11039,-11793,-12539,-13279,-14010,-14732,-15446,-16151,
  47. -16846,-17530,-18204,-18868,-19519,-20159,-20787,-21403,-22005,-22594,
  48. -23170,-23731,-24279,-24811,-25329,-25832,-26319,-26790,-27245,-27683,
  49. -28105,-28510,-28898,-29268,-29621,-29956,-30273,-30571,-30852,-31113,
  50. -31356,-31580,-31785,-31971,-32137,-32285,-32412,-32521,-32609,-32678,
  51. -32728,-32757,-32767,-32757,-32728,-32678,-32609,-32521,-32412,-32285,
  52. -32137,-31971,-31785,-31580,-31356,-31113,-30852,-30571,-30273,-29956,
  53. -29621,-29268,-28898,-28510,-28105,-27683,-27245,-26790,-26319,-25832,
  54. -25329,-24811,-24279,-23731,-23170,-22594,-22005,-21403,-20787,-20159,
  55. -19519,-18868,-18204,-17530,-16846,-16151,-15446,-14732,-14010,-13279,
  56. -12539,-11793,-11039,-10278, -9512, -8739, -7962, -7179, -6393, -5602,
  57. -4808, -4011, -3212, -2410, -1608, -804, 0
  58. };
  59. #if 0
  60. #! /usr/bin/perl
  61. use Math::Trig ':pi';
  62. $len = 256;
  63. print "const int16_t AudioWaveformSine[257] = {\n";
  64. for ($i=0; $i <= $len; $i++) {
  65. $f = sin($i / $len * 2 * pi);
  66. $d = sprintf "%.0f", $f * 32767.0;
  67. #print $d;
  68. printf "%6d", $d + 0;
  69. print "," if ($i < $len);
  70. print "\n" if ($i % 10) == 9;
  71. }
  72. print "\n" unless ($len % 10) == 9;
  73. print "};\n";
  74. #endif
  75. const int16_t AudioWaveformTriangle[257] = {
  76. 0, 512, 1024, 1536, 2048, 2560, 3072, 3584, 4096, 4608,
  77. 5120, 5632, 6144, 6656, 7168, 7680, 8192, 8704, 9216, 9728,
  78. 10240, 10752, 11264, 11776, 12288, 12800, 13312, 13824, 14336, 14848,
  79. 15360, 15872, 16384, 16895, 17407, 17919, 18431, 18943, 19455, 19967,
  80. 20479, 20991, 21503, 22015, 22527, 23039, 23551, 24063, 24575, 25087,
  81. 25599, 26111, 26623, 27135, 27647, 28159, 28671, 29183, 29695, 30207,
  82. 30719, 31231, 31743, 32255, 32767, 32255, 31743, 31231, 30719, 30207,
  83. 29695, 29183, 28671, 28159, 27647, 27135, 26623, 26111, 25599, 25087,
  84. 24575, 24063, 23551, 23039, 22527, 22015, 21503, 20991, 20479, 19967,
  85. 19455, 18943, 18431, 17919, 17407, 16895, 16384, 15872, 15360, 14848,
  86. 14336, 13824, 13312, 12800, 12288, 11776, 11264, 10752, 10240, 9728,
  87. 9216, 8704, 8192, 7680, 7168, 6656, 6144, 5632, 5120, 4608,
  88. 4096, 3584, 3072, 2560, 2048, 1536, 1024, 512, 0, -512,
  89. -1024, -1536, -2048, -2560, -3072, -3584, -4096, -4608, -5120, -5632,
  90. -6144, -6656, -7168, -7680, -8192, -8704, -9216, -9728,-10240,-10752,
  91. -11264,-11776,-12288,-12800,-13312,-13824,-14336,-14848,-15360,-15872,
  92. -16384,-16895,-17407,-17919,-18431,-18943,-19455,-19967,-20479,-20991,
  93. -21503,-22015,-22527,-23039,-23551,-24063,-24575,-25087,-25599,-26111,
  94. -26623,-27135,-27647,-28159,-28671,-29183,-29695,-30207,-30719,-31231,
  95. -31743,-32255,-32767,-32255,-31743,-31231,-30719,-30207,-29695,-29183,
  96. -28671,-28159,-27647,-27135,-26623,-26111,-25599,-25087,-24575,-24063,
  97. -23551,-23039,-22527,-22015,-21503,-20991,-20479,-19967,-19455,-18943,
  98. -18431,-17919,-17407,-16895,-16384,-15872,-15360,-14848,-14336,-13824,
  99. -13312,-12800,-12288,-11776,-11264,-10752,-10240, -9728, -9216, -8704,
  100. -8192, -7680, -7168, -6656, -6144, -5632, -5120, -4608, -4096, -3584,
  101. -3072, -2560, -2048, -1536, -1024, -512, 0
  102. };
  103. #if 0
  104. #! /usr/bin/perl
  105. $len = 256;
  106. print "const int16_t AudioWaveformTriangle[257] = {\n";
  107. for ($i=0; $i <= $len; $i++) {
  108. $f = 0;
  109. if ($i < $len / 4) {
  110. $f = $i / ($len / 4);
  111. } elsif ($i < $len * 3 / 4) {
  112. $f = 2 - $i / ($len / 4);
  113. } else {
  114. $f = $i / ($len / 4) - 4;
  115. #print "$i $f\n";
  116. }
  117. $d = sprintf "%.0f", $f * 32767.0;
  118. printf "%6d", $d + 0;
  119. print "," if ($i < $len);
  120. print "\n" if ($i % 10) == 9;
  121. }
  122. print "\n" unless ($len % 10) == 9;
  123. print "};\n";
  124. #endif
  125. const int16_t AudioWaveformSquare[257] = {
  126. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  127. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  128. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  129. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  130. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  131. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  132. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  133. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  134. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  135. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  136. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  137. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
  138. 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,-32767,-32767,
  139. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  140. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  141. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  142. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  143. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  144. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  145. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  146. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  147. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  148. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  149. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  150. -32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,-32767,
  151. -32767,-32767,-32767,-32767,-32767,-32767, 32767
  152. };
  153. #if 0
  154. #! /usr/bin/perl
  155. $len = 256;
  156. print "const int16_t AudioWaveformSquare[257] = {\n";
  157. for ($i=0; $i <= $len; $i++) {
  158. $f = 1.0;
  159. if ($i < $len / 2) {
  160. $f = 1.0;
  161. } elsif ($i < $len) {
  162. $f = -1.0;
  163. }
  164. $d = sprintf "%.0f", $f * 32767.0;
  165. printf "%6d", $d + 0;
  166. print "," if ($i < $len);
  167. print "\n" if ($i % 10) == 9;
  168. }
  169. print "\n" unless ($len % 10) == 9;
  170. print "};\n";
  171. #endif
  172. const int16_t AudioWaveformSawtooth[257] = {
  173. 0, 256, 512, 768, 1024, 1280, 1536, 1792, 2048, 2304,
  174. 2560, 2816, 3072, 3328, 3584, 3840, 4096, 4352, 4608, 4864,
  175. 5120, 5376, 5632, 5888, 6144, 6400, 6656, 6912, 7168, 7424,
  176. 7680, 7936, 8192, 8448, 8704, 8960, 9216, 9472, 9728, 9984,
  177. 10240, 10496, 10752, 11008, 11264, 11520, 11776, 12032, 12288, 12544,
  178. 12800, 13056, 13312, 13568, 13824, 14080, 14336, 14592, 14848, 15104,
  179. 15360, 15616, 15872, 16128, 16384, 16639, 16895, 17151, 17407, 17663,
  180. 17919, 18175, 18431, 18687, 18943, 19199, 19455, 19711, 19967, 20223,
  181. 20479, 20735, 20991, 21247, 21503, 21759, 22015, 22271, 22527, 22783,
  182. 23039, 23295, 23551, 23807, 24063, 24319, 24575, 24831, 25087, 25343,
  183. 25599, 25855, 26111, 26367, 26623, 26879, 27135, 27391, 27647, 27903,
  184. 28159, 28415, 28671, 28927, 29183, 29439, 29695, 29951, 30207, 30463,
  185. 30719, 30975, 31231, 31487, 31743, 31999, 32255, 32511,-32767,-32511,
  186. -32255,-31999,-31743,-31487,-31231,-30975,-30719,-30463,-30207,-29951,
  187. -29695,-29439,-29183,-28927,-28671,-28415,-28159,-27903,-27647,-27391,
  188. -27135,-26879,-26623,-26367,-26111,-25855,-25599,-25343,-25087,-24831,
  189. -24575,-24319,-24063,-23807,-23551,-23295,-23039,-22783,-22527,-22271,
  190. -22015,-21759,-21503,-21247,-20991,-20735,-20479,-20223,-19967,-19711,
  191. -19455,-19199,-18943,-18687,-18431,-18175,-17919,-17663,-17407,-17151,
  192. -16895,-16639,-16384,-16128,-15872,-15616,-15360,-15104,-14848,-14592,
  193. -14336,-14080,-13824,-13568,-13312,-13056,-12800,-12544,-12288,-12032,
  194. -11776,-11520,-11264,-11008,-10752,-10496,-10240, -9984, -9728, -9472,
  195. -9216, -8960, -8704, -8448, -8192, -7936, -7680, -7424, -7168, -6912,
  196. -6656, -6400, -6144, -5888, -5632, -5376, -5120, -4864, -4608, -4352,
  197. -4096, -3840, -3584, -3328, -3072, -2816, -2560, -2304, -2048, -1792,
  198. -1536, -1280, -1024, -768, -512, -256, 0
  199. };
  200. #if 0
  201. #! /usr/bin/perl
  202. $len = 256;
  203. print "const int16_t AudioWaveformSawtooth[257] = {\n";
  204. for ($i=0; $i <= $len; $i++) {
  205. $f = 0;
  206. if ($i < $len / 2) {
  207. $f = $i / $len * 2;
  208. } else {
  209. $f = -2 + $i / $len * 2;
  210. #print "$i $f\n";
  211. }
  212. $d = sprintf "%.0f", $f * 32767.0;
  213. printf "%6d", $d + 0;
  214. print "," if ($i < $len);
  215. print "\n" if ($i % 10) == 9;
  216. }
  217. print "\n" unless ($len % 10) == 9;
  218. print "};\n";
  219. #endif
  220. const int16_t fader_table[257] = {
  221. 0, 1, 4, 11, 19, 30, 44, 60, 78, 99,
  222. 123, 149, 177, 208, 241, 276, 314, 355, 398, 443,
  223. 490, 541, 593, 648, 705, 764, 826, 891, 957, 1026,
  224. 1097, 1171, 1247, 1325, 1405, 1488, 1572, 1660, 1749, 1840,
  225. 1934, 2030, 2128, 2228, 2330, 2435, 2541, 2650, 2761, 2873,
  226. 2988, 3105, 3224, 3344, 3467, 3592, 3718, 3847, 3977, 4109,
  227. 4243, 4379, 4517, 4657, 4798, 4941, 5086, 5232, 5380, 5530,
  228. 5682, 5835, 5989, 6145, 6303, 6462, 6623, 6785, 6949, 7114,
  229. 7281, 7448, 7618, 7788, 7960, 8133, 8307, 8483, 8660, 8838,
  230. 9017, 9197, 9378, 9560, 9743, 9928,10113,10299,10486,10674,
  231. 10863,11053,11244,11435,11627,11820,12013,12207,12402,12597,
  232. 12793,12989,13186,13384,13582,13780,13979,14178,14377,14577,
  233. 14777,14977,15177,15378,15579,15780,15981,16182,16383,16584,
  234. 16785,16986,17187,17387,17588,17788,17989,18188,18388,18588,
  235. 18787,18985,19184,19382,19579,19776,19972,20168,20364,20558,
  236. 20752,20946,21139,21331,21522,21712,21902,22091,22279,22466,
  237. 22652,22838,23022,23205,23388,23569,23749,23928,24106,24283,
  238. 24458,24633,24806,24977,25148,25317,25485,25651,25817,25980,
  239. 26142,26303,26463,26620,26776,26931,27084,27236,27385,27534,
  240. 27680,27825,27968,28109,28249,28386,28522,28656,28789,28919,
  241. 29048,29174,29299,29422,29542,29661,29778,29893,30006,30116,
  242. 30225,30331,30436,30538,30638,30736,30832,30926,31017,31107,
  243. 31194,31279,31361,31442,31520,31596,31669,31740,31809,31876,
  244. 31940,32002,32062,32119,32174,32226,32276,32324,32369,32412,
  245. 32452,32490,32526,32559,32590,32618,32644,32667,32688,32707,
  246. 32723,32737,32748,32756,32763,32766,32767
  247. };
  248. #if 0
  249. #! /usr/bin/perl
  250. print "const int16_t fader_table[257] = {\n";
  251. $len = 256;
  252. for ($i=0; $i < $len+1; $i++) {
  253. $a = cos(3.14149 * $i / $len);
  254. $in = (1 - $a) / 2;
  255. $d = $in * 32768;
  256. $d = 32767 if $d >= 32767.5;
  257. printf "%5d", $d;
  258. print "," if ($i < $len);
  259. print "\n" if ($i % 10) == 9;
  260. }
  261. print "\n};\n";
  262. #endif