VirtualBox

Changeset 73407 in vbox for trunk


Ignore:
Timestamp:
Jul 31, 2018 11:25:00 AM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
124025
Message:

Audio/DrvAudio: Added ns (nanoseconds) timing helper functions.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Audio/DrvAudio.h

    r73381 r73407  
    188188uint32_t DrvAudioHlpBytesToFrames(uint32_t cbBytes, const PPDMAUDIOPCMPROPS pProps);
    189189uint64_t DrvAudioHlpBytesToMs(uint32_t cbBytes, const PPDMAUDIOPCMPROPS pProps);
     190uint64_t DrvAudioHlpBytesToNano(uint32_t cbBytes, const PPDMAUDIOPCMPROPS pProps);
    190191uint32_t DrvAudioHlpFramesToBytes(uint32_t cFrames, const PPDMAUDIOPCMPROPS pProps);
    191192uint64_t DrvAudioHlpFramesToMs(uint32_t cFrames, const PPDMAUDIOPCMPROPS pProps);
     193uint64_t DrvAudioHlpFramesToNano(uint32_t cFrames, const PPDMAUDIOPCMPROPS pProps);
    192194uint32_t DrvAudioHlpMsToBytes(uint32_t uMs, const PPDMAUDIOPCMPROPS pProps);
     195uint32_t DrvAudioHlpNanoToBytes(uint32_t uNs, const PPDMAUDIOPCMPROPS pProps);
    193196uint32_t DrvAudioHlpMsToFrames(uint32_t uMs, const PPDMAUDIOPCMPROPS pProps);
     197uint32_t DrvAudioHlpNanoToFrames(uint32_t uNs, const PPDMAUDIOPCMPROPS pProps);
    194198/** @}  */
    195199
  • trunk/src/VBox/Devices/Audio/DrvAudioCommon.cpp

    r73381 r73407  
    11361136
    11371137/**
     1138 * Returns the time (in ns) for given byte amount and PCM properties.
     1139 *
     1140 * @return  uint64_t            Calculated time (in ns).
     1141 * @param   cbBytes             Amount of bytes to calculate time for.
     1142 * @param   pProps              PCM properties to calculate amount of bytes for.
     1143 */
     1144uint64_t DrvAudioHlpBytesToNano(uint32_t cbBytes, const PPDMAUDIOPCMPROPS pProps)
     1145{
     1146    AssertPtrReturn(pProps, 0);
     1147
     1148    if (!cbBytes)
     1149        return 0;
     1150
     1151    const float dbBytesPerMs = ((pProps->cBits / 8) * pProps->cChannels * pProps->uHz) / RT_NS_1SEC;
     1152    Assert(dbBytesPerMs >= 0.0f);
     1153    if (!dbBytesPerMs) /* Prevent division by zero. */
     1154        return 0;
     1155
     1156    return cbBytes / dbBytesPerMs;
     1157}
     1158
     1159/**
    11381160 * Returns the bytes for a given audio frames amount and PCM properties.
    11391161 *
     
    11731195
    11741196/**
     1197 * Returns the time (in ns) for given audio frames amount and PCM properties.
     1198 *
     1199 * @return  uint64_t            Calculated time (in ns).
     1200 * @param   cFrames             Amount of audio frames to calculate time for.
     1201 * @param   pProps              PCM properties to calculate time (in ns) for.
     1202 */
     1203uint64_t DrvAudioHlpFramesToNano(uint32_t cFrames, const PPDMAUDIOPCMPROPS pProps)
     1204{
     1205    AssertPtrReturn(pProps, 0);
     1206
     1207    if (!cFrames)
     1208        return 0;
     1209
     1210    if (!pProps->uHz) /* Prevent division by zero. */
     1211        return 0;
     1212
     1213    return cFrames / float(pProps->uHz / RT_NS_1SEC);
     1214}
     1215
     1216/**
    11751217 * Returns the amount of bytes for a given time (in ms) and PCM properties.
    11761218 *
     
    11901232
    11911233/**
    1192  * Returns the amount of audio for a given time (in ms) and PCM properties.
     1234 * Returns the amount of bytes for a given time (in ns) and PCM properties.
     1235 *
     1236 * @return  uint32_t            Calculated amount of bytes.
     1237 * @param   uNs                 Time (in ns) to calculate amount of bytes for.
     1238 * @param   pProps              PCM properties to calculate amount of bytes for.
     1239 */
     1240uint32_t DrvAudioHlpNanoToBytes(uint32_t uNs, const PPDMAUDIOPCMPROPS pProps)
     1241{
     1242    AssertPtrReturn(pProps, 0);
     1243
     1244    if (!uNs)
     1245        return 0;
     1246
     1247    return float(((pProps->cBits / 8) * pProps->cChannels * pProps->uHz) / RT_NS_1SEC) * uNs;
     1248}
     1249
     1250/**
     1251 * Returns the amount of audio frames for a given time (in ms) and PCM properties.
    11931252 *
    11941253 * @return  uint32_t            Calculated amount of audio frames.
    1195  * @param   uMs                 Time (in ms) to calculate amount of bytes for.
    1196  * @param   pProps              PCM properties to calculate amount of bytes for.
     1254 * @param   uMs                 Time (in ms) to calculate amount of frames for.
     1255 * @param   pProps              PCM properties to calculate amount of frames for.
    11971256 */
    11981257uint32_t DrvAudioHlpMsToFrames(uint32_t uMs, const PPDMAUDIOPCMPROPS pProps)
     
    12051264
    12061265    return DrvAudioHlpMsToBytes(uMs, pProps) / cbFrame;
     1266}
     1267
     1268/**
     1269 * Returns the amount of audio frames for a given time (in ns) and PCM properties.
     1270 *
     1271 * @return  uint32_t            Calculated amount of audio frames.
     1272 * @param   uNs                 Time (in ns) to calculate amount of frames for.
     1273 * @param   pProps              PCM properties to calculate amount of frames for.
     1274 */
     1275uint32_t DrvAudioHlpNanoToFrames(uint32_t uNs, const PPDMAUDIOPCMPROPS pProps)
     1276{
     1277    AssertPtrReturn(pProps, 0);
     1278
     1279    const uint32_t cbFrame = (pProps->cBits / 8) * pProps->cChannels;
     1280    if (!cbFrame) /* Prevent division by zero. */
     1281        return 0;
     1282
     1283    return DrvAudioHlpNanoToBytes(uNs, pProps) / cbFrame;
    12071284}
    12081285
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