From ae8822ec0327c4d5674f1dc79de926b230f27f1f Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 16 Apr 2025 15:12:41 -0400 Subject: Highly wip waveform view Signed-off-by: Andrew Opalach --- src/fruits/cmc/cmc.c | 25 ++++++ src/fruits/cmc/cmc.h | 8 ++ src/fruits/cmc/meson.build | 4 +- src/fruits/cmc/ui/pane_list.c | 82 +++++++++++++++-- src/fruits/cmc/ui/pane_log.c | 44 +++++++++ src/fruits/cmc/ui/ui.c | 38 +++++++- src/fruits/cmc/ui/ui.h | 15 ++++ src/fruits/cmc/ui/waveform.c | 202 ++++++++++++++++++++++++++++++++++++++++++ src/fruits/cmc/ui/waveform.h | 13 ++- 9 files changed, 419 insertions(+), 12 deletions(-) create mode 100644 src/fruits/cmc/ui/pane_log.c create mode 100644 src/fruits/cmc/ui/waveform.c (limited to 'src/fruits') diff --git a/src/fruits/cmc/cmc.c b/src/fruits/cmc/cmc.c index 6e62a4a..265550f 100644 --- a/src/fruits/cmc/cmc.c +++ b/src/fruits/cmc/cmc.c @@ -4,6 +4,7 @@ #include "../../server/common.h" #include "../../liana/list.h" +#include "../../codec/packet_ext.h" #include "../common.h" @@ -31,6 +32,7 @@ static struct cmc_search *get_search_by_id(struct cmc *c, s32 id) static void read_list_entry(struct nn_packet *packet, struct cmc_list_entry *entry, u32 id) { entry->id = id; + entry->node_id = nn_packet_read_u32(packet); entry->duration = nn_packet_read_u64(packet); entry->start = nn_packet_read_u64(packet); entry->paused_at = nn_packet_read_u64(packet); @@ -70,6 +72,9 @@ static void parse_initial_user_state(struct cmc *c, struct nn_packet *packet) u32 id = nn_packet_read_u32(packet); struct cmc_list_entry *entry = al_alloc_object(struct cmc_list_entry); read_list_entry(packet, entry, id); + if (list->current >= 0 && j == (u32)list->current) { + camu_client_request_visual_data(&c->client, entry->node_id); + } al_array_push(list->entries, entry); } al_array_push(c->lists, list); @@ -162,6 +167,7 @@ static void client_callback(void *userdata, u8 op, void *opaque) u32 id = nn_packet_read_u32(packet); struct cmc_list_entry *entry = al_array_at(list->entries, list->current); read_list_entry(packet, entry, id); + camu_client_request_visual_data(&c->client, entry->node_id); cmc_ui_queue_render(&c->ui); break; } @@ -193,6 +199,25 @@ static void client_callback(void *userdata, u8 op, void *opaque) } break; } + case CAMU_CLIENT_GOT_VISUAL_DATA: { + struct nn_packet *packet = (struct nn_packet *)opaque; + u32 node_id = nn_packet_read_u32(packet); + struct cmc_list *list = al_array_at(c->lists, 0); + struct cmc_list_entry *entry; + al_array_foreach(list->entries, i, entry) { + if (entry->node_id == node_id) { + if (entry->raw.data != NULL) break; + nn_packet_read_audio_format(packet, &entry->raw.fmt); + u32 size = nn_packet_read_u32(packet); + u8 *ptr; + NNWT_PACKET_READ_DATA(packet, size, ptr); + entry->raw.data = al_malloc(size); + al_memcpy(entry->raw.data, ptr, size); + entry->raw.sample_count = nn_packet_read_u32(packet); + } + } + break; + } } } diff --git a/src/fruits/cmc/cmc.h b/src/fruits/cmc/cmc.h index e2cdde3..ea714dc 100644 --- a/src/fruits/cmc/cmc.h +++ b/src/fruits/cmc/cmc.h @@ -6,6 +6,7 @@ #include "../../libclient/client.h" #include "../../portal/src/post_cache.h" +#include "../../codec/codec.h" #include "ui/ui.h" @@ -23,11 +24,18 @@ struct cmc_search { struct cmc_list_entry { u32 id; + u32 node_id; u64 duration; u64 start; u64 paused_at; u64 offset; wstr name; + struct { + struct camu_audio_format fmt; + u8 *data; + u32 sample_count; + char ***drawing; + } raw; }; struct cmc_list { diff --git a/src/fruits/cmc/meson.build b/src/fruits/cmc/meson.build index 6b7060d..a0fa097 100644 --- a/src/fruits/cmc/meson.build +++ b/src/fruits/cmc/meson.build @@ -12,8 +12,10 @@ if use_tui 'ui/ui.c', 'ui/pane_list.c', 'ui/pane_search.c', + 'ui/pane_log.c', + 'ui/waveform.c' ] - cmc_deps += [notcurses, notcurses_core] + cmc_deps += [notcurses, notcurses_core, codecs_client] endif if is_windows and meson.is_cross_build() diff --git a/src/fruits/cmc/ui/pane_list.c b/src/fruits/cmc/ui/pane_list.c index 695ff60..b62e0a6 100644 --- a/src/fruits/cmc/ui/pane_list.c +++ b/src/fruits/cmc/ui/pane_list.c @@ -7,6 +7,7 @@ #include "ui.h" #include "ext.h" +#include "waveform.h" void cmc_lp_init(struct cmc_ui *ui) { @@ -141,7 +142,7 @@ bool cmc_lp_handle_input(struct cmc_ui *ui, struct ncinput *input) CONSIDER_INPUT(); if (tab->move.active) { tab->move.active = false; - } else { + } else if (tab->selected != tab->list->current) { camu_client_skipto(&ui->c->client, &tab->list->name, tab->selected); } break; @@ -244,7 +245,7 @@ static void render_now_playing(struct cmc_ui *ui, struct cmc_list_tab *tab, u32 u64 pos = 0; u64 duration = 0; u64 remaining = 0; - u32 y_off = height - 6; + u32 y_off = height - 7; ncplane_set_styles(n, NCSTYLE_BOLD); if (entry) { @@ -288,10 +289,10 @@ static void render_now_playing(struct cmc_ui *ui, struct cmc_list_tab *tab, u32 ncplane_putstr_yx(n, y_off, 4 + offset, "โŽน"); if (entry) { - cmc_ui_putnwstr_yx(n, y_off, 6 + offset, MIN(entry->name.length, (u32)40), &entry->name); + cmc_ui_putnwstr_yx(n, y_off, 6 + offset, MIN(entry->name.length, (u32)60), &entry->name); } - bool show_remaining = true; + bool show_remaining = false; if (show_remaining) { offset = camu_print_time(timestr, sizeof(timestr), remaining, true, 4); } else { @@ -306,10 +307,81 @@ static void render_now_playing(struct cmc_ui *ui, struct cmc_list_tab *tab, u32 } ncplane_putstr_yx(n, y_off, width - 2 - offset, timestr); + /* ncplane_putchar_yx(n, height - 5, 2, 'A'); ncplane_putchar_yx(n, height - 4, 2, 'A'); ncplane_putchar_yx(n, height - 3, 2, 'A'); ncplane_putchar_yx(n, height - 2, 2, 'A'); + */ + + if (entry && entry->raw.data != NULL) { + u32 length = width - 2; + u32 top = height - 6; + u32 wave_height = 5; + if (entry->raw.drawing == NULL || entry != ui->lp.blitted) { + entry->raw.drawing = al_malloc(sizeof(char **) * wave_height); + for (u32 j = 0; j < wave_height; j++) { + entry->raw.drawing[j] = al_malloc(sizeof(char *) * length); + for (u32 i = 0; i < length; i++) { + entry->raw.drawing[j][i] = al_calloc(1, 6); + } + } + if (!ui->lp.w) { + struct ncplane_options opts = { 0 }; + opts.x = 1; + opts.y = top; + opts.cols = length; + opts.rows = wave_height; + ui->lp.w = ncplane_create(n, &opts); + } + /* + struct nccell ncl; + nccell_init(&ncl); + nccell_load(w, &ncl, " "); + nccell_set_bg_palindex(&ncl, 3); + ncplane_polyfill_yx(w, 0, 0, &ncl); + */ + u32 *rgba; + u32 pixel_width, pixel_height; + ncplane_pixel_geom(ui->lp.w, &pixel_height, &pixel_width, NULL, NULL, NULL, NULL); + //cmc_draw_waveform(entry, length, wave_height, false, entry->raw.drawing, &rgba); + cmc_draw_waveform(entry, pixel_width, pixel_height, false, entry->raw.drawing, &rgba); + if (ui->lp.vis) { + ncvisual_destroy(ui->lp.vis); + } + ui->lp.vis = ncvisual_from_rgba(rgba, pixel_height, pixel_width * 4, pixel_width); + } + + if (entry != ui->lp.blitted) { + ncplane_erase(ui->lp.w); + struct ncvisual_options vopts = { 0 }; + vopts.blitter = NCBLIT_PIXEL; + vopts.scaling = NCSCALE_STRETCH; + vopts.flags = NCVISUAL_OPTION_NOINTERPOLATE; + vopts.n = ui->lp.w; + vopts.x = 0; + vopts.y = 0; + ncvisual_blit(ui->nc, ui->lp.vis, &vopts); + ui->lp.blitted = entry; + } + + /* + ncplane_set_fg_palindex(n, 5); + for (u32 j = 0; j < wave_height; j++) { + for (u32 i = 0; i < length; i++) { + char *cell = entry->raw.drawing[j][i]; + if (cell[0] == 'R') { + ncplane_set_bg_palindex(n, 2); + } + ncplane_putstr_yx(n, top + j, 1 + i, cell + 1); + if (cell[0] == 'R') { + ncplane_set_bg_default(n); + } + } + } + ncplane_set_fg_default(n); + */ + } /* char buf[24]; @@ -330,7 +402,7 @@ void cmc_lp_render(struct cmc_ui *ui) if (ui->lp.tabs.count <= ui->lp.current) return; struct cmc_list_tab *tab = &al_array_at(ui->lp.tabs, ui->lp.current); - tab->page_length = height - 8; + tab->page_length = height - 9; render_list_entries(ui, tab, tab->page_length); render_now_playing(ui, tab, tab->page_length, height); } diff --git a/src/fruits/cmc/ui/pane_log.c b/src/fruits/cmc/ui/pane_log.c new file mode 100644 index 0000000..a35cf20 --- /dev/null +++ b/src/fruits/cmc/ui/pane_log.c @@ -0,0 +1,44 @@ +#include "ui.h" + +void cmc_logp_init(struct cmc_ui *ui) +{ + al_array_init(ui->logp.messages); +} + +void cmc_logp_layout(struct cmc_ui *ui, struct ncplane *parent) +{ + if (ui->logp.n) ncplane_destroy(ui->logp.n); + struct ncplane_options nopts = { 0 }; + nopts.rows = ncplane_dim_y(parent); + nopts.cols = ncplane_dim_x(parent); + ui->logp.n = ncplane_create(parent, &nopts); +} + +void cmc_logp_push_message(struct cmc_ui *ui, char *message) +{ + al_array_push(ui->logp.messages, message); +} + +void cmc_logp_erase(struct cmc_ui *ui) +{ + ncplane_erase(ui->logp.n); +} + +void cmc_logp_render(struct cmc_ui *ui) +{ + struct ncplane *n = ui->logp.n; + + u32 max_height = ncplane_dim_y(n); + + u32 size = ui->logp.messages.count; + u32 index = (size > max_height) ? size - max_height : 0; + for (u32 i = index; i < size; i++) { + // We can't use putnstr to control the width because these messages will have wide characters. + ncplane_putstr_yx(n, i - index, 0, al_array_at(ui->logp.messages, i)); + } + + for (u32 i = 0; i < index; i++) { + al_free(al_array_at(ui->logp.messages, i)); + } + al_array_remove_range(ui->logp.messages, 0, index); +} diff --git a/src/fruits/cmc/ui/ui.c b/src/fruits/cmc/ui/ui.c index 10357c0..b830042 100644 --- a/src/fruits/cmc/ui/ui.c +++ b/src/fruits/cmc/ui/ui.c @@ -1,4 +1,5 @@ #include +#include #include "ui.h" #include "ext.h" @@ -43,14 +44,23 @@ static void erase_previous_pane(struct cmc_ui *ui, u8 previous, u8 upcoming) case CMC_PANE_SEARCH: pn = ui->sp.n; break; + case CMC_PANE_LOG: + pn = ui->logp.n; + break; } ncplane_erase(pn); switch (upcoming) { case CMC_PANE_LIST: - ncplane_move_family_below(pn, ui->lp.n); + ncplane_move_top(ui->lp.n); + //ncplane_move_family_below(pn, ui->lp.n); break; case CMC_PANE_SEARCH: - ncplane_move_family_below(pn, ui->sp.n); + ncplane_move_top(ui->sp.n); + //ncplane_move_family_below(pn, ui->sp.n); + break; + case CMC_PANE_LOG: + ncplane_move_top(ui->logp.n); + //ncplane_move_family_below(pn, ui->logp.n); break; } } @@ -72,6 +82,9 @@ void cmc_ui_queue_render(struct cmc_ui *ui) case CMC_PANE_SEARCH: cmc_sp_erase(ui); break; + case CMC_PANE_LOG: + cmc_logp_erase(ui); + break; } if (ui->overlay_shown) { @@ -85,6 +98,9 @@ void cmc_ui_queue_render(struct cmc_ui *ui) case CMC_PANE_SEARCH: cmc_sp_render(ui); break; + case CMC_PANE_LOG: + cmc_logp_render(ui); + break; } if (ui->overlay_shown) { @@ -132,11 +148,12 @@ static void input_poll_callback(void *userdata, s32 revents) switch_to_pane(ui, CMC_PANE_LIST); break; case '2': - switch_to_pane(ui, CMC_PANE_SEARCH); break; case '3': + switch_to_pane(ui, CMC_PANE_SEARCH); break; case '4': + switch_to_pane(ui, CMC_PANE_LOG); break; case NCKEY_TAB: cmc_ui_toggle_overlay(ui); @@ -154,6 +171,9 @@ static void input_poll_callback(void *userdata, s32 revents) do_render |= true; } break; + case CMC_PANE_LOG: + do_render = true; + break; } if (do_render) { cmc_ui_queue_render(ui); @@ -187,6 +207,7 @@ static bool relayout(struct cmc_ui *ui) cmc_lp_layout(ui, ui->n); cmc_sp_layout(ui, ui->n); + cmc_logp_layout(ui, ui->n); if (ui->oln) ncplane_destroy(ui->oln); nopts.margin_r = MAX((u32)6, ncplane_dim_x(ui->n) / 2); @@ -206,6 +227,14 @@ static s32 resize_cb(struct ncplane *p) return 0; } +static s32 log_callback(void *userdata, u8 level, char *message) +{ + struct cmc_ui *ui = (struct cmc_ui *)userdata; + (void)level; + cmc_logp_push_message(ui, message); + return al_strnlen(message, AL_LOG_MESSAGE_SIZE); +} + bool cmc_ui_init(struct cmc_ui *ui, struct nn_event_loop *loop, struct cmc *c) { al_memset(ui, 0, sizeof(struct cmc_ui)); @@ -223,6 +252,9 @@ bool cmc_ui_init(struct cmc_ui *ui, struct nn_event_loop *loop, struct cmc *c) cmc_lp_init(ui); cmc_sp_init(ui); + cmc_logp_init(ui); + al_set_print(log_callback, ui); + ui->pending_layout = false; ui->pending_render = false; diff --git a/src/fruits/cmc/ui/ui.h b/src/fruits/cmc/ui/ui.h index b0ae730..302d735 100644 --- a/src/fruits/cmc/ui/ui.h +++ b/src/fruits/cmc/ui/ui.h @@ -10,6 +10,7 @@ enum { CMC_PANE_LIST = 0, CMC_PANE_SEARCH, + CMC_PANE_LOG }; struct cmc_list_tab { @@ -51,6 +52,9 @@ struct cmc_ui { u64 last_mouse_ts; struct { struct ncplane *n; + struct ncplane *w; + struct ncvisual *vis; + struct cmc_list_entry *blitted; array(struct cmc_list_tab) tabs; u32 current; } lp; // list pane. @@ -59,6 +63,10 @@ struct cmc_ui { array(struct cmc_search_tab) tabs; u32 current; } sp; // search pane. + struct { + struct ncplane *n; + array(char *) messages; + } logp; struct nn_poll input_poll; struct nn_timer render_timer; struct cmc *c; @@ -95,3 +103,10 @@ void cmc_sp_add_search(struct cmc_ui *ui, struct cmc_search *search); bool cmc_sp_handle_input(struct cmc_ui *ui, struct ncinput *input); void cmc_sp_erase(struct cmc_ui *ui); void cmc_sp_render(struct cmc_ui *ui); + +void cmc_logp_init(struct cmc_ui *ui); +void cmc_logp_layout(struct cmc_ui *ui, struct ncplane *parent); +void cmc_logp_push_message(struct cmc_ui *ui, char *message); +bool cmc_logp_handle_input(struct cmc_ui *ui, struct ncinput *input); +void cmc_logp_erase(struct cmc_ui *ui); +void cmc_logp_render(struct cmc_ui *ui); diff --git a/src/fruits/cmc/ui/waveform.c b/src/fruits/cmc/ui/waveform.c new file mode 100644 index 0000000..9bfc296 --- /dev/null +++ b/src/fruits/cmc/ui/waveform.c @@ -0,0 +1,202 @@ +#include +#include +#include "waveform.h" + +#define WITHIN_RMS(low, high, rms) \ + ((low >= 0 && low <= rms) || (high < 0 && high >= -rms) || (low < 0 && high > 0 && rms > 0)) + +#define OUTPUT_WAVEFORM_PNG +//#define WAVEFORM_REFERENCE_MODE +#ifdef OUTPUT_WAVEFORM_PNG +#include +#include +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include + +// min/max: #BC615E, #F4EAE0 +// rms: #FF928F, #E4CAAF +// +// min/max: #D93A50, #b22f41 +// rms: #f44f7a, #c84064 +// extra: #f6ac7f, #F5F4FA, #5b524b +// left channel: n == 0, right channel: n == 1. +#define COLOR_FOR_PIXEL(n, rms) rms ? \ + n == 0 ? nn_htonl(0xE4CAAF99) : nn_htonl(0xFF928FFF) : \ + n == 0 ? nn_htonl(0xF4EAE099) : nn_htonl(0xBC615EFF) + +// ss: current sample, ns: next sample. +#define HIT_WAVE(low, high, ss, ns) \ + ((ss <= low && ns > low) || (ss > high && ns <= high) || (ss > low && ns <= high)) + +static void test_write_bitmap(struct cmc_list_entry *entry, u32 width, u32 height, u32 **rgba) +{ + f64 start = nn_get_tick(); + + *rgba = al_calloc(width * height, 4); + + u32 channels = entry->raw.fmt.channel_count; + u32 samples_per_column = entry->raw.sample_count / width; + + s32 wave_min = INT16_MIN; + s32 wave_max = INT16_MAX; + s32 step = (wave_max * 2 + (height - 1)) / height; + //s32 step = (wave_max * 2 + (height / 2)) / height; + + s16 *steps = al_malloc(sizeof(s16) * (height + 1)); + for (s32 i = 0; i <= (s32)height; i++) { + steps[i] = (s16)MAX(wave_max - step * i, wave_min); + } + //steps[height] = wave_min; + al_assert(steps[height] == wave_min); + + s64 rms = 0; + s16 min, max; + s16 low, high; + s16 *data = (s16 *)entry->raw.data; + const u32 samples_stride = samples_per_column * channels; + for (u32 i = 0; i < width; i++) { + u32 f = i * samples_stride; + for (u32 n = 0; n < channels; n++) { + rms = 0; + min = wave_max, max = wave_min; + for (u32 m = 0; m <= samples_stride; m += channels) { + s16 ss = data[f + m + n]; + rms += ss * ss; + min = MIN(min, ss); + max = MAX(max, ss); + } + rms /= samples_per_column; + rms = sqrt(rms); +#ifndef WAVEFORM_REFERENCE_MODE + for (u32 j = 0; j < height; j++) { + low = steps[j + 1], high = steps[j]; + if (low < max && high >= min) { + (*rgba)[j * width + i] = COLOR_FOR_PIXEL(n, WITHIN_RMS(low, high, rms)); + } + } +#else + for (u32 m = 0; m < samples_stride; m += channels) { + s16 ss = data[f + m + n]; + s16 ns = data[f + m + n + channels]; + for (u32 j = 0; j < height; j++) { + low = steps[j + 1], high = steps[j]; + if (HIT_WAVE(low, high, ss, ns)) { + (*rgba)[j * width + i] = COLOR_FOR_PIXEL(n, WITHIN_RMS(low, high, rms)); + } + } + } +#endif + } + } + + log_info("generating waveform took: %.2fs.", nn_get_tick() - start); + + //stbi_write_png("./waveform.png", width, height, 4, *rgba, width * 4); + + /* + struct nn_file file; + nn_file_open(&file, &al_str_c("./raw_samples.raw"), NNWT_FILE_CREATE); + size_t bytes = entry->raw.sample_count * entry->raw.fmt.channel_count * camu_audio_format_bytes_per_sample(&entry->raw.fmt); + nn_file_write(&file, entry->raw.data, bytes); + nn_file_close(&file); + */ +} +#endif + +static const char *octant_lut[] = { + " ", "๐œบจ", "๐œบซ", "๐Ÿฎ‚", "๐œด€", "โ–˜", "๐œด", "๐œด‚", "๐œดƒ", "๐œด„", "โ–", "๐œด…", "๐œด†", "๐œด‡", "๐œดˆ", "โ–€", + "๐œด‰", "๐œดŠ", "๐œด‹", "๐œดŒ", "๐Ÿฏฆ", "๐œด", "๐œดŽ", "๐œด", "๐œด", "๐œด‘", "๐œด’", "๐œด“", "๐œด”", "๐œด•", "๐œด–", "๐œด—", + "๐œด˜", "๐œด™", "๐œดš", "๐œด›", "๐œดœ", "๐œด", "๐œดž", "๐œดŸ", "๐Ÿฏง", "๐œด ", "๐œดก", "๐œดข", "๐œดฃ", "๐œดค", "๐œดฅ", "๐œดฆ", + "๐œดง", "๐œดจ", "๐œดฉ", "๐œดช", "๐œดซ", "๐œดฌ", "๐œดญ", "๐œดฎ", "๐œดฏ", "๐œดฐ", "๐œดฑ", "๐œดฒ", "๐œดณ", "๐œดด", "๐œดต", "๐Ÿฎ…", + "๐œบฃ", "๐œดถ", "๐œดท", "๐œดธ", "๐œดน", "๐œดบ", "๐œดป", "๐œดผ", "๐œดฝ", "๐œดพ", "๐œดฟ", "๐œต€", "๐œต", "๐œต‚", "๐œตƒ", "๐œต„", + "โ––", "๐œต…", "๐œต†", "๐œต‡", "๐œตˆ", "โ–Œ", "๐œต‰", "๐œตŠ", "๐œต‹", "๐œตŒ", "โ–ž", "๐œต", "๐œตŽ", "๐œต", "๐œต", "โ–›", + "๐œต‘", "๐œต’", "๐œต“", "๐œต”", "๐œต•", "๐œต–", "๐œต—", "๐œต˜", "๐œต™", "๐œตš", "๐œต›", "๐œตœ", "๐œต", "๐œตž", "๐œตŸ", "๐œต ", + "๐œตก", "๐œตข", "๐œตฃ", "๐œตค", "๐œตฅ", "๐œตฆ", "๐œตง", "๐œตจ", "๐œตฉ", "๐œตช", "๐œตซ", "๐œตฌ", "๐œตญ", "๐œตฎ", "๐œตฏ", "๐œตฐ", + "๐œบ ", "๐œตฑ", "๐œตฒ", "๐œตณ", "๐œตด", "๐œตต", "๐œตถ", "๐œตท", "๐œตธ", "๐œตน", "๐œตบ", "๐œตป", "๐œตผ", "๐œตฝ", "๐œตพ", "๐œตฟ", + "๐œถ€", "๐œถ", "๐œถ‚", "๐œถƒ", "๐œถ„", "๐œถ…", "๐œถ†", "๐œถ‡", "๐œถˆ", "๐œถ‰", "๐œถŠ", "๐œถ‹", "๐œถŒ", "๐œถ", "๐œถŽ", "๐œถ", + "โ–—", "๐œถ", "๐œถ‘", "๐œถ’", "๐œถ“", "โ–š", "๐œถ”", "๐œถ•", "๐œถ–", "๐œถ—", "โ–", "๐œถ˜", "๐œถ™", "๐œถš", "๐œถ›", "โ–œ", + "๐œถœ", "๐œถ", "๐œถž", "๐œถŸ", "๐œถ ", "๐œถก", "๐œถข", "๐œถฃ", "๐œถค", "๐œถฅ", "๐œถฆ", "๐œถง", "๐œถจ", "๐œถฉ", "๐œถช", "๐œถซ", + "โ–‚", "๐œถฌ", "๐œถญ", "๐œถฎ", "๐œถฏ", "๐œถฐ", "๐œถฑ", "๐œถฒ", "๐œถณ", "๐œถด", "๐œถต", "๐œถถ", "๐œถท", "๐œถธ", "๐œถน", "๐œถบ", + "๐œถป", "๐œถผ", "๐œถฝ", "๐œถพ", "๐œถฟ", "๐œท€", "๐œท", "๐œท‚", "๐œทƒ", "๐œท„", "๐œท…", "๐œท†", "๐œท‡", "๐œทˆ", "๐œท‰", "๐œทŠ", + "๐œท‹", "๐œทŒ", "๐œท", "๐œทŽ", "๐œท", "๐œท", "๐œท‘", "๐œท’", "๐œท“", "๐œท”", "๐œท•", "๐œท–", "๐œท—", "๐œท˜", "๐œท™", "๐œทš", + "โ–„", "๐œท›", "๐œทœ", "๐œท", "๐œทž", "โ–™", "๐œทŸ", "๐œท ", "๐œทก", "๐œทข", "โ–Ÿ", "๐œทฃ", "โ–†", "๐œทค", "๐œทฅ", "โ–ˆ" +}; + +#define RMS_BIT (1 << 8) + +void cmc_draw_waveform(struct cmc_list_entry *entry, u32 width, u32 height, bool include_rms, char ***buf, u32 **rgba) +{ +#ifdef OUTPUT_WAVEFORM_PNG + test_write_bitmap(entry, width, height, rgba); + return; +#endif + + u32 channels = entry->raw.fmt.channel_count; + u32 samples_per_column = entry->raw.sample_count / (width * 2); + + s32 pixel_height = height * 4; + s32 wave_min = INT16_MIN; + s32 wave_max = INT16_MAX; + s32 step = (wave_max * 2 + (pixel_height - 1)) / pixel_height; + + s16 *steps = al_malloc(sizeof(s16) * (pixel_height + 1)); + for (s32 i = 0; i <= (s32)pixel_height; i++) { + steps[i] = (s16)MAX(wave_max - step * i, wave_min); + } + al_assert(steps[pixel_height] == wave_min); + + s64 rms = 0; + s16 min, max; + s16 low, high; + s16 *data = (s16 *)entry->raw.data; + const u32 samples_stride = samples_per_column * channels; + u16 masks_for_chars[height]; + //u32 slen[height]; + //al_memset(slen, 0, sizeof(u32) * height); + for (u32 i = 0; i < width; i++) { + al_memset(masks_for_chars, 0, sizeof(u16) * height); + for (u32 k = 0; k < 2; k++) { + u32 f = (i * 2 + k) * samples_stride; + for (u32 n = 0; n < channels; n++) { + rms = 0; + min = wave_max, max = wave_min; + for (u32 m = 0; m <= samples_stride; m += channels) { + s16 ss = data[f + m + n]; + rms += ss * ss; + min = MIN(min, ss); + max = MAX(max, ss); + } + rms /= samples_per_column; + rms = sqrt(rms); + for (u32 j = 0; j < height; j++) { + for (u32 l = 0; l < 4; l++) { + u32 step = j * 4 + l; + low = steps[step + 1], high = steps[step]; + if (low < max && high >= min) { + if (include_rms && WITHIN_RMS(low, high, rms)) { + masks_for_chars[j] |= RMS_BIT; + } else { + masks_for_chars[j] |= 1 << (l * 2 + k); + } + } + } + } + } + } + for (u32 j = 0; j < height; j++) { + if (masks_for_chars[j] & RMS_BIT) { + buf[j][i][0] = 'R'; + masks_for_chars[j] &= ~RMS_BIT; + } + u32 len = masks_for_chars[j] == 0 ? 2 : 5; + al_memcpy(buf[j][i] + 1, octant_lut[masks_for_chars[j]], len); + //slen[j] += len; + } + } + + /* + for (u32 j = 0; j < height; j++) { + buf[j][slen[j]] = '\0'; + } + */ +} diff --git a/src/fruits/cmc/ui/waveform.h b/src/fruits/cmc/ui/waveform.h index f3803dd..464d589 100644 --- a/src/fruits/cmc/ui/waveform.h +++ b/src/fruits/cmc/ui/waveform.h @@ -1,5 +1,4 @@ /* - notcurses.h: Halves @@ -84,9 +83,15 @@ AAAAAAAAAAAAAAAA ๐œดข 0b00101011 +๐œบจ๐œบซ๐Ÿฎ‚๐œด€โ–˜๐œด๐œด‚๐œดƒ๐œด„โ–๐œด…๐œด†๐œด‡๐œดˆโ–€๐œด‰๐œดŠ๐œด‹๐œดŒ๐Ÿฏฆ๐œด๐œดŽ๐œด๐œด๐œด‘๐œด’๐œด“๐œด”๐œด•๐œด–๐œด—๐œด˜๐œด™๐œดš๐œด›๐œดœ๐œด๐œดž๐œดŸ๐Ÿฏง๐œด ๐œดก๐œดข๐œดฃ๐œดค๐œดฅ๐œดฆ๐œดง๐œดจ๐œดฉ๐œดช๐œดซ๐œดฌ๐œดญ๐œดฎ๐œดฏ๐œดฐ๐œดฑ๐œดฒ๐œดณ๐œดด๐œดต๐Ÿฎ…๐œบฃ๐œดถ๐œดท๐œดธ๐œดน๐œดบ๐œดป๐œดผ๐œดฝ๐œดพ๐œดฟ๐œต€๐œต๐œต‚๐œตƒ๐œต„โ––๐œต…๐œต†๐œต‡๐œตˆโ–Œ๐œต‰๐œตŠ๐œต‹๐œตŒโ–ž๐œต๐œตŽ๐œต๐œตโ–›๐œต‘๐œต’๐œต“๐œต”๐œต•๐œต–๐œต—๐œต˜๐œต™๐œตš๐œต›๐œตœ๐œต๐œตž๐œตŸ๐œต ๐œตก๐œตข๐œตฃ๐œตค๐œตฅ๐œตฆ๐œตง๐œตจ๐œตฉ๐œตช๐œตซ๐œตฌ๐œตญ๐œตฎ๐œตฏ๐œตฐ๐œบ ๐œตฑ๐œตฒ๐œตณ๐œตด๐œตต๐œตถ๐œตท๐œตธ๐œตน๐œตบ๐œตป๐œตผ๐œตฝ๐œตพ๐œตฟ๐œถ€๐œถ๐œถ‚๐œถƒ๐œถ„๐œถ…๐œถ†๐œถ‡๐œถˆ๐œถ‰๐œถŠ๐œถ‹๐œถŒ๐œถ๐œถŽ๐œถโ–—๐œถ๐œถ‘๐œถ’๐œถ“โ–š๐œถ”๐œถ•๐œถ–๐œถ—โ–๐œถ˜๐œถ™๐œถš๐œถ›โ–œ๐œถœ๐œถ๐œถž๐œถŸ๐œถ ๐œถก๐œถข๐œถฃ๐œถค๐œถฅ๐œถฆ๐œถง๐œถจ๐œถฉ๐œถช๐œถซโ–‚๐œถฌ๐œถญ๐œถฎ๐œถฏ๐œถฐ๐œถฑ๐œถฒ๐œถณ๐œถด๐œถต๐œถถ๐œถท๐œถธ๐œถน๐œถบ๐œถป๐œถผ๐œถฝ๐œถพ๐œถฟ๐œท€๐œท๐œท‚๐œทƒ๐œท„๐œท…๐œท†๐œท‡๐œทˆ๐œท‰๐œทŠ๐œท‹๐œทŒ๐œท๐œทŽ๐œท๐œท๐œท‘๐œท’๐œท“๐œท”๐œท•๐œท–๐œท—๐œท˜๐œท™๐œทšโ–„๐œท›๐œทœ๐œท๐œทžโ–™๐œทŸ๐œท ๐œทก๐œทขโ–Ÿ๐œทฃโ–†๐œทค๐œทฅโ–ˆ */ -static char *bar_lut[] = { +#include + +#include "../cmc.h" + +/* +static const char *bar_lut[] = { [0] = " ๐œบ โ–—๐œถ–โ–" , [1] = "๐œบฃโ–‚๐œท‹๐œท“๐œท•", [2] = "โ––๐œถปโ–„๐œทก๐œทฅ", @@ -94,11 +99,13 @@ static char *bar_lut[] = { [4] = "โ–Œ๐œท€โ–™๐œทคโ–ˆ" }; -static char *bar_bottom_lut[] = { +static const char *bar_bottom_lut[] = { [0] = " ๐œบซโ–๐œดกโ–" , [1] = "๐œบจ๐Ÿฎ‚๐œด…๐œดข๐œถ˜", [2] = "โ–˜๐œด‚โ–€๐œดฆโ–œ", [3] = "๐œด๐œด๐œด—๐Ÿฎ…๐œถซ", [4] = "โ–Œ๐œตŠโ–›๐œตฐโ–ˆ" }; +*/ +void cmc_draw_waveform(struct cmc_list_entry *entry, u32 width, u32 height, bool include_rms, char ***buf, u32 **rgba); -- cgit v1.2.3-101-g0448