main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Simple mp3 player. Derived from libmad's example minimad.c.
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include "mad.h"
  7. /* Current input file */
  8. char *name;
  9. vlong offset;
  10. int rate = 44100;
  11. char *outfile;
  12. int vfd; /* /dev/volume */
  13. static enum mad_flow
  14. input(void *data, struct mad_stream *stream)
  15. {
  16. int fd, n, m;
  17. static uchar buf[32768];
  18. fd = (int)data;
  19. n = stream->bufend - stream->next_frame;
  20. memmove(buf, stream->next_frame, n);
  21. m = read(fd, buf+n, sizeof buf-n);
  22. offset += m;
  23. if(m < 0)
  24. sysfatal("reading input: %r");
  25. if(m == 0)
  26. return MAD_FLOW_STOP;
  27. n += m;
  28. mad_stream_buffer(stream, buf, n);
  29. return MAD_FLOW_CONTINUE;
  30. }
  31. /*
  32. * Dither 28-bit down to 16-bit. From mpg321.
  33. * I'm skeptical, but it's supposed to make the
  34. * samples sound better than just truncation.
  35. */
  36. typedef struct Dither Dither;
  37. struct Dither
  38. {
  39. mad_fixed_t error[3];
  40. mad_fixed_t random;
  41. };
  42. #define PRNG(x) (((x)*0x19660dL + 0x3c6ef35fL) & 0xffffffffL)
  43. enum
  44. {
  45. FracBits = MAD_F_FRACBITS,
  46. OutBits = 16,
  47. Round = 1 << (FracBits+1-OutBits-1), // sic
  48. ScaleBits = FracBits + 1 - OutBits,
  49. LowMask = (1<<ScaleBits) - 1,
  50. Min = -MAD_F_ONE,
  51. Max = MAD_F_ONE - 1,
  52. };
  53. int
  54. audiodither(mad_fixed_t v, Dither *d)
  55. {
  56. int out;
  57. mad_fixed_t random;
  58. /* noise shape */
  59. v += d->error[0] - d->error[1] + d->error[2];
  60. d->error[2] = d->error[1];
  61. d->error[1] = d->error[0] / 2;
  62. /* bias */
  63. out = v + Round;
  64. /* dither */
  65. random = PRNG(d->random);
  66. out += (random & LowMask) - (d->random & LowMask);
  67. d->random = random;
  68. /* clip */
  69. if(out > Max){
  70. out = Max;
  71. if(v > Max)
  72. v = Max;
  73. }else if(out < Min){
  74. out = Min;
  75. if(v < Min)
  76. v = Min;
  77. }
  78. /* quantize */
  79. out &= ~LowMask;
  80. /* error feedback */
  81. d->error[0] = v - out;
  82. /* scale */
  83. return out >> ScaleBits;
  84. }
  85. static enum mad_flow
  86. output(void *data, struct mad_header const* header, struct mad_pcm *pcm)
  87. {
  88. int i, n, v;
  89. mad_fixed_t const *left, *right;
  90. static Dither d;
  91. static uchar buf[16384], *p;
  92. if(pcm->samplerate != rate){
  93. rate = pcm->samplerate;
  94. if(vfd < 0)
  95. fprint(2, "warning: audio sample rate is %d Hz\n", rate);
  96. else
  97. fprint(vfd, "speed %d", rate);
  98. }
  99. p = buf;
  100. memset(&d, 0, sizeof d);
  101. n = pcm->length;
  102. switch(pcm->channels){
  103. case 1:
  104. left = pcm->samples[0];
  105. for(i=0; i<n; i++){
  106. v = audiodither(*left++, &d);
  107. /* stereoize */
  108. *p++ = v;
  109. *p++ = v>>8;
  110. *p++ = v;
  111. *p++ = v>>8;
  112. }
  113. break;
  114. case 2:
  115. left = pcm->samples[0];
  116. right = pcm->samples[1];
  117. for(i=0; i<n; i++){
  118. v = audiodither(*left++, &d);
  119. *p++ = v;
  120. *p++ = v>>8;
  121. v = audiodither(*right++, &d);
  122. *p++ = v;
  123. *p++ = v>>8;
  124. }
  125. break;
  126. }
  127. assert(p<=buf+sizeof buf);
  128. write(1, buf, p-buf);
  129. return MAD_FLOW_CONTINUE;
  130. }
  131. static enum mad_flow
  132. error(void *data, struct mad_stream *stream, struct mad_frame *frame)
  133. {
  134. if(stream->error == MAD_ERROR_LOSTSYNC)
  135. return MAD_FLOW_CONTINUE;
  136. sysfatal("%s:#%lld: %s",
  137. name, offset-(stream->bufend-stream->next_frame),
  138. mad_stream_errorstr(stream));
  139. return 0;
  140. }
  141. void
  142. play(int fd, char *nam)
  143. {
  144. struct mad_decoder decoder;
  145. name = nam;
  146. mad_decoder_init(&decoder, (void*)fd, input, nil, nil, output, error, nil);
  147. mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
  148. mad_decoder_finish(&decoder);
  149. }
  150. void
  151. usage(void)
  152. {
  153. fprint(2, "usage: mp3dec [-o outfile] [file...]\n");
  154. exits("usage");
  155. }
  156. void
  157. main(int argc, char **argv)
  158. {
  159. int i, fd;
  160. char *p;
  161. ARGBEGIN{
  162. case 'o':
  163. outfile = EARGF(usage());
  164. break;
  165. default:
  166. usage();
  167. }ARGEND
  168. if(outfile){
  169. if((fd = create(outfile, OWRITE, 0666)) < 0)
  170. sysfatal("create %s: %r", outfile);
  171. }else{
  172. if((fd = open("/dev/audio", OWRITE)) < 0)
  173. sysfatal("open /dev/audio: %r");
  174. vfd = open("/dev/volume", OWRITE);
  175. }
  176. if(fd != 1){
  177. dup(fd, 1);
  178. close(fd);
  179. }
  180. if(argc == 0){
  181. play(0, "standard input");
  182. }else{
  183. for(i=0; i<argc; i++){
  184. if((fd = open(argv[i], OREAD)) < 0)
  185. sysfatal("open %s: %r", argv[i]);
  186. play(fd, argv[i]);
  187. close(fd);
  188. }
  189. }
  190. exits(0);
  191. }