PageRenderTime 918ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/user/mpd/mpd-0.13.1/src/pcm_utils.c

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public
C | 470 lines | 385 code | 60 blank | 25 comment | 59 complexity | d68c06fdf07f875165f04092a72d5383 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, Unlicense, GPL-2.0, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0, ISC, MIT, 0BSD, LGPL-2.0
  1. /* the Music Player Daemon (MPD)
  2. * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
  3. * This project's homepage is: http://www.musicpd.org
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "pcm_utils.h"
  19. #include "mpd_types.h"
  20. #include "log.h"
  21. #include "utils.h"
  22. #include "conf.h"
  23. #include <string.h>
  24. #include <math.h>
  25. #include <assert.h>
  26. void pcm_volumeChange(char *buffer, int bufferSize, AudioFormat * format,
  27. int volume)
  28. {
  29. mpd_sint32 temp32;
  30. mpd_sint8 *buffer8 = (mpd_sint8 *) buffer;
  31. mpd_sint16 *buffer16 = (mpd_sint16 *) buffer;
  32. if (volume >= 1000)
  33. return;
  34. if (volume <= 0) {
  35. memset(buffer, 0, bufferSize);
  36. return;
  37. }
  38. switch (format->bits) {
  39. case 16:
  40. while (bufferSize > 0) {
  41. temp32 = *buffer16;
  42. temp32 *= volume;
  43. temp32 += rand() & 511;
  44. temp32 -= rand() & 511;
  45. temp32 += 500;
  46. temp32 /= 1000;
  47. *buffer16 = temp32 > 32767 ? 32767 :
  48. (temp32 < -32768 ? -32768 : temp32);
  49. buffer16++;
  50. bufferSize -= 2;
  51. }
  52. break;
  53. case 8:
  54. while (bufferSize > 0) {
  55. temp32 = *buffer8;
  56. temp32 *= volume;
  57. temp32 += rand() & 511;
  58. temp32 -= rand() & 511;
  59. temp32 += 500;
  60. temp32 /= 1000;
  61. *buffer8 = temp32 > 127 ? 127 :
  62. (temp32 < -128 ? -128 : temp32);
  63. buffer8++;
  64. bufferSize--;
  65. }
  66. break;
  67. default:
  68. FATAL("%i bits not supported by pcm_volumeChange!\n",
  69. format->bits);
  70. }
  71. }
  72. static void pcm_add(char *buffer1, char *buffer2, size_t bufferSize1,
  73. size_t bufferSize2, int vol1, int vol2,
  74. AudioFormat * format)
  75. {
  76. mpd_sint32 temp32;
  77. mpd_sint8 *buffer8_1 = (mpd_sint8 *) buffer1;
  78. mpd_sint8 *buffer8_2 = (mpd_sint8 *) buffer2;
  79. mpd_sint16 *buffer16_1 = (mpd_sint16 *) buffer1;
  80. mpd_sint16 *buffer16_2 = (mpd_sint16 *) buffer2;
  81. switch (format->bits) {
  82. case 16:
  83. while (bufferSize1 > 0 && bufferSize2 > 0) {
  84. temp32 =
  85. (vol1 * (*buffer16_1) +
  86. vol2 * (*buffer16_2));
  87. temp32 += rand() & 511;
  88. temp32 -= rand() & 511;
  89. temp32 += 500;
  90. temp32 /= 1000;
  91. *buffer16_1 =
  92. temp32 > 32767 ? 32767 : (temp32 <
  93. -32768 ? -32768 : temp32);
  94. buffer16_1++;
  95. buffer16_2++;
  96. bufferSize1 -= 2;
  97. bufferSize2 -= 2;
  98. }
  99. if (bufferSize2 > 0)
  100. memcpy(buffer16_1, buffer16_2, bufferSize2);
  101. break;
  102. case 8:
  103. while (bufferSize1 > 0 && bufferSize2 > 0) {
  104. temp32 =
  105. (vol1 * (*buffer8_1) + vol2 * (*buffer8_2));
  106. temp32 += rand() & 511;
  107. temp32 -= rand() & 511;
  108. temp32 += 500;
  109. temp32 /= 1000;
  110. *buffer8_1 =
  111. temp32 > 127 ? 127 : (temp32 <
  112. -128 ? -128 : temp32);
  113. buffer8_1++;
  114. buffer8_2++;
  115. bufferSize1--;
  116. bufferSize2--;
  117. }
  118. if (bufferSize2 > 0)
  119. memcpy(buffer8_1, buffer8_2, bufferSize2);
  120. break;
  121. default:
  122. FATAL("%i bits not supported by pcm_add!\n", format->bits);
  123. }
  124. }
  125. void pcm_mix(char *buffer1, char *buffer2, size_t bufferSize1,
  126. size_t bufferSize2, AudioFormat * format, float portion1)
  127. {
  128. int vol1;
  129. float s = sin(M_PI_2 * portion1);
  130. s *= s;
  131. vol1 = s * 1000 + 0.5;
  132. vol1 = vol1 > 1000 ? 1000 : (vol1 < 0 ? 0 : vol1);
  133. pcm_add(buffer1, buffer2, bufferSize1, bufferSize2, vol1, 1000 - vol1,
  134. format);
  135. }
  136. #ifdef HAVE_LIBSAMPLERATE
  137. static int pcm_getSampleRateConverter(void)
  138. {
  139. const char *conf = getConfigParamValue(CONF_SAMPLERATE_CONVERTER);
  140. long convalgo;
  141. char *test;
  142. size_t len;
  143. if (!conf) {
  144. convalgo = SRC_SINC_FASTEST;
  145. goto out;
  146. }
  147. convalgo = strtol(conf, &test, 10);
  148. if (*test == '\0' && src_get_name(convalgo))
  149. goto out;
  150. len = strlen(conf);
  151. for (convalgo = 0 ; ; convalgo++) {
  152. test = (char *)src_get_name(convalgo);
  153. if (!test) {
  154. convalgo = SRC_SINC_FASTEST;
  155. break;
  156. }
  157. if (strncasecmp(test, conf, len) == 0)
  158. goto out;
  159. }
  160. ERROR("unknown samplerate converter \"%s\"\n", conf);
  161. out:
  162. DEBUG("selecting samplerate converter \"%s\"\n",
  163. src_get_name(convalgo));
  164. return convalgo;
  165. }
  166. #endif
  167. #ifdef HAVE_LIBSAMPLERATE
  168. static size_t pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
  169. char *inBuffer, size_t inSize,
  170. mpd_uint32 outSampleRate, char *outBuffer,
  171. size_t outSize, ConvState *convState)
  172. {
  173. static int convalgo = -1;
  174. SRC_DATA *data = &convState->data;
  175. size_t dataInSize;
  176. size_t dataOutSize;
  177. int error;
  178. if (convalgo < 0)
  179. convalgo = pcm_getSampleRateConverter();
  180. /* (re)set the state/ratio if the in or out format changed */
  181. if ((channels != convState->lastChannels) ||
  182. (inSampleRate != convState->lastInSampleRate) ||
  183. (outSampleRate != convState->lastOutSampleRate)) {
  184. convState->error = 0;
  185. convState->lastChannels = channels;
  186. convState->lastInSampleRate = inSampleRate;
  187. convState->lastOutSampleRate = outSampleRate;
  188. if (convState->state)
  189. convState->state = src_delete(convState->state);
  190. convState->state = src_new(convalgo, channels, &error);
  191. if (!convState->state) {
  192. ERROR("cannot create new libsamplerate state: %s\n",
  193. src_strerror(error));
  194. convState->error = 1;
  195. return 0;
  196. }
  197. data->src_ratio = (double)outSampleRate / (double)inSampleRate;
  198. DEBUG("setting samplerate conversion ratio to %.2lf\n",
  199. data->src_ratio);
  200. src_set_ratio(convState->state, data->src_ratio);
  201. }
  202. /* there was an error previously, and nothing has changed */
  203. if (convState->error)
  204. return 0;
  205. data->input_frames = inSize / 2 / channels;
  206. dataInSize = data->input_frames * sizeof(float) * channels;
  207. if (dataInSize > convState->dataInSize) {
  208. convState->dataInSize = dataInSize;
  209. data->data_in = xrealloc(data->data_in, dataInSize);
  210. }
  211. data->output_frames = outSize / 2 / channels;
  212. dataOutSize = data->output_frames * sizeof(float) * channels;
  213. if (dataOutSize > convState->dataOutSize) {
  214. convState->dataOutSize = dataOutSize;
  215. data->data_out = xrealloc(data->data_out, dataOutSize);
  216. }
  217. src_short_to_float_array((short *)inBuffer, data->data_in,
  218. data->input_frames * channels);
  219. error = src_process(convState->state, data);
  220. if (error) {
  221. ERROR("error processing samples with libsamplerate: %s\n",
  222. src_strerror(error));
  223. convState->error = 1;
  224. return 0;
  225. }
  226. src_float_to_short_array(data->data_out, (short *)outBuffer,
  227. data->output_frames_gen * channels);
  228. return data->output_frames_gen * 2 * channels;
  229. }
  230. #else /* !HAVE_LIBSAMPLERATE */
  231. /* resampling code blatantly ripped from ESD */
  232. static size_t pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
  233. char *inBuffer, size_t inSize,
  234. mpd_uint32 outSampleRate, char *outBuffer,
  235. size_t outSize, ConvState *convState)
  236. {
  237. mpd_uint32 rd_dat = 0;
  238. mpd_uint32 wr_dat = 0;
  239. mpd_sint16 *in = (mpd_sint16 *)inBuffer;
  240. mpd_sint16 *out = (mpd_sint16 *)outBuffer;
  241. mpd_uint32 nlen = outSize / 2;
  242. mpd_sint16 lsample, rsample;
  243. switch (channels) {
  244. case 1:
  245. while (wr_dat < nlen) {
  246. rd_dat = wr_dat * inSampleRate / outSampleRate;
  247. lsample = in[rd_dat++];
  248. out[wr_dat++] = lsample;
  249. }
  250. break;
  251. case 2:
  252. while (wr_dat < nlen) {
  253. rd_dat = wr_dat * inSampleRate / outSampleRate;
  254. rd_dat &= ~1;
  255. lsample = in[rd_dat++];
  256. rsample = in[rd_dat++];
  257. out[wr_dat++] = lsample;
  258. out[wr_dat++] = rsample;
  259. }
  260. break;
  261. }
  262. return outSize;
  263. }
  264. #endif /* !HAVE_LIBSAMPLERATE */
  265. static char *pcm_convertChannels(mpd_sint8 channels, char *inBuffer,
  266. size_t inSize, size_t *outSize)
  267. {
  268. static char *buf;
  269. static size_t len;
  270. char *outBuffer = NULL;
  271. mpd_sint16 *in;
  272. mpd_sint16 *out;
  273. int inSamples, i;
  274. switch (channels) {
  275. /* convert from 1 -> 2 channels */
  276. case 1:
  277. *outSize = (inSize >> 1) << 2;
  278. if (*outSize > len) {
  279. len = *outSize;
  280. buf = xrealloc(buf, len);
  281. }
  282. outBuffer = buf;
  283. inSamples = inSize >> 1;
  284. in = (mpd_sint16 *)inBuffer;
  285. out = (mpd_sint16 *)outBuffer;
  286. for (i = 0; i < inSamples; i++) {
  287. *out++ = *in;
  288. *out++ = *in++;
  289. }
  290. break;
  291. /* convert from 2 -> 1 channels */
  292. case 2:
  293. *outSize = inSize >> 1;
  294. if (*outSize > len) {
  295. len = *outSize;
  296. buf = xrealloc(buf, len);
  297. }
  298. outBuffer = buf;
  299. inSamples = inSize >> 2;
  300. in = (mpd_sint16 *)inBuffer;
  301. out = (mpd_sint16 *)outBuffer;
  302. for (i = 0; i < inSamples; i++) {
  303. *out = (*in++) / 2;
  304. *out++ += (*in++) / 2;
  305. }
  306. break;
  307. default:
  308. ERROR("only 1 or 2 channels are supported for conversion!\n");
  309. }
  310. return outBuffer;
  311. }
  312. static char *pcm_convertTo16bit(mpd_sint8 bits, char *inBuffer, size_t inSize,
  313. size_t *outSize)
  314. {
  315. static char *buf;
  316. static size_t len;
  317. char *outBuffer = NULL;
  318. mpd_sint8 *in;
  319. mpd_sint16 *out;
  320. int i;
  321. switch (bits) {
  322. case 8:
  323. *outSize = inSize << 1;
  324. if (*outSize > len) {
  325. len = *outSize;
  326. buf = xrealloc(buf, len);
  327. }
  328. outBuffer = buf;
  329. in = (mpd_sint8 *)inBuffer;
  330. out = (mpd_sint16 *)outBuffer;
  331. for (i = 0; i < inSize; i++)
  332. *out++ = (*in++) << 8;
  333. break;
  334. case 16:
  335. *outSize = inSize;
  336. outBuffer = inBuffer;
  337. break;
  338. case 24:
  339. /* put dithering code from mp3_decode here */
  340. default:
  341. ERROR("only 8 or 16 bits are supported for conversion!\n");
  342. }
  343. return outBuffer;
  344. }
  345. /* outFormat bits must be 16 and channels must be 1 or 2! */
  346. size_t pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
  347. size_t inSize, AudioFormat * outFormat,
  348. char *outBuffer, ConvState *convState)
  349. {
  350. char *buf;
  351. size_t len;
  352. size_t outSize = pcm_sizeOfConvBuffer(inFormat, inSize, outFormat);
  353. assert(outFormat->bits == 16);
  354. assert(outFormat->channels == 2 || outFormat->channels == 1);
  355. /* everything else supports 16 bit only, so convert to that first */
  356. buf = pcm_convertTo16bit(inFormat->bits, inBuffer, inSize, &len);
  357. if (!buf)
  358. exit(EXIT_FAILURE);
  359. if (inFormat->channels != outFormat->channels) {
  360. buf = pcm_convertChannels(inFormat->channels, buf, len, &len);
  361. if (!buf)
  362. exit(EXIT_FAILURE);
  363. }
  364. if (inFormat->sampleRate == outFormat->sampleRate) {
  365. assert(outSize >= len);
  366. memcpy(outBuffer, buf, len);
  367. } else {
  368. len = pcm_convertSampleRate(outFormat->channels,
  369. inFormat->sampleRate, buf, len,
  370. outFormat->sampleRate, outBuffer,
  371. outSize, convState);
  372. if (len == 0)
  373. exit(EXIT_FAILURE);
  374. }
  375. return len;
  376. }
  377. size_t pcm_sizeOfConvBuffer(AudioFormat * inFormat, size_t inSize,
  378. AudioFormat * outFormat)
  379. {
  380. const double ratio = (double)outFormat->sampleRate /
  381. (double)inFormat->sampleRate;
  382. const int shift = 2 * outFormat->channels;
  383. size_t outSize = inSize;
  384. switch (inFormat->bits) {
  385. case 8:
  386. outSize <<= 1;
  387. break;
  388. case 16:
  389. break;
  390. default:
  391. FATAL("only 8 or 16 bits are supported for conversion!\n");
  392. }
  393. if (inFormat->channels != outFormat->channels) {
  394. switch (inFormat->channels) {
  395. case 1:
  396. outSize = (outSize >> 1) << 2;
  397. break;
  398. case 2:
  399. outSize >>= 1;
  400. break;
  401. default:
  402. FATAL("only 1 or 2 channels are supported "
  403. "for conversion!\n");
  404. }
  405. }
  406. outSize /= shift;
  407. outSize = floor(0.5 + (double)outSize * ratio);
  408. outSize *= shift;
  409. return outSize;
  410. }