53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <limits.h>
|
|
#include <string.h>
|
|
#include "jl_mem.h"
|
|
#include "fsample.h"
|
|
#include "sample.h"
|
|
|
|
void sample_load(Sample *s, char *path, SoundConfig *conf, int buf_size)
|
|
{
|
|
FSample fs;
|
|
fsample_load(&fs, path, conf, buf_size);
|
|
|
|
// Compute the new length.
|
|
int len = fs.len + buf_size;
|
|
len -= len % buf_size;
|
|
|
|
// Allocate new arrays.
|
|
s->L = jl_malloc_exit(len * sizeof(int16_t));
|
|
s->R = jl_malloc_exit(len * sizeof(int16_t));
|
|
s->len = len;
|
|
|
|
// Zero padding samples at end.
|
|
for(int i = fs.len; i < len; ++i) {
|
|
s->L[i] = s->R[i] = 0;
|
|
}
|
|
|
|
double max = fsample_max_amp(&fs);
|
|
|
|
// Use max value to compute gain.
|
|
s->gain = max / MAX_INT16;
|
|
|
|
// Copy data.
|
|
for(int i = 0; i < fs.len; i++) {
|
|
s->L[i] = fs.L[i] / s->gain;
|
|
s->R[i] = fs.R[i] / s->gain;
|
|
}
|
|
|
|
// Free the fsample data.
|
|
fsample_free(&fs);
|
|
}
|
|
|
|
void sample_free(Sample *s)
|
|
{
|
|
jl_free(s->L);
|
|
jl_free(s->R);
|
|
s->R = s->L = NULL;
|
|
s->len = 0;
|
|
}
|
|
|