testaudio: Properly display playback progress, regardless of data source.

main
Ryan C. Gordon 2023-10-16 13:56:43 -04:00
parent 8c39269279
commit b19e68c8ec
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 8 additions and 10 deletions

View File

@ -75,7 +75,7 @@ struct Thing
} poof;
struct {
SDL_AudioStream *stream;
int total_ticks;
int total_bytes;
Uint64 next_level_update;
Uint8 levels[5];
} stream;
@ -556,14 +556,11 @@ static void StreamThing_ontick(Thing *thing, Uint64 now)
/* are we playing? See if we're done, or update state. */
if (thing->line_connected_to->what == THING_LOGDEV) {
const int available = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
SDL_AudioSpec spec;
if (!available || (SDL_GetAudioStreamFormat(thing->data.stream.stream, NULL, &spec) < 0)) {
if (!available) {
DestroyThingInPoof(thing);
return;
} else {
const int ticksleft = (int) ((((Uint64) (available / SDL_AUDIO_FRAMESIZE(spec))) * 1000) / spec.freq);
const float pct = thing->data.stream.total_ticks ? (((float) (ticksleft)) / ((float) thing->data.stream.total_ticks)) : 0.0f;
thing->progress = 1.0f - pct;
thing->progress = 1.0f - (thing->data.stream.total_bytes ? (((float) (available)) / ((float) thing->data.stream.total_bytes)) : 0.0f);
}
}
@ -583,6 +580,9 @@ static void StreamThing_ondrag(Thing *thing, int button, float x, float y)
if (button == SDL_BUTTON_RIGHT) { /* this is kinda hacky, but use this to disconnect from a playing source. */
if (thing->line_connected_to) {
SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
SDL_FlushAudioStream(thing->data.stream.stream);
}
thing->line_connected_to = NULL;
}
}
@ -597,16 +597,14 @@ static void StreamThing_ondrop(Thing *thing, int button, float x, float y)
/* connect to a logical device! */
SDL_Log("Binding audio stream ('%s') to logical device %u", thing->titlebar, (unsigned int) droppable_highlighted_thing->data.logdev.devid);
if (thing->line_connected_to) {
const SDL_AudioSpec *spec = &droppable_highlighted_thing->data.logdev.spec;
SDL_UnbindAudioStream(thing->data.stream.stream); /* unbind from current device */
if (thing->line_connected_to->what == THING_LOGDEV_CAPTURE) {
SDL_FlushAudioStream(thing->data.stream.stream);
thing->data.stream.total_ticks = (int) ((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / SDL_AUDIO_FRAMESIZE(*spec))) * 1000) / spec->freq);
}
}
SDL_BindAudioStream(droppable_highlighted_thing->data.logdev.devid, thing->data.stream.stream); /* bind to new device! */
thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
thing->progress = 0.0f; /* ontick will adjust this if we're on an output device.*/
thing->data.stream.next_level_update = SDL_GetTicks() + 100;
thing->line_connected_to = droppable_highlighted_thing;
@ -644,7 +642,7 @@ static Thing *CreateStreamThing(const SDL_AudioSpec *spec, const Uint8 *buf, con
if (buf && buflen) {
SDL_PutAudioStreamData(thing->data.stream.stream, buf, (int) buflen);
SDL_FlushAudioStream(thing->data.stream.stream);
thing->data.stream.total_ticks = (int) ((((Uint64) (SDL_GetAudioStreamAvailable(thing->data.stream.stream) / SDL_AUDIO_FRAMESIZE(*spec))) * 1000) / spec->freq);
thing->data.stream.total_bytes = SDL_GetAudioStreamAvailable(thing->data.stream.stream);
}
thing->ontick = StreamThing_ontick;
thing->ondrag = StreamThing_ondrag;