diff options
| author | steven-na <noreply.github@stvnc.dev> | 2026-08-02 03:50:01 -0700 |
|---|---|---|
| committer | steven-na <noreply.github@stvnc.dev> | 2026-08-02 03:50:01 -0700 |
| commit | 6158827fd3b6480160739682d946e635f2dd1cb8 (patch) | |
| tree | 130278640c10bb987e1df888568462462736de23 /src/wav.c | |
| parent | 484b4eb7ec20d01230f22e159c33b7225af34ab9 (diff) | |
Added functions for generating [-1.0, 1.0] range values for wav inputs
Diffstat (limited to 'src/wav.c')
| -rw-r--r-- | src/wav.c | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -94,3 +94,68 @@ wav_fmt_chunk_t make_wav_fmt_chunk(u32 num_channels, u32 sample_rate, u16 bits_p return o; } + +f64 *read_8bps_data(smrt_arena_t *arena, wav_data_t data) { + f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count); + if (!vs) return NULL; + + for (u64 i = 0; i < data.sample_count; i++) { + u8 sample = data.samples[i]; + f64 amplitude = (f64)(sample - 128) / (f64)(0b1<<7); + + vs[i] = amplitude; + } + + return vs; +} + +f64 *read_16bps_data(smrt_arena_t *arena, wav_data_t data) { + f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count); + if (!vs) return NULL; + + for (u64 i = 0; i < data.sample_count; i++) { + u8 low = data.samples[ i*2] ; + u8 high = data.samples[(i*2)+1]; + i16 intermediate = low | (high << 8); + f64 amplitude = (f64)intermediate / (f64)(0b1<<15); + + vs[i] = amplitude; + } + + return vs; +} + +f64 *read_24bps_data(smrt_arena_t *arena, wav_data_t data) { + f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count); + if (!vs) return NULL; + + for (u64 i = 0; i < data.sample_count; i++) { + u8 low = data.samples[ i*3] ; + u8 mid = data.samples[(i*3)+1]; + u8 high = data.samples[(i*3)+2]; + i32 intermediate = low | (mid << 8) | (high << 16); + if (high & 0b10000000) intermediate |= 0xFF000000; + f64 amplitude = (f64)intermediate / ((f64)(0b1 << 23)); + + vs[i] = amplitude; + } + + return vs; +} + +f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data) { + f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count); + if (!vs) return NULL; + + for (u64 i = 0; i < data.sample_count; i++) { + u8 one = data.samples[ i*4 ]; + u8 two = data.samples[(i*4)+1]; + u8 three = data.samples[(i*4)+2]; + u8 four = data.samples[(i*4)+3]; + u32 intermediate = one | two << 8 | three << 16 | four << 24; + + memcpy(&vs[i], &intermediate, sizeof(u32)); + } + + return vs; +} |