Mercurial > sdl-ios-xcode
annotate src/audio/dsp/SDL_dspaudio.c @ 1228:f4a3a4129d04
From Mike Frysinger and/or Gentoo:
- libsdl-SDL_stretch-PIC.patch
ignoring the general fact of how SDL_stretch relies on executing dynamic code,
the inline asm should let gcc handle the a details for getting the actual
address for _copy_row as it will do the right thing
test case: http://dev.gentoo.org/~vapier/libsdl/sdl-stretch.tar.bz2
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Thu, 05 Jan 2006 07:20:12 +0000 |
parents | c5dedfdb4e42 |
children | 644b39bf7253 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
354
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
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. | |
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 | |
13 Library General Public License for more details. | |
14 | |
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 | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
248
diff
changeset
|
20 slouken@libsdl.org |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
21 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
22 Modified in Oct 2004 by Hannu Savolainen |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
23 hannu@opensound.com |
0 | 24 */ |
25 | |
26 #ifdef SAVE_RCSID | |
27 static char rcsid = | |
28 "@(#) $Id$"; | |
29 #endif | |
30 | |
31 /* Allow access to a raw mixing buffer */ | |
32 | |
33 #include <stdlib.h> | |
34 #include <stdio.h> | |
35 #include <string.h> | |
36 #include <errno.h> | |
37 #include <unistd.h> | |
38 #include <fcntl.h> | |
39 #include <signal.h> | |
40 #include <sys/time.h> | |
41 #include <sys/ioctl.h> | |
42 #include <sys/stat.h> | |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
43 #ifdef OSS_USE_SOUNDCARD_H |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
44 /* This is installed on some systems */ |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
45 #include <soundcard.h> |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
46 #else |
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
47 /* This is recommended by OSS */ |
0 | 48 #include <sys/soundcard.h> |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
93
diff
changeset
|
49 #endif |
0 | 50 |
51 #include "SDL_audio.h" | |
52 #include "SDL_error.h" | |
53 #include "SDL_audiomem.h" | |
54 #include "SDL_audio_c.h" | |
55 #include "SDL_timer.h" | |
56 #include "SDL_audiodev_c.h" | |
57 #include "SDL_dspaudio.h" | |
58 | |
59 /* The tag name used by DSP audio */ | |
60 #define DSP_DRIVER_NAME "dsp" | |
61 | |
62 /* Open the audio device for playback, and don't block if busy */ | |
63 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) | |
64 | |
65 /* Audio driver functions */ | |
66 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
67 static void DSP_WaitAudio(_THIS); | |
68 static void DSP_PlayAudio(_THIS); | |
69 static Uint8 *DSP_GetAudioBuf(_THIS); | |
70 static void DSP_CloseAudio(_THIS); | |
71 | |
72 /* Audio driver bootstrap functions */ | |
73 | |
74 static int Audio_Available(void) | |
75 { | |
76 int fd; | |
77 int available; | |
78 | |
79 available = 0; | |
80 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0); | |
81 if ( fd >= 0 ) { | |
82 available = 1; | |
83 close(fd); | |
84 } | |
85 return(available); | |
86 } | |
87 | |
88 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
89 { | |
90 free(device->hidden); | |
91 free(device); | |
92 } | |
93 | |
94 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
95 { | |
96 SDL_AudioDevice *this; | |
97 | |
98 /* Initialize all variables that we clean on shutdown */ | |
99 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
100 if ( this ) { | |
101 memset(this, 0, (sizeof *this)); | |
102 this->hidden = (struct SDL_PrivateAudioData *) | |
103 malloc((sizeof *this->hidden)); | |
104 } | |
105 if ( (this == NULL) || (this->hidden == NULL) ) { | |
106 SDL_OutOfMemory(); | |
107 if ( this ) { | |
108 free(this); | |
109 } | |
110 return(0); | |
111 } | |
112 memset(this->hidden, 0, (sizeof *this->hidden)); | |
113 audio_fd = -1; | |
114 | |
115 /* Set the function pointers */ | |
116 this->OpenAudio = DSP_OpenAudio; | |
117 this->WaitAudio = DSP_WaitAudio; | |
118 this->PlayAudio = DSP_PlayAudio; | |
119 this->GetAudioBuf = DSP_GetAudioBuf; | |
120 this->CloseAudio = DSP_CloseAudio; | |
121 | |
122 this->free = Audio_DeleteDevice; | |
123 | |
124 return this; | |
125 } | |
126 | |
127 AudioBootStrap DSP_bootstrap = { | |
128 DSP_DRIVER_NAME, "OSS /dev/dsp standard audio", | |
129 Audio_Available, Audio_CreateDevice | |
130 }; | |
131 | |
132 /* This function waits until it is possible to write a full sound buffer */ | |
133 static void DSP_WaitAudio(_THIS) | |
134 { | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
135 /* Not needed at all since OSS handles waiting automagically */ |
0 | 136 } |
137 | |
138 static void DSP_PlayAudio(_THIS) | |
139 { | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
140 if (write(audio_fd, mixbuf, mixlen)==-1) |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
141 { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
142 perror("Audio write"); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
143 this->enabled = 0; |
0 | 144 } |
145 | |
146 #ifdef DEBUG_AUDIO | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
147 fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen); |
0 | 148 #endif |
149 } | |
150 | |
151 static Uint8 *DSP_GetAudioBuf(_THIS) | |
152 { | |
153 return(mixbuf); | |
154 } | |
155 | |
156 static void DSP_CloseAudio(_THIS) | |
157 { | |
158 if ( mixbuf != NULL ) { | |
159 SDL_FreeAudioMem(mixbuf); | |
160 mixbuf = NULL; | |
161 } | |
162 if ( audio_fd >= 0 ) { | |
163 close(audio_fd); | |
164 audio_fd = -1; | |
165 } | |
166 } | |
167 | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
168 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec) |
0 | 169 { |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
170 char audiodev[1024]; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
171 int format; |
0 | 172 int value; |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
173 int frag_spec; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
174 Uint16 test_format; |
0 | 175 |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
176 /* Open the audio device */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
177 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); |
0 | 178 if ( audio_fd < 0 ) { |
179 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); | |
180 return(-1); | |
181 } | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
182 mixbuf = NULL; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
183 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
184 /* Make the file descriptor use blocking writes with fcntl() */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
185 { long flags; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
186 flags = fcntl(audio_fd, F_GETFL); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
187 flags &= ~O_NONBLOCK; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
188 if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
189 SDL_SetError("Couldn't set audio blocking mode"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
190 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
191 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
192 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
193 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
194 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
195 /* Get a list of supported hardware formats */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
196 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
197 perror("SNDCTL_DSP_GETFMTS"); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
198 SDL_SetError("Couldn't get audio format list"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
199 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
200 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
201 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
202 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
203 /* Try for a closest match on audio format */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
204 format = 0; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
205 for ( test_format = SDL_FirstAudioFormat(spec->format); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
206 ! format && test_format; ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
207 #ifdef DEBUG_AUDIO |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
208 fprintf(stderr, "Trying format 0x%4.4x\n", test_format); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
209 #endif |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
210 switch ( test_format ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
211 case AUDIO_U8: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
212 if ( value & AFMT_U8 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
213 format = AFMT_U8; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
214 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
215 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
216 case AUDIO_S16LSB: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
217 if ( value & AFMT_S16_LE ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
218 format = AFMT_S16_LE; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
219 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
220 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
221 case AUDIO_S16MSB: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
222 if ( value & AFMT_S16_BE ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
223 format = AFMT_S16_BE; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
224 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
225 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
226 #if 0 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
227 /* |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
228 * These formats are not used by any real life systems so they are not |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
229 * needed here. |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
230 */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
231 case AUDIO_S8: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
232 if ( value & AFMT_S8 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
233 format = AFMT_S8; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
234 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
235 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
236 case AUDIO_U16LSB: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
237 if ( value & AFMT_U16_LE ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
238 format = AFMT_U16_LE; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
239 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
240 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
241 case AUDIO_U16MSB: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
242 if ( value & AFMT_U16_BE ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
243 format = AFMT_U16_BE; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
244 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
245 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
246 #endif |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
247 default: |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
248 format = 0; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
249 break; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
250 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
251 if ( ! format ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
252 test_format = SDL_NextAudioFormat(); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
253 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
254 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
255 if ( format == 0 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
256 SDL_SetError("Couldn't find any hardware audio formats"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
257 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
258 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
259 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
260 spec->format = test_format; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
261 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
262 /* Set the audio format */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
263 value = format; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
264 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
265 (value != format) ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
266 perror("SNDCTL_DSP_SETFMT"); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
267 SDL_SetError("Couldn't set audio format"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
268 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
269 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
270 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
271 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
272 /* Set the number of channels of output */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
273 value = spec->channels; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
274 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
275 perror("SNDCTL_DSP_CHANNELS"); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
276 SDL_SetError("Cannot set the number of channels"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
277 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
278 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
279 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
280 spec->channels = value; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
281 |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
282 /* Set the DSP frequency */ |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
283 value = spec->freq; |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
284 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) { |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
285 perror("SNDCTL_DSP_SPEED"); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
286 SDL_SetError("Couldn't set audio frequency"); |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
287 DSP_CloseAudio(this); |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
288 return(-1); |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
289 } |
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
290 spec->freq = value; |
0 | 291 |
292 /* Calculate the final parameters for this audio specification */ | |
293 SDL_CalculateAudioSpec(spec); | |
294 | |
295 /* Determine the power of two of the fragment size */ | |
296 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); | |
297 if ( (0x01<<frag_spec) != spec->size ) { | |
298 SDL_SetError("Fragment size must be a power of two"); | |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
299 DSP_CloseAudio(this); |
0 | 300 return(-1); |
301 } | |
302 frag_spec |= 0x00020000; /* two fragments, for low latency */ | |
303 | |
304 /* Set the audio buffering parameters */ | |
305 #ifdef DEBUG_AUDIO | |
306 fprintf(stderr, "Requesting %d fragments of size %d\n", | |
307 (frag_spec >> 16), 1<<(frag_spec&0xFFFF)); | |
308 #endif | |
309 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { | |
968
4675910b0b7b
Date: Mon, 11 Oct 2004 15:17:27 +0300 (EEST)
Sam Lantinga <slouken@libsdl.org>
parents:
960
diff
changeset
|
310 perror("SNDCTL_DSP_SETFRAGMENT"); |
0 | 311 fprintf(stderr, "Warning: Couldn't set audio fragment size\n"); |
312 } | |
313 #ifdef DEBUG_AUDIO | |
314 { audio_buf_info info; | |
315 ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info); | |
316 fprintf(stderr, "fragments = %d\n", info.fragments); | |
317 fprintf(stderr, "fragstotal = %d\n", info.fragstotal); | |
318 fprintf(stderr, "fragsize = %d\n", info.fragsize); | |
319 fprintf(stderr, "bytes = %d\n", info.bytes); | |
320 } | |
321 #endif | |
322 | |
323 /* Allocate mixing buffer */ | |
324 mixlen = spec->size; | |
325 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); | |
326 if ( mixbuf == NULL ) { | |
1037
c5dedfdb4e42
Date: Tue, 01 Feb 2005 17:53:07 -0800
Sam Lantinga <slouken@libsdl.org>
parents:
968
diff
changeset
|
327 DSP_CloseAudio(this); |
0 | 328 return(-1); |
329 } | |
330 memset(mixbuf, spec->silence, spec->size); | |
331 | |
332 /* Get the parent process id (we're the parent of the audio thread) */ | |
333 parent = getpid(); | |
334 | |
335 /* We're ready to rock and roll. :-) */ | |
336 return(0); | |
337 } |