VirtualBox

Changeset 87996 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Mar 7, 2021 7:55:35 PM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
143119
Message:

Audio: Converted DrvAudioHlpFramesToNano and DrvAudioHlpFramesToMilli to use integer arithmetics rather than floating point. bugref:9890

Location:
trunk/src/VBox/Devices/Audio
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Audio/DrvAudioCommon.cpp

    r87994 r87996  
    13421342 * @param   pProps      The PCM properties to use.
    13431343 * @param   cFrames     Number of audio frames to convert.
     1344 * @note    No rounding here, result is floored.
    13441345 */
    13451346uint64_t DrvAudioHlpFramesToMilli(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
     
    13471348    AssertPtrReturn(pProps, 0);
    13481349
    1349     if (!cFrames)
    1350         return 0;
    1351 
    1352     if (!pProps->uHz) /* Prevent division by zero. */
    1353         return 0;
    1354 
    1355     /** @todo r=bird: How to do this w/o any floating point:
    1356     * @code
    1357     *  ASMMultU32ByU32DivByU32(cFrames, RT_MS_1SEC, pProps->uHz);
    1358     * // or
    1359     *  (uint64_t)cFrames * RT_MS_1SEC / pProps->uHz
    1360     * @endcode
    1361     */
    1362     return cFrames / ((double)pProps->uHz / (double)RT_MS_1SEC);
     1350    /* Check input to prevent division by chainsaw: */
     1351    uint32_t const uHz = pProps->uHz;
     1352    if (uHz)
     1353        return ASMMultU32ByU32DivByU32(cFrames, RT_MS_1SEC, uHz);
     1354    return 0;
    13631355}
    13641356
     
    13691361 * @param   pProps      The PCM properties to use.
    13701362 * @param   cFrames     Number of audio frames to convert.
     1363 * @note    No rounding here, result is floored.
    13711364 */
    13721365uint64_t DrvAudioHlpFramesToNano(PCPDMAUDIOPCMPROPS pProps, uint32_t cFrames)
     
    13741367    AssertPtrReturn(pProps, 0);
    13751368
    1376     if (!cFrames)
    1377         return 0;
    1378 
    1379     if (!pProps->uHz) /* Prevent division by zero. */
    1380         return 0;
    1381 
    1382     return cFrames / ((double)pProps->uHz / (double)RT_NS_1SEC);
     1369    /* Check input to prevent division by chainsaw: */
     1370    uint32_t const uHz = pProps->uHz;
     1371    if (uHz)
     1372        return ASMMultU32ByU32DivByU32(cFrames, RT_NS_1SEC, uHz);
     1373    return 0;
    13831374}
    13841375
  • trunk/src/VBox/Devices/Audio/testcase/tstAudioMixBuffer.cpp

    r87994 r87996  
    6464
    6565
    66     RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToNano(&s_Cfg441StereoS16, 44100)) == RT_NS_1SEC,
    67                       ("ns=%RU64\n", u64));
     66    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToNano(&s_Cfg441StereoS16, 44100)) == RT_NS_1SEC, ("ns=%RU64\n", u64));
     67    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToNano(&s_Cfg441StereoS16,     1)) == 22675,      ("ns=%RU64\n", u64));
     68    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToNano(&s_Cfg441StereoS16,    31)) == 702947,     ("ns=%RU64\n", u64));
     69    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToNano(&s_Cfg441StereoS16,   255)) == 5782312,    ("ns=%RU64\n", u64));
    6870    //RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMicro(&s_Cfg441StereoS16, 44100)) == RT_US_1SEC,
    6971    //                  ("us=%RU64\n", u64));
    70     RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMilli(&s_Cfg441StereoS16, 44100)) == RT_MS_1SEC,
    71                       ("ms=%RU64\n", u64));
     72    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMilli(&s_Cfg441StereoS16, 44100)) == RT_MS_1SEC, ("ms=%RU64\n", u64));
     73    RTTESTI_CHECK_MSG((u64 = DrvAudioHlpFramesToMilli(&s_Cfg441StereoS16,   255)) == 5,          ("ms=%RU64\n", u64));
    7274
    7375}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette