Mercurial > sdl-ios-xcode
annotate src/audio/dma/SDL_dmaaudio.c @ 4157:baf615f9f2a0 SDL-1.2
Date: Thu, 16 Oct 2008 20:27:34 +0400
From: "Ilya Kasnacheev" <ilya.kasnacheev@gmail.com>
To: sdl@lists.libsdl.org
Subject: [SDL] SDL for Windows CE: a few GAPI patches
Hi *!
I've just ported a POWDER roguelike ( http://www.zincland.com/powder/ ) to
Windows CE (PDAs, Windows Mobile/Pocket PC). To do that, I had to get libsdl
working. Thanks for the awesome project files, it built without a hitch.
Nevertheless, I've found quite a few bugs in Windows CE (GAPI) SDL
implementation, which I've solved and now present as a serie of patches.
I'll try carefully annotate them. Please annotate them so I can work
toward accepting
them into the main source tree since without them SDL isn't really working on
Windows CE (I wonder why nobody fixed them before, btw: why isn't SDL popular as
a way to develop Windows CE games? Where are no ports?)
These changes can't be considered flawless, but they can be considered working
because I've yet to hear complains about things I fixed and POWDER build for
Windows CE is now considered stable.
Note: my comments start with !!, delete them before applying.
diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.c
SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c
--- SDL-1.2.13/src/video/gapi/SDL_gapivideo.c 2007-12-31
07:48:00.000000000 +0300
+++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c 2008-10-16
20:02:11.000000000 +0400
@@ -643,6 +643,7 @@
}
gapi->userOrientation = SDL_ORIENTATION_UP;
+ gapi->systemOrientation = SDL_ORIENTATION_UP;
video->flags = SDL_FULLSCREEN; /* Clear flags, GAPI supports
fullscreen only */
/* GAPI or VGA? */
@@ -661,18 +662,21 @@
}
/* detect user landscape mode */
- if( (width > height) && (GetSystemMetrics(SM_CXSCREEN) <
GetSystemMetrics(SM_CYSCREEN)))
+ if( (width > height) && (gapi->gxProperties.cxWidth <
gapi->gxProperties.cyHeight))
gapi->userOrientation = SDL_ORIENTATION_RIGHT;
+ if(GetSystemMetrics(SM_CYSCREEN) < GetSystemMetrics(SM_CXSCREEN))
+ gapi->systemOrientation = SDL_ORIENTATION_RIGHT;
+
/* shall we apply hires fix? for example when we do not use
hires resource */
gapi->hiresFix = 0;
- if( gapi->userOrientation == SDL_ORIENTATION_RIGHT )
+ if( gapi->systemOrientation == gapi->userOrientation )
{
- if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height
> GetSystemMetrics(SM_CXSCREEN)))
+ if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height
> GetSystemMetrics(SM_CYSCREEN)))
gapi->hiresFix = 1;
} else
- if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height
> GetSystemMetrics(SM_CYSCREEN)))
- if( !((width == GetSystemMetrics(SM_CYSCREEN))
&& (height == GetSystemMetrics(SM_CXSCREEN)))) // user portrait,
device landscape
+ if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height
> GetSystemMetrics(SM_CXSCREEN)))
+// if( !((width == gapi->gxProperties.cyHeight)
&& (height == gapi->gxProperties.cxWidth))) // user portrait, device
landscape
gapi->hiresFix = 1;
switch( gapi->userOrientation )
!! It used to query system metrics which return dimensions according to screen
!! orientation, which can really be portrait, left landscape or right landscape.
!! This is presumably incorrect because we couldn't care less about user mode
!! dimensions - all we want are the GAPI framebuffer dimensions, which
only match
!! user dimensions in one of possible orientations.
!! There's a fair dose of cargo cult programming involved in this fix, but it
!! used to work only in one orientation (portrait for PDAs, where frame-buffer
!! have same orientation as user screen), and now it works on all orientations.
@@ -742,21 +746,30 @@
WIN_FlushMessageQueue();
/* Open GAPI display */
- if( !gapi->useVga && this->hidden->useGXOpenDisplay )
+ if( !gapi->useVga && this->hidden->useGXOpenDisplay &&
!this->hidden->alreadyGXOpened )
+ {
+ this->hidden->alreadyGXOpened = 1;
if( !gapi->gxFunc.GXOpenDisplay(SDL_Window, GX_FULLSCREEN) )
{
SDL_SetError("Couldn't initialize GAPI");
return(NULL);
}
+ }
#if REPORT_VIDEO_INFO
printf("Video properties:\n");
printf("display bpp: %d\n", gapi->gxProperties.cBPP);
printf("display width: %d\n", gapi->gxProperties.cxWidth);
printf("display height: %d\n", gapi->gxProperties.cyHeight);
+ printf("system display width: %d\n", GetSystemMetrics(SM_CXSCREEN));
+ printf("system display height: %d\n", GetSystemMetrics(SM_CYSCREEN));
printf("x pitch: %d\n", gapi->gxProperties.cbxPitch);
printf("y pitch: %d\n", gapi->gxProperties.cbyPitch);
printf("gapi flags: 0x%x\n", gapi->gxProperties.ffFormat);
+ printf("user orientation: %d\n", gapi->userOrientation);
+ printf("system orientation: %d\n", gapi->userOrientation);
+ printf("gapi orientation: %d\n", gapi->gapiOrientation);
+
if( !gapi->useVga && this->hidden->useGXOpenDisplay && gapi->needUpdate)
{
!! Previous version used to call gapi->gxFunc.GXOpenDisplay each time the video
!! mode would be changed. You shouldn't, because this call has a
meaning "Lock the
!! GAPI framebuffer, designate it as busy", so the second call will fail (it is
!! already locked/busy).
!! Testing might not find that because most programs set up the video mode only
!! once, but POWDER does this once in a while, so it crashed when in
320x240 mode
!! (640x480 mode doesn't use that code, it worked fine).
diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.h
SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h
--- SDL-1.2.13/src/video/gapi/SDL_gapivideo.h 2007-12-31
07:48:00.000000000 +0300
+++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h 2008-10-16
20:02:11.000000000 +0400
@@ -132,12 +132,17 @@
#define NUM_MODELISTS 4 /* 8, 16, 24, and 32 bits-per-pixel */
int SDL_nummodes[NUM_MODELISTS];
SDL_Rect **SDL_modelist[NUM_MODELISTS];
+ // The orientation of the video mode user wants to get
+ // Probably restricted to UP and RIGHT
enum SDL_ScreenOrientation userOrientation;
int invert;
char hiresFix; // using hires mode without defining hires resource
// --------------
int useGXOpenDisplay; /* use GXOpenDispplay */
+ int alreadyGXOpened;
int w, h;
+ // The orientation of GAPI framebuffer.
+ // Never changes on the same device.
enum SDL_ScreenOrientation gapiOrientation;
void *buffer; // may be 8, 16, 24, 32 bpp
@@ -153,6 +158,10 @@
int startOffset; // in bytes
int useVga;
int suspended; // do not pu anything into video memory
+ // The orientation of the system, as defined by SM_CXSCREEN
and SM_CYSCREEN
+ // User can change it by using 'screen layout' in system options
+ // Restricted to UP or RIGHT
+ enum SDL_ScreenOrientation systemOrientation;
};
!! This is a flag variable, see the previous comment
!! And yet another orientation: now we have to keep three of them in mind.
diff -bru SDL-1.2.13/src/video/wincommon/SDL_sysevents.c
SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c
--- SDL-1.2.13/src/video/wincommon/SDL_sysevents.c 2007-12-31
07:48:02.000000000 +0300
+++ SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c 2008-10-16
20:02:12.000000000 +0400
@@ -160,10 +160,22 @@
#endif */
}
break;
+ // FIXME: Older version used just SDL_VideoSurface->(w, h)
+ // w and h are "clipped" while x and y are "raw", which caused
+ // x in former and y in latter case to be clipped in a
wrong direction,
+ // thus offsetting the coordinate on 2 x clip pixels
+ // (like, 128 for 640 -> 512 clipping).
+ // We will now try to extract and use raw values.
+ // The way to do that RIGHT is do
(orientation-dependent) clipping before
+ // doing this transform, but it's hardly possible.
+
+ // SEE SDL_mouse.c /ClipOffset to understand these calculations.
case SDL_ORIENTATION_RIGHT:
if (!SDL_VideoSurface)
break;
- rotatedX = SDL_VideoSurface->w - *y;
+ rotatedX = (2 *
((SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
+ SDL_VideoSurface->format->BytesPerPixel))
+ + SDL_VideoSurface->w - *y;
rotatedY = *x;
*x = rotatedX;
*y = rotatedY;
@@ -172,7 +184,8 @@
if (!SDL_VideoSurface)
break;
rotatedX = *y;
- rotatedY = SDL_VideoSurface->h - *x;
+ rotatedY = (2 *
(SDL_VideoSurface->offset/SDL_VideoSurface->pitch))
+ + SDL_VideoSurface->h - *x;
*x = rotatedX;
*y = rotatedY;
break;
!! That's the trickest part, hence the long comment.
!! GAPI would really support only 320x240 or 640x480 mode, if application
!! requested the different screen size (as POWDER did, wishing
256x192), then SDL
!! is going to grab the first mode that fits the requested, and pad the screen
!! with black bars (as they do with wide-screen films).
!! It would also get, say, 240x320 mode, and to turn it into 256x192 it would
!! need to rotate mouse clicks.
!! It worked, but one bug slipped through: it would receive mouse clicks
!! unpadded, then rotate them, and then pad the black bars. The
problem is: rotate
!! is done by GAPI driver while padding is done by SDL core. SDL core
doesn't know
!! anything about rotating, so it would pad one of dimensions incorrectly.
I understand that some of my claims (or code) might seem unbacked, but you can
always grab the POWDER binary, compile your own libsdl with one or more of
those fixes turned off, and see how weird it would misbehave. I can even supply
you with those custom builds of libsdl if you don't want to set up the build
environment for windows ce, you'll just need a PDA or a smartphone with it.
I plan to take care of SDL on Windows CE as long as I maintain the POWDER port.
POWDER is good for that because it:
Employs both padded (with centered image, black bars) and unpadded
(image occupies full screen) graphics; initializes video more than
once; uses both 320x240 and 640x480 video; uses both stylus and
buttons.
There's still a list of unresolved issues which I'm planning to fix:
1) Arrow buttons on PDA return weird scancodes compared to PC, this
caused the game to misbehave before I've fixed that. You can see it on
those diagrams:
http://wrar.name/upload/powder-htc.png
http://wrar.name/upload/powder-pda.png
2) SDL (or underlying windows) doesn't care to rotate arrow presses
when we're in a low-res GAPI mode, but it will rotate them in VGA mode
(because of different screen orientations, the same arrow buttons can
suddently mean different directions). Solution: we should stick to
GAPI user orientation (the orientation the program supposedly wants)
and rotate the keys on our own.
_______________________________________________
SDL mailing list
SDL@lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 07 Nov 2008 04:15:36 +0000 |
parents | d910939febfa |
children | 782fd950bd46 c121d94672cb a1b03ba2fcd0 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
94
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* Allow access to a raw mixing buffer */ | |
25 | |
26 #include <stdio.h> | |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
27 #include <string.h> /* For strerror() */ |
0 | 28 #include <errno.h> |
29 #include <unistd.h> | |
30 #include <fcntl.h> | |
31 #include <signal.h> | |
32 #include <sys/types.h> | |
33 #include <sys/time.h> | |
34 #include <sys/ioctl.h> | |
35 #include <sys/stat.h> | |
36 #include <sys/mman.h> | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
37 |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
38 #if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
92
diff
changeset
|
39 /* This is installed on some systems */ |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
92
diff
changeset
|
40 #include <soundcard.h> |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
92
diff
changeset
|
41 #else |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
92
diff
changeset
|
42 /* This is recommended by OSS */ |
0 | 43 #include <sys/soundcard.h> |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
92
diff
changeset
|
44 #endif |
0 | 45 |
46 #ifndef MAP_FAILED | |
47 #define MAP_FAILED ((Uint8 *)-1) | |
48 #endif | |
49 | |
1358
c71e05b4dc2e
More header massaging... works great on Windows. ;-)
Sam Lantinga <slouken@libsdl.org>
parents:
1341
diff
changeset
|
50 #include "SDL_timer.h" |
0 | 51 #include "SDL_audio.h" |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
52 #include "../SDL_audio_c.h" |
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
53 #include "../SDL_audiodev_c.h" |
0 | 54 #include "SDL_dmaaudio.h" |
55 | |
56 /* The tag name used by DMA audio */ | |
57 #define DMA_DRIVER_NAME "dma" | |
58 | |
59 /* Open the audio device for playback, and don't block if busy */ | |
60 #define OPEN_FLAGS (O_RDWR|O_NONBLOCK) | |
61 | |
62 /* Audio driver functions */ | |
63 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
64 static void DMA_WaitAudio(_THIS); | |
65 static void DMA_PlayAudio(_THIS); | |
66 static Uint8 *DMA_GetAudioBuf(_THIS); | |
67 static void DMA_CloseAudio(_THIS); | |
68 | |
69 /* Audio driver bootstrap functions */ | |
70 | |
71 static int Audio_Available(void) | |
72 { | |
73 int available; | |
74 int fd; | |
75 | |
76 available = 0; | |
77 | |
78 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0); | |
79 if ( fd >= 0 ) { | |
80 int caps; | |
81 struct audio_buf_info info; | |
82 | |
83 if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) && | |
84 (caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) && | |
85 (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) { | |
86 available = 1; | |
87 } | |
88 close(fd); | |
89 } | |
90 return(available); | |
91 } | |
92 | |
93 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
94 { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
95 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
96 SDL_free(device); |
0 | 97 } |
98 | |
99 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
100 { | |
101 SDL_AudioDevice *this; | |
102 | |
103 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
104 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); |
0 | 105 if ( this ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
106 SDL_memset(this, 0, (sizeof *this)); |
0 | 107 this->hidden = (struct SDL_PrivateAudioData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
108 SDL_malloc((sizeof *this->hidden)); |
0 | 109 } |
110 if ( (this == NULL) || (this->hidden == NULL) ) { | |
111 SDL_OutOfMemory(); | |
112 if ( this ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
113 SDL_free(this); |
0 | 114 } |
115 return(0); | |
116 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
117 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); |
0 | 118 audio_fd = -1; |
119 | |
120 /* Set the function pointers */ | |
121 this->OpenAudio = DMA_OpenAudio; | |
122 this->WaitAudio = DMA_WaitAudio; | |
123 this->PlayAudio = DMA_PlayAudio; | |
124 this->GetAudioBuf = DMA_GetAudioBuf; | |
125 this->CloseAudio = DMA_CloseAudio; | |
126 | |
127 this->free = Audio_DeleteDevice; | |
128 | |
129 return this; | |
130 } | |
131 | |
132 AudioBootStrap DMA_bootstrap = { | |
133 DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio", | |
134 Audio_Available, Audio_CreateDevice | |
135 }; | |
136 | |
137 /* This function waits until it is possible to write a full sound buffer */ | |
138 static void DMA_WaitAudio(_THIS) | |
139 { | |
140 fd_set fdset; | |
141 | |
142 /* Check to see if the thread-parent process is still alive */ | |
143 { static int cnt = 0; | |
144 /* Note that this only works with thread implementations | |
145 that use a different process id for each thread. | |
146 */ | |
147 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
148 if ( kill(parent, 0) < 0 ) { | |
149 this->enabled = 0; | |
150 } | |
151 } | |
152 } | |
153 | |
154 /* See if we need to use timed audio synchronization */ | |
155 if ( frame_ticks ) { | |
156 /* Use timer for general audio synchronization */ | |
157 Sint32 ticks; | |
158 | |
159 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS; | |
160 if ( ticks > 0 ) { | |
161 SDL_Delay(ticks); | |
162 } | |
163 } else { | |
164 /* Use select() for audio synchronization */ | |
165 struct timeval timeout; | |
166 FD_ZERO(&fdset); | |
167 FD_SET(audio_fd, &fdset); | |
168 timeout.tv_sec = 10; | |
169 timeout.tv_usec = 0; | |
170 #ifdef DEBUG_AUDIO | |
171 fprintf(stderr, "Waiting for audio to get ready\n"); | |
172 #endif | |
173 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) { | |
174 const char *message = | |
175 #ifdef AUDIO_OSPACE_HACK | |
176 "Audio timeout - buggy audio driver? (trying ospace)"; | |
177 #else | |
178 "Audio timeout - buggy audio driver? (disabled)"; | |
179 #endif | |
180 /* In general we should never print to the screen, | |
181 but in this case we have no other way of letting | |
182 the user know what happened. | |
183 */ | |
184 fprintf(stderr, "SDL: %s\n", message); | |
185 #ifdef AUDIO_OSPACE_HACK | |
186 /* We may be able to use GET_OSPACE trick */ | |
187 frame_ticks = (float)(this->spec->samples*1000) / | |
188 this->spec->freq; | |
189 next_frame = SDL_GetTicks()+frame_ticks; | |
190 #else | |
191 this->enabled = 0; | |
192 /* Don't try to close - may hang */ | |
193 audio_fd = -1; | |
194 #ifdef DEBUG_AUDIO | |
195 fprintf(stderr, "Done disabling audio\n"); | |
196 #endif | |
197 #endif /* AUDIO_OSPACE_HACK */ | |
198 } | |
199 #ifdef DEBUG_AUDIO | |
200 fprintf(stderr, "Ready!\n"); | |
201 #endif | |
202 } | |
203 } | |
204 | |
205 static void DMA_PlayAudio(_THIS) | |
206 { | |
207 /* If timer synchronization is enabled, set the next write frame */ | |
208 if ( frame_ticks ) { | |
209 next_frame += frame_ticks; | |
210 } | |
211 return; | |
212 } | |
213 | |
214 static Uint8 *DMA_GetAudioBuf(_THIS) | |
215 { | |
216 count_info info; | |
217 int playing; | |
218 int filling; | |
219 | |
220 /* Get number of blocks, looping if we're not using select() */ | |
221 do { | |
222 if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) { | |
223 /* Uh oh... */ | |
224 this->enabled = 0; | |
225 return(NULL); | |
226 } | |
227 } while ( frame_ticks && (info.blocks < 1) ); | |
228 #ifdef DEBUG_AUDIO | |
229 if ( info.blocks > 1 ) { | |
230 printf("Warning: audio underflow (%d frags)\n", info.blocks-1); | |
231 } | |
232 #endif | |
233 playing = info.ptr / this->spec.size; | |
234 filling = (playing + 1)%num_buffers; | |
235 return (dma_buf + (filling * this->spec.size)); | |
236 } | |
237 | |
238 static void DMA_CloseAudio(_THIS) | |
239 { | |
240 if ( dma_buf != NULL ) { | |
241 munmap(dma_buf, dma_len); | |
242 dma_buf = NULL; | |
243 } | |
244 if ( audio_fd >= 0 ) { | |
245 close(audio_fd); | |
246 audio_fd = -1; | |
247 } | |
248 } | |
249 | |
250 static int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo, | |
251 SDL_AudioSpec *spec) | |
252 { | |
253 int frag_spec; | |
254 int value; | |
255 | |
256 /* Close and then reopen the audio device */ | |
257 close(audio_fd); | |
258 audio_fd = open(audiodev, O_RDWR, 0); | |
259 if ( audio_fd < 0 ) { | |
260 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); | |
261 return(-1); | |
262 } | |
263 | |
264 /* Calculate the final parameters for this audio specification */ | |
265 SDL_CalculateAudioSpec(spec); | |
266 | |
267 /* Determine the power of two of the fragment size */ | |
268 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); | |
269 if ( (0x01<<frag_spec) != spec->size ) { | |
270 SDL_SetError("Fragment size must be a power of two"); | |
271 return(-1); | |
272 } | |
273 | |
274 /* Set the audio buffering parameters */ | |
275 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { | |
276 SDL_SetError("Couldn't set audio fragment spec"); | |
277 return(-1); | |
278 } | |
279 | |
280 /* Set the audio format */ | |
281 value = format; | |
282 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || | |
283 (value != format) ) { | |
284 SDL_SetError("Couldn't set audio format"); | |
285 return(-1); | |
286 } | |
287 | |
288 /* Set mono or stereo audio */ | |
289 value = (spec->channels > 1); | |
290 if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) || | |
291 (value != stereo) ) { | |
292 SDL_SetError("Couldn't set audio channels"); | |
293 return(-1); | |
294 } | |
295 | |
296 /* Set the DSP frequency */ | |
297 value = spec->freq; | |
960
eec28a5278be
Date: Sat, 9 Oct 2004 02:46:18 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
298 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { |
0 | 299 SDL_SetError("Couldn't set audio frequency"); |
300 return(-1); | |
301 } | |
302 spec->freq = value; | |
303 | |
304 /* We successfully re-opened the audio */ | |
305 return(0); | |
306 } | |
307 | |
308 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
309 { | |
310 char audiodev[1024]; | |
311 int format; | |
312 int stereo; | |
313 int value; | |
314 Uint16 test_format; | |
315 struct audio_buf_info info; | |
316 | |
317 /* Reset the timer synchronization flag */ | |
318 frame_ticks = 0.0; | |
319 | |
320 /* Open the audio device */ | |
321 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); | |
322 if ( audio_fd < 0 ) { | |
323 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); | |
324 return(-1); | |
325 } | |
326 dma_buf = NULL; | |
327 ioctl(audio_fd, SNDCTL_DSP_RESET, 0); | |
328 | |
329 /* Get a list of supported hardware formats */ | |
330 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { | |
331 SDL_SetError("Couldn't get audio format list"); | |
332 return(-1); | |
333 } | |
334 | |
335 /* Try for a closest match on audio format */ | |
336 format = 0; | |
337 for ( test_format = SDL_FirstAudioFormat(spec->format); | |
338 ! format && test_format; ) { | |
339 #ifdef DEBUG_AUDIO | |
340 fprintf(stderr, "Trying format 0x%4.4x\n", test_format); | |
341 #endif | |
342 switch ( test_format ) { | |
343 case AUDIO_U8: | |
344 if ( value & AFMT_U8 ) { | |
345 format = AFMT_U8; | |
346 } | |
347 break; | |
348 case AUDIO_S8: | |
349 if ( value & AFMT_S8 ) { | |
350 format = AFMT_S8; | |
351 } | |
352 break; | |
353 case AUDIO_S16LSB: | |
354 if ( value & AFMT_S16_LE ) { | |
355 format = AFMT_S16_LE; | |
356 } | |
357 break; | |
358 case AUDIO_S16MSB: | |
359 if ( value & AFMT_S16_BE ) { | |
360 format = AFMT_S16_BE; | |
361 } | |
362 break; | |
363 case AUDIO_U16LSB: | |
364 if ( value & AFMT_U16_LE ) { | |
365 format = AFMT_U16_LE; | |
366 } | |
367 break; | |
368 case AUDIO_U16MSB: | |
369 if ( value & AFMT_U16_BE ) { | |
370 format = AFMT_U16_BE; | |
371 } | |
372 break; | |
373 default: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
374 format = 0; |
0 | 375 break; |
376 } | |
377 if ( ! format ) { | |
378 test_format = SDL_NextAudioFormat(); | |
379 } | |
380 } | |
381 if ( format == 0 ) { | |
382 SDL_SetError("Couldn't find any hardware audio formats"); | |
383 return(-1); | |
384 } | |
385 spec->format = test_format; | |
386 | |
387 /* Set the audio format */ | |
388 value = format; | |
389 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || | |
390 (value != format) ) { | |
391 SDL_SetError("Couldn't set audio format"); | |
392 return(-1); | |
393 } | |
394 | |
395 /* Set mono or stereo audio (currently only two channels supported) */ | |
396 stereo = (spec->channels > 1); | |
397 ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo); | |
398 if ( stereo ) { | |
399 spec->channels = 2; | |
400 } else { | |
401 spec->channels = 1; | |
402 } | |
403 | |
404 /* Because some drivers don't allow setting the buffer size | |
405 after setting the format, we must re-open the audio device | |
406 once we know what format and channels are supported | |
407 */ | |
408 if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) { | |
409 /* Error is set by DMA_ReopenAudio() */ | |
410 return(-1); | |
411 } | |
412 | |
413 /* Memory map the audio buffer */ | |
414 if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) { | |
415 SDL_SetError("Couldn't get OSPACE parameters"); | |
416 return(-1); | |
417 } | |
418 spec->size = info.fragsize; | |
419 spec->samples = spec->size / ((spec->format & 0xFF) / 8); | |
420 spec->samples /= spec->channels; | |
421 num_buffers = info.fragstotal; | |
422 dma_len = num_buffers*spec->size; | |
423 dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED, | |
424 audio_fd, 0); | |
425 if ( dma_buf == MAP_FAILED ) { | |
426 SDL_SetError("DMA memory map failed"); | |
427 dma_buf = NULL; | |
428 return(-1); | |
429 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
430 SDL_memset(dma_buf, spec->silence, dma_len); |
0 | 431 |
432 /* Check to see if we need to use select() workaround */ | |
433 { char *workaround; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
434 workaround = SDL_getenv("SDL_DSP_NOSELECT"); |
0 | 435 if ( workaround ) { |
436 frame_ticks = (float)(spec->samples*1000)/spec->freq; | |
437 next_frame = SDL_GetTicks()+frame_ticks; | |
438 } | |
439 } | |
440 | |
441 /* Trigger audio playback */ | |
442 value = 0; | |
443 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value); | |
444 value = PCM_ENABLE_OUTPUT; | |
445 if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) { | |
446 SDL_SetError("Couldn't trigger audio output"); | |
447 return(-1); | |
448 } | |
449 | |
450 /* Get the parent process id (we're the parent of the audio thread) */ | |
451 parent = getpid(); | |
452 | |
453 /* We're ready to rock and roll. :-) */ | |
454 return(0); | |
455 } |