27 lines
1.6 KiB
C
27 lines
1.6 KiB
C
|
|
#include "xtc/xtc_sample.h"
|
||
|
|
#include "wave_internal.h"
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <math.h>
|
||
|
|
struct xtc_sample {xtc_wave wave;};
|
||
|
|
static int range(const xtc_sample *s,size_t begin,size_t end){return s && begin<end && end<=s->wave.frames;}
|
||
|
|
void xtc_sample_destroy(xtc_sample *s){if(s){free(s->wave.samples);free(s);}}
|
||
|
|
xtc_result xtc_sample_load(const char *path,xtc_sample **out)
|
||
|
|
{
|
||
|
|
xtc_sample *s;xtc_result r;if(!path || !out)return XTC_ERR_INVALID_ARGUMENT;
|
||
|
|
s=calloc(1,sizeof(*s));if(!s)return XTC_ERR_OUT_OF_MEMORY;
|
||
|
|
r=xtc_wave_load(path,&s->wave);
|
||
|
|
if(r==XTC_OK && s->wave.frames>XTC_SAMPLE_MAX_FRAMES)r=XTC_ERR_CAPACITY;
|
||
|
|
if(r!=XTC_OK){xtc_sample_destroy(s);return r;}*out=s;return XTC_OK;
|
||
|
|
}
|
||
|
|
xtc_result xtc_sample_info(const xtc_sample *s,unsigned int *rate,size_t *frames)
|
||
|
|
{if(!s || !rate || !frames)return XTC_ERR_INVALID_ARGUMENT;*rate=s->wave.rate;*frames=s->wave.frames;return XTC_OK;}
|
||
|
|
xtc_result xtc_sample_read(const xtc_sample *s,size_t frame,float *out)
|
||
|
|
{if(!s || !out || frame>=s->wave.frames)return XTC_ERR_INVALID_ARGUMENT;*out=s->wave.samples[frame];return XTC_OK;}
|
||
|
|
xtc_result xtc_sample_analyze(const xtc_sample *s,size_t begin,size_t end,xtc_sample_stats *out)
|
||
|
|
{
|
||
|
|
size_t i;double sum=0,square=0;xtc_sample_stats result;
|
||
|
|
if(!range(s,begin,end) || !out)return XTC_ERR_INVALID_ARGUMENT;
|
||
|
|
result.minimum=result.maximum=s->wave.samples[begin];
|
||
|
|
for(i=begin;i<end;++i){float v=s->wave.samples[i];if(v<result.minimum)result.minimum=v;if(v>result.maximum)result.maximum=v;sum+=v;square+=(double)v*v;}
|
||
|
|
result.mean=sum/(double)(end-begin);result.rms=sqrt(square/(double)(end-begin));*out=result;return XTC_OK;
|
||
|
|
}
|