test: Updated testaudiohotplug to SDL3 audio API.
parent
2be5f726d4
commit
f883b9fc64
|
@ -31,9 +31,6 @@ static SDL_AudioSpec spec;
|
|||
static Uint8 *sound = NULL; /* Pointer to wave data */
|
||||
static Uint32 soundlen = 0; /* Length of wave data */
|
||||
|
||||
static int posindex = 0;
|
||||
static Uint32 positions[64];
|
||||
|
||||
static SDLTest_CommonState *state;
|
||||
|
||||
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
||||
|
@ -48,31 +45,6 @@ quit(int rc)
|
|||
}
|
||||
}
|
||||
|
||||
static void SDLCALL
|
||||
fillerup(void *_pos, Uint8 *stream, int len)
|
||||
{
|
||||
Uint32 pos = *((Uint32 *)_pos);
|
||||
Uint8 *waveptr;
|
||||
int waveleft;
|
||||
|
||||
/* Set up the pointers */
|
||||
waveptr = sound + pos;
|
||||
waveleft = soundlen - pos;
|
||||
|
||||
/* Go! */
|
||||
while (waveleft <= len) {
|
||||
SDL_memcpy(stream, waveptr, waveleft);
|
||||
stream += waveleft;
|
||||
len -= waveleft;
|
||||
waveptr = sound;
|
||||
waveleft = soundlen;
|
||||
pos = 0;
|
||||
}
|
||||
SDL_memcpy(stream, waveptr, len);
|
||||
pos += len;
|
||||
*((Uint32 *)_pos) = pos;
|
||||
}
|
||||
|
||||
static int done = 0;
|
||||
|
||||
static void poked(int sig)
|
||||
|
@ -97,28 +69,35 @@ static void iteration(void)
|
|||
done = 1;
|
||||
}
|
||||
} else if (e.type == SDL_EVENT_AUDIO_DEVICE_ADDED) {
|
||||
int index = e.adevice.which;
|
||||
int iscapture = e.adevice.iscapture;
|
||||
const char *name = SDL_GetAudioDeviceName(index, iscapture);
|
||||
const SDL_AudioDeviceID which = (SDL_AudioDeviceID ) e.adevice.which;
|
||||
const SDL_bool iscapture = e.adevice.iscapture ? SDL_TRUE : SDL_FALSE;
|
||||
char *name = SDL_GetAudioDeviceName(which);
|
||||
if (name != NULL) {
|
||||
SDL_Log("New %s audio device at index %u: %s\n", devtypestr(iscapture), (unsigned int)index, name);
|
||||
SDL_Log("New %s audio device at id %u: %s", devtypestr(iscapture), (unsigned int)which, name);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device at index %u, but failed to get the name: %s\n",
|
||||
devtypestr(iscapture), (unsigned int)index, SDL_GetError());
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Got new %s device, id %u, but failed to get the name: %s",
|
||||
devtypestr(iscapture), (unsigned int)which, SDL_GetError());
|
||||
continue;
|
||||
}
|
||||
if (!iscapture) {
|
||||
positions[posindex] = 0;
|
||||
spec.userdata = &positions[posindex++];
|
||||
spec.callback = fillerup;
|
||||
dev = SDL_OpenAudioDevice(name, 0, &spec, NULL, 0);
|
||||
dev = SDL_OpenAudioDevice(which, &spec);
|
||||
if (!dev) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open '%s': %s\n", name, SDL_GetError());
|
||||
} else {
|
||||
SDL_AudioStream *stream;
|
||||
SDL_Log("Opened '%s' as %u\n", name, (unsigned int)dev);
|
||||
SDL_PlayAudioDevice(dev);
|
||||
stream = SDL_CreateAndBindAudioStream(dev, &spec);
|
||||
if (!stream) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create/bind an audio stream to %u ('%s'): %s", (unsigned int) dev, name, SDL_GetError());
|
||||
SDL_CloseAudioDevice(dev);
|
||||
}
|
||||
/* !!! FIXME: laziness, this used to loop the audio, but we'll just play it once for now on each connect. */
|
||||
SDL_PutAudioStreamData(stream, sound, soundlen);
|
||||
SDL_FlushAudioStream(stream);
|
||||
/* !!! FIXME: this is leaking the stream for now. We'll wire it up to a dictionary or whatever later. */
|
||||
}
|
||||
}
|
||||
SDL_free(name);
|
||||
} else if (e.type == SDL_EVENT_AUDIO_DEVICE_REMOVED) {
|
||||
dev = (SDL_AudioDeviceID)e.adevice.which;
|
||||
SDL_Log("%s device %u removed.\n", devtypestr(e.adevice.iscapture), (unsigned int)dev);
|
||||
|
@ -194,7 +173,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
/* Load the wave file into memory */
|
||||
if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == NULL) {
|
||||
if (SDL_LoadWAV(filename, &spec, &sound, &soundlen) == -1) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
|
||||
quit(1);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue