1
0

AudioFormat.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "main.h"
  2. #include "AudioLayer.h"
  3. unsigned long AudioFormat::AudioSamplesToMilliseconds(unsigned long samples)
  4. {
  5. return MulDiv(samples, 1000, SampleRate());
  6. }
  7. unsigned long AudioFormat::AudioBytesToMilliseconds(unsigned long bytes)
  8. {
  9. return MulDiv(AudioBytesToSamples(bytes), 1000, SampleRate());
  10. }
  11. unsigned long AudioFormat::AudioMillisecondsToBytes(DWORD milliseconds)
  12. {
  13. return AudioSamplesToBytes(MulDiv(milliseconds, SampleRate(), 1000));
  14. }
  15. unsigned long AudioFormat::AudioDurationToBytes(QWORD duration)
  16. {
  17. // TODO: potential integer overflow
  18. return AudioSamplesToBytes(MulDiv((int)duration, SampleRate(), 1000*10000));
  19. }
  20. unsigned long AudioFormat::AudioBytesToSamples(unsigned long bytes)
  21. {
  22. return bytes / waveFormat->Format.nBlockAlign;
  23. }
  24. unsigned long AudioFormat::AudioSamplesToBytes(unsigned long samples)
  25. {
  26. return samples * waveFormat->Format.nBlockAlign;
  27. }
  28. long AudioFormat::Channels()
  29. {
  30. return waveFormat->Format.nChannels;
  31. }
  32. long AudioFormat::ValidBits()
  33. {
  34. if (waveFormat->Format.wFormatTag == WAVE_FORMAT_PCM)
  35. {
  36. return waveFormat->Format.wBitsPerSample;
  37. }
  38. if (waveFormat->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
  39. {
  40. return waveFormat->Samples.wValidBitsPerSample;
  41. }
  42. return 0;
  43. }
  44. long AudioFormat::BitSize()
  45. {
  46. return waveFormat->Format.wBitsPerSample;
  47. }
  48. long AudioFormat::SampleRate()
  49. {
  50. return waveFormat->Format.nSamplesPerSec;
  51. }