hermite_resampler.h 3.61 KB
Newer Older
OV2's avatar
OV2 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Simple resampler based on bsnes's ruby audio library */

#ifndef __HERMITE_RESAMPLER_H
#define __HERMITE_RESAMPLER_H

#include "resampler.h"

#undef CLAMP
#undef SHORT_CLAMP
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define SHORT_CLAMP(n) ((short) CLAMP((n), -32768, 32767))

class HermiteResampler : public Resampler
{
    protected:

17 18
        uint32 r_step;
        uint32 r_frac;
19
        int   r_left[4], r_right[4];
OV2's avatar
OV2 committed
20

21
        static inline int32
22
        hermite (int32 mu1, int32 a, int32 b, int32 c, int32 d)
OV2's avatar
OV2 committed
23
        {
24
            int32 mu2, mu3, m0, m1, a0, a1, a2, a3;
OV2's avatar
OV2 committed
25

26 27
            mu2 = (mu1 * mu1) >> 15;
            mu3 = (mu2 * mu1) >> 15;
OV2's avatar
OV2 committed
28

29 30
            m0 = (c - a) << 14;
            m1 = (d - b) << 14;
OV2's avatar
OV2 committed
31

32 33 34 35
            a0 = (mu3 << 1) - (3 * mu2) + (1 << 15);
            a1 =  mu3       - (mu2 << 1) + mu1;
            a2 =  mu3       -  mu2;
            a3 =  3 * mu2   - (mu3 << 1);
OV2's avatar
OV2 committed
36

37
            return ((a0 * b) +
38 39
                   ((a1 * m0) >> 15) +
                   ((a2 * m1) >> 15) +
40
                    (a3 * c)) >> 15;
OV2's avatar
OV2 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        }

    public:
        HermiteResampler (int num_samples) : Resampler (num_samples)
        {
            clear ();
        }

        ~HermiteResampler ()
        {
        }

        void
        time_ratio (double ratio)
        {
56
            r_step = (1 << 16) * ratio;
OV2's avatar
OV2 committed
57 58 59 60 61 62 63
            clear ();
        }

        void
        clear (void)
        {
            ring_buffer::clear ();
64
            r_frac = (1 << 16);
OV2's avatar
OV2 committed
65 66 67 68 69 70 71 72
            r_left [0] = r_left [1] = r_left [2] = r_left [3] = 0;
            r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0;
        }

        void
        read (short *data, int num_samples)
        {
            int i_position = start >> 1;
73
            int max_samples = buffer_size >> 1;
OV2's avatar
OV2 committed
74 75 76 77 78 79 80 81
            short *internal_buffer = (short *) buffer;
            int o_position = 0;
            int consumed = 0;

            while (o_position < num_samples && consumed < buffer_size)
            {
                int s_left = internal_buffer[i_position];
                int s_right = internal_buffer[i_position + 1];
82
                int hermite_val;
OV2's avatar
OV2 committed
83

84
                while (r_frac <= (1 << 16) && o_position < num_samples)
OV2's avatar
OV2 committed
85
                {
86
                    hermite_val = hermite (r_frac >> 1, r_left [0], r_left [1], r_left [2], r_left [3]);
87
                    data[o_position]     = SHORT_CLAMP (hermite_val);
88
                    hermite_val = hermite (r_frac >> 1, r_right[0], r_right[1], r_right[2], r_right[3]);
89
                    data[o_position + 1] = SHORT_CLAMP (hermite_val);
OV2's avatar
OV2 committed
90 91 92 93 94 95

                    o_position += 2;

                    r_frac += r_step;
                }

96
                if (r_frac > (1 << 16))
OV2's avatar
OV2 committed
97 98 99 100 101
                {
                    r_left [0] = r_left [1];
                    r_left [1] = r_left [2];
                    r_left [2] = r_left [3];
                    r_left [3] = s_left;
102

OV2's avatar
OV2 committed
103 104 105
                    r_right[0] = r_right[1];
                    r_right[1] = r_right[2];
                    r_right[2] = r_right[3];
106 107
                    r_right[3] = s_right;

108
                    r_frac -= (1 << 16);
109

OV2's avatar
OV2 committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    i_position += 2;
                    if (i_position >= max_samples)
                        i_position -= max_samples;
                    consumed += 2;
                }
            }

            size -= consumed << 1;
            start += consumed << 1;
            if (start >= buffer_size)
                start -= buffer_size;
        }

        inline int
        avail (void)
        {
126
            return ((((uint32) size) << 14) - r_frac) / r_step * 2;
OV2's avatar
OV2 committed
127 128 129 130
        }
};

#endif /* __HERMITE_RESAMPLER_H */