Remove more useless junk.
[SDL.s60v3.git] / src / audio / symbian / SDL_epocaudio.cpp
blobd49de3f69ccb4feceb2b317f5bcadfa323b4655e
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 int iBufferSize;
117 TUint8* iBuffer;
118 TTime iStart;
119 CSimpleWait* iWait;
120 bool iPause;
123 CEpocAudio::CEpocAudio(int aBufferSize, int aFill, int aRate, int aChannels, TUint32 aType)
124 : iRate(aRate)
125 , iChannels(aChannels)
126 , iType(aType)
127 , iBufferSize(aBufferSize)
128 , iPause(true)
130 iBuffer = new TUint8[iBufferSize];
131 memset(iBuffer, aFill, iBufferSize);
134 CEpocAudio::~CEpocAudio()
136 if(iWait != NULL)
138 iWait->Cancel();
139 delete iWait;
141 if(iPlayer != NULL)
143 iPlayer->Close();
144 delete iPlayer;
146 delete iBuffer;
149 inline CEpocAudio& CEpocAudio::Current(SDL_AudioDevice* thisdevice)
151 return *(CEpocAudio*)thisdevice->hidden;
154 void CEpocAudio::Free(SDL_AudioDevice* thisdevice)
156 CEpocAudio* ea = (CEpocAudio*)thisdevice->hidden;
157 if(ea)
159 delete ea;
160 thisdevice->hidden = NULL;
162 CActiveScheduler* as = CActiveScheduler::Current();
163 ASSERT(as->StackDepth() == 0);
164 delete as;
165 CActiveScheduler::Install(NULL);
167 ASSERT(thisdevice->hidden == NULL);
170 bool CEpocAudio::SetPause(bool aPause)
172 if(iPlayer != NULL && aPause != iPause)
174 if(aPause)
175 iPlayer->Stop();
176 else
177 iPlayer->Start();
180 iPause = aPause;
182 return iPause;
185 void CEpocAudio::ThreadInitL(void* aDevice)
187 CActiveScheduler* as = new CActiveScheduler();
188 CActiveScheduler::Install(as);
190 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem((TSdlCleanupOperation)EPOC_CloseAudio, aDevice));
192 iWait = new CSimpleWait;
194 iPlayer = new CStreamPlayer(*this, *this);
195 iPlayer->OpenStream(iRate, iChannels, iType);
198 TUint8* CEpocAudio::Buffer()
200 iStart.UniversalTime();
201 return iBuffer;
204 void CEpocAudio::Complete(int aState, int aError)
206 if(iPlayer->Closed())
207 return;
209 switch(aError)
211 case KErrUnderflow:
212 case KErrInUse:
213 iPlayer->Start();
214 break;
215 case KErrAbort:
216 iPlayer->Open();
220 TPtrC8 CEpocAudio::Data()
222 if(iPause)
223 return KNullDesC8();
225 TPtrC8 data(iBuffer, iBufferSize);
227 iPause = true;
228 if(iWait->IsActive())
230 iWait->Cancel();
231 CActiveScheduler::Stop();
234 return data;
237 void CEpocAudio::Play()
239 iPause = false;
242 void CEpocAudio::Wait()
244 if(!iPause)
246 const TInt64 bufMs = TInt64(iBufferSize) * TInt64(1000000);
247 const TInt64 specTime = bufMs / TInt64(iRate * iChannels * 2);
248 iWait->After(specTime);
250 CActiveScheduler::Start();
251 TTime end;
252 end.UniversalTime();
253 const TTimeIntervalMicroSeconds delta = end.MicroSecondsFrom(iStart);
255 const int diff = specTime - delta.Int64();
257 if(diff > 0)
259 usleep(diff);
262 else
264 usleep(10000);
268 /* Audio driver bootstrap functions */
270 AudioBootStrap EPOCAudio_bootstrap = {
271 "epoc\0\0\0",
272 "EPOC streaming audio\0\0\0",
273 Audio_Available,
274 Audio_CreateDevice
278 static SDL_AudioDevice *Audio_CreateDevice(int /*devindex*/)
280 SDL_AudioDevice *thisdevice;
282 /* Initialize all variables that we clean on shutdown */
283 thisdevice = new SDL_AudioDevice;
284 if ( thisdevice )
286 memset(thisdevice, 0, (sizeof *thisdevice));
287 thisdevice->hidden = NULL;
289 else
291 SDL_OutOfMemory();
292 return(0);
295 /* Set the function pointers */
296 thisdevice->OpenAudio = EPOC_OpenAudio;
297 thisdevice->WaitAudio = EPOC_WaitAudio;
298 thisdevice->PlayAudio = EPOC_PlayAudio;
299 thisdevice->GetAudioBuf = EPOC_GetAudioBuf;
300 thisdevice->CloseAudio = EPOC_CloseAudio;
301 thisdevice->ThreadInit = EPOC_ThreadInit;
302 thisdevice->free = Audio_DeleteDevice;
304 return thisdevice;
307 static void Audio_DeleteDevice(SDL_AudioDevice *device)
309 delete device;
312 static int Audio_Available(void)
314 return(1); // Audio stream modules should be always there!
317 static int EPOC_OpenAudio(SDL_AudioDevice *thisdevice, SDL_AudioSpec *spec)
319 TUint32 type = KMMFFourCCCodePCM16;
321 switch(spec->format)
323 case AUDIO_U16LSB:
324 type = KMMFFourCCCodePCMU16;
325 break;
327 case AUDIO_S16LSB:
328 type = KMMFFourCCCodePCM16;
329 break;
331 case AUDIO_U16MSB:
332 type = KMMFFourCCCodePCMU16B;
333 break;
335 case AUDIO_S16MSB:
336 type = KMMFFourCCCodePCM16B;
337 break;
339 case AUDIO_U8:
340 type = KMMFFourCCCodePCMU8;
341 break;
343 case AUDIO_S8:
344 type = KMMFFourCCCodePCM8;
345 break;
347 default:
348 spec->format = AUDIO_S16LSB;
351 if(spec->channels > 2)
352 spec->channels = 2;
354 spec->freq = CStreamPlayer::ClosestSupportedRate(spec->freq);
356 /* Allocate mixing buffer */
357 const int buflen = spec->size;
359 thisdevice->hidden = (SDL_PrivateAudioData*)new CEpocAudio(buflen, spec->silence, spec->freq, spec->channels, type);
360 thisdevice->enabled = 0; // enable only after audio engine has been initialized!
362 return 0;
365 static void EPOC_CloseAudio(SDL_AudioDevice* thisdevice)
367 CEpocAudio::Free(thisdevice);
370 static void EPOC_ThreadInit(SDL_AudioDevice *thisdevice)
372 CEpocAudio::Current(thisdevice).ThreadInitL(thisdevice);
373 RThread().SetPriority(EPriorityMore);
374 thisdevice->enabled = 1;
377 /* This function waits until it is possible to write a full sound buffer */
378 static void EPOC_WaitAudio(SDL_AudioDevice* thisdevice)
380 CEpocAudio::Current(thisdevice).Wait();
383 static void EPOC_PlayAudio(SDL_AudioDevice* thisdevice)
385 if(CEpocAudio::Current(thisdevice).SetPause(SDL_GetAudioStatus() == SDL_AUDIO_PAUSED))
386 SDL_Delay(500); //hold on the busy loop
387 else
388 CEpocAudio::Current(thisdevice).Play();
391 static Uint8 *EPOC_GetAudioBuf(SDL_AudioDevice* thisdevice)
393 return CEpocAudio::Current(thisdevice).Buffer();