diff options
author | dvdli <dvdli@google.com> | 2021-06-29 21:35:37 +0800 |
---|---|---|
committer | dvdli <dvdli@google.com> | 2021-07-13 14:45:37 +0800 |
commit | 71355a504522da5a307f758416c04a49981763fe (patch) | |
tree | c96a31227f87bd92f3ead527cacddcfbd885b9b2 | |
parent | a55af2abdaa130be92f6c2a9e5436a807ba0faa2 (diff) |
fix remaining_data_size is 0 when not playing a wave file
-rw-r--r-- | utils/tinyplay.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/utils/tinyplay.c b/utils/tinyplay.c index c6abd2b..9f72bbb 100644 --- a/utils/tinyplay.c +++ b/utils/tinyplay.c @@ -104,6 +104,7 @@ struct ctx { struct chunk_fmt chunk_fmt; FILE *file; + size_t file_size; }; static bool is_wave_file(const char *filetype) @@ -183,6 +184,9 @@ static int ctx_init(struct ctx* ctx, struct cmd *cmd) ctx->file = stdin; } else { ctx->file = fopen(cmd->filename, "rb"); + fseek(ctx->file, 0L, SEEK_END); + ctx->file_size = ftell(ctx->file); + fseek(ctx->file, 0L, SEEK_SET); } if (ctx->file == NULL) { @@ -199,6 +203,7 @@ static int ctx_init(struct ctx* ctx, struct cmd *cmd) config->rate = ctx->chunk_fmt.sample_rate; bits = ctx->chunk_fmt.bits_per_sample; is_float = ctx->chunk_fmt.audio_format == WAVE_FORMAT_IEEE_FLOAT; + ctx->file_size = (size_t) ctx->chunk_header.sz; } if (is_float) { @@ -441,9 +446,10 @@ int sample_is_playable(const struct cmd *cmd) int play_sample(struct ctx *ctx) { char *buffer; + bool is_stdin_source = ctx->file == stdin; size_t buffer_size = 0; size_t num_read = 0; - size_t remaining_data_size = ctx->chunk_header.sz; + size_t remaining_data_size = is_stdin_source ? SIZE_MAX : ctx->file_size; size_t played_data_size = 0; size_t read_size = 0; const struct pcm_config *config = pcm_get_config(ctx->pcm); @@ -473,12 +479,21 @@ int play_sample(struct ctx *ctx) fprintf(stderr, "error playing sample. %s\n", pcm_get_error(ctx->pcm)); break; } - remaining_data_size -= num_read; + + if (!is_stdin_source) { + remaining_data_size -= num_read; + } played_data_size += pcm_frames_to_bytes(ctx->pcm, written_frames); } } while (!close && num_read > 0 && remaining_data_size > 0); - printf("Played %zu bytes. Remains %zu bytes.\n", played_data_size, remaining_data_size); + printf("Played %zu bytes. ", played_data_size); + if (is_stdin_source) { + printf("\n"); + } else { + printf("Remains %zu bytes.\n", remaining_data_size); + } + pcm_wait(ctx->pcm, -1); free(buffer); |