1 /*******************************************************************
\r
3 * File: Audio_mediaserver.cpp
\r
5 * Author: Peter van Sebille (peter@yipton.net)
\r
7 * Modified/adapted for picodriveN by notaz, 2006
\r
9 * (c) Copyright 2006, notaz
\r
10 * (c) Copyright 2001, Peter van Sebille
\r
11 * All Rights Reserved
\r
13 *******************************************************************/
\r
15 #include "audio_mediaserver.h"
\r
19 //#define DEBUGPRINT(x...)
\r
22 const TInt KUpdatesPerSec = 10;
\r
23 const TInt KBlockTime = 1000000 / KUpdatesPerSec;
\r
24 const TInt KMaxLag = 200000; // max sound lag, lower values increase chance of underflow
\r
25 const TInt KMaxUnderflows = 50; // max underflows/API errors we are going allow in a row (to prevent lockups)
\r
28 /*******************************************
\r
32 *******************************************/
\r
34 CGameAudioMS::CGameAudioMS(TInt aRate, TBool aStereo, TInt aWritesPerSec)
\r
35 : iRate(aRate), iStereo(aStereo), iWritesPerSec(aWritesPerSec)
\r
40 CGameAudioMS* CGameAudioMS::NewL(TInt aRate, TBool aStereo, TInt aWritesPerSec)
\r
42 DEBUGPRINT(_L("CGameAudioMS::NewL(%i, %i, %i)"), aRate, aStereo, aWritesPerSec);
\r
43 CGameAudioMS* self = new(ELeave) CGameAudioMS(aRate, aStereo, aWritesPerSec);
\r
44 CleanupStack::PushL(self);
\r
46 CleanupStack::Pop(); // self
\r
51 CGameAudioMS::~CGameAudioMS()
\r
53 DEBUGPRINT(_L("CGameAudioMS::~CGameAudioMS()"));
\r
54 if(iMdaAudioOutputStream) {
\r
55 iScheduler->Schedule(); // let it finish it's stuff
\r
56 iMdaAudioOutputStream->Stop();
\r
57 delete iMdaAudioOutputStream;
\r
59 if(iServer) delete iServer;
\r
61 for (TInt i=0; i<KSoundBuffers; i++)
\r
62 delete iSoundBuffers[i];
\r
65 //if(iScheduler) delete iScheduler;
\r
69 void CGameAudioMS::ConstructL()
\r
71 iServer = CMdaServer::NewL();
\r
73 // iScheduler = CPolledActiveScheduler::NewL();
\r
74 iScheduler = CPolledActiveScheduler::Instance();
\r
77 case 11025: iMdaAudioDataSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate11025Hz; break;
\r
78 case 16000: iMdaAudioDataSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate16000Hz; break;
\r
79 case 22050: iMdaAudioDataSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate22050Hz; break;
\r
80 case 44100: iMdaAudioDataSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate44100Hz; break;
\r
81 default: iMdaAudioDataSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate8000Hz; break;
\r
84 iMdaAudioDataSettings.iChannels = (iStereo) ? TMdaAudioDataSettings::EChannelsStereo : TMdaAudioDataSettings::EChannelsMono;
\r
85 iMdaAudioDataSettings.iCaps = TMdaAudioDataSettings::ESampleRateFixed | iMdaAudioDataSettings.iSampleRate;
\r
86 iMdaAudioDataSettings.iFlags = TMdaAudioDataSettings::ENoNetworkRouting;
\r
88 int pcmFrames = iRate / iWritesPerSec;
\r
89 pcmFrames += iRate - (iRate / iWritesPerSec) * iWritesPerSec; // add division remainder too for our buffer size
\r
90 iBufferedFrames = iWritesPerSec / KUpdatesPerSec;
\r
92 TInt bytesPerFrame = pcmFrames << (iStereo?2:1);
\r
93 for (TInt i=0 ; i<KSoundBuffers ; i++)
\r
95 iSoundBuffers[i] = HBufC8::NewL(bytesPerFrame * iBufferedFrames);
\r
96 iSoundBuffers[i]->Des().FillZ (bytesPerFrame * iBufferedFrames);
\r
100 iCurrentBufferSize = 0;
\r
102 // here we actually test if we can create and open CMdaAudioOutputStream at all, but really create and use it later.
\r
103 iMdaAudioOutputStream = CMdaAudioOutputStream::NewL(iListener, iServer);
\r
104 if(iMdaAudioOutputStream) {
\r
105 iVolume = iMdaAudioOutputStream->MaxVolume();
\r
106 DEBUGPRINT(_L("MaxVolume: %i"), iVolume);
\r
107 delete iMdaAudioOutputStream;
\r
108 iMdaAudioOutputStream = 0;
\r
112 // returns a pointer to buffer for next frame,
\r
113 // to be used when iSoundBuffers are used directly
\r
114 TInt16 *CGameAudioMS::NextFrameL(TInt aPcmFrames)
\r
116 iCurrentPosition += aPcmFrames << (iStereo?1:0);
\r
117 iCurrentBufferSize += aPcmFrames << (iStereo?2:1);
\r
119 if (++iFrameCount == iBufferedFrames)
\r
124 iScheduler->Schedule();
\r
126 if(iListener.iUnderflowed) {
\r
127 if(iListener.iUnderflowed > KMaxUnderflows) {
\r
128 delete iMdaAudioOutputStream;
\r
129 iMdaAudioOutputStream = 0;
\r
132 UnderflowedL(); // not again!
\r
135 return iCurrentPosition;
\r
138 void CGameAudioMS::WriteBlockL()
\r
140 iScheduler->Schedule();
\r
141 // do not write until stream is open
\r
142 if(!iListener.iIsOpen) WaitForOpenToCompleteL();
\r
143 //if(!iListener.iHasCopied) WaitForCopyToCompleteL(); // almost never happens anyway and sometimes even deadlocks?
\r
144 //iListener.iHasCopied = EFalse;
\r
147 if(!iListener.iUnderflowed) {
\r
149 // don't write if sound is lagging too much
\r
150 delta = iTime - iMdaAudioOutputStream->Position().Int64();
\r
151 if (delta > MAKE_TINT64(0, KMaxLag))
\r
152 // another query sometimes returns very different result
\r
153 delta = iTime - iMdaAudioOutputStream->Position().Int64();
\r
155 if(delta <= MAKE_TINT64(0, KMaxLag)) {
\r
156 //RDebug::Print(_L("delta: %i"), iTime.Low() - iMdaAudioOutputStream->Position().Int64().Low());
\r
157 iSoundBuffers[iCurrentBuffer]->Des().SetLength(iCurrentBufferSize);
\r
158 iMdaAudioOutputStream->WriteL(*iSoundBuffers[iCurrentBuffer]);
\r
159 iTime += KBlockTime;
\r
161 DEBUGPRINT(_L("lag: %i"), I64LOW(delta));
\r
166 if (++iCurrentBuffer == KSoundBuffers)
\r
167 iCurrentBuffer = 0;
\r
168 iCurrentPosition = (TInt16*) iSoundBuffers[iCurrentBuffer]->Ptr();
\r
169 iCurrentBufferSize = 0;
\r
172 void CGameAudioMS::Pause()
\r
174 if(!iMdaAudioOutputStream) return;
\r
176 iScheduler->Schedule(); // let it finish it's stuff
\r
177 iMdaAudioOutputStream->Stop();
\r
178 delete iMdaAudioOutputStream;
\r
179 iMdaAudioOutputStream = 0;
\r
182 // call this before doing any playback!
\r
183 TInt16 *CGameAudioMS::ResumeL()
\r
185 DEBUGPRINT(_L("CGameAudioMS::Resume()"));
\r
186 iScheduler->Schedule();
\r
188 // we act a bit strange here: simulate buffer underflow, which actually starts audio
\r
189 iListener.iIsOpen = ETrue;
\r
190 iListener.iUnderflowed = 1;
\r
191 iListener.iLastError = 0;
\r
193 iCurrentBufferSize = 0;
\r
194 iCurrentPosition = (TInt16*) iSoundBuffers[iCurrentBuffer]->Ptr();
\r
195 return iCurrentPosition;
\r
198 // handles underflow condition
\r
199 void CGameAudioMS::UnderflowedL()
\r
201 DEBUGPRINT(_L("UnderflowedL()"));
\r
203 if (iListener.iLastError != KErrUnderflow)
\r
205 // recreate the stream
\r
206 //iMdaAudioOutputStream->Stop();
\r
207 if(iMdaAudioOutputStream) delete iMdaAudioOutputStream;
\r
208 iMdaAudioOutputStream = CMdaAudioOutputStream::NewL(iListener, iServer);
\r
209 iMdaAudioOutputStream->Open(&iMdaAudioDataSettings);
\r
210 iMdaAudioOutputStream->SetAudioPropertiesL(iMdaAudioDataSettings.iSampleRate, iMdaAudioDataSettings.iChannels);
\r
211 iMdaAudioOutputStream->SetVolume(iVolume); // new in UIQ3
\r
213 iListener.iIsOpen = EFalse; // wait for it to open
\r
214 //iListener.iHasCopied = ETrue; // but don't wait for last copy to complete
\r
215 // let it open and feed some stuff to make it happy
\r
217 iScheduler->Schedule();
\r
218 iListener.iLastError = 0;
\r
219 if(!iListener.iIsOpen) WaitForOpenToCompleteL();
\r
221 iListener.iLastError = iListener.iUnderflowed = 0;
\r
223 iTime = iMdaAudioOutputStream->Position().Int64();
\r
226 void CGameAudioMS::WaitForOpenToCompleteL()
\r
228 DEBUGPRINT(_L("CGameAudioMS::WaitForOpenToCompleteL"));
\r
229 TInt count = 20; // 2 seconds
\r
230 TInt waitPeriod = 100 * 1000;
\r
232 if(!iListener.iIsOpen) {
\r
233 // it is often enough to do this
\r
235 iScheduler->Schedule();
\r
237 while (!iListener.iIsOpen && --count)
\r
239 User::After(waitPeriod);
\r
240 iScheduler->Schedule();
\r
242 if (!iListener.iIsOpen)
\r
243 User::LeaveIfError(KErrNotSupported);
\r
246 void CGameAudioMS::ChangeVolume(TInt aUp)
\r
248 //DEBUGPRINT(_L("CGameAudioMS::ChangeVolume(%i)"), aUp);
\r
250 if (iMdaAudioOutputStream) {
\r
252 if (iVolume < iMdaAudioOutputStream->MaxVolume()) iVolume+=5;
\r
254 if (iVolume > 0) iVolume-=5;
\r
256 iMdaAudioOutputStream->SetVolume(iVolume);
\r
260 void TGameAudioEventListener::MaoscOpenComplete(TInt aError)
\r
262 DEBUGPRINT(_L("CGameAudioMS::MaoscOpenComplete, error=%d"), aError);
\r
266 iLastError = aError;
\r
269 else iUnderflowed = 0;
\r
272 void TGameAudioEventListener::MaoscBufferCopied(TInt aError, const TDesC8& aBuffer)
\r
275 DEBUGPRINT(_L("CGameAudioMS::MaoscBufferCopied, error=%d"), aError);
\r
277 // iHasCopied = ETrue;
\r
279 if(aError) { // shit!
\r
280 iLastError = aError;
\r
285 void TGameAudioEventListener::MaoscPlayComplete(TInt aError)
\r
287 DEBUGPRINT(_L("CGameAudioMS::MaoscPlayComplete: %i"), aError);
\r
289 iLastError = aError;
\r
290 iUnderflowed++; // never happened to me while testing, but just in case
\r