Simplify code.
[SDL.s60v3.git] / src / audio / symbian / SDL_epocaudio.cpp
blobb0e9fa0650231143193dd4d7b7297bfd99ad0302
1 /*
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
19 Sam Lantinga
20 slouken@devolution.com
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <sys/time.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
34 #include "epoc_sdl.h"
36 #include <e32hal.h>
39 extern "C" {
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"
46 #include "SDL_sysaudio.h"
49 #include "streamplayer.h"
51 /* Audio driver functions */
53 static int EPOC_OpenAudio(SDL_AudioDevice *thisdevice, SDL_AudioSpec *spec);
54 static void EPOC_WaitAudio(SDL_AudioDevice *thisdevice);
55 static void EPOC_PlayAudio(SDL_AudioDevice *thisdevice);
56 static Uint8 *EPOC_GetAudioBuf(SDL_AudioDevice *thisdevice);
57 static void EPOC_CloseAudio(SDL_AudioDevice *thisdevice);
58 static void EPOC_ThreadInit(SDL_AudioDevice *thisdevice);
60 static int Audio_Available(void);
61 static SDL_AudioDevice *Audio_CreateDevice(int devindex);
62 static void Audio_DeleteDevice(SDL_AudioDevice *device);
64 class CSimpleWait : public CTimer
66 public:
67 CSimpleWait();
68 void Wait(TTimeIntervalMicroSeconds32 aWait);
70 private:
71 void RunL();
74 CSimpleWait::CSimpleWait()
75 : CTimer(CActive::EPriorityStandard)
77 CActiveScheduler::Add(this);
78 ConstructL();
81 void CSimpleWait::Wait(TTimeIntervalMicroSeconds32 aWait)
83 After(aWait);
84 CActiveScheduler::Start();
87 void CSimpleWait::RunL()
89 CActiveScheduler::Stop();
92 class CEpocAudio : public CBase, public MStreamObs, public MStreamProvider
94 public:
95 CEpocAudio(int aBufferSize, int aFill, int aRate, int aChannels, TUint32 aType);
96 ~CEpocAudio();
98 inline static CEpocAudio& Current(SDL_AudioDevice* thisdevice);
100 static void Free(SDL_AudioDevice* thisdevice);
102 void Wait();
103 void Play();
104 void ThreadInitL(void* aDevice);
105 TUint8* Buffer();
106 bool SetPause(bool aPause);
108 private:
109 void Complete(int aState, int aError);
110 TPtrC8 Data();
112 CStreamPlayer* iPlayer;
113 int iRate;
114 int iChannels;
115 TUint32 iType;
116 TThreadId iTid;
117 int iBufferSize;
118 TUint8* iBuffer;
119 TTime iStart;
120 int iTune;
121 CSimpleWait* iWait;
122 bool iPause;
125 CEpocAudio::CEpocAudio(int aBufferSize, int aFill, int aRate, int aChannels, TUint32 aType)
126 : iRate(aRate)
127 , iChannels(aChannels)
128 , iType(aType)
129 , iBufferSize(aBufferSize)
130 , iPause(true)
132 iBuffer = new TUint8[iBufferSize];
133 memset(iBuffer, aFill, iBufferSize);
136 CEpocAudio::~CEpocAudio()
138 if(iWait != NULL)
140 iWait->Cancel();
141 delete iWait;
143 if(iPlayer != NULL)
145 iPlayer->Close();
146 delete iPlayer;
148 delete iBuffer;
151 inline CEpocAudio& CEpocAudio::Current(SDL_AudioDevice* thisdevice)
153 return *(CEpocAudio*)thisdevice->hidden;
156 void CEpocAudio::Free(SDL_AudioDevice* thisdevice)
158 CEpocAudio* ea = (CEpocAudio*)thisdevice->hidden;
159 if(ea)
161 ASSERT(ea->iTid == RThread().Id());
162 delete ea;
163 thisdevice->hidden = NULL;
165 CActiveScheduler* as = CActiveScheduler::Current();
166 ASSERT(as->StackDepth() == 0);
167 delete as;
168 CActiveScheduler::Install(NULL);
170 ASSERT(thisdevice->hidden == NULL);
173 bool CEpocAudio::SetPause(bool aPause)
175 if(iPlayer != NULL && aPause != iPause)
177 if(aPause)
178 iPlayer->Stop();
179 else
180 iPlayer->Start();
183 iPause = aPause;
185 return iPause;
188 void CEpocAudio::ThreadInitL(void* aDevice)
190 iTid = RThread().Id();
191 CActiveScheduler* as = new CActiveScheduler();
192 CActiveScheduler::Install(as);
194 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem((TSdlCleanupOperation)EPOC_CloseAudio, aDevice));
196 iWait = new CSimpleWait;
198 iPlayer = new CStreamPlayer(*this, *this);
199 iPlayer->OpenStream(iRate, iChannels, iType);
202 TUint8* CEpocAudio::Buffer()
204 iStart.UniversalTime();
205 return iBuffer;
208 void CEpocAudio::Complete(int aState, int aError)
210 if(iPlayer->Closed())
211 return;
213 switch(aError)
215 case KErrUnderflow:
216 case KErrInUse:
217 iPlayer->Start();
218 break;
219 case KErrAbort:
220 iPlayer->Open();
224 TPtrC8 CEpocAudio::Data()
226 if(iPause)
227 return KNullDesC8();
229 TPtrC8 data(iBuffer, iBufferSize);
231 iPause = true;
232 if(iWait->IsActive())
234 iWait->Cancel();
235 CActiveScheduler::Stop();
238 return data;
241 void CEpocAudio::Play()
243 iPause = false;
246 void CEpocAudio::Wait()
248 if(!iPause)
250 const TInt64 bufMs = TInt64(iBufferSize) * TInt64(1000000);
251 const TInt64 specTime = bufMs / TInt64(iRate * iChannels * 2);
252 iWait->After(specTime);
254 CActiveScheduler::Start();
255 TTime end;
256 end.UniversalTime();
257 const TTimeIntervalMicroSeconds delta = end.MicroSecondsFrom(iStart);
259 const int diff = specTime - delta.Int64();
261 if(diff > 0)
263 usleep(diff);
266 else
268 usleep(10000);
272 /* Audio driver bootstrap functions */
274 AudioBootStrap EPOCAudio_bootstrap = {
275 "epoc\0\0\0",
276 "EPOC streaming audio\0\0\0",
277 Audio_Available,
278 Audio_CreateDevice
282 static SDL_AudioDevice *Audio_CreateDevice(int /*devindex*/)
284 SDL_AudioDevice *thisdevice;
286 /* Initialize all variables that we clean on shutdown */
287 thisdevice = new SDL_AudioDevice;
288 if ( thisdevice )
290 memset(thisdevice, 0, (sizeof *thisdevice));
291 thisdevice->hidden = NULL;
293 else
295 SDL_OutOfMemory();
296 return(0);
299 /* Set the function pointers */
300 thisdevice->OpenAudio = EPOC_OpenAudio;
301 thisdevice->WaitAudio = EPOC_WaitAudio;
302 thisdevice->PlayAudio = EPOC_PlayAudio;
303 thisdevice->GetAudioBuf = EPOC_GetAudioBuf;
304 thisdevice->CloseAudio = EPOC_CloseAudio;
305 thisdevice->ThreadInit = EPOC_ThreadInit;
306 thisdevice->free = Audio_DeleteDevice;
308 return thisdevice;
311 static void Audio_DeleteDevice(SDL_AudioDevice *device)
313 delete device;
316 static int Audio_Available(void)
318 return(1); // Audio stream modules should be always there!
321 static int EPOC_OpenAudio(SDL_AudioDevice *thisdevice, SDL_AudioSpec *spec)
323 TUint32 type = KMMFFourCCCodePCM16;
325 switch(spec->format)
327 case AUDIO_U16LSB:
328 type = KMMFFourCCCodePCMU16;
329 break;
331 case AUDIO_S16LSB:
332 type = KMMFFourCCCodePCM16;
333 break;
335 case AUDIO_U16MSB:
336 type = KMMFFourCCCodePCMU16B;
337 break;
339 case AUDIO_S16MSB:
340 type = KMMFFourCCCodePCM16B;
341 break;
343 case AUDIO_U8:
344 type = KMMFFourCCCodePCMU8;
345 break;
347 case AUDIO_S8:
348 type = KMMFFourCCCodePCM8;
349 break;
351 default:
352 spec->format = AUDIO_S16LSB;
355 if(spec->channels > 2)
356 spec->channels = 2;
358 spec->freq = CStreamPlayer::ClosestSupportedRate(spec->freq);
360 /* Allocate mixing buffer */
361 const int buflen = spec->size;
363 thisdevice->hidden = (SDL_PrivateAudioData*)new CEpocAudio(buflen, spec->silence, spec->freq, spec->channels, type);
364 thisdevice->enabled = 0; // enable only after audio engine has been initialized!
366 return 0;
369 static void EPOC_CloseAudio(SDL_AudioDevice* thisdevice)
371 CEpocAudio::Free(thisdevice);
374 static void EPOC_ThreadInit(SDL_AudioDevice *thisdevice)
376 CEpocAudio::Current(thisdevice).ThreadInitL(thisdevice);
377 RThread().SetPriority(EPriorityMore);
378 thisdevice->enabled = 1;
381 /* This function waits until it is possible to write a full sound buffer */
382 static void EPOC_WaitAudio(SDL_AudioDevice* thisdevice)
384 CEpocAudio::Current(thisdevice).Wait();
387 static void EPOC_PlayAudio(SDL_AudioDevice* thisdevice)
389 if(CEpocAudio::Current(thisdevice).SetPause(SDL_GetAudioStatus() == SDL_AUDIO_PAUSED))
390 SDL_Delay(500); //hold on the busy loop
391 else
392 CEpocAudio::Current(thisdevice).Play();
395 static Uint8 *EPOC_GetAudioBuf(SDL_AudioDevice* thisdevice)
397 return CEpocAudio::Current(thisdevice).Buffer();