打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MyMinimad ── Linux下用libmad写的mp3解码播放程序

程序说明:其实本来应该是在output函数中设置采样率和声道数的,但有莫名奇妙的问题。

所以定了个一般化的

#define SAMPLE_RATE 44100
#define CHANNELS 2
#define PCM_DEVICE "plughw:0,0"

即:mp3的采样率为44100Hz,声道数为2(立体声)

源代码:

  1. /*
  2. * 本程序是从 minimad 改进而来,如要更详细的说明请参看 minimad.c
  3. *
  4. * MyMinimad.c , 2009/12/25 , SiChuan University , China
  5. *
  6. * 编译: gcc MyMinimad.c -o MyMinimad -lmad -lasound
  7. * 运行: ./MyMinimad filename.mp3
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <sys/stat.h>
  13. #include <sys/mman.h>
  14. #include <sys/soundcard.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/fcntl.h>
  17. #include <sys/types.h>
  18. #include <mad.h>
  19. #include <alsa/asoundlib.h>
  20. #define SAMPLE_RATE 44100
  21. #define CHANNELS 2
  22. #define PCM_DEVICE "plughw:0,0"
  23. static snd_pcm_hw_params_t *hwparams = NULL;
  24. static snd_pcm_t *pcm_handle = NULL;
  25. struct buffer
  26. {
  27. unsigned char const *start;
  28. unsigned long length;
  29. };
  30. static int decode (unsigned char const *, unsigned long);
  31. static int init_alsa ();
  32. int main (int argc, char *argv[])
  33. {
  34. struct stat stat;
  35. void *fdm;
  36. char const *file;
  37. int fd;
  38. file = argv[1];
  39. fd = open (file, O_RDONLY);
  40. if (fstat (fd, &stat) == -1 || stat.st_size == 0)
  41. return -1;
  42. fdm = mmap (0, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
  43. if (fdm == MAP_FAILED)
  44. return -1;
  45. if (init_alsa () == -1)
  46. {
  47. fprintf (stderr, "init_alsa() error/n");
  48. return -1;
  49. }
  50. decode (fdm, stat.st_size);
  51. if (munmap (fdm, stat.st_size) == -1)
  52. return -1;
  53. return 0;
  54. }
  55. static int init_alsa ()
  56. {
  57. snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
  58. char *pcm_name;
  59. int rate = SAMPLE_RATE; /* Sample rate */
  60. int exact_rate; /* Sample rate returned by */
  61. pcm_name = strdup (PCM_DEVICE);
  62. snd_pcm_hw_params_alloca (&hwparams);
  63. if (snd_pcm_open (&pcm_handle, pcm_name, stream, 0) < 0)
  64. {
  65. fprintf (stderr, "Error opening PCM device %s/n", pcm_name);
  66. return -1;
  67. }
  68. if (snd_pcm_hw_params_any (pcm_handle, hwparams) < 0)
  69. {
  70. fprintf (stderr, "Can not configure this PCM device./n");
  71. return -1;
  72. }
  73. if (snd_pcm_hw_params_set_access (pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
  74. {
  75. fprintf (stderr, "Error setting access./n");
  76. return -1;
  77. }
  78. if (snd_pcm_hw_params_set_format (pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE) < 0)
  79. {
  80. fprintf (stderr, "Error setting format./n");
  81. return -1;
  82. }
  83. exact_rate = rate;
  84. if (snd_pcm_hw_params_set_rate_near (pcm_handle, hwparams, &exact_rate, 0) < 0)
  85. {
  86. fprintf (stderr, "Error setting rate./n");
  87. return -1;
  88. }
  89. if (rate != exact_rate)
  90. {
  91. fprintf (stderr, "The rate %d Hz is not supported by your hardware./n==> Using %d Hz instead./n", rate, exact_rate);
  92. }
  93. if (snd_pcm_hw_params_set_channels (pcm_handle, hwparams, CHANNELS) < 0)
  94. {
  95. fprintf (stderr, "Error setting channels./n");
  96. return -1;
  97. }
  98. if (snd_pcm_hw_params (pcm_handle, hwparams) < 0)
  99. {
  100. fprintf (stderr, "Error setting HW params./n");
  101. return -1;
  102. }
  103. return 0;
  104. }

  105. static enum mad_flow input (void *data, struct mad_stream *stream)
  106. {
  107. struct buffer *buffer = data;
  108. if (!buffer->length)
  109. return MAD_FLOW_STOP;
  110. mad_stream_buffer (stream, buffer->start, buffer->length);
  111. buffer->length = 0;
  112. return MAD_FLOW_CONTINUE;
  113. }
  114. /*这一段是处理采样后的pcm音频 */
  115. static inline signed int scale (mad_fixed_t sample)
  116. {
  117. sample += (1L << (MAD_F_FRACBITS - 16));
  118. if (sample >= MAD_F_ONE)
  119. sample = MAD_F_ONE - 1;
  120. else if (sample < -MAD_F_ONE)
  121. sample = -MAD_F_ONE;
  122. return sample >> (MAD_F_FRACBITS + 1 - 16);
  123. }
  124. static enum mad_flow output (void *data, struct mad_header const *header, struct mad_pcm *pcm)
  125. {
  126. unsigned int nchannels, nsamples, n;
  127. mad_fixed_t const *left_ch, *right_ch;
  128. unsigned char Output[6912], *OutputPtr;
  129. int fmt, wrote, speed, exact_rate, err, dir;
  130. nchannels = pcm->channels;
  131. n = nsamples = pcm->length;
  132. left_ch = pcm->samples[0];
  133. right_ch = pcm->samples[1];
  134. fmt = AFMT_S16_LE;
  135. speed = pcm->samplerate * 2; /*播放速度是采样率的两倍 */
  136. OutputPtr = Output;
  137. while (nsamples--)
  138. {
  139. signed int sample;
  140. sample = scale (*left_ch++);
  141. *(OutputPtr++) = sample >> 0;
  142. *(OutputPtr++) = sample >> 8;
  143. if (nchannels == 2)
  144. {
  145. sample = scale (*right_ch++);
  146. *(OutputPtr++) = sample >> 0;
  147. *(OutputPtr++) = sample >> 8;
  148. }
  149. }
  150. OutputPtr = Output;
  151. snd_pcm_writei (pcm_handle, OutputPtr, n);
  152. OutputPtr = Output;
  153. return MAD_FLOW_CONTINUE;
  154. }
  155. static enum mad_flow error (void *data, struct mad_stream *stream, struct mad_frame *frame)
  156. {
  157. return MAD_FLOW_CONTINUE;
  158. }
  159. static int decode (unsigned char const *start, unsigned long length)
  160. {
  161. struct buffer buffer;
  162. struct mad_decoder decoder;
  163. int result;
  164. buffer.start = start;
  165. buffer.length = length;
  166. mad_decoder_init (&decoder, &buffer, input, 0, 0, output, error, 0);
  167. mad_decoder_options (&decoder, 0);
  168. result = mad_decoder_run (&decoder, MAD_DECODER_MODE_SYNC);
  169. mad_decoder_finish (&decoder);
  170. return result;
  171. }


 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ALSA声音编程介绍(译文)
基于ALSA的WAV播放和录音程序
ALSA Audio API手册
alsa用户空间编程
基于 libmad 的简单 MP3 流媒体播放器的实现
Linux音频驱动构架及音频设备简单测试方法 .
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服