summaryrefslogtreecommitdiff
path: root/src/fruits/cmc/ui/util/waveform.c
blob: 624069e8c197ddc24633bd179f75e07ce8af53bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <al/log.h>
#include <al/math.h>

#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 <nnwt/file.h>
#include <nnwt/net.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>

// 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)
{
    (void)include_rms;
    (void)buf;
    (void)entry;
    (void)width;
    (void)height;
    (void)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';
//    }
//    */
}