2
0

audio_win_int.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* public domain */
  2. #include "qemu/osdep.h"
  3. #include "qemu-common.h"
  4. #define AUDIO_CAP "win-int"
  5. #include <windows.h>
  6. #include <mmsystem.h>
  7. #include "audio.h"
  8. #include "audio_int.h"
  9. #include "audio_win_int.h"
  10. int waveformat_from_audio_settings (WAVEFORMATEX *wfx,
  11. struct audsettings *as)
  12. {
  13. memset (wfx, 0, sizeof (*wfx));
  14. wfx->wFormatTag = WAVE_FORMAT_PCM;
  15. wfx->nChannels = as->nchannels;
  16. wfx->nSamplesPerSec = as->freq;
  17. wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
  18. wfx->nBlockAlign = 1 << (as->nchannels == 2);
  19. wfx->cbSize = 0;
  20. switch (as->fmt) {
  21. case AUDIO_FORMAT_S8:
  22. case AUDIO_FORMAT_U8:
  23. wfx->wBitsPerSample = 8;
  24. break;
  25. case AUDIO_FORMAT_S16:
  26. case AUDIO_FORMAT_U16:
  27. wfx->wBitsPerSample = 16;
  28. wfx->nAvgBytesPerSec <<= 1;
  29. wfx->nBlockAlign <<= 1;
  30. break;
  31. case AUDIO_FORMAT_S32:
  32. case AUDIO_FORMAT_U32:
  33. wfx->wBitsPerSample = 32;
  34. wfx->nAvgBytesPerSec <<= 2;
  35. wfx->nBlockAlign <<= 2;
  36. break;
  37. default:
  38. dolog ("Internal logic error: Bad audio format %d\n", as->freq);
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int waveformat_to_audio_settings (WAVEFORMATEX *wfx,
  44. struct audsettings *as)
  45. {
  46. if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
  47. dolog ("Invalid wave format, tag is not PCM, but %d\n",
  48. wfx->wFormatTag);
  49. return -1;
  50. }
  51. if (!wfx->nSamplesPerSec) {
  52. dolog ("Invalid wave format, frequency is zero\n");
  53. return -1;
  54. }
  55. as->freq = wfx->nSamplesPerSec;
  56. switch (wfx->nChannels) {
  57. case 1:
  58. as->nchannels = 1;
  59. break;
  60. case 2:
  61. as->nchannels = 2;
  62. break;
  63. default:
  64. dolog (
  65. "Invalid wave format, number of channels is not 1 or 2, but %d\n",
  66. wfx->nChannels
  67. );
  68. return -1;
  69. }
  70. switch (wfx->wBitsPerSample) {
  71. case 8:
  72. as->fmt = AUDIO_FORMAT_U8;
  73. break;
  74. case 16:
  75. as->fmt = AUDIO_FORMAT_S16;
  76. break;
  77. case 32:
  78. as->fmt = AUDIO_FORMAT_S32;
  79. break;
  80. default:
  81. dolog ("Invalid wave format, bits per sample is not "
  82. "8, 16 or 32, but %d\n",
  83. wfx->wBitsPerSample);
  84. return -1;
  85. }
  86. return 0;
  87. }