summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-05 14:30:13 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-05 14:30:13 -0700
commitcabb2e6d5101506e59abb8be1d87408acabb1136 (patch)
tree532dba66e4a00e624fd96c05fe37fb9b6ad72e67
parent5ae677003dd79f031595b47df99e5b2a7d118ed9 (diff)
Few new helper funcs
-rw-r--r--src/wav.c25
-rw-r--r--src/wav.h7
-rw-r--r--tests/wav.c2
3 files changed, 34 insertions, 0 deletions
diff --git a/src/wav.c b/src/wav.c
index 9796160..ab8919d 100644
--- a/src/wav.c
+++ b/src/wav.c
@@ -1,4 +1,5 @@
#include "wav.h"
+#include "common.h"
#include "smrt_arena.h"
#include <assert.h>
@@ -260,3 +261,27 @@ f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data, u16 num_channel
return vs;
}
+
+f64 *combine_channels(smrt_arena_t *arena, f64 **channels, u64 num_channels, u64 sample_count) {
+ assert(num_channels != 0 && "num_channels must be >0");
+
+ f64 *samples = SMRTA_ALLOC_ARRAY(arena, f64, num_channels * sample_count);
+
+ for (u64 s = 0; s < sample_count; s++) {
+ for (u64 c = 0; c < num_channels; c++) {
+ samples[s*num_channels + c] = channels[c][s];
+ }
+ }
+
+ return samples;
+}
+
+f32 *f64_to_f32(smrt_arena_t *arena, f64 *samples, u64 sample_count) {
+ f32 *o = SMRTA_ALLOC_ARRAY(arena, f32, sample_count);
+
+ for (u64 i = 0; i < sample_count; i++) {
+ o[i] = (f32)samples[i];
+ }
+
+ return o;
+}
diff --git a/src/wav.h b/src/wav.h
index 484c6da..00ab37d 100644
--- a/src/wav.h
+++ b/src/wav.h
@@ -62,6 +62,7 @@ wav_fmt_chunk_t make_wav_fmt_chunk(u32 num_channels ,
/// Load WAV data into [-1.0, 1.0] f64 amplitudes. Populates -o inputs with information.
/// samples will be amplitude data for each channel from the WAV file, of length sample_count.
+/// align_up_memoryn will align allocation up to nearest multiple (For FFT)
void wav_load(smrt_arena_t *arena, FILE *wav, f64 *** samples_o ,
u16 *channel_count_o ,
u64 * sample_count_o ,
@@ -73,3 +74,9 @@ f64 * read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channel
f64 * read_16bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn);
f64 * read_24bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn);
f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn);
+
+/// Combine channel_count arrays of sample_count samples from **channels
+/// Interleaving for WAV format audio storage.
+f64 *combine_channels(smrt_arena_t *arena, f64 **channels, u64 channel_count, u64 sample_count);
+
+f32 *f64_to_f32(smrt_arena_t *arena, f64 *samples, u64 sample_count);
diff --git a/tests/wav.c b/tests/wav.c
index 839b31f..dae2ef1 100644
--- a/tests/wav.c
+++ b/tests/wav.c
@@ -12,6 +12,7 @@
#include <criterion/criterion.h>
#include <criterion/internal/assert.h>
#include <criterion/internal/test.h>
+#include <criterion/redirect.h>
#include <string.h>
Test(wav, read_file) {
@@ -241,6 +242,7 @@ static int non_pcm_fd;
void non_pcm_setup(void) {
non_pcm_fd = mkstemp(non_pcm_path);
cr_assert_geq(non_pcm_fd, 0, "mkstemp failed");
+ cr_redirect_stderr(); // load_wav_file perror()s on the rejected format; that's expected
}
void non_pcm_teardown(void) {