#include #include #include #include #include #include xas_bank *xas_bank_new(xas_audio_format format, size_t entry_size, size_t entry_count) { xas_bank *bank; size_t entry_size_total = sizeof(xas_bank_entry) + format.sample_size * entry_size; size_t total = sizeof(xas_bank) + entry_count * entry_size_total; if ((bank = malloc(total)) == NULL) { goto error_malloc_bank; } memset(bank, '\0', total); bank->format.channels = XAS_AUDIO_MONO; bank->format.sample_size = format.sample_size; bank->format.sample_rate = format.sample_rate; bank->entry_size = entry_size; bank->entry_count = entry_count; return bank; error_malloc_bank: return NULL; } void xas_bank_destroy(xas_bank *bank) { free(bank); } ssize_t xas_bank_record(xas_bank *bank, xas_audio_stream *input, size_t entry_index, size_t count) { xas_bank_entry *entry = &((xas_bank_entry *)(bank + 1))[entry_index]; size_t left = count, index_o = 0; if (!xas_audio_format_eq(bank->format, input->format)) { errno = EINVAL; goto error_invalid_format; } if (count > bank->entry_size) { count = bank->entry_size; } while (left) { ssize_t readlen, amount = left > count? count: left; void *buf; if ((readlen = xas_audio_stream_read(input, &buf, amount)) < 0) { goto error_audio_stream_read; } else if (readlen == 0) { break; } xas_audio_copy(bank->format, entry + 1, buf, index_o, 0, readlen); left -= readlen; index_o += readlen; } return entry->duration = index_o; error_audio_stream_read: error_invalid_format: return -1; } ssize_t xas_bank_load_file(xas_bank *bank, size_t entry_index, const char *path) { ssize_t ret; xas_audio_stream *riff; if ((riff = xas_riff_open_file(path, O_RDONLY)) == NULL) { goto error_riff_open_file; } if ((ret = xas_bank_record(bank, riff, entry_index, bank->entry_size)) < 0) { goto error_record; } xas_audio_stream_destroy(riff); return ret; error_record: xas_audio_stream_destroy(riff); error_riff_open_file: return -1; } static int set_gain(xas_bank_player *player, float gain) { xas_bank_player_set_gain(player, gain); return 0; } xas_bank_player *xas_bank_player_new(xas_bank *bank) { xas_bank_player *player; if ((player = malloc(sizeof(*player))) == NULL) { goto error_malloc_player; } player->obj.start = (xas_object_start_callback)xas_bank_player_start; player->obj.stop = (xas_object_stop_callback)xas_bank_player_stop; player->obj.set_gain = (xas_object_set_gain_callback)set_gain; player->bank = bank; player->status = XAS_BANK_PLAYER_STOPPED; player->gain = 0.0f; player->entry = 0; player->index = 0; return player; error_malloc_player: return NULL; } void xas_bank_player_destroy(xas_bank_player *player) { free(player); } int xas_bank_player_set_entry(xas_bank_player *player, size_t entry) { if (entry > player->bank->entry_count) { errno = EINVAL; goto error_invalid_entry; } player->entry = entry; return 0; error_invalid_entry: return -1; } void xas_bank_player_set_gain(xas_bank_player *player, float gain) { player->gain = gain; } int xas_bank_player_start(xas_bank_player *player) { if (player->status == XAS_BANK_PLAYER_PLAYING) { return 0; } player->status = XAS_BANK_PLAYER_PLAYING; player->index = 0; return 0; } int xas_bank_player_stop(xas_bank_player *player) { if (player->status == XAS_BANK_PLAYER_STOPPED) { return 0; } player->status = XAS_BANK_PLAYER_STOPPED; player->gain = 0.0; player->entry = 0; player->index = 0; return 1; } int xas_bank_player_playing(xas_bank_player *player) { return player->status == XAS_BANK_PLAYER_PLAYING; } static ssize_t stream_fill(xas_bank_player *player, int16_t *dest, size_t count, xas_audio_stream *stream) { xas_bank *bank = player->bank; size_t index_o = 0, left = count; while (left) { if (player->status == XAS_BANK_PLAYER_PLAYING) { xas_bank_entry *entry = &((xas_bank_entry *)(bank + 1))[player->entry]; int16_t *src = (int16_t *)(entry + 1); size_t remaining = entry->duration - player->index, amount = remaining < left? remaining: left; size_t i; for (i=0; igain * src[player->index + i]; } left -= amount; player->index += amount; index_o += amount; if (player->index == entry->duration) { xas_bank_player_stop(player); } } else { xas_audio_zero(bank->format, dest, index_o, left); index_o += left; left = 0; } } return count; } void stream_cleanup(xas_bank_player *player, xas_audio_stream *stream) { return; } xas_audio_stream *xas_bank_player_stream_new(xas_bank_player *player) { return xas_audio_stream_new_source((xas_audio_fill)stream_fill, (xas_audio_cleanup)stream_cleanup, player->bank->format, player->bank->entry_size, player); }