Remove pause from audio player.
[SDL.s60v3.git] / src / audio / symbian / SDL_epocaudio.cpp
blobb5bf1b900d8912ee160b07e255da904bd3d372ef
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();
105 TUint8* Buffer();
107 private:
108 void Complete(int aState, int aError);
109 TPtrC8 Data();
111 CStreamPlayer* iPlayer;
112 int iRate;
113 int iChannels;
114 TUint32 iType;
115 int iBufferSize;
116 TUint8* iBuffer;
117 TTime iStart;
118 CSimpleWait* iWait;
121 CEpocAudio::CEpocAudio(int aBufferSize, int aFill, int aRate, int aChannels, TUint32 aType)
122 : iRate(aRate)
123 , iChannels(aChannels)
124 , iType(aType)
125 , iBufferSize(aBufferSize)
127 iBuffer = new TUint8[iBufferSize];
128 memset(iBuffer, aFill, iBufferSize);
131 CEpocAudio::~CEpocAudio()
133 if(iWait != NULL)
135 iWait->Cancel();
136 delete iWait;
138 if(iPlayer != NULL)
140 iPlayer->Close();
141 delete iPlayer;
143 delete iBuffer;
146 inline CEpocAudio& CEpocAudio::Current(SDL_AudioDevice* thisdevice)
148 return *(CEpocAudio*)thisdevice->hidden;
151 void CEpocAudio::Free(SDL_AudioDevice* thisdevice)
153 CEpocAudio* ea = (CEpocAudio*)thisdevice->hidden;
154 if(ea)
156 delete ea;
157 thisdevice->hidden = NULL;
159 CActiveScheduler* as = CActiveScheduler::Current();
160 ASSERT(as->StackDepth() == 0);
161 delete as;
162 CActiveScheduler::Install(NULL);
164 ASSERT(thisdevice->hidden == NULL);
167 void CEpocAudio::ThreadInitL()
169 iWait = new CSimpleWait;
171 iPlayer = new CStreamPlayer(*this, *this);
172 iPlayer->OpenStream(iRate, iChannels, iType);
174 /// \todo Implement proper start/pause conditions
175 iPlayer->Start();
178 TUint8* CEpocAudio::Buffer()
180 iStart.UniversalTime();
181 return iBuffer;
184 void CEpocAudio::Complete(int aState, int aError)
186 if(iPlayer->Closed())
187 return;
189 switch(aError)
191 case KErrUnderflow:
192 case KErrInUse:
193 iPlayer->Start();
194 break;
195 case KErrAbort:
196 iPlayer->Open();
200 TPtrC8 CEpocAudio::Data()
202 TPtrC8 data(iBuffer, iBufferSize);
204 if(iWait->IsActive())
206 iWait->Cancel();
207 CActiveScheduler::Stop();
210 return data;
213 void CEpocAudio::Play()
217 void CEpocAudio::Wait()
219 const TInt64 bufMs = TInt64(iBufferSize) * TInt64(1000000);
220 const TInt64 specTime = bufMs / TInt64(iRate * iChannels * 2);
221 iWait->After(specTime);
223 CActiveScheduler::Start();
224 TTime end;
225 end.UniversalTime();
226 const TTimeIntervalMicroSeconds delta = end.MicroSecondsFrom(iStart);
228 const int diff = specTime - delta.Int64();
230 if(diff > 0)
232 usleep(diff);
236 /* Audio driver bootstrap functions */
238 AudioBootStrap EPOCAudio_bootstrap = {
239 "epoc\0\0\0",
240 "EPOC streaming audio\0\0\0",
241 Audio_Available,
242 Audio_CreateDevice
246 static SDL_AudioDevice *Audio_CreateDevice(int /*devindex*/)
248 SDL_AudioDevice *thisdevice;
250 /* Initialize all variables that we clean on shutdown */
251 thisdevice = new SDL_AudioDevice;
252 if ( thisdevice )
254 memset(thisdevice, 0, (sizeof *thisdevice));
255 thisdevice->hidden = NULL;
257 else
259 SDL_OutOfMemory();
260 return(0);
263 /* Set the function pointers */
264 thisdevice->OpenAudio = EPOC_OpenAudio;
265 thisdevice->WaitAudio = EPOC_WaitAudio;
266 thisdevice->PlayAudio = EPOC_PlayAudio;
267 thisdevice->GetAudioBuf = EPOC_GetAudioBuf;
268 thisdevice->CloseAudio = EPOC_CloseAudio;
269 thisdevice->ThreadInit = EPOC_ThreadInit;
270 thisdevice->free = Audio_DeleteDevice;
272 return thisdevice;
275 static void Audio_DeleteDevice(SDL_AudioDevice *device)
277 delete device;
280 static int Audio_Available(void)
282 return(1); // Audio stream modules should be always there!
285 static int EPOC_OpenAudio(SDL_AudioDevice *thisdevice, SDL_AudioSpec *spec)
287 TUint32 type = KMMFFourCCCodePCM16;
289 switch(spec->format)
291 case AUDIO_U16LSB:
292 type = KMMFFourCCCodePCMU16;
293 break;
295 case AUDIO_S16LSB:
296 type = KMMFFourCCCodePCM16;
297 break;
299 case AUDIO_U16MSB:
300 type = KMMFFourCCCodePCMU16B;
301 break;
303 case AUDIO_S16MSB:
304 type = KMMFFourCCCodePCM16B;
305 break;
307 case AUDIO_U8:
308 type = KMMFFourCCCodePCMU8;
309 break;
311 case AUDIO_S8:
312 type = KMMFFourCCCodePCM8;
313 break;
315 default:
316 spec->format = AUDIO_S16LSB;
319 if(spec->channels > 2)
320 spec->channels = 2;
322 spec->freq = CStreamPlayer::ClosestSupportedRate(spec->freq);
324 /* Allocate mixing buffer */
325 const int buflen = spec->size;
327 thisdevice->hidden = (SDL_PrivateAudioData*)new CEpocAudio(buflen, spec->silence, spec->freq, spec->channels, type);
328 thisdevice->enabled = 0; // enable only after audio engine has been initialized!
330 return 0;
333 static void EPOC_CloseAudio(SDL_AudioDevice* thisdevice)
335 CEpocAudio::Free(thisdevice);
338 static void EPOC_ThreadInit(SDL_AudioDevice *thisdevice)
340 CActiveScheduler* as = new CActiveScheduler();
341 CActiveScheduler::Install(as);
343 EpocSdlEnv::AppendCleanupItem(TSdlCleanupItem((TSdlCleanupOperation)EPOC_CloseAudio, thisdevice));
345 CEpocAudio::Current(thisdevice).ThreadInitL();
346 RThread().SetPriority(EPriorityMore);
347 thisdevice->enabled = 1;
350 /* This function waits until it is possible to write a full sound buffer */
351 static void EPOC_WaitAudio(SDL_AudioDevice* thisdevice)
353 CEpocAudio::Current(thisdevice).Wait();
356 static void EPOC_PlayAudio(SDL_AudioDevice* thisdevice)
358 CEpocAudio::Current(thisdevice).Play();
361 static Uint8 *EPOC_GetAudioBuf(SDL_AudioDevice* thisdevice)
363 return CEpocAudio::Current(thisdevice).Buffer();