audio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "mpg123.h"
  2. void audio_info_struct_init(struct audio_info_struct *ai)
  3. {
  4. #ifdef AUDIO_USES_FD
  5. ai->fn = -1;
  6. #endif
  7. #ifdef SGI
  8. #if 0
  9. ALconfig config;
  10. ALport port;
  11. #endif
  12. #endif
  13. ai->rate = -1;
  14. ai->gain = -1;
  15. ai->output = -1;
  16. #ifdef ALSA
  17. ai->handle = NULL;
  18. ai->alsa_format.format = -1;
  19. ai->alsa_format.rate = -1;
  20. ai->alsa_format.channels = -1;
  21. #endif
  22. ai->device = NULL;
  23. ai->channels = -1;
  24. ai->format = -1;
  25. }
  26. #define NUM_CHANNELS 2
  27. #define NUM_ENCODINGS 6
  28. #define NUM_RATES 10
  29. struct audio_name audio_val2name[NUM_ENCODINGS+1] = {
  30. { AUDIO_FORMAT_SIGNED_16 , "signed 16 bit" , "s16 " } ,
  31. { AUDIO_FORMAT_UNSIGNED_16, "unsigned 16 bit" , "u16 " } ,
  32. { AUDIO_FORMAT_UNSIGNED_8 , "unsigned 8 bit" , "u8 " } ,
  33. { AUDIO_FORMAT_SIGNED_8 , "signed 8 bit" , "s8 " } ,
  34. { AUDIO_FORMAT_ULAW_8 , "mu-law (8 bit)" , "ulaw " } ,
  35. { AUDIO_FORMAT_ALAW_8 , "a-law (8 bit)" , "alaw " } ,
  36. { -1 , NULL }
  37. };
  38. #if 0
  39. static char *channel_name[NUM_CHANNELS] =
  40. { "mono" , "stereo" };
  41. #endif
  42. static int channels[NUM_CHANNELS] = { 1 , 2 };
  43. static int rates[NUM_RATES] = {
  44. 8000, 11025, 12000,
  45. 16000, 22050, 24000,
  46. 32000, 44100, 48000,
  47. 8000 /* 8000 = dummy for user forced */
  48. };
  49. static int encodings[NUM_ENCODINGS] = {
  50. AUDIO_FORMAT_SIGNED_16,
  51. AUDIO_FORMAT_UNSIGNED_16,
  52. AUDIO_FORMAT_UNSIGNED_8,
  53. AUDIO_FORMAT_SIGNED_8,
  54. AUDIO_FORMAT_ULAW_8,
  55. AUDIO_FORMAT_ALAW_8
  56. };
  57. static char capabilities[NUM_CHANNELS][NUM_ENCODINGS][NUM_RATES];
  58. void audio_capabilities(struct audio_info_struct *ai)
  59. {
  60. int fmts;
  61. int i,j,k,k1=NUM_RATES-1;
  62. struct audio_info_struct ai1 = *ai;
  63. if (param.outmode != DECODE_AUDIO) {
  64. memset(capabilities,1,sizeof(capabilities));
  65. return;
  66. }
  67. memset(capabilities,0,sizeof(capabilities));
  68. if(param.force_rate) {
  69. rates[NUM_RATES-1] = param.force_rate;
  70. k1 = NUM_RATES;
  71. }
  72. if(audio_open(&ai1) < 0) {
  73. perror("audio");
  74. exit(1);
  75. }
  76. for(i=0;i<NUM_CHANNELS;i++) {
  77. for(j=0;j<NUM_RATES;j++) {
  78. ai1.channels = channels[i];
  79. ai1.rate = rates[j];
  80. fmts = audio_get_formats(&ai1);
  81. if(fmts < 0)
  82. continue;
  83. for(k=0;k<NUM_ENCODINGS;k++) {
  84. if((fmts & encodings[k]) == encodings[k])
  85. capabilities[i][k][j] = 1;
  86. }
  87. }
  88. }
  89. audio_close(&ai1);
  90. if(param.verbose > 1) {
  91. fprintf(stderr,"\nAudio capabilities:\n |");
  92. for(j=0;j<NUM_ENCODINGS;j++) {
  93. fprintf(stderr," %5s |",audio_val2name[j].sname);
  94. }
  95. fprintf(stderr,"\n --------------------------------------------------------\n");
  96. for(k=0;k<k1;k++) {
  97. fprintf(stderr," %5d |",rates[k]);
  98. for(j=0;j<NUM_ENCODINGS;j++) {
  99. if(capabilities[0][j][k]) {
  100. if(capabilities[1][j][k])
  101. fprintf(stderr," M/S |");
  102. else
  103. fprintf(stderr," M |");
  104. }
  105. else if(capabilities[1][j][k])
  106. fprintf(stderr," S |");
  107. else
  108. fprintf(stderr," |");
  109. }
  110. fprintf(stderr,"\n");
  111. }
  112. fprintf(stderr,"\n");
  113. }
  114. }
  115. static int rate2num(int r)
  116. {
  117. int i;
  118. for(i=0;i<NUM_RATES;i++)
  119. if(rates[i] == r)
  120. return i;
  121. return -1;
  122. }
  123. static int audio_fit_cap_helper(struct audio_info_struct *ai,int rn,int f0,int f2,int c)
  124. {
  125. int i;
  126. if(rn >= 0) {
  127. for(i=f0;i<f2;i++) {
  128. if(capabilities[c][i][rn]) {
  129. ai->rate = rates[rn];
  130. ai->format = encodings[i];
  131. ai->channels = channels[c];
  132. return 1;
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. /*
  139. * c=num of channels of stream
  140. * r=rate of stream
  141. */
  142. void audio_fit_capabilities(struct audio_info_struct *ai,int c,int r)
  143. {
  144. int rn;
  145. int f0=0;
  146. if(param.force_8bit) {
  147. f0 = 2;
  148. }
  149. c--; /* stereo=1 ,mono=0 */
  150. if(param.force_mono >= 0)
  151. c = 0;
  152. if(param.force_stereo)
  153. c = 1;
  154. if(param.force_rate) {
  155. rn = rate2num(param.force_rate);
  156. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  157. return;
  158. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  159. return;
  160. if(c == 1 && !param.force_stereo)
  161. c = 0;
  162. else if(c == 0 && !param.force_mono)
  163. c = 1;
  164. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  165. return;
  166. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  167. return;
  168. fprintf(stderr,"No supported rate found!\n");
  169. exit(1);
  170. }
  171. rn = rate2num(r>>0);
  172. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  173. return;
  174. rn = rate2num(r>>1);
  175. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  176. return;
  177. rn = rate2num(r>>2);
  178. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  179. return;
  180. rn = rate2num(r>>0);
  181. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  182. return;
  183. rn = rate2num(r>>1);
  184. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  185. return;
  186. rn = rate2num(r>>2);
  187. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  188. return;
  189. if(c == 1 && !param.force_stereo)
  190. c = 0;
  191. else if(c == 0 && !param.force_mono)
  192. c = 1;
  193. rn = rate2num(r>>0);
  194. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  195. return;
  196. rn = rate2num(r>>1);
  197. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  198. return;
  199. rn = rate2num(r>>2);
  200. if(audio_fit_cap_helper(ai,rn,f0,2,c))
  201. return;
  202. rn = rate2num(r>>0);
  203. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  204. return;
  205. rn = rate2num(r>>1);
  206. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  207. return;
  208. rn = rate2num(r>>2);
  209. if(audio_fit_cap_helper(ai,rn,2,NUM_ENCODINGS,c))
  210. return;
  211. fprintf(stderr,"No supported rate found!\n");
  212. exit(1);
  213. }
  214. char *audio_encoding_name(int format)
  215. {
  216. int i;
  217. for(i=0;i<NUM_ENCODINGS;i++) {
  218. if(audio_val2name[i].val == format)
  219. return audio_val2name[i].name;
  220. }
  221. return "Unknown";
  222. }
  223. #if !defined(SOLARIS) && !defined(__NetBSD__) || defined(NAS)
  224. void audio_queueflush(struct audio_info_struct *ai)
  225. {
  226. }
  227. #endif