sine_make.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*****************************************************************
  2. >file name : apps/common/sine_make.c
  3. >author : lichao
  4. >create time : Sun 05 May 2019 08:37:35 PM CST
  5. *****************************************************************/
  6. #include "system/includes.h"
  7. #include "sine_make.h"
  8. #define SINE_USE_MALLOC 1
  9. struct audio_sin_maker {
  10. u8 open;
  11. u8 id;
  12. u8 sin_num;
  13. u8 channel;
  14. u8 repeat;
  15. u8 next_param;
  16. u16 fade_points;
  17. /*int sample_rate;*/
  18. int volume;
  19. int points;
  20. int sin_index;
  21. int win_sin_index;
  22. const struct sin_param *sin;
  23. };
  24. #ifndef SINE_USE_MALLOC
  25. static struct audio_sin_maker sin_maker_handle;
  26. #endif
  27. void *sin_tone_open(const struct sin_param *param, int num, u8 channel, u8 repeat)
  28. {
  29. if (!param || !num) {
  30. return NULL;
  31. }
  32. #ifdef SINE_USE_MALLOC
  33. struct audio_sin_maker *sin = zalloc(sizeof(struct audio_sin_maker));
  34. #else
  35. struct audio_sin_maker *sin = &sin_maker_handle;
  36. #endif
  37. if (!sin) {
  38. return NULL;
  39. }
  40. if (sin->open) {
  41. return NULL;
  42. }
  43. sin->sin = param;
  44. sin->sin_num = num;
  45. sin->open = 1;
  46. sin->points = param[0].points;
  47. sin->id = 0;
  48. sin->channel = channel;
  49. sin->repeat = repeat;
  50. #if (defined CONFIG_CPU_BR18 || \
  51. defined CONFIG_CPU_BR21)
  52. /*这里如果加多fade points,会导致结束的时候有噪声,不是在淡出尾部*/
  53. sin->fade_points = 0;
  54. #else
  55. sin->fade_points = 480;
  56. #endif
  57. sin->next_param = 1;
  58. return (void *)sin;
  59. }
  60. #if !defined(SINE_MAKE_IN_MASK)
  61. const int sf_sin_tab1[513] = {
  62. 0x00000000, 0x0000c910, 0x0001921f, 0x00025b2d, 0x0003243a, 0x0003ed45, 0x0004b64e, 0x00057f53,
  63. 0x00064855, 0x00071154, 0x0007da4e, 0x0008a343, 0x00096c33, 0x000a351d, 0x000afe00, 0x000bc6dd,
  64. 0x000c8fb3, 0x000d5881, 0x000e2147, 0x000eea03, 0x000fb2b7, 0x00107b61, 0x00114401, 0x00120c96,
  65. 0x0012d521, 0x00139d9f, 0x00146611, 0x00152e77, 0x0015f6d0, 0x0016bf1b, 0x00178758, 0x00184f87,
  66. 0x001917a7, 0x0019dfb7, 0x001aa7b7, 0x001b6fa7, 0x001c3786, 0x001cff53, 0x001dc70f, 0x001e8eb8,
  67. 0x001f564e, 0x00201dd1, 0x0020e541, 0x0021ac9b, 0x002273e2, 0x00233b13, 0x0024022e, 0x0024c933,
  68. 0x00259021, 0x002656f8, 0x00271db7, 0x0027e45f, 0x0028aaed, 0x00297163, 0x002a37bf, 0x002afe01,
  69. 0x002bc429, 0x002c8a35, 0x002d5026, 0x002e15fb, 0x002edbb4, 0x002fa14f, 0x003066ce, 0x00312c2e,
  70. 0x0031f170, 0x0032b694, 0x00337b98, 0x0034407c, 0x00350540, 0x0035c9e4, 0x00368e66, 0x003752c6,
  71. 0x00381705, 0x0038db21, 0x00399f19, 0x003a62ef, 0x003b26a0, 0x003bea2c, 0x003cad94, 0x003d70d7,
  72. 0x003e33f3, 0x003ef6e9, 0x003fb9b8, 0x00407c60, 0x00413ee0, 0x00420138, 0x0042c367, 0x0043856d,
  73. 0x0044474a, 0x004508fc, 0x0045ca83, 0x00468be0, 0x00474d11, 0x00480e16, 0x0048ceef, 0x00498f9a,
  74. 0x004a5019, 0x004b1069, 0x004bd08b, 0x004c907f, 0x004d5043, 0x004e0fd8, 0x004ecf3c, 0x004f8e70,
  75. 0x00504d72, 0x00510c43, 0x0051cae3, 0x0052894f, 0x00534789, 0x0054058f, 0x0054c362, 0x00558100,
  76. 0x00563e6a, 0x0056fb9e, 0x0057b89d, 0x00587565, 0x005931f7, 0x0059ee52, 0x005aaa76, 0x005b6662,
  77. 0x005c2215, 0x005cdd8f, 0x005d98d0, 0x005e53d8, 0x005f0ea5, 0x005fc937, 0x0060838f, 0x00613dab,
  78. 0x0061f78b, 0x0062b12e, 0x00636a95, 0x006423be, 0x0064dcaa, 0x00659557, 0x00664dc6, 0x006705f5,
  79. 0x0067bde5, 0x00687595, 0x00692d05, 0x0069e433, 0x006a9b21, 0x006b51cc, 0x006c0836, 0x006cbe5c,
  80. 0x006d7440, 0x006e29e0, 0x006edf3d, 0x006f9454, 0x00704927, 0x0070fdb5, 0x0071b1fd, 0x007265ff,
  81. 0x007319ba, 0x0073cd2f, 0x0074805c, 0x00753341, 0x0075e5dd, 0x00769831, 0x00774a3c, 0x0077fbfe,
  82. 0x0078ad75, 0x00795ea2, 0x007a0f84, 0x007ac01a, 0x007b7065, 0x007c2064, 0x007cd016, 0x007d7f7c,
  83. 0x007e2e93, 0x007edd5d, 0x007f8bd9, 0x00803a06, 0x0080e7e4, 0x00819573, 0x008242b1, 0x0082ef9f,
  84. 0x00839c3d, 0x00844889, 0x0084f484, 0x0085a02c, 0x00864b82, 0x0086f686, 0x0087a136, 0x00884b92,
  85. 0x0088f59b, 0x00899f4e, 0x008a48ad, 0x008af1b7, 0x008b9a6b, 0x008c42c9, 0x008cead0, 0x008d9281,
  86. 0x008e39da, 0x008ee0db, 0x008f8784, 0x00902dd5, 0x0090d3cd, 0x0091796b, 0x00921eb0, 0x0092c39a,
  87. 0x0093682a, 0x00940c5f, 0x0094b039, 0x009553b7, 0x0095f6d9, 0x0096999f, 0x00973c07, 0x0097de12,
  88. 0x00987fc0, 0x0099210f, 0x0099c200, 0x009a6293, 0x009b02c6, 0x009ba299, 0x009c420c, 0x009ce11f,
  89. 0x009d7fd1, 0x009e1e22, 0x009ebc12, 0x009f599f, 0x009ff6cb, 0x00a09393, 0x00a12ff9, 0x00a1cbfb,
  90. 0x00a26799, 0x00a302d3, 0x00a39da9, 0x00a4381a, 0x00a4d225, 0x00a56bcb, 0x00a6050a, 0x00a69de3,
  91. 0x00a73656, 0x00a7ce61, 0x00a86605, 0x00a8fd41, 0x00a99415, 0x00aa2a80, 0x00aac082, 0x00ab561b,
  92. 0x00abeb4a, 0x00ac800f, 0x00ad1469, 0x00ada859, 0x00ae3bde, 0x00aecef7, 0x00af61a5, 0x00aff3e6,
  93. 0x00b085bb, 0x00b11722, 0x00b1a81d, 0x00b238aa, 0x00b2c8c9, 0x00b3587a, 0x00b3e7bc, 0x00b4768f,
  94. 0x00b504f3, 0x00b592e7, 0x00b6206c, 0x00b6ad7f, 0x00b73a23, 0x00b7c655, 0x00b85216, 0x00b8dd65,
  95. 0x00b96842, 0x00b9f2ac, 0x00ba7ca4, 0x00bb0629, 0x00bb8f3b, 0x00bc17d9, 0x00bca003, 0x00bd27b8,
  96. 0x00bdaef9, 0x00be35c5, 0x00bebc1b, 0x00bf41fc, 0x00bfc767, 0x00c04c5c, 0x00c0d0da, 0x00c154e1,
  97. 0x00c1d870, 0x00c25b89, 0x00c2de29, 0x00c36051, 0x00c3e200, 0x00c46337, 0x00c4e3f5, 0x00c56439,
  98. 0x00c5e403, 0x00c66354, 0x00c6e22a, 0x00c76085, 0x00c7de65, 0x00c85bca, 0x00c8d8b3, 0x00c95521,
  99. 0x00c9d112, 0x00ca4c87, 0x00cac77f, 0x00cb41fa, 0x00cbbbf8, 0x00cc3578, 0x00ccae79, 0x00cd26fd,
  100. 0x00cd9f02, 0x00ce1689, 0x00ce8d90, 0x00cf0417, 0x00cf7a1f, 0x00cfefa8, 0x00d064af, 0x00d0d937,
  101. 0x00d14d3d, 0x00d1c0c2, 0x00d233c6, 0x00d2a649, 0x00d31849, 0x00d389c7, 0x00d3fac3, 0x00d46b3b,
  102. 0x00d4db31, 0x00d54aa4, 0x00d5b993, 0x00d627fe, 0x00d695e5, 0x00d70348, 0x00d77026, 0x00d7dc7f,
  103. 0x00d84853, 0x00d8b3a1, 0x00d91e6a, 0x00d988ad, 0x00d9f26a, 0x00da5ba0, 0x00dac450, 0x00db2c79,
  104. 0x00db941a, 0x00dbfb34, 0x00dc61c7, 0x00dcc7d1, 0x00dd2d53, 0x00dd924d, 0x00ddf6be, 0x00de5aa6,
  105. 0x00debe05, 0x00df20db, 0x00df8327, 0x00dfe4e9, 0x00e04621, 0x00e0a6cf, 0x00e106f2, 0x00e1668a,
  106. 0x00e1c598, 0x00e2241a, 0x00e28210, 0x00e2df7b, 0x00e33c5a, 0x00e398ac, 0x00e3f473, 0x00e44fac,
  107. 0x00e4aa59, 0x00e50479, 0x00e55e0b, 0x00e5b710, 0x00e60f88, 0x00e66771, 0x00e6becc, 0x00e71599,
  108. 0x00e76bd8, 0x00e7c187, 0x00e816a8, 0x00e86b39, 0x00e8bf3c, 0x00e912ae, 0x00e96591, 0x00e9b7e4,
  109. 0x00ea09a7, 0x00ea5ad9, 0x00eaab7b, 0x00eafb8c, 0x00eb4b0c, 0x00eb99fb, 0x00ebe858, 0x00ec3624,
  110. 0x00ec835e, 0x00ecd007, 0x00ed1c1d, 0x00ed67a1, 0x00edb293, 0x00edfcf2, 0x00ee46be, 0x00ee8ff8,
  111. 0x00eed89e, 0x00ef20b0, 0x00ef6830, 0x00efaf1b, 0x00eff573, 0x00f03b37, 0x00f08066, 0x00f0c501,
  112. 0x00f10908, 0x00f14c7a, 0x00f18f57, 0x00f1d19f, 0x00f21352, 0x00f25470, 0x00f294f8, 0x00f2d4eb,
  113. 0x00f31447, 0x00f3530e, 0x00f3913f, 0x00f3ced9, 0x00f40bdd, 0x00f4484b, 0x00f48422, 0x00f4bf62,
  114. 0x00f4fa0b, 0x00f5341d, 0x00f56d97, 0x00f5a67b, 0x00f5dec6, 0x00f6167a, 0x00f64d97, 0x00f6841b,
  115. 0x00f6ba07, 0x00f6ef5b, 0x00f72417, 0x00f7583a, 0x00f78bc5, 0x00f7beb7, 0x00f7f110, 0x00f822d1,
  116. 0x00f853f8, 0x00f88486, 0x00f8b47b, 0x00f8e3d6, 0x00f91298, 0x00f940c0, 0x00f96e4e, 0x00f99b43,
  117. 0x00f9c79d, 0x00f9f35e, 0x00fa1e84, 0x00fa4910, 0x00fa7302, 0x00fa9c59, 0x00fac516, 0x00faed37,
  118. 0x00fb14be, 0x00fb3bab, 0x00fb61fc, 0x00fb87b2, 0x00fbaccd, 0x00fbd14d, 0x00fbf531, 0x00fc187a,
  119. 0x00fc3b28, 0x00fc5d3a, 0x00fc7eb0, 0x00fc9f8a, 0x00fcbfc9, 0x00fcdf6c, 0x00fcfe73, 0x00fd1cdd,
  120. 0x00fd3aac, 0x00fd57de, 0x00fd7474, 0x00fd906e, 0x00fdabcc, 0x00fdc68c, 0x00fde0b1, 0x00fdfa38,
  121. 0x00fe1324, 0x00fe2b72, 0x00fe4323, 0x00fe5a38, 0x00fe70b0, 0x00fe868b, 0x00fe9bc9, 0x00feb069,
  122. 0x00fec46d, 0x00fed7d4, 0x00feea9d, 0x00fefcc9, 0x00ff0e58, 0x00ff1f49, 0x00ff2f9d, 0x00ff3f54,
  123. 0x00ff4e6d, 0x00ff5ce9, 0x00ff6ac7, 0x00ff7808, 0x00ff84ab, 0x00ff90b1, 0x00ff9c18, 0x00ffa6e3,
  124. 0x00ffb10f, 0x00ffba9e, 0x00ffc38f, 0x00ffcbe2, 0x00ffd397, 0x00ffdaaf, 0x00ffe129, 0x00ffe705,
  125. 0x00ffec43, 0x00fff0e3, 0x00fff4e6, 0x00fff84a, 0x00fffb11, 0x00fffd39, 0x00fffec4, 0x00ffffb1,
  126. 0x00ffffff,
  127. };
  128. #else
  129. extern const int sf_sin_tab1[513] ;
  130. #endif
  131. #define SINE_INT_ZOOM 16384
  132. #define SINE_INT_ZBIT 14
  133. #define __int64 long long
  134. /*软件索引实现sin生成*/
  135. static int get_sine_value(int mx_idx)
  136. {
  137. int ret = 0;
  138. int idx = 0;
  139. int tm_idx = mx_idx & 0x1FFFFFF; //2^25
  140. int phase = tm_idx & 0x3FFF;
  141. int tp_idx0 = tm_idx >> 14;
  142. int tp_idx1 = tp_idx0 + 1;
  143. int sign = 1, dt0, dt1;
  144. if (tp_idx0 > 1024) {
  145. sign = -1;
  146. tp_idx0 = 2048 - tp_idx0;
  147. }
  148. if (tp_idx0 < 513) {
  149. dt0 = sf_sin_tab1[tp_idx0];
  150. } else {
  151. dt0 = sf_sin_tab1[1024 - tp_idx0];
  152. }
  153. if (tp_idx1 > 1024) {
  154. sign = -1;
  155. tp_idx1 = 2048 - tp_idx1;
  156. }
  157. if (tp_idx1 < 513) {
  158. dt1 = sf_sin_tab1[tp_idx1];
  159. } else {
  160. dt1 = sf_sin_tab1[1024 - tp_idx1];
  161. }
  162. ret = ((__int64)dt0 * (SINE_INT_ZOOM - phase) + (__int64)dt1 * phase) >> SINE_INT_ZBIT;
  163. ret *= sign;
  164. return ret;
  165. }
  166. static void hw_sin_value(int a, int *sin_res, u8 precision)
  167. {
  168. u64 s64 = a;
  169. *sin_res = __asm_sine(s64, precision);
  170. }
  171. int sin_tone_make(void *_maker, void *data, int len)
  172. {
  173. struct audio_sin_maker *maker = (struct audio_sin_maker *)_maker;
  174. s16 *pcm = (s16 *)data;
  175. int sin_value = 0;
  176. int win_sin_value = 0;
  177. int add_idx = 0, sub_vol = 0, win_add_idx = 0;
  178. int offset = 0;
  179. u8 id = maker->id;
  180. u8 sin_num = maker->sin_num;
  181. u8 repeat = maker->repeat;
  182. u8 channel = maker->channel;
  183. int sin_index;
  184. int win_sin_index;
  185. int volume;
  186. u32 reamin_points = len / 2 / channel;
  187. do {
  188. if (maker->next_param) {
  189. maker->volume = SINE_TOTAL_VOLUME;
  190. maker->sin_index = 0;
  191. maker->win_sin_index = 0;
  192. maker->next_param = 0;
  193. }
  194. u8 win = maker->sin[id].win;
  195. add_idx = ((u64)(1 << 25) * maker->sin[id].freq / DEFAULT_SINE_SAMPLE_RATE) >> 9;
  196. if (win) {
  197. win_add_idx = ((u64)(1 << 25) * maker->sin[id].decay / DEFAULT_SINE_SAMPLE_RATE) >> 9;
  198. /*sub_vol = 0;*/
  199. } else {
  200. sub_vol = maker->sin[id].decay;
  201. }
  202. sin_index = maker->sin_index;
  203. win_sin_index = maker->win_sin_index;
  204. volume = maker->volume;
  205. u32 points = 0;
  206. if (maker->fade_points) {
  207. points = maker->fade_points > reamin_points ? reamin_points : maker->fade_points;
  208. sub_vol = 0;
  209. maker->fade_points -= points;
  210. } else {
  211. points = maker->points > reamin_points ? reamin_points : maker->points;
  212. maker->points -= points;
  213. }
  214. reamin_points -= points;
  215. while (points--) {
  216. /*hw_sin_value(sin_index, &sin_value, 0);*/
  217. sin_value = __asm_sine((s64)sin_index, 2);
  218. if (win) {
  219. /*hw_sin_value(win_sin_index, &win_sin_value, 0);*/
  220. win_sin_value = __asm_sine((s64)win_sin_index, 2);
  221. #if ((defined CONFIG_CPU_BR36) || (defined CONFIG_CPU_BR28))
  222. sin_value = ((s64)sin_value * (s64)win_sin_value) >> 44;
  223. #else
  224. sin_value = ((s64)sin_value * (s64)win_sin_value) >> 34;
  225. #endif
  226. win_sin_index += win_add_idx;
  227. win_sin_index &= 0x1ffffff;
  228. } else {
  229. #if ((defined CONFIG_CPU_BR36) || (defined CONFIG_CPU_BR28))
  230. sin_value = ((s64)volume * sin_value) >> 39;
  231. #else
  232. sin_value = ((s64)volume * sin_value) >> 34;
  233. #endif
  234. }
  235. sin_index += add_idx;
  236. sin_index &= 0x1ffffff;
  237. volume -= sub_vol;
  238. if (volume < 0) {
  239. volume = 0;
  240. }
  241. *pcm++ = sin_value;
  242. if (channel == 2) {
  243. *pcm++ = sin_value;
  244. } else if (channel == 4) {
  245. *pcm++ = sin_value;
  246. *pcm++ = sin_value;
  247. *pcm++ = sin_value;
  248. }
  249. }
  250. maker->volume = volume;
  251. maker->sin_index = sin_index;
  252. maker->win_sin_index = win_sin_index;
  253. if (!maker->points) {
  254. if (++id >= sin_num) {
  255. if (!repeat) {
  256. break;
  257. }
  258. id = 0;
  259. }
  260. maker->points = maker->sin[id].points;
  261. maker->id = id;
  262. maker->next_param = 1;
  263. }
  264. if (!reamin_points) {
  265. break;
  266. }
  267. } while (1);
  268. return len - (reamin_points * 2 * channel);
  269. }
  270. int sin_tone_points(void *_maker)
  271. {
  272. struct audio_sin_maker *maker = (struct audio_sin_maker *)_maker;
  273. int points = 0;
  274. u8 i = 0;
  275. for (i = 0; i < maker->sin_num; i++) {
  276. points += maker->sin[i].points;
  277. }
  278. return points;
  279. }
  280. void sin_tone_close(void *_maker)
  281. {
  282. struct audio_sin_maker *maker = (struct audio_sin_maker *)_maker;
  283. #ifdef SINE_USE_MALLOC
  284. if (maker) {
  285. free(maker);
  286. }
  287. #else
  288. if (sin_maker_handle.open) {
  289. sin_maker_handle.open = 0;
  290. }
  291. #endif
  292. }
  293. #if 0
  294. #include "MathFunc_float.h"
  295. #ifndef DATA16
  296. #define DATA16 32767
  297. #endif
  298. /*********************************
  299. * fc : 正弦波中心频率
  300. * fs : 采样频率
  301. * FrameSize :每次计算输出点数
  302. * idx :当前计算起始indix
  303. * rst :结果存放地址
  304. * *******************************/
  305. void SinWave_Generator(int fc, int fs, int FrameSize, int idx, short *rst)
  306. {
  307. float tmp0, tmp1, tmp2;
  308. tmp0 = (float)fc / fs;
  309. for (int i = 0; i < FrameSize; i++) {
  310. tmp1 = (i + idx) * tmp0 * 2;
  311. sin_float(tmp1, &tmp2);
  312. *rst = (short)(tmp2 * DATA16);
  313. rst++;
  314. }
  315. }
  316. /*********************************
  317. * fs : 采样频率
  318. * FrameSize :每次计算输出点数
  319. * idx :当前计算起始indix
  320. * ts :期望一次扫频所持续时间
  321. * rst :结果存放地址
  322. * *******************************/
  323. void SweepSin_Generator(int fs, int FrameSize, int idx, float ts, short *rst)
  324. {
  325. float fp, fc, tmp1, tmp2;
  326. int Ncnt, NPoint, DPoint;
  327. NPoint = fs * ts;
  328. // fp = (fs/2)/(fs*ts);
  329. fp = 1 / (2 * 2 * ts);
  330. for (int i = 0; i < FrameSize; i++) {
  331. Ncnt = (i + idx) / NPoint;
  332. DPoint = (i + idx) - Ncnt * NPoint;
  333. //printf("idx:%d \n",DPoint);
  334. tmp1 = ((DPoint * fp) / fs) * DPoint * 2;
  335. sin_float(tmp1, &tmp2);
  336. //printf("Sin[%d]:%d",i,(int)(tmp2*1000));
  337. *rst = (short)(tmp2 * DATA16);
  338. rst++;
  339. }
  340. }
  341. int sin_idx = 0;
  342. void sin_pcm_fill(void *buf, u32 len)
  343. {
  344. SinWave_Generator(1000, 16000, len / 2, sin_idx, buf);
  345. sin_idx += len / 2;
  346. }
  347. void sweepsin_pcm_fill(void *buf, u32 len)
  348. {
  349. SweepSin_Generator(16000, len / 2, sin_idx, 25, buf);
  350. sin_idx += len / 2;
  351. }
  352. #endif