VirtualBox

Changeset 89340 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
May 28, 2021 10:13:37 AM (4 years ago)
Author:
vboxsync
Message:

AudioMixer: Moving a few functions, merging the volume setter worker into the public function. bugref:9890

File:
1 edited

Legend:

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

    r89339 r89340  
    138138AssertCompile(AUDIOMIXBUF_VOL_0DB <= 0x40000000);   /* Must always hold. */
    139139AssertCompile(AUDIOMIXBUF_VOL_0DB == 0x40000000);   /* For now -- when only attenuation is used. */
    140 
    141 
    142 /**
    143  * Destroys (uninitializes) a mixing buffer.
    144  *
    145  * @param   pMixBuf                 Mixing buffer to destroy.
    146  */
    147 void AudioMixBufDestroy(PAUDIOMIXBUF pMixBuf)
    148 {
    149     if (!pMixBuf)
    150         return;
    151 
    152     /* Ignore calls for an uninitialized (zeroed) or already destroyed instance.  Happens a lot. */
    153     if (   pMixBuf->uMagic == 0
    154         || pMixBuf->uMagic == ~AUDIOMIXBUF_MAGIC)
    155     {
    156         Assert(!pMixBuf->pszName);
    157         Assert(!pMixBuf->pFrames);
    158         Assert(!pMixBuf->cFrames);
    159         return;
    160     }
    161 
    162     Assert(pMixBuf->uMagic == AUDIOMIXBUF_MAGIC);
    163     pMixBuf->uMagic = ~AUDIOMIXBUF_MAGIC;
    164 
    165     if (pMixBuf->pszName)
    166     {
    167         AUDMIXBUF_LOG(("%s\n", pMixBuf->pszName));
    168 
    169         RTStrFree(pMixBuf->pszName);
    170         pMixBuf->pszName = NULL;
    171     }
    172 
    173     if (pMixBuf->pFrames)
    174     {
    175         Assert(pMixBuf->cFrames);
    176 
    177         RTMemFree(pMixBuf->pFrames);
    178         pMixBuf->pFrames = NULL;
    179     }
    180 
    181     pMixBuf->cFrames = 0;
    182 }
    183 
    184 /**
    185  * Returns the size (in audio frames) of free audio buffer space.
    186  *
    187  * @return  uint32_t                Size (in audio frames) of free audio buffer space.
    188  * @param   pMixBuf                 Mixing buffer to return free size for.
    189  */
    190 uint32_t AudioMixBufFree(PAUDIOMIXBUF pMixBuf)
    191 {
    192     AssertPtrReturn(pMixBuf, 0);
    193 
    194     uint32_t const cFrames = pMixBuf->cFrames;
    195     uint32_t       cUsed   = pMixBuf->cUsed;
    196     AssertStmt(cUsed <= cFrames, cUsed = cFrames);
    197     uint32_t const cFramesFree = cFrames - cUsed;
    198 
    199     AUDMIXBUF_LOG(("%s: %RU32 of %RU32\n", pMixBuf->pszName, cFramesFree, cFrames));
    200     return cFramesFree;
    201 }
    202 
    203 /**
    204  * Returns the size (in bytes) of free audio buffer space.
    205  *
    206  * @return  uint32_t                Size (in bytes) of free audio buffer space.
    207  * @param   pMixBuf                 Mixing buffer to return free size for.
    208  */
    209 uint32_t AudioMixBufFreeBytes(PAUDIOMIXBUF pMixBuf)
    210 {
    211     return AUDIOMIXBUF_F2B(pMixBuf, AudioMixBufFree(pMixBuf));
    212 }
    213140
    214141
     
    725652
    726653/**
    727  * Converts a PDM audio volume to an internal mixing buffer volume.
    728  *
    729  * @returns VBox status code.
    730  * @param   pVolDst                 Where to store the converted mixing buffer volume.
    731  * @param   pVolSrc                 Volume to convert.
    732  */
    733 static int audioMixBufConvVol(PAUDMIXBUFVOL pVolDst, PCPDMAUDIOVOLUME pVolSrc)
    734 {
    735     if (!pVolSrc->fMuted) /* Only change/convert the volume value if we're not muted. */
    736     {
    737         uint8_t uVolL = pVolSrc->uLeft  & 0xFF;
    738         uint8_t uVolR = pVolSrc->uRight & 0xFF;
    739 
    740         /** @todo Ensure that the input is in the correct range/initialized! */
    741         pVolDst->uLeft  = s_aVolumeConv[uVolL] * (AUDIOMIXBUF_VOL_0DB >> 16);
    742         pVolDst->uRight = s_aVolumeConv[uVolR] * (AUDIOMIXBUF_VOL_0DB >> 16);
    743     }
    744 
    745     pVolDst->fMuted = pVolSrc->fMuted;
    746 
    747     return VINF_SUCCESS;
    748 }
    749 
    750 /**
    751654 * Initializes a mixing buffer.
    752655 *
     
    801704    return VERR_NO_MEMORY;
    802705}
     706
     707/**
     708 * Destroys (uninitializes) a mixing buffer.
     709 *
     710 * @param   pMixBuf                 Mixing buffer to destroy.
     711 */
     712void AudioMixBufDestroy(PAUDIOMIXBUF pMixBuf)
     713{
     714    if (!pMixBuf)
     715        return;
     716
     717    /* Ignore calls for an uninitialized (zeroed) or already destroyed instance.  Happens a lot. */
     718    if (   pMixBuf->uMagic == 0
     719        || pMixBuf->uMagic == ~AUDIOMIXBUF_MAGIC)
     720    {
     721        Assert(!pMixBuf->pszName);
     722        Assert(!pMixBuf->pFrames);
     723        Assert(!pMixBuf->cFrames);
     724        return;
     725    }
     726
     727    Assert(pMixBuf->uMagic == AUDIOMIXBUF_MAGIC);
     728    pMixBuf->uMagic = ~AUDIOMIXBUF_MAGIC;
     729
     730    if (pMixBuf->pszName)
     731    {
     732        AUDMIXBUF_LOG(("%s\n", pMixBuf->pszName));
     733
     734        RTStrFree(pMixBuf->pszName);
     735        pMixBuf->pszName = NULL;
     736    }
     737
     738    if (pMixBuf->pFrames)
     739    {
     740        Assert(pMixBuf->cFrames);
     741
     742        RTMemFree(pMixBuf->pFrames);
     743        pMixBuf->pFrames = NULL;
     744    }
     745
     746    pMixBuf->cFrames = 0;
     747}
     748
     749/**
     750 * Returns the maximum amount of audio frames this buffer can hold.
     751 *
     752 * @return  uint32_t                Size (in audio frames) the mixing buffer can hold.
     753 * @param   pMixBuf                 Mixing buffer to retrieve maximum for.
     754 */
     755uint32_t AudioMixBufSize(PAUDIOMIXBUF pMixBuf)
     756{
     757    AssertPtrReturn(pMixBuf, 0);
     758    return pMixBuf->cFrames;
     759}
     760
     761/**
     762 * Returns the maximum amount of bytes this buffer can hold.
     763 *
     764 * @return  uint32_t                Size (in bytes) the mixing buffer can hold.
     765 * @param   pMixBuf                 Mixing buffer to retrieve maximum for.
     766 */
     767uint32_t AudioMixBufSizeBytes(PAUDIOMIXBUF pMixBuf)
     768{
     769    AssertPtrReturn(pMixBuf, 0);
     770    return AUDIOMIXBUF_F2B(pMixBuf, pMixBuf->cFrames);
     771}
     772
     773/**
     774 * Returns the current write position of a mixing buffer.
     775 *
     776 * @returns VBox status code.
     777 * @param   pMixBuf                 Mixing buffer to return position for.
     778 */
     779uint32_t AudioMixBufWritePos(PAUDIOMIXBUF pMixBuf)
     780{
     781    AssertPtrReturn(pMixBuf, 0);
     782
     783    return pMixBuf->offWrite;
     784}
     785
     786/**
     787 * Returns the size (in audio frames) of free audio buffer space.
     788 *
     789 * @return  uint32_t                Size (in audio frames) of free audio buffer space.
     790 * @param   pMixBuf                 Mixing buffer to return free size for.
     791 */
     792uint32_t AudioMixBufFree(PAUDIOMIXBUF pMixBuf)
     793{
     794    AssertPtrReturn(pMixBuf, 0);
     795
     796    uint32_t const cFrames = pMixBuf->cFrames;
     797    uint32_t       cUsed   = pMixBuf->cUsed;
     798    AssertStmt(cUsed <= cFrames, cUsed = cFrames);
     799    uint32_t const cFramesFree = cFrames - cUsed;
     800
     801    AUDMIXBUF_LOG(("%s: %RU32 of %RU32\n", pMixBuf->pszName, cFramesFree, cFrames));
     802    return cFramesFree;
     803}
     804
     805/**
     806 * Returns the size (in bytes) of free audio buffer space.
     807 *
     808 * @return  uint32_t                Size (in bytes) of free audio buffer space.
     809 * @param   pMixBuf                 Mixing buffer to return free size for.
     810 */
     811uint32_t AudioMixBufFreeBytes(PAUDIOMIXBUF pMixBuf)
     812{
     813    return AUDIOMIXBUF_F2B(pMixBuf, AudioMixBufFree(pMixBuf));
     814}
     815
    803816
    804817/**
     
    18751888 * @param   pMixBuf     The mixing buffer.
    18761889 * @param   cFrames     Number of frames to advance.
    1877  * @sa      AudioMixBufAdvance
     1890 * @sa      AudioMixBufAdvance, AudioMixBufSetVolume
    18781891 */
    18791892void AudioMixBufCommit(PAUDIOMIXBUF pMixBuf, uint32_t cFrames)
     
    18931906
    18941907/**
    1895  * Sets the overall (master) volume.
    1896  *
    1897  * @param   pMixBuf                 Mixing buffer to set volume for.
    1898  * @param   pVol                    Pointer to volume structure to set.
     1908 * Sets the volume.
     1909 *
     1910 * The volume adjustments are applied by AudioMixBufCommit().
     1911 *
     1912 * @param   pMixBuf     Mixing buffer to set volume for.
     1913 * @param   pVol        Pointer to volume structure to set.
    18991914 */
    19001915void AudioMixBufSetVolume(PAUDIOMIXBUF pMixBuf, PCPDMAUDIOVOLUME pVol)
     
    19051920    LogFlowFunc(("%s: lVol=%RU8, rVol=%RU8, fMuted=%RTbool\n", pMixBuf->pszName, pVol->uLeft, pVol->uRight, pVol->fMuted));
    19061921
    1907     int rc2 = audioMixBufConvVol(&pMixBuf->Volume, pVol);
    1908     AssertRC(rc2);
    1909 }
    1910 
    1911 /**
    1912  * Returns the maximum amount of audio frames this buffer can hold.
    1913  *
    1914  * @return  uint32_t                Size (in audio frames) the mixing buffer can hold.
    1915  * @param   pMixBuf                 Mixing buffer to retrieve maximum for.
    1916  */
    1917 uint32_t AudioMixBufSize(PAUDIOMIXBUF pMixBuf)
    1918 {
    1919     AssertPtrReturn(pMixBuf, 0);
    1920     return pMixBuf->cFrames;
    1921 }
    1922 
    1923 /**
    1924  * Returns the maximum amount of bytes this buffer can hold.
    1925  *
    1926  * @return  uint32_t                Size (in bytes) the mixing buffer can hold.
    1927  * @param   pMixBuf                 Mixing buffer to retrieve maximum for.
    1928  */
    1929 uint32_t AudioMixBufSizeBytes(PAUDIOMIXBUF pMixBuf)
    1930 {
    1931     AssertPtrReturn(pMixBuf, 0);
    1932     return AUDIOMIXBUF_F2B(pMixBuf, pMixBuf->cFrames);
    1933 }
    1934 
    1935 /**
    1936  * Returns the current write position of a mixing buffer.
    1937  *
    1938  * @returns VBox status code.
    1939  * @param   pMixBuf                 Mixing buffer to return position for.
    1940  */
    1941 uint32_t AudioMixBufWritePos(PAUDIOMIXBUF pMixBuf)
    1942 {
    1943     AssertPtrReturn(pMixBuf, 0);
    1944 
    1945     return pMixBuf->offWrite;
    1946 }
    1947 
     1922    /*
     1923     * Convert PDM audio volume to the internal format.
     1924     */
     1925    if (!pVol->fMuted)
     1926    {
     1927        pMixBuf->Volume.fMuted = false;
     1928        AssertCompileSize(pVol->uLeft, sizeof(uint8_t));
     1929        pMixBuf->Volume.uLeft  = s_aVolumeConv[pVol->uLeft ] * (AUDIOMIXBUF_VOL_0DB >> 16);
     1930        pMixBuf->Volume.uRight = s_aVolumeConv[pVol->uRight] * (AUDIOMIXBUF_VOL_0DB >> 16);
     1931    }
     1932    else
     1933    {
     1934        pMixBuf->Volume.fMuted = true;
     1935        pMixBuf->Volume.uLeft  = 0;
     1936        pMixBuf->Volume.uRight = 0;
     1937    }
     1938}
     1939
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