comparison src/audio/dummy/SDL_dummyaudio.c @ 3784:37c9c4590689 SDL-ryan-multiple-audio-device

First batch of heavy lifting on supporting multiple audio devices at once. This has a long way to go yet, most of the drivers aren't updated for the new interfaces, and it's still got some obvious bugs, FIXMEs, and wistlist items. Don't use yet.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 01 Oct 2006 05:24:03 +0000
parents c121d94672cb
children 866c310e2cb5
comparison
equal deleted inserted replaced
3783:dc3870a3f30d 3784:37c9c4590689
35 35
36 /* The tag name used by DUMMY audio */ 36 /* The tag name used by DUMMY audio */
37 #define DUMMYAUD_DRIVER_NAME "dummy" 37 #define DUMMYAUD_DRIVER_NAME "dummy"
38 38
39 /* Audio driver functions */ 39 /* Audio driver functions */
40 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec); 40 static int DUMMYAUD_OpenAudio(_THIS, const char *devname, int iscapture);
41 static void DUMMYAUD_WaitAudio(_THIS); 41 static void DUMMYAUD_WaitAudio(_THIS);
42 static void DUMMYAUD_PlayAudio(_THIS); 42 static void DUMMYAUD_PlayAudio(_THIS);
43 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS); 43 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
44 static void DUMMYAUD_CloseAudio(_THIS); 44 static void DUMMYAUD_CloseAudio(_THIS);
45 45
46 /* Audio driver bootstrap functions */ 46 /* Audio driver bootstrap functions */
47 static int 47 static int
48 DUMMYAUD_Available(void) 48 DUMMYAUD_Available(void)
49 { 49 {
50 /* !!! FIXME: check this at a higher level... */
51 /* only ever use this driver if explicitly requested. */
50 const char *envr = SDL_getenv("SDL_AUDIODRIVER"); 52 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
51 if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) { 53 if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
52 return (1); 54 return (1);
53 } 55 }
54 return (0); 56 return (0);
55 } 57 }
56 58
57 static void 59 static int
58 DUMMYAUD_DeleteDevice(SDL_AudioDevice * device) 60 DUMMYAUD_Init(SDL_AudioDriverImpl *impl)
59 { 61 {
60 SDL_free(device->hidden); 62 /* Set the function pointers */
61 SDL_free(device); 63 impl->OpenAudio = DUMMYAUD_OpenAudio;
62 } 64 impl->WaitAudio = DUMMYAUD_WaitAudio;
65 impl->PlayAudio = DUMMYAUD_PlayAudio;
66 impl->GetAudioBuf = DUMMYAUD_GetAudioBuf;
67 impl->CloseAudio = DUMMYAUD_CloseAudio;
63 68
64 static SDL_AudioDevice * 69 return 1;
65 DUMMYAUD_CreateDevice(int devindex)
66 {
67 SDL_AudioDevice *this;
68
69 /* Initialize all variables that we clean on shutdown */
70 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
71 if (this) {
72 SDL_memset(this, 0, (sizeof *this));
73 this->hidden = (struct SDL_PrivateAudioData *)
74 SDL_malloc((sizeof *this->hidden));
75 }
76 if ((this == NULL) || (this->hidden == NULL)) {
77 SDL_OutOfMemory();
78 if (this) {
79 SDL_free(this);
80 }
81 return (0);
82 }
83 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
84
85 /* Set the function pointers */
86 this->OpenAudio = DUMMYAUD_OpenAudio;
87 this->WaitAudio = DUMMYAUD_WaitAudio;
88 this->PlayAudio = DUMMYAUD_PlayAudio;
89 this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
90 this->CloseAudio = DUMMYAUD_CloseAudio;
91
92 this->free = DUMMYAUD_DeleteDevice;
93
94 return this;
95 } 70 }
96 71
97 AudioBootStrap DUMMYAUD_bootstrap = { 72 AudioBootStrap DUMMYAUD_bootstrap = {
98 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver", 73 DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
99 DUMMYAUD_Available, DUMMYAUD_CreateDevice 74 DUMMYAUD_Available, DUMMYAUD_Init
100 }; 75 };
101 76
102 /* This function waits until it is possible to write a full sound buffer */ 77 /* This function waits until it is possible to write a full sound buffer */
103 static void 78 static void
104 DUMMYAUD_WaitAudio(_THIS) 79 DUMMYAUD_WaitAudio(_THIS)
127 { 102 {
128 if (this->hidden->mixbuf != NULL) { 103 if (this->hidden->mixbuf != NULL) {
129 SDL_FreeAudioMem(this->hidden->mixbuf); 104 SDL_FreeAudioMem(this->hidden->mixbuf);
130 this->hidden->mixbuf = NULL; 105 this->hidden->mixbuf = NULL;
131 } 106 }
107 SDL_free(this->hidden);
108 this->hidden = NULL;
132 } 109 }
133 110
134 static int 111 static int
135 DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec * spec) 112 DUMMYAUD_OpenAudio(_THIS, const char *devname, int iscapture)
136 { 113 {
137 float bytes_per_sec = 0.0f; 114 float bytes_per_sec = 0.0f;
138 115
116 /* Initialize all variables that we clean on shutdown */
117 this->hidden = (struct SDL_PrivateAudioData *)
118 SDL_malloc((sizeof *this->hidden));
119 if (this->hidden == NULL) {
120 SDL_OutOfMemory();
121 return 0;
122 }
123 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
124
139 /* Allocate mixing buffer */ 125 /* Allocate mixing buffer */
140 this->hidden->mixlen = spec->size; 126 this->hidden->mixlen = this->spec.size;
141 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen); 127 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
142 if (this->hidden->mixbuf == NULL) { 128 if (this->hidden->mixbuf == NULL) {
143 return (-1); 129 DUMMYAUD_CloseAudio(this);
130 return 0;
144 } 131 }
145 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size); 132 SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
146 133
147 bytes_per_sec = (float) (((spec->format & 0xFF) / 8) * 134 bytes_per_sec = (float) (SDL_AUDIO_BITSIZE(this->spec.format) / 8) *
148 spec->channels * spec->freq); 135 this->spec.channels * this->spec.freq;
149 136
150 /* 137 /*
151 * We try to make this request more audio at the correct rate for 138 * We try to make this request more audio at the correct rate for
152 * a given audio spec, so timing stays fairly faithful. 139 * a given audio spec, so timing stays fairly faithful.
153 * Also, we have it not block at all for the first two calls, so 140 * Also, we have it not block at all for the first two calls, so
154 * it seems like we're filling two audio fragments right out of the 141 * it seems like we're filling two audio fragments right out of the
155 * gate, like other SDL drivers tend to do. 142 * gate, like other SDL drivers tend to do.
156 */ 143 */
157 this->hidden->initial_calls = 2; 144 this->hidden->initial_calls = 2;
158 this->hidden->write_delay = 145 this->hidden->write_delay =
159 (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f); 146 (Uint32) ((((float) this->spec.size) / bytes_per_sec) * 1000.0f);
160 147
161 /* We're ready to rock and roll. :-) */ 148 /* We're ready to rock and roll. :-) */
162 return (0); 149 return 1;
163 } 150 }
164 151
165 /* vi: set ts=4 sw=4 expandtab: */ 152 /* vi: set ts=4 sw=4 expandtab: */