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 const int KAudioBuffers
= 2;
67 class CSimpleWait
: public CTimer
70 void Wait(TTimeIntervalMicroSeconds32 aWait
);
71 static CSimpleWait
* NewL();
77 CSimpleWait
* CSimpleWait::NewL()
79 CSimpleWait
* wait
= new (ELeave
) CSimpleWait();
80 CleanupStack::PushL(wait
);
86 void CSimpleWait::Wait(TTimeIntervalMicroSeconds32 aWait
)
89 CActiveScheduler::Start();
92 CSimpleWait::CSimpleWait() : CTimer(CActive::EPriorityStandard
)
94 CActiveScheduler::Add(this);
97 void CSimpleWait::RunL()
99 CActiveScheduler::Stop();
102 class CEpocAudio
: public CBase
, public MStreamObs
, public MStreamProvider
105 static void* NewL(int BufferSize
, int aFill
);
106 inline static CEpocAudio
& Current(SDL_AudioDevice
* thisdevice
);
108 static void Free(SDL_AudioDevice
* thisdevice
);
112 void ThreadInitL(TAny
* aDevice
);
113 void Open(int iRate
, int iChannels
, TUint32 aType
, int aBytes
);
116 TBool
SetPause(TBool aPause
);
119 CEpocAudio(int aBufferSize
);
120 void Complete(int aState
, int aError
);
122 void ConstructL(int aFill
);
125 CStreamPlayer
* iPlayer
;
139 inline CEpocAudio
& CEpocAudio::Current(SDL_AudioDevice
* thisdevice
)
141 return *static_cast<CEpocAudio
*>((void*)thisdevice
->hidden
);
144 void CEpocAudio::Free(SDL_AudioDevice
* thisdevice
)
146 CEpocAudio
* ea
= static_cast<CEpocAudio
*>((void*)thisdevice
->hidden
);
149 ASSERT(ea
->iTid
== RThread().Id());
151 thisdevice
->hidden
= NULL
;
153 CActiveScheduler
* as
= CActiveScheduler::Current();
154 ASSERT(as
->StackDepth() == 0);
156 CActiveScheduler::Install(NULL
);
158 ASSERT(thisdevice
->hidden
== NULL
);
161 CEpocAudio::CEpocAudio(int aBufferSize
)
162 : iBufferSize(aBufferSize
), iPosition(-1)
166 void* CEpocAudio::NewL(int aBufferSize
, int aFill
)
168 CEpocAudio
* eAudioLib
= new (ELeave
) CEpocAudio(aBufferSize
);
169 CleanupStack::PushL(eAudioLib
);
170 eAudioLib
->ConstructL(aFill
);
175 void CEpocAudio::ConstructL(int aFill
)
177 iBuffer
= new TUint8
[KAudioBuffers
* iBufferSize
];
178 memset(iBuffer
, aFill
, KAudioBuffers
* iBufferSize
);
182 TBool
CEpocAudio::SetPause(TBool aPause
)
184 if(aPause
&& iPosition
>= 0)
190 if(!aPause
&& iPosition
< 0)
196 return iPosition
< 0;
199 void CEpocAudio::ThreadInitL(TAny
* aDevice
)
201 iTid
= RThread().Id();
202 CActiveScheduler
* as
= new (ELeave
) CActiveScheduler();
203 CActiveScheduler::Install(as
);
205 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem((TSdlCleanupOperation
)EPOC_CloseAudio
, aDevice
));
207 iWait
= CSimpleWait::NewL();
209 iPlayer
= new (ELeave
) CStreamPlayer(*this, *this);
210 iPlayer
->ConstructL();
211 iPlayer
->OpenStream(iRate
, iChannels
, iType
);
214 TUint8
* CEpocAudio::Buffer()
216 iStart
.UniversalTime();
220 CEpocAudio::~CEpocAudio()
235 void CEpocAudio::Complete(int aState
, int aError
)
237 if(iPlayer
->Closed())
251 const int KClip
= 256;
253 TPtrC8
CEpocAudio::Data()
258 TPtrC8
data(iAudioPtr
+ iPosition
, KClip
);
261 if(iPosition
>= iBufferSize
)
263 iAudioPtr
+= iBufferSize
;
265 if((iAudioPtr
- iBuffer
) >= KAudioBuffers
* iBufferSize
)
269 if(iWait
->IsActive())
272 CActiveScheduler::Stop();
278 void CEpocAudio::Play()
283 void CEpocAudio::Wait()
287 const TInt64 bufMs
= TInt64(iBufferSize
- KClip
) * TInt64(1000000);
288 const TInt64 specTime
= bufMs
/ TInt64(iRate
* iChannels
* 2);
289 iWait
->After(specTime
);
291 CActiveScheduler::Start();
294 const TTimeIntervalMicroSeconds delta
= end
.MicroSecondsFrom(iStart
);
296 const int diff
= specTime
- delta
.Int64();
298 if(diff
> 0 && diff
< 200000)
309 void CEpocAudio::Open(int aRate
, int aChannels
, TUint32 aType
, int aBytes
)
312 iChannels
= aChannels
;
314 iBufferRate
= iRate
* iChannels
* aBytes
; //1/x
317 /* Audio driver bootstrap functions */
319 AudioBootStrap EPOCAudio_bootstrap
= {
321 "EPOC streaming audio\0\0\0",
327 static SDL_AudioDevice
*Audio_CreateDevice(int /*devindex*/)
329 SDL_AudioDevice
*thisdevice
;
331 /* Initialize all variables that we clean on shutdown */
332 thisdevice
= new SDL_AudioDevice
;
335 memset(thisdevice
, 0, (sizeof *thisdevice
));
336 thisdevice
->hidden
= NULL
;
344 /* Set the function pointers */
345 thisdevice
->OpenAudio
= EPOC_OpenAudio
;
346 thisdevice
->WaitAudio
= EPOC_WaitAudio
;
347 thisdevice
->PlayAudio
= EPOC_PlayAudio
;
348 thisdevice
->GetAudioBuf
= EPOC_GetAudioBuf
;
349 thisdevice
->CloseAudio
= EPOC_CloseAudio
;
350 thisdevice
->ThreadInit
= EPOC_ThreadInit
;
351 thisdevice
->free
= Audio_DeleteDevice
;
356 static void Audio_DeleteDevice(SDL_AudioDevice
*device
)
361 static int Audio_Available(void)
363 return(1); // Audio stream modules should be always there!
366 static int EPOC_OpenAudio(SDL_AudioDevice
*thisdevice
, SDL_AudioSpec
*spec
)
368 TUint32 type
= KMMFFourCCCodePCM16
;
374 type
= KMMFFourCCCodePCMU16
;
378 type
= KMMFFourCCCodePCM16
;
382 type
= KMMFFourCCCodePCMU16B
;
386 type
= KMMFFourCCCodePCM16B
;
390 type
= KMMFFourCCCodePCMU8
;
394 type
= KMMFFourCCCodePCM8
;
398 spec
->format
= AUDIO_S16LSB
;
401 if(spec
->channels
> 2)
404 spec
->freq
= CStreamPlayer::ClosestSupportedRate(spec
->freq
);
406 /* Allocate mixing buffer */
407 const int buflen
= spec
->size
;
409 TRAPD(err
, thisdevice
->hidden
= static_cast<SDL_PrivateAudioData
*>(CEpocAudio::NewL(buflen
, spec
->silence
)));
413 CEpocAudio::Current(thisdevice
).Open(spec
->freq
, spec
->channels
, type
, bytes
);
415 CEpocAudio::Current(thisdevice
).SetPause(ETrue
);
417 thisdevice
->enabled
= 0; // enable only after audio engine has been initialized!
422 static void EPOC_CloseAudio(SDL_AudioDevice
* thisdevice
)
424 CEpocAudio::Free(thisdevice
);
427 static void EPOC_ThreadInit(SDL_AudioDevice
*thisdevice
)
429 CEpocAudio::Current(thisdevice
).ThreadInitL(thisdevice
);
430 RThread().SetPriority(EPriorityMore
);
431 thisdevice
->enabled
= 1;
434 /* This function waits until it is possible to write a full sound buffer */
435 static void EPOC_WaitAudio(SDL_AudioDevice
* thisdevice
)
437 CEpocAudio::Current(thisdevice
).Wait();
440 static void EPOC_PlayAudio(SDL_AudioDevice
* thisdevice
)
442 if(CEpocAudio::Current(thisdevice
).SetPause(SDL_GetAudioStatus() == SDL_AUDIO_PAUSED
))
443 SDL_Delay(500); //hold on the busy loop
445 CEpocAudio::Current(thisdevice
).Play();
448 static Uint8
*EPOC_GetAudioBuf(SDL_AudioDevice
* thisdevice
)
450 return CEpocAudio::Current(thisdevice
).Buffer();