2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 slouken@devolution.com
31 #include <sys/ioctl.h>
40 #include "SDL_audio.h"
41 #include "SDL_error.h"
42 #include "SDL_audiomem.h"
43 #include "SDL_audio_c.h"
44 #include "SDL_timer.h"
45 #include "SDL_audiodev_c.h"
48 #include "SDL_epocaudio.h"
50 #include "streamplayer.h"
52 /* Audio driver functions */
54 static int EPOC_OpenAudio(SDL_AudioDevice
*thisdevice
, SDL_AudioSpec
*spec
);
55 static void EPOC_WaitAudio(SDL_AudioDevice
*thisdevice
);
56 static void EPOC_PlayAudio(SDL_AudioDevice
*thisdevice
);
57 static Uint8
*EPOC_GetAudioBuf(SDL_AudioDevice
*thisdevice
);
58 static void EPOC_CloseAudio(SDL_AudioDevice
*thisdevice
);
59 static void EPOC_ThreadInit(SDL_AudioDevice
*thisdevice
);
61 static int Audio_Available(void);
62 static SDL_AudioDevice
*Audio_CreateDevice(int devindex
);
63 static void Audio_DeleteDevice(SDL_AudioDevice
*device
);
65 class CSimpleWait
: public CTimer
68 void Wait(TTimeIntervalMicroSeconds32 aWait
);
69 static CSimpleWait
* NewL();
75 CSimpleWait
* CSimpleWait::NewL()
77 CSimpleWait
* wait
= new CSimpleWait();
78 CleanupStack::PushL(wait
);
84 void CSimpleWait::Wait(TTimeIntervalMicroSeconds32 aWait
)
87 CActiveScheduler::Start();
90 CSimpleWait::CSimpleWait() : CTimer(CActive::EPriorityStandard
)
92 CActiveScheduler::Add(this);
95 void CSimpleWait::RunL()
97 CActiveScheduler::Stop();
100 class CEpocAudio
: public CBase
, public MStreamObs
, public MStreamProvider
103 static void* NewL(int BufferSize
, int aFill
);
104 inline static CEpocAudio
& Current(SDL_AudioDevice
* thisdevice
);
106 static void Free(SDL_AudioDevice
* thisdevice
);
110 void ThreadInitL(void* aDevice
);
111 void Open(int iRate
, int iChannels
, TUint32 aType
, int aBytes
);
114 TBool
SetPause(TBool aPause
);
117 CEpocAudio(int aBufferSize
);
118 void Complete(int aState
, int aError
);
120 void ConstructL(int aFill
);
123 CStreamPlayer
* iPlayer
;
137 inline CEpocAudio
& CEpocAudio::Current(SDL_AudioDevice
* thisdevice
)
139 return *static_cast<CEpocAudio
*>((void*)thisdevice
->hidden
);
142 void CEpocAudio::Free(SDL_AudioDevice
* thisdevice
)
144 CEpocAudio
* ea
= static_cast<CEpocAudio
*>((void*)thisdevice
->hidden
);
147 ASSERT(ea
->iTid
== RThread().Id());
149 thisdevice
->hidden
= NULL
;
151 CActiveScheduler
* as
= CActiveScheduler::Current();
152 ASSERT(as
->StackDepth() == 0);
154 CActiveScheduler::Install(NULL
);
156 ASSERT(thisdevice
->hidden
== NULL
);
159 CEpocAudio::CEpocAudio(int aBufferSize
)
160 : iBufferSize(aBufferSize
), iPosition(-1)
164 void* CEpocAudio::NewL(int aBufferSize
, int aFill
)
166 CEpocAudio
* eAudioLib
= new CEpocAudio(aBufferSize
);
167 CleanupStack::PushL(eAudioLib
);
168 eAudioLib
->ConstructL(aFill
);
173 void CEpocAudio::ConstructL(int aFill
)
175 iBuffer
= new TUint8
[iBufferSize
];
176 memset(iBuffer
, aFill
, iBufferSize
);
180 TBool
CEpocAudio::SetPause(TBool aPause
)
182 if(aPause
&& iPosition
>= 0)
188 if(!aPause
&& iPosition
< 0)
194 return iPosition
< 0;
197 void CEpocAudio::ThreadInitL(void* aDevice
)
199 iTid
= RThread().Id();
200 CActiveScheduler
* as
= new CActiveScheduler();
201 CActiveScheduler::Install(as
);
203 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem((TSdlCleanupOperation
)EPOC_CloseAudio
, aDevice
));
205 iWait
= CSimpleWait::NewL();
207 iPlayer
= new CStreamPlayer(*this, *this);
208 iPlayer
->ConstructL();
209 iPlayer
->OpenStream(iRate
, iChannels
, iType
);
212 TUint8
* CEpocAudio::Buffer()
214 iStart
.UniversalTime();
218 CEpocAudio::~CEpocAudio()
233 void CEpocAudio::Complete(int aState
, int aError
)
235 if(iPlayer
->Closed())
249 const int KClip
= 256;
251 TPtrC8
CEpocAudio::Data()
256 TPtrC8
data(iAudioPtr
+ iPosition
, KClip
);
259 if(iPosition
>= iBufferSize
)
261 iAudioPtr
+= iBufferSize
;
263 if((iAudioPtr
- iBuffer
) >= iBufferSize
)
267 if(iWait
->IsActive())
270 CActiveScheduler::Stop();
276 void CEpocAudio::Play()
281 void CEpocAudio::Wait()
285 const TInt64 bufMs
= TInt64(iBufferSize
- KClip
) * TInt64(1000000);
286 const TInt64 specTime
= bufMs
/ TInt64(iRate
* iChannels
* 2);
287 iWait
->After(specTime
);
289 CActiveScheduler::Start();
292 const TTimeIntervalMicroSeconds delta
= end
.MicroSecondsFrom(iStart
);
294 const int diff
= specTime
- delta
.Int64();
296 if(diff
> 0 && diff
< 200000)
307 void CEpocAudio::Open(int aRate
, int aChannels
, TUint32 aType
, int aBytes
)
310 iChannels
= aChannels
;
312 iBufferRate
= iRate
* iChannels
* aBytes
; //1/x
315 /* Audio driver bootstrap functions */
317 AudioBootStrap EPOCAudio_bootstrap
= {
319 "EPOC streaming audio\0\0\0",
325 static SDL_AudioDevice
*Audio_CreateDevice(int /*devindex*/)
327 SDL_AudioDevice
*thisdevice
;
329 /* Initialize all variables that we clean on shutdown */
330 thisdevice
= new SDL_AudioDevice
;
333 memset(thisdevice
, 0, (sizeof *thisdevice
));
334 thisdevice
->hidden
= NULL
;
342 /* Set the function pointers */
343 thisdevice
->OpenAudio
= EPOC_OpenAudio
;
344 thisdevice
->WaitAudio
= EPOC_WaitAudio
;
345 thisdevice
->PlayAudio
= EPOC_PlayAudio
;
346 thisdevice
->GetAudioBuf
= EPOC_GetAudioBuf
;
347 thisdevice
->CloseAudio
= EPOC_CloseAudio
;
348 thisdevice
->ThreadInit
= EPOC_ThreadInit
;
349 thisdevice
->free
= Audio_DeleteDevice
;
354 static void Audio_DeleteDevice(SDL_AudioDevice
*device
)
359 static int Audio_Available(void)
361 return(1); // Audio stream modules should be always there!
364 static int EPOC_OpenAudio(SDL_AudioDevice
*thisdevice
, SDL_AudioSpec
*spec
)
366 TUint32 type
= KMMFFourCCCodePCM16
;
372 type
= KMMFFourCCCodePCMU16
;
376 type
= KMMFFourCCCodePCM16
;
380 type
= KMMFFourCCCodePCMU16B
;
384 type
= KMMFFourCCCodePCM16B
;
388 type
= KMMFFourCCCodePCMU8
;
392 type
= KMMFFourCCCodePCM8
;
396 spec
->format
= AUDIO_S16LSB
;
399 if(spec
->channels
> 2)
402 spec
->freq
= CStreamPlayer::ClosestSupportedRate(spec
->freq
);
404 /* Allocate mixing buffer */
405 const int buflen
= spec
->size
;
407 TRAPD(err
, thisdevice
->hidden
= static_cast<SDL_PrivateAudioData
*>(CEpocAudio::NewL(buflen
, spec
->silence
)));
411 CEpocAudio::Current(thisdevice
).Open(spec
->freq
, spec
->channels
, type
, bytes
);
413 CEpocAudio::Current(thisdevice
).SetPause(ETrue
);
415 thisdevice
->enabled
= 0; // enable only after audio engine has been initialized!
420 static void EPOC_CloseAudio(SDL_AudioDevice
* thisdevice
)
422 CEpocAudio::Free(thisdevice
);
425 static void EPOC_ThreadInit(SDL_AudioDevice
*thisdevice
)
427 CEpocAudio::Current(thisdevice
).ThreadInitL(thisdevice
);
428 RThread().SetPriority(EPriorityMore
);
429 thisdevice
->enabled
= 1;
432 /* This function waits until it is possible to write a full sound buffer */
433 static void EPOC_WaitAudio(SDL_AudioDevice
* thisdevice
)
435 CEpocAudio::Current(thisdevice
).Wait();
438 static void EPOC_PlayAudio(SDL_AudioDevice
* thisdevice
)
440 if(CEpocAudio::Current(thisdevice
).SetPause(SDL_GetAudioStatus() == SDL_AUDIO_PAUSED
))
441 SDL_Delay(500); //hold on the busy loop
443 CEpocAudio::Current(thisdevice
).Play();
446 static Uint8
*EPOC_GetAudioBuf(SDL_AudioDevice
* thisdevice
)
448 return CEpocAudio::Current(thisdevice
).Buffer();