summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-08-05 01:22:18 -0700
committersteven-na <noreply.github@stvnc.dev>2026-08-05 01:22:18 -0700
commit85d6a21304d80d1a5ca093a198118e986fe81c46 (patch)
treeffe803f3264303fa1ff50dc870282cb4dad07061
parentba124c0c03551d7b17f9f789ce6ab5b9f897f9f5 (diff)
Memory align wav functs
-rw-r--r--src/wav.c39
-rw-r--r--src/wav.h15
-rw-r--r--tests/wav.c22
3 files changed, 42 insertions, 34 deletions
diff --git a/src/wav.c b/src/wav.c
index 7d00c1d..9796160 100644
--- a/src/wav.c
+++ b/src/wav.c
@@ -30,7 +30,7 @@ b32 seek_to_chunk(FILE *file, char const *chunk_name) {
return true;
}
-wav_data_t load_wav_file(smrt_arena_t *arena, FILE *wav_file, wav_master_chunk_t *master_o, wav_fmt_chunk_t *format_o) {
+wav_data_t load_wav_file(smrt_arena_t *arena, FILE *wav_file, wav_master_chunk_t *master_o, wav_fmt_chunk_t *format_o, u64 align_up_memoryn) {
wav_data_t data = { 0 };
if (!wav_file) return data;
@@ -54,7 +54,7 @@ wav_data_t load_wav_file(smrt_arena_t *arena, FILE *wav_file, wav_master_chunk_t
u32 sampled_data_size;
fread(&sampled_data_size, 4, 1, wav_file);
- data.samples = smrt_arena_push(arena, sampled_data_size, true);
+ data.samples = smrt_arena_push(arena, ALIGN_UP_POW2(sampled_data_size, align_up_memoryn), true);
if (!data.samples) return data;
fread((u8*)data.samples, sampled_data_size, 1, wav_file);
@@ -99,7 +99,7 @@ wav_fmt_chunk_t make_wav_fmt_chunk(u32 num_channels, u32 sample_rate, u16 bits_p
return o;
}
-void wav_load(smrt_arena_t *arena, FILE *wav, f64 ***samples_o ,u16 *channel_count_o ,u64 *sample_count_o, u32 *sample_rate_o, smrt_arena_t **conflicts, u64 num_conflicts) {
+void wav_load(smrt_arena_t *arena, FILE *wav, f64 ***samples_o ,u16 *channel_count_o ,u64 *sample_count_o, u32 *sample_rate_o, u64 align_up_memoryn, smrt_arena_t **conflicts, u64 num_conflicts) {
smrta_temp_t scratch = smrta_scratch_start(conflicts, num_conflicts);
wav_master_chunk_t m;
@@ -110,14 +110,15 @@ void wav_load(smrt_arena_t *arena, FILE *wav, f64 ***samples_o ,u16 *channel_cou
wav_data_t data = load_wav_file(scratch.arena,
wav,
&m,
- &f);
+ &f,
+ align_up_memoryn);
if (!data.samples) {
perror("Failed to load data.");
goto failed;
}
- *samples_o = SMRTA_ALLOC_ARRAY(arena, f64*, f.num_channels);
+ *samples_o = smrt_arena_push(arena, ALIGN_UP_POW2(sizeof(f64*) * f.num_channels, align_up_memoryn), true);
*channel_count_o = f.num_channels;
@@ -130,19 +131,22 @@ void wav_load(smrt_arena_t *arena, FILE *wav, f64 ***samples_o ,u16 *channel_cou
d = read_8bps_data(arena,
data,
f.num_channels,
- c);
+ c,
+ align_up_memoryn);
break;
case 16:
d = read_16bps_data(arena,
data,
f.num_channels,
- c);
+ c,
+ align_up_memoryn);
break;
case 24:
d = read_24bps_data(arena,
data,
f.num_channels,
- c);
+ c,
+ align_up_memoryn);
break;
default:
perror("Only PCM integer 8,16,24 bits data can be read.");
@@ -155,7 +159,8 @@ void wav_load(smrt_arena_t *arena, FILE *wav, f64 ***samples_o ,u16 *channel_cou
d = read_32bps_float_data(arena,
data,
f.num_channels,
- c);
+ c,
+ align_up_memoryn);
break;
default:
perror("Only 32 bits float data can be read.");
@@ -183,10 +188,10 @@ failed:
return;
}
-f64 *read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel) {
+f64 *read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn) {
assert(num_channels != 0 && "num_channels must be >0");
- f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count);
+ f64 *vs = smrt_arena_push(arena, ALIGN_UP_POW2(sizeof(f64) * data.sample_count, align_up_memoryn), true);
if (!vs) return NULL;
for (u64 i = 0; i < data.sample_count; i++) {
@@ -199,10 +204,10 @@ f64 *read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16
return vs;
}
-f64 *read_16bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel) {
+f64 *read_16bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn) {
assert(num_channels != 0 && "num_channels must be >0");
- f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count);
+ f64 *vs = smrt_arena_push(arena, ALIGN_UP_POW2(sizeof(f64) * data.sample_count, align_up_memoryn), true);
if (!vs) return NULL;
for (u64 i = 0; i < data.sample_count; i++) {
@@ -217,10 +222,10 @@ f64 *read_16bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16
return vs;
}
-f64 *read_24bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel) {
+f64 *read_24bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn) {
assert(num_channels != 0 && "num_channels must be >0");
- f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count);
+ f64 *vs = smrt_arena_push(arena, ALIGN_UP_POW2(sizeof(f64) * data.sample_count, align_up_memoryn), true);
if (!vs) return NULL;
for (u64 i = 0; i < data.sample_count; i++) {
@@ -237,10 +242,10 @@ f64 *read_24bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16
return vs;
}
-f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel) {
+f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn) {
assert(num_channels != 0 && "num_channels must be >0");
- f64 *vs = SMRTA_ALLOC_ARRAY(arena, f64, data.sample_count);
+ f64 *vs = smrt_arena_push(arena, ALIGN_UP_POW2(sizeof(f64) * data.sample_count, align_up_memoryn), true);
if (!vs) return NULL;
for (u64 i = 0; i < data.sample_count; i++) {
diff --git a/src/wav.h b/src/wav.h
index ddb37dd..484c6da 100644
--- a/src/wav.h
+++ b/src/wav.h
@@ -42,11 +42,13 @@ typedef struct {
} wav_fmt_chunk_t;
#pragma pack(pop)
-/// Load WAV data from wav_file. Data will be allocated on arena
+/// Load WAV data from wav_file. Data will be allocated on arena, with allocation
+/// size aligned up to align_up_memoryn.
wav_data_t load_wav_file(smrt_arena_t * arena ,
FILE *wav_file ,
wav_master_chunk_t *master_o ,
- wav_fmt_chunk_t *format_o);
+ wav_fmt_chunk_t *format_o ,
+ u64 align_up_memoryn);
/// Write WAV headers and data to wav_file.
b32 write_wav_file(FILE * wav_file ,
@@ -64,9 +66,10 @@ void wav_load(smrt_arena_t *arena, FILE *wav, f64 *** samples_o
u16 *channel_count_o ,
u64 * sample_count_o ,
u32 * sample_rate_o ,
+ u64 align_up_memoryn ,
smrt_arena_t **conflicts, u64 num_conflicts );
-f64 * read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel);
-f64 * read_16bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel);
-f64 * read_24bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel);
-f64 *read_32bps_float_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel);
+f64 * read_8bps_data(smrt_arena_t *arena, wav_data_t data, u16 num_channels, u16 channel, u64 align_up_memoryn);
+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);
diff --git a/tests/wav.c b/tests/wav.c
index 6a29197..53a62eb 100644
--- a/tests/wav.c
+++ b/tests/wav.c
@@ -23,7 +23,7 @@ Test(wav, read_file) {
FILE *wav_file = fopen("./tests/fixtures/sine.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -46,7 +46,7 @@ Test(wav, read_sample_data_matches_source) {
FILE *wav_file = fopen("./tests/fixtures/sine.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -69,7 +69,7 @@ Test(wav, read_missing_file_returns_null) {
FILE *wav_file = fopen("./tests/fixtures/does_not_exist.wav", "rb");
cr_assert_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
cr_expect_eq(data.samples, NULL);
@@ -85,7 +85,7 @@ Test(wav, read_stereo_16bit) {
FILE *wav_file = fopen("./tests/fixtures/stereo16.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -111,7 +111,7 @@ Test(wav, read_mono_8bit) {
FILE *wav_file = fopen("./tests/fixtures/mono8.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -137,7 +137,7 @@ Test(wav, read_skips_unknown_chunk_before_fmt) {
FILE *wav_file = fopen("./tests/fixtures/extra_chunk.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -159,7 +159,7 @@ Test(wav, read_odd_sized_chunk_before_fmt, .timeout = 2) {
FILE *wav_file = fopen("./tests/fixtures/odd_chunk.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -180,7 +180,7 @@ Test(wav, read_missing_data_chunk, .timeout = 2) {
FILE *wav_file = fopen("./tests/fixtures/missing_data_chunk.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -198,7 +198,7 @@ Test(wav, read_arena_too_small_for_data) {
FILE *wav_file = fopen("./tests/fixtures/sine.wav", "rb");
cr_assert_not_null(wav_file);
- wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk);
+ wav_data_t data = load_wav_file(arena, wav_file, &mchunk, &fmtchunk, 1);
fclose(wav_file);
@@ -270,7 +270,7 @@ Test(wav, read_rejects_non_pcm_format, .init = non_pcm_setup, .fini = non_pcm_te
wav_master_chunk_t mchunk;
wav_fmt_chunk_t read_fmtchunk;
- wav_data_t read_data = load_wav_file(arena, readback_file, &mchunk, &read_fmtchunk);
+ wav_data_t read_data = load_wav_file(arena, readback_file, &mchunk, &read_fmtchunk, 1);
fclose(readback_file);
@@ -311,7 +311,7 @@ Test(wav, write_sine, .init = write_sine_setup, .fini = write_sine_teardown) {
wav_master_chunk_t mchunk;
wav_fmt_chunk_t read_fmtchunk;
- wav_data_t read_data = load_wav_file(arena, readback_file, &mchunk, &read_fmtchunk);
+ wav_data_t read_data = load_wav_file(arena, readback_file, &mchunk, &read_fmtchunk, 1);
fclose(readback_file);