Mercurial > sdl-ios-xcode
annotate src/audio/alsa/SDL_alsa_audio.c @ 765:4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
nanosleep because nanosleep may not be portable to all systems
using SDL with the ALSA backend. This may be a moot point with
the switch to blocking writes anyway...
Date: Sat, 27 Dec 2003 21:47:36 +0100
From: Michel Daenzer
To: Debian Bug Tracking System
Subject: [SDL] Bug#225252: [PATCH] ALSA fixes
Package: libsdl1.2debian-all
Version: 1.2.6-2
Severity: normal
Tags: patch
For SDL 1.2.6, the ALSA backend was changed to call snd_pcm_open() with
SND_PCM_NONBLOCK. That's a good idea per se, however, it causes high CPU
usage, interrupted sound and stuttering in some games here. Taking a nanosleep
whenever snd_pcm_writei() returns -EAGAIN fixes this, but I think it's more
efficient to use blocking mode for the actual sound playback. Feedback from the
SDL and ALSA lists appreciated.
The patch also fixes the default ALSA device to be used.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 04 Jan 2004 15:40:50 +0000 |
parents | 5d07f9a47f17 |
children | b8d311d90021 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 | |
24 | |
25 /* Allow access to a raw mixing buffer */ | |
26 | |
27 #include <stdlib.h> | |
28 #include <stdio.h> | |
29 #include <string.h> | |
30 #include <errno.h> | |
31 #include <unistd.h> | |
32 #include <fcntl.h> | |
33 #include <signal.h> | |
34 #include <sys/types.h> | |
35 #include <sys/time.h> | |
36 | |
37 #include "SDL_audio.h" | |
38 #include "SDL_error.h" | |
39 #include "SDL_audiomem.h" | |
40 #include "SDL_audio_c.h" | |
41 #include "SDL_timer.h" | |
42 #include "SDL_alsa_audio.h" | |
43 | |
44 /* The tag name used by ALSA audio */ | |
45 #define DRIVER_NAME "alsa" | |
46 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
47 /* The default ALSA audio driver */ |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
48 #define DEFAULT_DEVICE "default" |
0 | 49 |
50 /* Audio driver functions */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
51 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
52 static void ALSA_WaitAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
53 static void ALSA_PlayAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
54 static Uint8 *ALSA_GetAudioBuf(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
55 static void ALSA_CloseAudio(_THIS); |
0 | 56 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
57 static const char *get_audio_device() |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
58 { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
59 const char *device; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
60 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
61 device = getenv("AUDIODEV"); /* Is there a standard variable name? */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
62 if ( device == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
63 device = DEFAULT_DEVICE; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
64 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 return device; |
0 | 66 } |
67 | |
68 /* Audio driver bootstrap functions */ | |
69 | |
70 static int Audio_Available(void) | |
71 { | |
72 int available; | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
73 int status; |
0 | 74 snd_pcm_t *handle; |
75 | |
76 available = 0; | |
547
5d07f9a47f17
Open the ALSA devices in non-blocking mode (thanks Aleksey!)
Sam Lantinga <slouken@libsdl.org>
parents:
357
diff
changeset
|
77 status = snd_pcm_open(&handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
78 if ( status >= 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
79 available = 1; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
80 snd_pcm_close(handle); |
0 | 81 } |
82 return(available); | |
83 } | |
84 | |
85 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
86 { | |
87 free(device->hidden); | |
88 free(device); | |
89 } | |
90 | |
91 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
92 { | |
93 SDL_AudioDevice *this; | |
94 | |
95 /* Initialize all variables that we clean on shutdown */ | |
96 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
97 if ( this ) { | |
98 memset(this, 0, (sizeof *this)); | |
99 this->hidden = (struct SDL_PrivateAudioData *) | |
100 malloc((sizeof *this->hidden)); | |
101 } | |
102 if ( (this == NULL) || (this->hidden == NULL) ) { | |
103 SDL_OutOfMemory(); | |
104 if ( this ) { | |
105 free(this); | |
106 } | |
107 return(0); | |
108 } | |
109 memset(this->hidden, 0, (sizeof *this->hidden)); | |
110 | |
111 /* Set the function pointers */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
112 this->OpenAudio = ALSA_OpenAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
113 this->WaitAudio = ALSA_WaitAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
114 this->PlayAudio = ALSA_PlayAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
115 this->GetAudioBuf = ALSA_GetAudioBuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
116 this->CloseAudio = ALSA_CloseAudio; |
0 | 117 |
118 this->free = Audio_DeleteDevice; | |
119 | |
120 return this; | |
121 } | |
122 | |
123 AudioBootStrap ALSA_bootstrap = { | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
124 DRIVER_NAME, "ALSA 0.9 PCM audio", |
0 | 125 Audio_Available, Audio_CreateDevice |
126 }; | |
127 | |
128 /* This function waits until it is possible to write a full sound buffer */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
129 static void ALSA_WaitAudio(_THIS) |
0 | 130 { |
131 /* Check to see if the thread-parent process is still alive */ | |
132 { static int cnt = 0; | |
133 /* Note that this only works with thread implementations | |
134 that use a different process id for each thread. | |
135 */ | |
136 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
137 if ( kill(parent, 0) < 0 ) { | |
138 this->enabled = 0; | |
139 } | |
140 } | |
141 } | |
142 } | |
143 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
144 static void ALSA_PlayAudio(_THIS) |
0 | 145 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
146 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
147 int sample_len; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
148 signed short *sample_buf; |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
149 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
150 sample_len = this->spec.samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
151 sample_buf = (signed short *)mixbuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
152 while ( sample_len > 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
153 status = snd_pcm_writei(pcm_handle, sample_buf, sample_len); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
154 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
155 if ( status == -EAGAIN ) { |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
156 SDL_Delay(1); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
157 continue; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
158 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
159 if ( status == -ESTRPIPE ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
160 do { |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
161 SDL_Delay(1); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
162 status = snd_pcm_resume(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
163 } while ( status == -EAGAIN ); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
164 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
165 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
166 status = snd_pcm_prepare(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
167 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
168 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
169 /* Hmm, not much we can do - abort */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
170 this->enabled = 0; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
171 return; |
0 | 172 } |
356
a1e54d1ba16f
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
354
diff
changeset
|
173 continue; |
0 | 174 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
175 sample_buf += status * this->spec.channels; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
176 sample_len -= status; |
0 | 177 } |
178 } | |
179 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
180 static Uint8 *ALSA_GetAudioBuf(_THIS) |
0 | 181 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
182 return(mixbuf); |
0 | 183 } |
184 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
185 static void ALSA_CloseAudio(_THIS) |
0 | 186 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
187 if ( mixbuf != NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
188 SDL_FreeAudioMem(mixbuf); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
189 mixbuf = NULL; |
0 | 190 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
191 if ( pcm_handle ) { |
357
5868b0f832f2
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
356
diff
changeset
|
192 snd_pcm_drain(pcm_handle); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
193 snd_pcm_close(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
194 pcm_handle = NULL; |
0 | 195 } |
196 } | |
197 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
198 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) |
0 | 199 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
200 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
201 snd_pcm_hw_params_t *params; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
202 snd_pcm_format_t format; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
203 snd_pcm_uframes_t frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
204 Uint16 test_format; |
0 | 205 |
206 /* Open the audio device */ | |
547
5d07f9a47f17
Open the ALSA devices in non-blocking mode (thanks Aleksey!)
Sam Lantinga <slouken@libsdl.org>
parents:
357
diff
changeset
|
207 status = snd_pcm_open(&pcm_handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
208 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
209 SDL_SetError("Couldn't open audio device: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
210 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
211 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
212 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
213 /* Figure out what the hardware is capable of */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
214 snd_pcm_hw_params_alloca(¶ms); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
215 status = snd_pcm_hw_params_any(pcm_handle, params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
216 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
217 SDL_SetError("Couldn't get hardware config: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
218 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
219 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
220 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
221 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
222 /* SDL only uses interleaved sample output */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
223 status = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
224 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
225 SDL_SetError("Couldn't set interleaved access: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
226 ALSA_CloseAudio(this); |
0 | 227 return(-1); |
228 } | |
229 | |
230 /* Try for a closest match on audio format */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
231 status = -1; |
0 | 232 for ( test_format = SDL_FirstAudioFormat(spec->format); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
233 test_format && (status < 0); ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
234 switch ( test_format ) { |
0 | 235 case AUDIO_U8: |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
236 format = SND_PCM_FORMAT_U8; |
0 | 237 break; |
238 case AUDIO_S8: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
239 format = SND_PCM_FORMAT_S8; |
0 | 240 break; |
241 case AUDIO_S16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
242 format = SND_PCM_FORMAT_S16_LE; |
0 | 243 break; |
244 case AUDIO_S16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
245 format = SND_PCM_FORMAT_S16_BE; |
0 | 246 break; |
247 case AUDIO_U16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
248 format = SND_PCM_FORMAT_U16_LE; |
0 | 249 break; |
250 case AUDIO_U16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
251 format = SND_PCM_FORMAT_U16_BE; |
0 | 252 break; |
253 default: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
254 format = 0; |
0 | 255 break; |
256 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
257 if ( format != 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
258 status = snd_pcm_hw_params_set_format(pcm_handle, params, format); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
259 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
260 if ( status < 0 ) { |
0 | 261 test_format = SDL_NextAudioFormat(); |
262 } | |
263 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
264 if ( status < 0 ) { |
0 | 265 SDL_SetError("Couldn't find any hardware audio formats"); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
266 ALSA_CloseAudio(this); |
0 | 267 return(-1); |
268 } | |
269 spec->format = test_format; | |
270 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
271 /* Set the number of channels */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
272 status = snd_pcm_hw_params_set_channels(pcm_handle, params, spec->channels); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
273 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
274 status = snd_pcm_hw_params_get_channels(params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
275 if ( (status <= 0) || (status > 2) ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
276 SDL_SetError("Couldn't set audio channels"); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
277 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
278 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
279 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
280 spec->channels = status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
281 } |
0 | 282 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
283 /* Set the audio rate */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
284 status = snd_pcm_hw_params_set_rate_near(pcm_handle, params, spec->freq, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
285 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
286 SDL_SetError("Couldn't set audio frequency: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
287 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
288 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
289 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
290 spec->freq = status; |
0 | 291 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
292 /* Set the buffer size, in samples */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
293 frames = spec->samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
294 frames = snd_pcm_hw_params_set_period_size_near(pcm_handle, params, frames, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
295 spec->samples = frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
296 snd_pcm_hw_params_set_periods_near(pcm_handle, params, 2, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
297 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
298 /* "set" the hardware with the desired parameters */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
299 status = snd_pcm_hw_params(pcm_handle, params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
300 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
301 SDL_SetError("Couldn't set audio parameters: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
302 ALSA_CloseAudio(this); |
0 | 303 return(-1); |
304 } | |
305 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
306 /* Calculate the final parameters for this audio specification */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
307 SDL_CalculateAudioSpec(spec); |
0 | 308 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
309 /* Allocate mixing buffer */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
310 mixlen = spec->size; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
311 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
312 if ( mixbuf == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
313 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
314 return(-1); |
0 | 315 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
316 memset(mixbuf, spec->silence, spec->size); |
0 | 317 |
318 /* Get the parent process id (we're the parent of the audio thread) */ | |
319 parent = getpid(); | |
320 | |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
321 /* Switch to blocking mode for playback */ |
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
322 snd_pcm_nonblock(pcm_handle, 0); |
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
323 |
0 | 324 /* We're ready to rock and roll. :-) */ |
325 return(0); | |
326 } |