小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

MyMinimad ── Linux下用libmad寫的mp3解碼播放程序

 felwell 2018-07-02

程序說明:其實本來應(yīng)該是在output函數(shù)中設(shè)置采樣率和聲道數(shù)的,但有莫名奇妙的問題。

所以定了個一般化的

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

即:mp3的采樣率為44100Hz,聲道數(shù)為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. }


 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多