comparison src/audio/dart/SDL_dart.c @ 3839:506fc6ca82cb SDL-ryan-multiple-audio-device

OS/2 audio code works in 1.3 now.
author Ryan C. Gordon <icculus@icculus.org>
date Sun, 08 Oct 2006 08:57:28 +0000
parents b225d9820ee3
children 66fb40445587
comparison
equal deleted inserted replaced
3838:45e566003276 3839:506fc6ca82cb
72 72
73 73
74 static int 74 static int
75 DART_OpenDevice(_THIS, const char *devname, int iscapture) 75 DART_OpenDevice(_THIS, const char *devname, int iscapture)
76 { 76 {
77 SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); 77 SDL_AudioFormat test_format = SDL_FirstAudioFormat(_this->spec.format);
78 int valid_datatype = 0; 78 int valid_datatype = 0;
79 MCI_AMP_OPEN_PARMS AmpOpenParms; 79 MCI_AMP_OPEN_PARMS AmpOpenParms;
80 MCI_GENERIC_PARMS GenericParms;
81 int iDeviceOrd = 0; // Default device to be used 80 int iDeviceOrd = 0; // Default device to be used
82 int bOpenShared = 1; // Try opening it shared 81 int bOpenShared = 1; // Try opening it shared
83 int iBits = 16; // Default is 16 bits signed 82 int iBits = 16; // Default is 16 bits signed
84 int iFreq = 44100; // Default is 44KHz 83 int iFreq = 44100; // Default is 44KHz
85 int iChannels = 2; // Default is 2 channels (Stereo) 84 int iChannels = 2; // Default is 2 channels (Stereo)
88 int iOpenMode; 87 int iOpenMode;
89 int iSilence; 88 int iSilence;
90 int rc; 89 int rc;
91 90
92 /* Initialize all variables that we clean on shutdown */ 91 /* Initialize all variables that we clean on shutdown */
93 this->hidden = (struct SDL_PrivateAudioData *) 92 _this->hidden = (struct SDL_PrivateAudioData *)
94 SDL_malloc((sizeof *this->hidden)); 93 SDL_malloc((sizeof *_this->hidden));
95 if (this->hidden == NULL) { 94 if (_this->hidden == NULL) {
96 SDL_OutOfMemory(); 95 SDL_OutOfMemory();
97 return 0; 96 return 0;
98 } 97 }
99 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 98 SDL_memset(_this->hidden, 0, (sizeof *_this->hidden));
100 99
101 // First thing is to try to open a given DART device! 100 // First thing is to try to open a given DART device!
102 SDL_memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS)); 101 SDL_memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS));
103 // pszDeviceType should contain the device type in low word, and device ordinal in high word! 102 // pszDeviceType should contain the device type in low word, and device ordinal in high word!
104 AmpOpenParms.pszDeviceType = 103 AmpOpenParms.pszDeviceType =
108 if (bOpenShared) 107 if (bOpenShared)
109 iOpenMode |= MCI_OPEN_SHAREABLE; 108 iOpenMode |= MCI_OPEN_SHAREABLE;
110 109
111 rc = mciSendCommand(0, MCI_OPEN, iOpenMode, (PVOID) & AmpOpenParms, 0); 110 rc = mciSendCommand(0, MCI_OPEN, iOpenMode, (PVOID) & AmpOpenParms, 0);
112 if (rc != MCIERR_SUCCESS) { // No audio available?? 111 if (rc != MCIERR_SUCCESS) { // No audio available??
113 DART_CloseDevice(this); 112 DART_CloseDevice(_this);
113 SDL_SetError("DART: Couldn't open audio device.");
114 return 0; 114 return 0;
115 } 115 }
116 116
117 // Save the device ID we got from DART! 117 // Save the device ID we got from DART!
118 // We will use this in the next calls! 118 // We will use this in the next calls!
119 _this->hidden->iCurrDeviceOrd = iDeviceOrd = AmpOpenParms.usDeviceID; 119 _this->hidden->iCurrDeviceOrd = iDeviceOrd = AmpOpenParms.usDeviceID;
120 120
121 // Determine the audio parameters from the AudioSpec 121 // Determine the audio parameters from the AudioSpec
122 if (this->spec.channels > 4) 122 if (_this->spec.channels > 4)
123 this->spec.channels = 4; 123 _this->spec.channels = 4;
124 124
125 while ((!valid_datatype) && (test_format)) { 125 while ((!valid_datatype) && (test_format)) {
126 this->spec.format = test_format; 126 _this->spec.format = test_format;
127 valid_datatype = 1; 127 valid_datatype = 1;
128 switch (test_format) { 128 switch (test_format) {
129 case AUDIO_U8: 129 case AUDIO_U8:
130 // Unsigned 8 bit audio data 130 // Unsigned 8 bit audio data
131 iSilence = 0x80; 131 iSilence = 0x80;
147 } 147 }
148 } 148 }
149 149
150 if (!valid_datatype) { // shouldn't happen, but just in case... 150 if (!valid_datatype) { // shouldn't happen, but just in case...
151 // Close DART, and exit with error code! 151 // Close DART, and exit with error code!
152 DART_CloseDevice(this); 152 DART_CloseDevice(_this);
153 SDL_SetError("Unsupported audio format"); 153 SDL_SetError("Unsupported audio format");
154 return 0; 154 return 0;
155 } 155 }
156 156
157 _this->hidden->iCurrFreq = iFreq = this->spec.freq; 157 _this->hidden->iCurrFreq = iFreq = _this->spec.freq;
158 _this->hidden->iCurrChannels = iChannels = this->spec.channels; 158 _this->hidden->iCurrChannels = iChannels = _this->spec.channels;
159 /* Update the fragment size as size in bytes */ 159 /* Update the fragment size as size in bytes */
160 SDL_CalculateAudioSpec(&this->spec); 160 SDL_CalculateAudioSpec(&_this->spec);
161 _this->hidden->iCurrBufSize = iBufSize = this->spec.size; 161 _this->hidden->iCurrBufSize = iBufSize = _this->spec.size;
162 162
163 // Now query this device if it supports the given freq/bits/channels! 163 // Now query this device if it supports the given freq/bits/channels!
164 SDL_memset(&(_this->hidden->MixSetupParms), 0, 164 SDL_memset(&(_this->hidden->MixSetupParms), 0,
165 sizeof(MCI_MIXSETUP_PARMS)); 165 sizeof(MCI_MIXSETUP_PARMS));
166 _this->hidden->MixSetupParms.ulBitsPerSample = iBits; 166 _this->hidden->MixSetupParms.ulBitsPerSample = iBits;
173 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP, 173 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP,
174 MCI_WAIT | MCI_MIXSETUP_QUERYMODE, 174 MCI_WAIT | MCI_MIXSETUP_QUERYMODE,
175 &(_this->hidden->MixSetupParms), 0); 175 &(_this->hidden->MixSetupParms), 0);
176 if (rc != MCIERR_SUCCESS) { // The device cannot handle this format! 176 if (rc != MCIERR_SUCCESS) { // The device cannot handle this format!
177 // Close DART, and exit with error code! 177 // Close DART, and exit with error code!
178 DART_CloseDevice(this); 178 DART_CloseDevice(_this);
179 SDL_SetError("Audio device doesn't support requested audio format"); 179 SDL_SetError("Audio device doesn't support requested audio format");
180 return 0; 180 return 0;
181 } 181 }
182 // The device can handle this format, so initialize! 182 // The device can handle this format, so initialize!
183 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP, 183 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP,
184 MCI_WAIT | MCI_MIXSETUP_INIT, 184 MCI_WAIT | MCI_MIXSETUP_INIT,
185 &(_this->hidden->MixSetupParms), 0); 185 &(_this->hidden->MixSetupParms), 0);
186 if (rc != MCIERR_SUCCESS) { // The device could not be opened! 186 if (rc != MCIERR_SUCCESS) { // The device could not be opened!
187 // Close DART, and exit with error code! 187 // Close DART, and exit with error code!
188 DART_CloseDevice(this); 188 DART_CloseDevice(_this);
189 SDL_SetError("Audio device could not be set up"); 189 SDL_SetError("Audio device could not be set up");
190 return 0; 190 return 0;
191 } 191 }
192 // Ok, the device is initialized. 192 // Ok, the device is initialized.
193 // Now we should allocate buffers. For this, we need a place where 193 // Now we should allocate buffers. For this, we need a place where
194 // the buffer descriptors will be: 194 // the buffer descriptors will be:
195 _this->hidden->pMixBuffers = 195 _this->hidden->pMixBuffers =
196 (MCI_MIX_BUFFER *) SDL_malloc(sizeof(MCI_MIX_BUFFER) * iNumBufs); 196 (MCI_MIX_BUFFER *) SDL_malloc(sizeof(MCI_MIX_BUFFER) * iNumBufs);
197 if (!(_this->hidden->pMixBuffers)) { // Not enough memory! 197 if (!(_this->hidden->pMixBuffers)) { // Not enough memory!
198 // Close DART, and exit with error code! 198 // Close DART, and exit with error code!
199 DART_CloseDevice(this); 199 DART_CloseDevice(_this);
200 SDL_OutOfMemory(); 200 SDL_OutOfMemory();
201 return 0; 201 return 0;
202 } 202 }
203 // Now that we have the place for buffer list, we can ask DART for the 203 // Now that we have the place for buffer list, we can ask DART for the
204 // buffers! 204 // buffers!
211 &(_this->hidden->BufferParms), 0); 211 &(_this->hidden->BufferParms), 0);
212 if ((rc != MCIERR_SUCCESS) 212 if ((rc != MCIERR_SUCCESS)
213 || (iNumBufs != _this->hidden->BufferParms.ulNumBuffers) 213 || (iNumBufs != _this->hidden->BufferParms.ulNumBuffers)
214 || (_this->hidden->BufferParms.ulBufferSize == 0)) { // Could not allocate memory! 214 || (_this->hidden->BufferParms.ulBufferSize == 0)) { // Could not allocate memory!
215 // Close DART, and exit with error code! 215 // Close DART, and exit with error code!
216 DART_CloseDevice(this); 216 DART_CloseDevice(_this);
217 SDL_SetError("DART could not allocate buffers"); 217 SDL_SetError("DART could not allocate buffers");
218 return 0; 218 return 0;
219 } 219 }
220 _this->hidden->iCurrNumBufs = iNumBufs; 220 _this->hidden->iCurrNumBufs = iNumBufs;
221 221
226 pMixBufferDesc pBufferDesc = 226 pMixBufferDesc pBufferDesc =
227 (pMixBufferDesc) SDL_malloc(sizeof(tMixBufferDesc));; 227 (pMixBufferDesc) SDL_malloc(sizeof(tMixBufferDesc));;
228 // Check if this buffer was really allocated by DART 228 // Check if this buffer was really allocated by DART
229 if ((!(_this->hidden->pMixBuffers[i].pBuffer)) 229 if ((!(_this->hidden->pMixBuffers[i].pBuffer))
230 || (!pBufferDesc)) { // Wrong buffer! 230 || (!pBufferDesc)) { // Wrong buffer!
231 DART_CloseDevice(this); 231 DART_CloseDevice(_this);
232 SDL_SetError("Error at internal buffer check"); 232 SDL_SetError("Error at internal buffer check");
233 return 0; 233 return 0;
234 } 234 }
235 pBufferDesc->iBufferUsage = BUFFER_EMPTY; 235 pBufferDesc->iBufferUsage = BUFFER_EMPTY;
236 pBufferDesc->pSDLAudioDevice = _this; 236 pBufferDesc->pSDLAudioDevice = _this;
249 _this->hidden->iLastPlayedBuf = -1; 249 _this->hidden->iLastPlayedBuf = -1;
250 // Create event semaphore 250 // Create event semaphore
251 if (DosCreateEventSem 251 if (DosCreateEventSem
252 (NULL, &(_this->hidden->hevAudioBufferPlayed), 0, FALSE) != NO_ERROR) 252 (NULL, &(_this->hidden->hevAudioBufferPlayed), 0, FALSE) != NO_ERROR)
253 { 253 {
254 DART_CloseDevice(this); 254 DART_CloseDevice(_this);
255 SDL_SetError("Could not create event semaphore"); 255 SDL_SetError("Could not create event semaphore");
256 return 0; 256 return 0;
257 } 257 }
258 258
259 return 1; 259 return 1;