summaryrefslogtreecommitdiff
path: root/kernel/framework/audio/grc3inc.inc
blob: 98c920570bd0112cbc5008570908714e6c9ffa9f (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Purpose: Resampling routines for GRC3
 *
 * GRC library version 3.1
 */
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

static __inline__ int32_t SUFFIX (grc3_upsample_) (grc3state_t * grc,
						   TYPEIN * src,
						   TYPEOUT * dst, uint32_t sz,
						   uint32_t bufsz,
						   int32_t inc,
						   int32_t offset)
{
  GRCvreg uint32_t ptr = grc->ptr;
  uint32_t srcrate = grc->srcrate;
  uint32_t dstrate = grc->dstrate;
  GRCvreg int32_t *history = grc->historyptr;
  uint32_t filtfactor = grc->filtfactor;
  uint32_t dstsz = 0;

  grc->insz = sz;

  src += offset;
  dst += offset;

  while (sz > 0)
    {
      while (ptr < dstrate)
	{
	  if (dstsz >= bufsz)
	    goto endloop;
	  OUT (dst[0],
	       DOCLAMP (QSUFFIX (_conv31_)
			(history, _grc_sat6 (ptr, filtfactor))));
	  ptr += srcrate;
#ifndef INCOUT
	  dst += inc;
#else
	  INCOUT
#endif
	    dstsz++;
	}

      history++;
      if (history >= (grc->history + GRC3_MAXHISTORY * 2))
	history -= GRC3_MAXHISTORY;

      history[0] = history[-GRC3_MAXHISTORY] = IN (((*src)));

      ptr -= dstrate;

      sz--;
#ifndef INCIN
      src += inc;
#else
      INCIN
#endif
    }
endloop:

  grc->ptr = ptr;
  grc->historyptr = history;
  grc->outsz = dstsz;
  grc->insz -= sz;

  return (int32_t) dstsz;
}

__inline__ int32_t SUFFIX (grc3_dnsample_) (grc3state_t * grc, TYPEIN * src,
					    TYPEOUT * dst, uint32_t sz,
					    uint32_t bufsz, int32_t inc,
					    int32_t offset)
{
  GRCvreg uint32_t ptr = grc->ptr;
  uint32_t srcrate = grc->srcrate;
  uint32_t dstrate = grc->dstrate;
  uint32_t sat = grc->sat;
  GRCvreg int32_t *history = grc->historyptr;
  uint32_t filtfactor = grc->filtfactor;
  uint32_t dstsz = 0;

  grc->insz = sz;

  src += offset;
  dst += offset;

  while (sz > 0)
    {
      while (ptr >= srcrate)
	{
	  if (dstsz >= bufsz)
	    goto endloop;
	  ptr -= srcrate;
	  OUT (dst[0],
	       DOCLAMP (QSUFFIX (_conv31d_)
			(history, _grc_sat6 (ptr, filtfactor),
			 grc->ptr_incv)));
#ifndef INCOUT
	  dst += inc;
#else
	  INCOUT
#endif
	    dstsz++;
	}

      history++;
      if (history >= (grc->history + GRC3_MAXHISTORY * 2))
	history -= GRC3_MAXHISTORY;

      /* TODO: for better quality multiplier is worth moving to output cascade */
      history[0] = history[-GRC3_MAXHISTORY] =
	_grc_sat31 (IN (((*src))), sat);

      ptr += dstrate;

      sz--;
#ifndef INCIN
      src += inc;
#else
      INCIN
#endif
    }
endloop:

  grc->ptr = ptr;
  grc->historyptr = history;
  grc->outsz = dstsz;
  grc->insz -= sz;

  return (int32_t) dstsz;
}

static __inline__ int32_t SUFFIX (grc3_resample_) (grc3state_t * grc,
						   void *src, void *dst,
						   uint32_t sz,
						   uint32_t bufsz,
						   int32_t inc,
						   int32_t offset)
{
  if (grc->srcrate <= grc->dstrate)
    return SUFFIX (grc3_upsample_) (grc, (TYPEIN *) src, (TYPEOUT *) dst, sz,
				    bufsz, inc, offset);
  else
    return SUFFIX (grc3_dnsample_) (grc, (TYPEIN *) src, (TYPEOUT *) dst, sz,
				    bufsz, inc, offset);
}

#undef TYPEIN
#undef TYPEOUT
#undef INCIN
#undef INCOUT
#undef SUFFIX
#undef IN
#undef OUT