summaryrefslogtreecommitdiffstats
path: root/resample.c
diff options
context:
space:
mode:
Diffstat (limited to 'resample.c')
-rw-r--r--resample.c90
1 files changed, 45 insertions, 45 deletions
diff --git a/resample.c b/resample.c
index ee9e540..3ed5c05 100644
--- a/resample.c
+++ b/resample.c
@@ -9,14 +9,6 @@
#define SINC_RADIUS 10
-struct resample_state {
- float delta;
-
- int sfreq, dfreq;
- struct interpol_state interpol;
-};
-
-
static int lcd(int a, int b) {
assert(a >= 1 && b >= 1);
@@ -44,13 +36,13 @@ void resample_done(struct resample_state *s) {
interpol_done(&s->interpol);
}
-void resample_get(struct resample_state *s, float* sp, int *sl, float *dp, int *dl, int sfreq, int dfreq) {
+void resample_get(struct resample_state *s, struct qbuf *sq, struct qbuf *dq, int sfreq, int dfreq) {
int di;
float x = 0;
-
- assert(s && sp && dp && sfreq > 0 && dfreq > 0);
- assert(*sl > 0);
- assert(*dl > 0);
+ float *sp, *dp;
+ size_t sl, dl;
+
+ assert(s && sq && dq && sfreq > 0 && dfreq > 0);
if (sfreq != s->sfreq || dfreq != s->dfreq) {
interpol_done(&s->interpol);
@@ -59,38 +51,50 @@ void resample_get(struct resample_state *s, float* sp, int *sl, float *dp, int *
s->dfreq = dfreq;
}
+ sp = qbuf_pull(sq, &sl);
+ dp = qbuf_push(dq, &dl);
+
+ assert(sp && dp && sl && dl);
+
+ sl /= sizeof(float);
+ dl /= sizeof(float);
+
di = 0;
- while (di < *dl) {
+ while (di < dl) {
x = (float) di*sfreq/dfreq + s->delta;
- if (x + SINC_RADIUS + 1 > *sl)
+ if (x + SINC_RADIUS + 1 > sl)
break;
- *(dp++) = interpol_get(&s->interpol, sp, *sl, x);
+ *(dp++) = interpol_get(&s->interpol, sp, sl, x);
di++;
}
- *dl = di;
+ qbuf_push_validate(dq, di*sizeof(float));
if (x >= SINC_RADIUS) {
- *sl = (int) (x - SINC_RADIUS);
- s->delta = x - *sl;
- } else
- *sl = 0;
+ int rn;
+ rn = (int) (x - SINC_RADIUS);
+ assert(rn <= sl);
+ s->delta = x - rn;
+ fprintf(stderr, "foo\n");
+ qbuf_pull_invalidate(sq, rn*sizeof(float));
+ fprintf(stderr, "bar\n");
+ }
}
#if (TEST == 7)
#define BUFSIZE (10*1024)
-void sinbuf(float *buf, int l) {
+static void sinbuf(float *buf, int l) {
static int i = 0;
for (; l > 0; l--, i = (i+1 == 100) ? 0 : i+1)
*(buf++) = sin(i*2*M_PI/100);
}
-void convbuf(float *buf, int l) {
+static void convbuf(float *buf, int l) {
int16_t *p = (int16_t*) buf;
for (; l > 0; l--, p+=2) {
float v = ((float) *p + (float) *(p+1))/0x7FFF;
@@ -102,44 +106,40 @@ void convbuf(float *buf, int l) {
int main(int argc, char *argv[]) {
struct resample_state resample;
- float inbuf[BUFSIZE];
- int total_n_inbuf = 0;
-
+ struct qbuf qi, qo;
+
if (argc > 1) {
stdin = fopen(argv[1], "r");
assert(stdin);
}
+ qbuf_init(&qi, BUFSIZE);
+ qbuf_init(&qo, BUFSIZE);
resample_init(&resample);
while (!feof(stdin)) {
- float outbuf[BUFSIZE];
- int n_outbuf = sizeof(outbuf) / sizeof(float);
- int n_inbuf, c;
-
+ int c;
+ float *ip, *up;
+ size_t il, ol;
+
fprintf(stderr, "LOOP!\n");
- c = fread(inbuf + total_n_inbuf, sizeof(float), sizeof(inbuf)/sizeof(float) - total_n_inbuf, stdin);
- convbuf(inbuf + total_n_inbuf, c);
- n_inbuf = (total_n_inbuf += c);
-
+ ip = qbuf_push(&qi, &il);
-
-/* sinbuf(inbuf + total_n_inbuf, sizeof(inbuf)/sizeof(float) - total_n_inbuf); */
-/* n_inbuf = total_n_inbuf = sizeof(inbuf)/sizeof(float); *\/ */
-
+ c = fread(ip, sizeof(float), il/sizeof(float), stdin);
+ convbuf(ip, c);
+ qbuf_push_validate(&qi, c*sizeof(float));
- resample_get(&resample, inbuf, &n_inbuf, outbuf, &n_outbuf, 44100, 8000);
-
- fwrite(outbuf, sizeof(float), n_outbuf, stdout);
+ resample_get(&resample, &qi, &qo, 44100, 8000);
- if (n_inbuf) {
- total_n_inbuf -= n_inbuf;
- memmove(inbuf, inbuf + n_inbuf, total_n_inbuf*sizeof(float));
- }
+ up = qbuf_pull(&qo, &ol);
+ fwrite(up, sizeof(float), ol/sizeof(float), stdout);
+ qbuf_pull_invalidate(&qo, ol);
}
resample_done(&resample);
+ qbuf_done(&qi);
+ qbuf_done(&qo);
return 0;
}