comparison src/audio/disk/SDL_diskaudio.c @ 2049:5f6550e5184f

Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 17 Oct 2006 09:15:21 +0000
parents c121d94672cb
children 866052b01ee5
comparison
equal deleted inserted replaced
2048:6067c7f9a672 2049:5f6550e5184f
32 #include "SDL_rwops.h" 32 #include "SDL_rwops.h"
33 #include "SDL_timer.h" 33 #include "SDL_timer.h"
34 #include "SDL_audio.h" 34 #include "SDL_audio.h"
35 #include "../SDL_audiomem.h" 35 #include "../SDL_audiomem.h"
36 #include "../SDL_audio_c.h" 36 #include "../SDL_audio_c.h"
37 #include "../SDL_audiodev_c.h"
38 #include "SDL_diskaudio.h" 37 #include "SDL_diskaudio.h"
39 38
40 /* The tag name used by DISK audio */ 39 /* The tag name used by DISK audio */
41 #define DISKAUD_DRIVER_NAME "disk" 40 #define DISKAUD_DRIVER_NAME "disk"
42 41
44 #define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE" 43 #define DISKENVR_OUTFILE "SDL_DISKAUDIOFILE"
45 #define DISKDEFAULT_OUTFILE "sdlaudio.raw" 44 #define DISKDEFAULT_OUTFILE "sdlaudio.raw"
46 #define DISKENVR_WRITEDELAY "SDL_DISKAUDIODELAY" 45 #define DISKENVR_WRITEDELAY "SDL_DISKAUDIODELAY"
47 #define DISKDEFAULT_WRITEDELAY 150 46 #define DISKDEFAULT_WRITEDELAY 150
48 47
49 /* Audio driver functions */
50 static int DISKAUD_OpenAudio(_THIS, SDL_AudioSpec * spec);
51 static void DISKAUD_WaitAudio(_THIS);
52 static void DISKAUD_PlayAudio(_THIS);
53 static Uint8 *DISKAUD_GetAudioBuf(_THIS);
54 static void DISKAUD_CloseAudio(_THIS);
55
56 static const char * 48 static const char *
57 DISKAUD_GetOutputFilename(void) 49 DISKAUD_GetOutputFilename(const char *devname)
58 { 50 {
59 const char *envr = SDL_getenv(DISKENVR_OUTFILE); 51 if (devname == NULL) {
60 return ((envr != NULL) ? envr : DISKDEFAULT_OUTFILE); 52 devname = SDL_getenv(DISKENVR_OUTFILE);
53 if (devname == NULL) {
54 devname = DISKDEFAULT_OUTFILE;
55 }
56 }
57 return devname;
61 } 58 }
62
63 /* Audio driver bootstrap functions */
64 static int
65 DISKAUD_Available(void)
66 {
67 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
68 if (envr && (SDL_strcmp(envr, DISKAUD_DRIVER_NAME) == 0)) {
69 return (1);
70 }
71 return (0);
72 }
73
74 static void
75 DISKAUD_DeleteDevice(SDL_AudioDevice * device)
76 {
77 SDL_free(device->hidden);
78 SDL_free(device);
79 }
80
81 static SDL_AudioDevice *
82 DISKAUD_CreateDevice(int devindex)
83 {
84 SDL_AudioDevice *this;
85 const char *envr;
86
87 /* Initialize all variables that we clean on shutdown */
88 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
89 if (this) {
90 SDL_memset(this, 0, (sizeof *this));
91 this->hidden = (struct SDL_PrivateAudioData *)
92 SDL_malloc((sizeof *this->hidden));
93 }
94 if ((this == NULL) || (this->hidden == NULL)) {
95 SDL_OutOfMemory();
96 if (this) {
97 SDL_free(this);
98 }
99 return (0);
100 }
101 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
102
103 envr = SDL_getenv(DISKENVR_WRITEDELAY);
104 this->hidden->write_delay =
105 (envr) ? SDL_atoi(envr) : DISKDEFAULT_WRITEDELAY;
106
107 /* Set the function pointers */
108 this->OpenAudio = DISKAUD_OpenAudio;
109 this->WaitAudio = DISKAUD_WaitAudio;
110 this->PlayAudio = DISKAUD_PlayAudio;
111 this->GetAudioBuf = DISKAUD_GetAudioBuf;
112 this->CloseAudio = DISKAUD_CloseAudio;
113
114 this->free = DISKAUD_DeleteDevice;
115
116 return this;
117 }
118
119 AudioBootStrap DISKAUD_bootstrap = {
120 DISKAUD_DRIVER_NAME, "direct-to-disk audio",
121 DISKAUD_Available, DISKAUD_CreateDevice
122 };
123 59
124 /* This function waits until it is possible to write a full sound buffer */ 60 /* This function waits until it is possible to write a full sound buffer */
125 static void 61 static void
126 DISKAUD_WaitAudio(_THIS) 62 DISKAUD_WaitDevice(_THIS)
127 { 63 {
128 SDL_Delay(this->hidden->write_delay); 64 SDL_Delay(this->hidden->write_delay);
129 } 65 }
130 66
131 static void 67 static void
132 DISKAUD_PlayAudio(_THIS) 68 DISKAUD_PlayDevice(_THIS)
133 { 69 {
134 int written; 70 int written;
135 71
136 /* Write the audio data */ 72 /* Write the audio data */
137 written = SDL_RWwrite(this->hidden->output, 73 written = SDL_RWwrite(this->hidden->output,
145 fprintf(stderr, "Wrote %d bytes of audio data\n", written); 81 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
146 #endif 82 #endif
147 } 83 }
148 84
149 static Uint8 * 85 static Uint8 *
150 DISKAUD_GetAudioBuf(_THIS) 86 DISKAUD_GetDeviceBuf(_THIS)
151 { 87 {
152 return (this->hidden->mixbuf); 88 return (this->hidden->mixbuf);
153 } 89 }
154 90
155 static void 91 static void
156 DISKAUD_CloseAudio(_THIS) 92 DISKAUD_CloseDevice(_THIS)
157 { 93 {
158 if (this->hidden->mixbuf != NULL) { 94 if (this->hidden != NULL) {
159 SDL_FreeAudioMem(this->hidden->mixbuf); 95 if (this->hidden->mixbuf != NULL) {
160 this->hidden->mixbuf = NULL; 96 SDL_FreeAudioMem(this->hidden->mixbuf);
161 } 97 this->hidden->mixbuf = NULL;
162 if (this->hidden->output != NULL) { 98 }
163 SDL_RWclose(this->hidden->output); 99 if (this->hidden->output != NULL) {
164 this->hidden->output = NULL; 100 SDL_RWclose(this->hidden->output);
101 this->hidden->output = NULL;
102 }
103 SDL_free(this->hidden);
104 this->hidden = NULL;
165 } 105 }
166 } 106 }
167 107
168 static int 108 static int
169 DISKAUD_OpenAudio(_THIS, SDL_AudioSpec * spec) 109 DISKAUD_OpenDevice(_THIS, const char *devname, int iscapture)
170 { 110 {
171 const char *fname = DISKAUD_GetOutputFilename(); 111 const char *envr = SDL_getenv(DISKENVR_WRITEDELAY);
112 const char *fname = DISKAUD_GetOutputFilename(devname);
113
114 this->hidden = (struct SDL_PrivateAudioData *)
115 SDL_malloc(sizeof (*this->hidden));
116 if (this->hidden == NULL) {
117 SDL_OutOfMemory();
118 return 0;
119 }
120 SDL_memset(this->hidden, 0, sizeof (*this->hidden));
172 121
173 /* Open the audio device */ 122 /* Open the audio device */
174 this->hidden->output = SDL_RWFromFile(fname, "wb"); 123 this->hidden->output = SDL_RWFromFile(fname, "wb");
175 if (this->hidden->output == NULL) { 124 if (this->hidden->output == NULL) {
176 return (-1); 125 DISKAUD_CloseDevice(this);
126 return 0;
177 } 127 }
128
129 /* Allocate mixing buffer */
130 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
131 if (this->hidden->mixbuf == NULL) {
132 DISKAUD_CloseDevice(this);
133 return 0;
134 }
135 SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
136
137 this->hidden->mixlen = this->spec.size;
138 this->hidden->write_delay =
139 (envr) ? SDL_atoi(envr) : DISKDEFAULT_WRITEDELAY;
140
178 #if HAVE_STDIO_H 141 #if HAVE_STDIO_H
179 fprintf(stderr, "WARNING: You are using the SDL disk writer" 142 fprintf(stderr,
180 " audio driver!\n Writing to file [%s].\n", fname); 143 "WARNING: You are using the SDL disk writer audio driver!\n"
144 " Writing to file [%s].\n", fname);
181 #endif 145 #endif
182 146
183 /* Allocate mixing buffer */
184 this->hidden->mixlen = spec->size;
185 this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
186 if (this->hidden->mixbuf == NULL) {
187 return (-1);
188 }
189 SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
190
191 /* We're ready to rock and roll. :-) */ 147 /* We're ready to rock and roll. :-) */
192 return (0); 148 return 1;
193 } 149 }
194 150
151 static int
152 DISKAUD_Init(SDL_AudioDriverImpl *impl)
153 {
154 /* Set the function pointers */
155 impl->OpenDevice = DISKAUD_OpenDevice;
156 impl->WaitDevice = DISKAUD_WaitDevice;
157 impl->PlayDevice = DISKAUD_PlayDevice;
158 impl->GetDeviceBuf = DISKAUD_GetDeviceBuf;
159 impl->CloseDevice = DISKAUD_CloseDevice;
160
161 return 1;
162 }
163
164 AudioBootStrap DISKAUD_bootstrap = {
165 DISKAUD_DRIVER_NAME, "direct-to-disk audio", DISKAUD_Init, 1
166 };
167
195 /* vi: set ts=4 sw=4 expandtab: */ 168 /* vi: set ts=4 sw=4 expandtab: */