waveformat.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #define INITGUID
  2. #include <mmdeviceapi.h>
  3. #undef INITGUID
  4. #include <mmreg.h>
  5. static WAVEFORMATEXTENSIBLE format_24_stereo =
  6. {
  7. {
  8. WAVE_FORMAT_EXTENSIBLE,
  9. 2,
  10. 88200,
  11. 705600,
  12. 8,
  13. 32,
  14. 22,
  15. },
  16. 24,
  17. SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT,
  18. KSDATAFORMAT_SUBTYPE_PCM
  19. };
  20. static WORD BitsPerSampleContainer(int bitspersamp)
  21. {
  22. if (bitspersamp <= 8) {
  23. return 8;
  24. } else if (bitspersamp <= 16) {
  25. return 16;
  26. } else if (bitspersamp <= 24) {
  27. return 24;
  28. } else {
  29. return 32;
  30. }
  31. }
  32. static WORD ChannelMask(int numchannels)
  33. {
  34. switch(numchannels) {
  35. case 1:
  36. return SPEAKER_FRONT_CENTER;
  37. case 2:
  38. return SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT;
  39. case 4: // quadraphonic
  40. return SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT;
  41. case 6: // 5.1
  42. return SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY;
  43. default:
  44. return 0;
  45. }
  46. }
  47. WAVEFORMATEXTENSIBLE WaveFormatForParameters(int samplerate, int numchannels, int bitspersamp)
  48. {
  49. WAVEFORMATEXTENSIBLE wave_format;
  50. wave_format.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; /* format type */
  51. wave_format.Format.nChannels = numchannels; /* number of channels (i.e. mono, stereo...) */
  52. wave_format.Format.nSamplesPerSec = samplerate; /* sample rate */
  53. wave_format.Format.nAvgBytesPerSec = BitsPerSampleContainer(bitspersamp) * numchannels * samplerate / 8; /* for buffer estimation */
  54. wave_format.Format.nBlockAlign = BitsPerSampleContainer(bitspersamp) * numchannels / 8; /* block size of data */
  55. wave_format.Format.wBitsPerSample = BitsPerSampleContainer(bitspersamp);
  56. wave_format.Format.cbSize = 22; /* the count in bytes of the size of */
  57. /* extra information (after cbSize) */
  58. wave_format.Samples.wValidBitsPerSample = bitspersamp;
  59. wave_format.dwChannelMask = ChannelMask(numchannels);
  60. wave_format.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
  61. return wave_format;
  62. }