VirtualBox

Ignore:
Timestamp:
May 30, 2021 12:56:12 AM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
144728
Message:

tstAudioMixBuffer: Converted tstVolume to new mixer buffer interface. bugref:9890

File:
1 edited

Legend:

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

    r89376 r89377  
    757757}
    758758
    759 #if 0 /** @todo rewrite to non-parent/child setup */
    760759/* Test volume control. */
    761 static int tstVolume(RTTEST hTest)
     760static void tstVolume(RTTEST hTest)
    762761{
    763762    RTTestSub(hTest, "Volume control (44.1kHz S16 2ch)");
    764     uint32_t         cBufSize = 256;
    765 
    766     /* Same for parent/child. */
    767     /* 44100Hz, 2 Channels, S16 */
    768     PDMAUDIOPCMPROPS cfg = PDMAUDIOPCMPROPS_INITIALIZER(
     763    uint32_t const cBufSize = 256;
     764
     765    /*
     766     * Configure a mixbuf where we read and write 44.1kHz S16 2ch.
     767     */
     768    PDMAUDIOPCMPROPS const Cfg = PDMAUDIOPCMPROPS_INITIALIZER(
    769769        2,                                                                  /* Bytes */
    770770        true,                                                               /* Signed */
     
    773773        false                                                               /* Swap Endian */
    774774    );
    775 
    776     RTTESTI_CHECK(AudioHlpPcmPropsAreValid(&cfg));
    777 
    778     PDMAUDIOVOLUME vol = { false, 0, 0 };   /* Not muted. */
    779     AUDIOMIXBUF parent;
    780     RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &cfg, cBufSize));
    781 
    782     AUDIOMIXBUF child;
    783     RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &cfg, cBufSize));
    784     RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent));
    785 
    786     /* A few 16-bit signed samples. */
    787     int16_t     aFrames16S[16] = { INT16_MIN, INT16_MIN + 1, -128, -64, -4, -1, 0, 1,
    788                                    2, 255, 256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0 };
    789 
    790     /*
    791      * Writing + mixing from child -> parent.
    792      */
    793     uint32_t    cbBuf = 256;
    794     char        achBuf[256];
    795     uint32_t    cFramesRead, cFramesWritten, cFramesMixed;
    796 
    797     uint32_t cFramesChild  = 8;
    798     uint32_t cFramesParent = cFramesChild;
    799     uint32_t cFramesTotalRead;
    800     int16_t *pSrc16;
    801     int16_t *pDst16;
    802 
    803     /**** Volume control test ****/
    804     RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Volume control test %uHz %uch \n", cfg.uHz, PDMAudioPropsChannels(&cfg));
    805 
    806     /* 1) Full volume/0dB attenuation (255). */
    807     vol.uLeft = vol.uRight = 255;
    808     AudioMixBufSetVolume(&child, &vol);
    809 
    810     RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
    811     RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
    812     RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
    813 
    814     cFramesTotalRead = 0;
    815     for (;;)
     775    AUDIOMIXBUF MixBuf;
     776    RTTESTI_CHECK_RC_RETV(AudioMixBufInit(&MixBuf, "Volume", &Cfg, cBufSize), VINF_SUCCESS);
     777
     778    AUDIOMIXBUFWRITESTATE WriteState;
     779    RTTESTI_CHECK_RC_RETV(AudioMixBufInitWriteState(&MixBuf, &WriteState, &Cfg), VINF_SUCCESS);
     780
     781    AUDIOMIXBUFPEEKSTATE PeekState;
     782    RTTESTI_CHECK_RC_RETV(AudioMixBufInitPeekState(&MixBuf, &PeekState, &Cfg), VINF_SUCCESS);
     783
     784    /*
     785     * A few 16-bit signed test samples.
     786     */
     787    static int16_t const s_aFrames16S[16] =
    816788    {
    817         RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
    818         if (!cFramesRead)
    819             break;
    820         cFramesTotalRead += cFramesRead;
    821         AudioMixBufReleaseReadBlock(&parent, cFramesRead);
    822         AudioMixBufFinish(&parent, cFramesRead);
    823     }
    824     RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
     789        INT16_MIN,  INT16_MIN + 1, -128,           -64,            -4,            -1,         0, 1,
     790                2,            255,  256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0,
     791    };
     792
     793    /*
     794     * 1) Full volume/0dB attenuation (255).
     795     */
     796    PDMAUDIOVOLUME Vol;
     797    Vol.fMuted = false;
     798    Vol.uLeft  = 255;
     799    Vol.uRight = 255;
     800    AudioMixBufSetVolume(&MixBuf, &Vol);
     801
     802    /* Write all the test frames to the mixer buffer: */
     803    uint32_t cFramesWritten;
     804    AudioMixBufWrite(&MixBuf, &WriteState, &s_aFrames16S[0], sizeof(s_aFrames16S), 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
     805    RTTESTI_CHECK(cFramesWritten == RT_ELEMENTS(s_aFrames16S) / 2);
     806    AudioMixBufCommit(&MixBuf, cFramesWritten);
     807
     808    /* Read them back.  We should get them back just like we wrote them. */
     809    uint16_t au16Buf[cBufSize * 2];
     810    uint32_t cFramesPeeked;
     811    uint32_t cbPeeked;
     812    AudioMixBufPeek(&MixBuf, 0 /*offSrcFrame*/, cFramesWritten, &cFramesPeeked, &PeekState, au16Buf, sizeof(au16Buf), &cbPeeked);
     813    RTTESTI_CHECK(cFramesPeeked == cFramesWritten);
     814    RTTESTI_CHECK(cbPeeked == PDMAudioPropsFramesToBytes(&Cfg, cFramesPeeked));
     815    AudioMixBufAdvance(&MixBuf, cFramesPeeked);
    825816
    826817    /* Check that at 0dB the frames came out unharmed. */
    827     pSrc16 = &aFrames16S[0];
    828     pDst16 = (int16_t *)achBuf;
    829 
    830     for (unsigned i = 0; i < cFramesParent * 2 /* stereo */; ++i)
    831     {
    832         RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
    833         ++pSrc16;
    834         ++pDst16;
    835     }
    836     AudioMixBufReset(&child);
    837 
    838     /* 2) Half volume/-6dB attenuation (16 steps down). */
    839     vol.uLeft = vol.uRight = 255 - 16;
    840     AudioMixBufSetVolume(&child, &vol);
    841 
    842     RTTESTI_CHECK_RC_OK(AudioMixBufWriteCirc(&child, &aFrames16S, sizeof(aFrames16S), &cFramesWritten));
    843     RTTESTI_CHECK_MSG(cFramesWritten == cFramesChild, ("Child: Expected %RU32 written frames, got %RU32\n", cFramesChild, cFramesWritten));
    844     RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, cFramesWritten, &cFramesMixed));
    845 
    846     cFramesTotalRead = 0;
    847     for (;;)
    848     {
    849         RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufAcquireReadBlock(&parent, achBuf, cbBuf, &cFramesRead));
    850         if (!cFramesRead)
    851             break;
    852         cFramesTotalRead += cFramesRead;
    853         AudioMixBufReleaseReadBlock(&parent, cFramesRead);
    854         AudioMixBufFinish(&parent, cFramesRead);
    855     }
    856     RTTESTI_CHECK_MSG(cFramesTotalRead == cFramesParent, ("Parent: Expected %RU32 mixed frames, got %RU32\n", cFramesParent, cFramesTotalRead));
     818    if (memcmp(au16Buf, s_aFrames16S, sizeof(s_aFrames16S)) != 0)
     819        RTTestFailed(hTest,
     820                     "0dB test failed\n"
     821                     "mismatch: %.*Rhxs\n"
     822                     "expected: %.*Rhxs\n",
     823                     sizeof(s_aFrames16S), au16Buf, sizeof(s_aFrames16S), s_aFrames16S);
     824
     825    /*
     826     * 2) Half volume/-6dB attenuation (16 steps down).
     827     */
     828    Vol.uLeft  = 255 - 16;
     829    Vol.uRight = 255 - 16;
     830    AudioMixBufSetVolume(&MixBuf, &Vol);
     831
     832    /* Write all the test frames to the mixer buffer: */
     833    AudioMixBufWrite(&MixBuf, &WriteState, &s_aFrames16S[0], sizeof(s_aFrames16S), 0 /*offDstFrame*/, cBufSize, &cFramesWritten);
     834    RTTESTI_CHECK(cFramesWritten == RT_ELEMENTS(s_aFrames16S) / 2);
     835    AudioMixBufCommit(&MixBuf, cFramesWritten);
     836
     837    /* Read them back.  We should get them back just like we wrote them. */
     838    AudioMixBufPeek(&MixBuf, 0 /*offSrcFrame*/, cFramesWritten, &cFramesPeeked, &PeekState, au16Buf, sizeof(au16Buf), &cbPeeked);
     839    RTTESTI_CHECK(cFramesPeeked == cFramesWritten);
     840    RTTESTI_CHECK(cbPeeked == PDMAudioPropsFramesToBytes(&Cfg, cFramesPeeked));
     841    AudioMixBufAdvance(&MixBuf, cFramesPeeked);
    857842
    858843    /* Check that at -6dB the sample values are halved. */
    859     pSrc16 = &aFrames16S[0];
    860     pDst16 = (int16_t *)achBuf;
    861 
    862     for (unsigned i = 0; i < cFramesParent * 2 /* stereo */; ++i)
    863     {
    864         /* Watch out! For negative values, x >> 1 is not the same as x / 2. */
    865         RTTESTI_CHECK_MSG(*pSrc16 >> 1 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16));
    866         ++pSrc16;
    867         ++pDst16;
    868     }
    869 
    870     AudioMixBufDestroy(&parent);
    871     AudioMixBufDestroy(&child);
    872 
    873     return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS;
     844    int16_t ai16Expect[sizeof(s_aFrames16S) / 2];
     845    memcpy(ai16Expect, s_aFrames16S, sizeof(ai16Expect));
     846    for (uintptr_t i = 0; i < RT_ELEMENTS(ai16Expect); i++)
     847        ai16Expect[i] >>= 1; /* /= 2 - not the same for signed numbers; */
     848    if (memcmp(au16Buf, ai16Expect, sizeof(ai16Expect)) != 0)
     849        RTTestFailed(hTest,
     850                     "-6dB test failed\n"
     851                     "mismatch: %.*Rhxs\n"
     852                     "expected: %.*Rhxs\n"
     853                     "wrote:    %.*Rhxs\n",
     854                     sizeof(ai16Expect), au16Buf, sizeof(ai16Expect), ai16Expect, sizeof(s_aFrames16S), s_aFrames16S);
     855
     856    AudioMixBufTerm(&MixBuf);
    874857}
    875 #endif
     858
    876859
    877860int main(int argc, char **argv)
     
    919902    //tstNewPeek(hTest, 22050, 44100);
    920903
    921     //tstVolume(hTest);
     904    tstVolume(hTest);
    922905
    923906    /*
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