X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fuiq3%2Fengine%2Faudio_mediaserver.cpp;h=c9788d3c689e489b466066f8a3d440629d5664cf;hb=ca482e5de8bacb70db55f43afe02f93fe6fe3f16;hp=aff12d11ef5edc46a41973c39c1925057eda8d00;hpb=a6e5aa41816d1d46889012331eeeee6c5ba1dd1e;p=picodrive.git diff --git a/platform/uiq3/engine/audio_mediaserver.cpp b/platform/uiq3/engine/audio_mediaserver.cpp index aff12d1..c9788d3 100644 --- a/platform/uiq3/engine/audio_mediaserver.cpp +++ b/platform/uiq3/engine/audio_mediaserver.cpp @@ -15,6 +15,7 @@ #include "audio_mediaserver.h" #include "debug.h" +//#define DEBUG_UNDERFLOWS //#undef DEBUGPRINT //#define DEBUGPRINT(x...) @@ -31,16 +32,16 @@ const TInt KMaxUnderflows = 50; // max underflows/API errors we are going allow * *******************************************/ -CGameAudioMS::CGameAudioMS(TInt aRate, TBool aStereo, TInt aWritesPerSec) -: iRate(aRate), iStereo(aStereo), iWritesPerSec(aWritesPerSec) +CGameAudioMS::CGameAudioMS(TInt aRate, TBool aStereo, TInt aWritesPerSec, TInt aVolume) +: iRate(aRate), iStereo(aStereo), iWritesPerSec(aWritesPerSec), iVolume(aVolume) { } -CGameAudioMS* CGameAudioMS::NewL(TInt aRate, TBool aStereo, TInt aWritesPerSec) +CGameAudioMS* CGameAudioMS::NewL(TInt aRate, TBool aStereo, TInt aWritesPerSec, TInt aVolume) { - DEBUGPRINT(_L("CGameAudioMS::NewL(%i, %i, %i)"), aRate, aStereo, aWritesPerSec); - CGameAudioMS* self = new(ELeave) CGameAudioMS(aRate, aStereo, aWritesPerSec); + DEBUGPRINT(_L("CGameAudioMS::NewL(%i, %i, %i, %i)"), aRate, aStereo, aWritesPerSec, aVolume); + CGameAudioMS* self = new(ELeave) CGameAudioMS(aRate, aStereo, aWritesPerSec, aVolume); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(); // self @@ -85,25 +86,29 @@ void CGameAudioMS::ConstructL() iMdaAudioDataSettings.iCaps = TMdaAudioDataSettings::ESampleRateFixed | iMdaAudioDataSettings.iSampleRate; iMdaAudioDataSettings.iFlags = TMdaAudioDataSettings::ENoNetworkRouting; - int pcmFrames = iRate / iWritesPerSec; - pcmFrames += iRate - (iRate / iWritesPerSec) * iWritesPerSec; // add division remainder too for our buffer size - iBufferedFrames = iWritesPerSec / KUpdatesPerSec; + iMaxWriteSamples = iRate / iWritesPerSec; + if (iRate % iWritesPerSec) + iMaxWriteSamples++; + int bufferedFrames = iWritesPerSec / KUpdatesPerSec; - TInt bytesPerFrame = pcmFrames << (iStereo?2:1); + iBufferSize = iMaxWriteSamples * (iStereo ? 4 : 2); + iBufferSize *= bufferedFrames; for (TInt i=0 ; iDes().FillZ (bytesPerFrame * iBufferedFrames); + iSoundBuffers[i] = HBufC8::NewL(iBufferSize); + iSoundBuffers[i]->Des().FillZ (iBufferSize); } iCurrentBuffer = 0; iCurrentBufferSize = 0; + DEBUGPRINT(_L("sound: iMaxWriteSamples: %i, iBufferSize: %i"), iMaxWriteSamples, iBufferSize); + // here we actually test if we can create and open CMdaAudioOutputStream at all, but really create and use it later. iMdaAudioOutputStream = CMdaAudioOutputStream::NewL(iListener, iServer); - if(iMdaAudioOutputStream) { - iVolume = iMdaAudioOutputStream->MaxVolume(); - DEBUGPRINT(_L("MaxVolume: %i"), iVolume); + if (iMdaAudioOutputStream) { + if (iVolume < 0 || iVolume > iMdaAudioOutputStream->MaxVolume()) + iVolume = iMdaAudioOutputStream->MaxVolume(); delete iMdaAudioOutputStream; iMdaAudioOutputStream = 0; } @@ -113,11 +118,18 @@ void CGameAudioMS::ConstructL() // to be used when iSoundBuffers are used directly TInt16 *CGameAudioMS::NextFrameL(TInt aPcmFrames) { - iCurrentPosition += aPcmFrames << (iStereo?1:0); - iCurrentBufferSize += aPcmFrames << (iStereo?2:1); + TInt mul = iStereo ? 4 : 2; + TInt bytes = aPcmFrames * mul; + iCurrentPosition += bytes / 2; + iCurrentBufferSize += bytes; - if (++iFrameCount == iBufferedFrames) + if (aPcmFrames > iMaxWriteSamples) { + DEBUGPRINT(_L("too many samples: %i > %i"), aPcmFrames, iMaxWriteSamples); + } + + if (iCurrentBufferSize + iMaxWriteSamples * mul > iBufferSize) { + //DEBUGPRINT(_L("write on iCurrentBufferSize %i"), iCurrentBufferSize); WriteBlockL(); } @@ -162,9 +174,9 @@ void CGameAudioMS::WriteBlockL() } } - iFrameCount = 0; if (++iCurrentBuffer == KSoundBuffers) iCurrentBuffer = 0; + iSoundBuffers[iCurrentBuffer]->Des().SetMax(); iCurrentPosition = (TInt16*) iSoundBuffers[iCurrentBuffer]->Ptr(); iCurrentBufferSize = 0; } @@ -189,7 +201,6 @@ TInt16 *CGameAudioMS::ResumeL() iListener.iIsOpen = ETrue; iListener.iUnderflowed = 1; iListener.iLastError = 0; - iFrameCount = 0; iCurrentBufferSize = 0; iCurrentPosition = (TInt16*) iSoundBuffers[iCurrentBuffer]->Ptr(); return iCurrentPosition; @@ -198,7 +209,9 @@ TInt16 *CGameAudioMS::ResumeL() // handles underflow condition void CGameAudioMS::UnderflowedL() { +#ifdef DEBUG_UNDERFLOWS DEBUGPRINT(_L("UnderflowedL()")); +#endif if (iListener.iLastError != KErrUnderflow) { @@ -243,23 +256,30 @@ void CGameAudioMS::WaitForOpenToCompleteL() User::LeaveIfError(KErrNotSupported); } -void CGameAudioMS::ChangeVolume(TInt aUp) +TInt CGameAudioMS::ChangeVolume(TInt aUp) { //DEBUGPRINT(_L("CGameAudioMS::ChangeVolume(%i)"), aUp); if (iMdaAudioOutputStream) { if (aUp) { - if (iVolume < iMdaAudioOutputStream->MaxVolume()) iVolume+=5; + iVolume += 5; + if (iVolume > iMdaAudioOutputStream->MaxVolume()) + iVolume = iMdaAudioOutputStream->MaxVolume(); } else { - if (iVolume > 0) iVolume-=5; + iVolume -= 5; + if (iVolume < 0) iVolume = 0; } iMdaAudioOutputStream->SetVolume(iVolume); } + + return iVolume; } void TGameAudioEventListener::MaoscOpenComplete(TInt aError) { +#ifdef DEBUG_UNDERFLOWS DEBUGPRINT(_L("CGameAudioMS::MaoscOpenComplete, error=%d"), aError); +#endif iIsOpen = ETrue; if(aError) { @@ -284,7 +304,9 @@ void TGameAudioEventListener::MaoscBufferCopied(TInt aError, const TDesC8& aBuff void TGameAudioEventListener::MaoscPlayComplete(TInt aError) { +#ifdef DEBUG_UNDERFLOWS DEBUGPRINT(_L("CGameAudioMS::MaoscPlayComplete: %i"), aError); +#endif if(aError) { iLastError = aError; iUnderflowed++; // never happened to me while testing, but just in case