cainclass /ac3_function/org/si/sion/effector/SiEffectDownSampler.as

Language ActionScript Lines 213
MD5 Hash 981e615742b851da74c2c51ae9c1ae88
Repository http://cainclass.googlecode.com/svn/trunk/ View Raw File
  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//----------------------------------------------------------------------------------------------------
// Down sampler
//  Copyright (c) 2009 keim All rights reserved.
//  Distributed under BSD-style license (see org.si.license.txt).
//----------------------------------------------------------------------------------------------------

package org.si.sion.effector {
    /** Down sampler. */
    public class SiEffectDownSampler extends SiEffectBase
    {
    // variables
    //------------------------------------------------------------
        private var _freqShift:int = 0;
        private var _bitConv0:Number = 1;
        private var _bitConv1:Number = 1;
        private var _channelCount:int = 2;
        
        
        
        
    // constructor
    //------------------------------------------------------------
        /** Constructor. 
         *  @param freqShift frequency shift 0=44.1kHz, 1=22.05kHz, 2=11.025kHz.
         *  @param bitRate bit rate of the sample
         *  @param channelCount channel count 1=monoral, 2=stereo
         */
        function SiEffectDownSampler(freqShift:int=0, bitRate:int=16, channelCount:int=2) 
        {
            setParameters(freqShift, bitRate, channelCount);
        }
        
        
        /** set parameter
         *  @param freqShift frequency shift 0=44.1kHz, 1=22.05kHz, 2=11.025kHz.
         *  @param bitRate bit rate of the sample
         *  @param channelCount channel count 1=monoral, 2=stereo
         */
        public function setParameters(freqShift:int=0, bitRate:int=16, channelCount:int=2) : void 
        {
            _freqShift = freqShift;
            _bitConv0 = 1<<bitRate;
            _bitConv1 = 1/_bitConv0;
            _channelCount = channelCount;
        }
        
        
        
        
    // callback functions
    //------------------------------------------------------------
        /** @private */
        override public function initialize() : void
        {
            setParameters();
        }
        
        
        /** @private */
        override public function mmlCallback(args:Vector.<Number>) : void
        {
            setParameters((!isNaN(args[0])) ? args[0] : 0,
                          (!isNaN(args[1])) ? args[1] : 16,
                          (!isNaN(args[2])) ? args[2] : 2);
        }
        
        
        /** @private */
        override public function prepareProcess() : int
        {
            return 2;
        }
        
        
        /** @private */
        override public function process(channels:int, buffer:Vector.<Number>, startIndex:int, length:int) : int
        {
            startIndex <<= 1;
            length <<= 1;
            var i:int, j:int, jmax:int, bc0:Number, l:Number, r:Number, imax:int=startIndex+length;
            if (_channelCount == 1) {
                switch (_freqShift) {
                case 0:
                    bc0 = 0.5 * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        l =  buffer[i]; i++;
                        l += buffer[i]; i--;
                        l = (int(l * bc0)) * _bitConv1;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                    }
                    break;
                case 1:
                    bc0 = 0.25 * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        l =  buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i-=3;
                        l = (int(l * bc0)) * _bitConv1;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                    }
                    break;
                case 2:
                    bc0 = 0.125 * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        l =  buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i++;
                        l += buffer[i]; i-=7;
                        l = (int(l * bc0)) * _bitConv1;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                        buffer[i] = l; i++;
                    }
                    break;
                default:
                    jmax = 2<<_freqShift;
                    bc0 = (1/jmax) * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        for (j=0, l=0; j<jmax; j++, i++) {
                            l += buffer[i];
                        }
                        i -= jmax;
                        l = (int(l * bc0)) * _bitConv1;
                        for (j=0; j<jmax; j++, i++) {
                            buffer[i] = l;
                        }
                    }
                    break;
                }
            } else {
                switch (_freqShift) {
                case 0:
                    for (i=startIndex; i<imax; i++) {
                        buffer[i] = (int(buffer[i] * _bitConv0)) * _bitConv1;
                    }
                    break;
                case 1:
                    bc0 = 0.5 * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        l =  buffer[i]; i++;
                        r =  buffer[i]; i++;
                        l += buffer[i]; i++;
                        r += buffer[i]; i-=3;
                        l = (int(l * bc0)) * _bitConv1;
                        r = (int(r * bc0)) * _bitConv1;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                    }
                    break;
                case 2:
                    bc0 = 0.25 * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        l =  buffer[i]; i++;
                        r =  buffer[i]; i++;
                        l += buffer[i]; i++;
                        r += buffer[i]; i++;
                        l += buffer[i]; i++;
                        r += buffer[i]; i++;
                        l += buffer[i]; i++;
                        r += buffer[i]; i-=7;
                        l = (int(l * bc0)) * _bitConv1;
                        r = (int(r * bc0)) * _bitConv1;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                        buffer[i] = l; i++;
                        buffer[i] = r; i++;
                    }
                    break;
                default:
                    jmax = 1<<_freqShift;
                    bc0 = (1/jmax) * _bitConv0;
                    for (i=startIndex; i<imax;) {
                        for (j=0, l=0, r=0; j<jmax; j++, i++) {
                            l += buffer[i];
                            r += buffer[i];
                        }
                        i -= jmax;
                        l = (int(l * bc0)) * _bitConv1;
                        r = (int(r * bc0)) * _bitConv1;
                        for (j=0; j<jmax; j++) {
                            buffer[i] = l; i++;
                            buffer[i] = r; i++;
                        }
                    }
                    break;
                }
            }
            return _channelCount;
        }
    }
}
Back to Top