Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. 44100 * 20.4 = 899640
  2.  
  3. size_t SoundEffect::GetSampleDurationMS() const
  4. {
  5. if ( !pImpl->mWaveFormat || !pImpl->mWaveFormat->nSamplesPerSec )
  6. return 0;
  7.  
  8. uint64_t samples = GetSampleDuration();
  9. return static_cast<size_t>( ( samples * 1000 ) / pImpl->mWaveFormat->nSamplesPerSec );
  10. }
  11.  
  12. size_t SoundEffect::GetSampleDuration() const
  13. {
  14. if ( !pImpl->mWaveFormat || !pImpl->mWaveFormat->nChannels )
  15. return 0;
  16.  
  17. switch( GetFormatTag( pImpl->mWaveFormat ) )
  18. {
  19. case WAVE_FORMAT_ADPCM:
  20. {
  21. auto adpcmFmt = reinterpret_cast<const ADPCMWAVEFORMAT*>( pImpl->mWaveFormat );
  22.  
  23. uint64_t duration = uint64_t( pImpl->mAudioBytes / adpcmFmt->wfx.nBlockAlign ) * adpcmFmt->wSamplesPerBlock;
  24. int partial = pImpl->mAudioBytes % adpcmFmt->wfx.nBlockAlign;
  25. if ( partial )
  26. {
  27. if ( partial >= ( 7 * adpcmFmt->wfx.nChannels ) )
  28. duration += ( partial * 2 / adpcmFmt->wfx.nChannels - 12 );
  29. }
  30. return static_cast<size_t>( duration );
  31. }
  32.  
  33. #if defined(_XBOX_ONE) || (_WIN32_WINNT < _WIN32_WINNT_WIN8)
  34.  
  35. case WAVE_FORMAT_WMAUDIO2:
  36. case WAVE_FORMAT_WMAUDIO3:
  37. if ( pImpl->mSeekTable && pImpl->mSeekCount > 0 )
  38. {
  39. return pImpl->mSeekTable[ pImpl->mSeekCount - 1 ] / uint32_t( 2 * pImpl->mWaveFormat->nChannels );
  40. }
  41. break;
  42.  
  43. #endif
  44.  
  45. #if defined(_XBOX_ONE) && defined(_TITLE)
  46.  
  47. case WAVE_FORMAT_XMA2:
  48. return reinterpret_cast<const XMA2WAVEFORMATEX*>( pImpl->mWaveFormat )->SamplesEncoded;
  49.  
  50. #endif
  51.  
  52. default:
  53. if ( pImpl->mWaveFormat->wBitsPerSample > 0 )
  54. {
  55. return static_cast<size_t>( ( uint64_t( pImpl->mAudioBytes ) * 8 )
  56. / uint64_t( pImpl->mWaveFormat->wBitsPerSample * pImpl->mWaveFormat->nChannels ) );
  57. }
  58. }
  59.  
  60. return 0;
  61. }
  62.  
  63. inline uint32_t GetFormatTag( const WAVEFORMATEX* wfx )
  64. {
  65. if ( wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
  66. {
  67. if ( wfx->cbSize < ( sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) ) )
  68. return 0;
  69.  
  70. static const GUID s_wfexBase = {0x00000000, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71};
  71.  
  72. auto wfex = reinterpret_cast<const WAVEFORMATEXTENSIBLE*>( wfx );
  73.  
  74. if ( memcmp( reinterpret_cast<const BYTE*>(&wfex->SubFormat) + sizeof(DWORD),
  75. reinterpret_cast<const BYTE*>(&s_wfexBase) + sizeof(DWORD), sizeof(GUID) - sizeof(DWORD) ) != 0 )
  76. {
  77. return 0;
  78. }
  79.  
  80. return wfex->SubFormat.Data1;
  81. }
  82. else
  83. {
  84. return wfx->wFormatTag;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement