summaryrefslogtreecommitdiffstats
path: root/resample.c
blob: ee9e540d5036d572a1e9228c1567d682f6ad4a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>

#include "resample.h"
#include "interpol.h"

#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);

    while (a != b) {
        while (a > b) {
            a = a-b;
        }

        while (b > a) {
            b = b-a;
        }
    }

    return a;
}

void resample_init(struct resample_state *s) {
    assert(s);
    memset(s, 0, sizeof(struct resample_state));
    s->sfreq = s->dfreq = -1;
}

void resample_done(struct resample_state *s) {
    assert(s);
    interpol_done(&s->interpol);
}

void resample_get(struct resample_state *s, float* sp, int *sl, float *dp, int *dl, int sfreq, int dfreq) {
    int di;
    float x = 0;

    assert(s && sp && dp && sfreq > 0 && dfreq > 0);
    assert(*sl > 0);
    assert(*dl > 0);

    if (sfreq != s->sfreq || dfreq != s->dfreq) {
        interpol_done(&s->interpol);
        interpol_init(&s->interpol, dfreq/lcd(sfreq,dfreq), SINC_RADIUS);
        s->sfreq = sfreq;
        s->dfreq = dfreq;
    }

    di = 0;
    while (di < *dl) {
        x = (float) di*sfreq/dfreq + s->delta;

        if (x + SINC_RADIUS + 1 > *sl)
            break;
        
        *(dp++) = interpol_get(&s->interpol, sp, *sl, x);
        di++;
    }
    
    *dl = di;

    if (x >= SINC_RADIUS) {
        *sl = (int) (x - SINC_RADIUS);
        s->delta = x - *sl;
    } else
        *sl = 0;
}

#if (TEST == 7)

#define BUFSIZE (10*1024)

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) {
    int16_t *p = (int16_t*) buf;
    for (; l > 0; l--, p+=2) {
        float v = ((float) *p + (float) *(p+1))/0x7FFF;
        *(buf++) = (v < -1) ? -1 : ((v > 1) ? 1 : v);
    }
            
}


int main(int argc, char *argv[]) {
    struct resample_state resample;
    float inbuf[BUFSIZE];
    int total_n_inbuf = 0;

    if (argc > 1) {
        stdin = fopen(argv[1], "r");
        assert(stdin);
    }

    resample_init(&resample);
    
    while (!feof(stdin)) {
        float outbuf[BUFSIZE];
        int n_outbuf = sizeof(outbuf) / sizeof(float);
        int n_inbuf, c;

        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);

        
        
/*          sinbuf(inbuf + total_n_inbuf, sizeof(inbuf)/sizeof(float) - total_n_inbuf); */
/*          n_inbuf = total_n_inbuf = sizeof(inbuf)/sizeof(float); *\/ */


        resample_get(&resample, inbuf, &n_inbuf, outbuf, &n_outbuf, 44100, 8000);
        
        fwrite(outbuf, sizeof(float), n_outbuf, stdout);

        if (n_inbuf) {
            total_n_inbuf -= n_inbuf;
            memmove(inbuf, inbuf + n_inbuf, total_n_inbuf*sizeof(float));
        }
    }

    resample_done(&resample);
    
    return 0;
}

#endif