static AS3_Val prediction(void* self, AS3_Val args) {
	AS3_Val	in_arr = AS3_Undefined(), out_arr = AS3_Array(0);
	float *tmp, d, e;
	int i,j,k,l, n,x,y, newx,newy, il, dx,dy;
	LAYER *pL;

	AS3_ArrayValue( args, "AS3ValType", &in_arr );

	for(i=0; i < 1024; ++i)
    	data[i] = AS3_IntValue(AS3_Get(in_arr, AS3_Int(4*i+1))) /255.0;
    	
    n = 1;
    x = 32;
    y = 32;
    
    #define DATA(l,j,i) data[((l)*y + (j))*x + (i)]
    #define O(k,dy,dx) o[((k)*newy + (dy))*newx + (dx)]
    #define W(k,l,j,i) pL->w[(((k)*n + (l))*pL->y + (j))*pL->x + (i)]
    
    for (il=0; il < nl; ++il) {
        flyield();
        pL = L+il;
        newx = x+1-pL->x;
        newy = y+1-pL->y;

        for (dx=0; dx < newx; ++dx)
        for (dy=0; dy < newy; ++dy)
        for (k=0; k < pL->n; ++k) {
            d = pL->b[k];
            for (l=0; l < n; ++l)
            for(j=0; j < pL->y; ++j)
            for(i=0; i < pL->x; ++i)
                d += DATA(l,j+dy,i+dx)*W(k,l,j,i);
            O(k,dy,dx) = d;
        }

        if(pL->maxpool) {
            for (k=0; k < pL->n; ++k)
            for (dx=0; dx < newx; dx+=2)
            for (dy=0; dy < newy; dy+=2) {
                d=O(k,dy,dx);
                e=O(k,dy,dx+1); if(e>d) d=e;
                e=O(k,dy+1,dx); if(e>d) d=e;
                e=O(k,dy+1,dx+1); if(e>d) d=e;
                O(k,dy/2,dx/2)=d;
            }
            newx /= 2;
            newy /= 2;
        }

        for (dx=0; dx < newx; ++dx)
        for (dy=0; dy < newy; ++dy) {
            e = 0;
            for (k=0; k < pL->n; ++k) {
                d = O(k,dy,dx);
                if(pL->nonlin==1) d=1.0/(1.0 + exp(-d));
                else if(pL->nonlin==2) d=tanh(d);
                else if(pL->nonlin==3) { d=exp(d); e += d; }
                O(k,dy,dx) = d;
            }
            if(pL->nonlin==3 && e)
            for (k=0; k < pL->n; ++k)
                O(k,dy,dx) /= e;
        }
        
        tmp = data;
        data = o;
        o = tmp;
        
        x = newx;
        y = newy;
        n = pL->n;
    }

	for(i=0; i < n*x*y; ++i)
        AS3_Set(out_arr, AS3_Int(i), AS3_Number(data[i]));

	return out_arr;
}
Ejemplo n.º 2
0
/**
 * Scan in a wavetable. Wavetable should be at least one longer than the table size.
 */
static AS3_Val wavetableIn(void *self, AS3_Val args)
{
	AS3_Val settings;
	int bufferPosition; int channels; int frames;
	float *buffer; 
	int sourceBufferPosition;
	float *sourceBuffer;
	double phaseArg; float phase;
	double phaseAddArg; float phaseAdd;
	double phaseResetArg; float phaseReset;
	int tableSize;
	int count; 
	int intPhase;
	float *wavetablePosition;
	float *scratch;
	double y1Arg, y2Arg;
	float y1, y2;
	float fractional, fractionalIncrement, instantBend;
	
	
	
	AS3_ArrayValue(args, "IntType, IntType, IntType, IntType, AS3ValType", &bufferPosition, &sourceBufferPosition, &channels, &frames, &settings);
	AS3_ObjectValue(settings, "tableSize:IntType, phase:DoubleType, phaseAdd:DoubleType, phaseReset:DoubleType, y1:DoubleType, y2:DoubleType",
		&tableSize, &phaseArg, &phaseAddArg, &phaseResetArg, &y1Arg, &y2Arg);

	buffer = (float *) bufferPosition;
	sourceBuffer = (float *) sourceBufferPosition; 
	phaseAdd = (float) phaseAddArg * tableSize; // num source frames to add per output frames
	phase = (float) phaseArg * tableSize; // translate into a frame count into the table
	phaseReset = (float) phaseResetArg * tableSize;
	y1 = (float) y1Arg;
	y2 = (float) y2Arg;
	
	// Expand the pitch modulation into scratch
	// expandLine(scratch1, y1, y2, frames); // draws spline segment into scratch1
	// scratch = (float *) scratch1;	
		
	// Make sure we got everything right
	//sprintf(trace, "Wavetable size=%d phase=%f phaseAdd=%f y1=%f y2=%f", tableSize, phase, phaseAdd, y1, y2);
	//sztrace(trace);	
				
	count=frames;
	fractional = 0.0;
	fractionalIncrement = 1 / (float) frames;
	
	if (channels == 1) {
		while (count--) {
			while (phase >= tableSize) {
				if (phaseReset == -1) {
					// no looping!
					return 0; 
				} else {
					// wrap phase to the loop point
					phase -= tableSize; 
					phase += phaseReset;
				}
			}
			intPhase = (int) phase; // int phase
			wavetablePosition = sourceBuffer + intPhase;
			*buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+1), phase - intPhase);
			// Increment phase by adjusting phaseAdd for instantaneous pitch bend 
			instantBend = interpolate(y1, y2, fractional);
			fractional += fractionalIncrement;
			phase +=  phaseAdd * shiftToFreq(instantBend); 
		}		
	} else if (channels == 2 ) {
		while (count--) {
			while (phase >= tableSize) {
				if (phaseReset == -1) {
					// no looping!
					return 0; 
				} else {
					// wrap phase to the loop point
					phase -= tableSize; 
					phase += phaseReset;
				}
			}
			intPhase = ((int)(phase*0.5))*2; // int phase, round to even frames, for each stereo frame pair
			wavetablePosition = sourceBuffer + intPhase;
			*buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+2), phase - intPhase);
			*buffer++ = interpolate(*(wavetablePosition+1), *(wavetablePosition+3), phase - intPhase);
			// Increment phase by adjusting phaseAdd for instantaneous pitch bend 
			instantBend = interpolate(y1, y2, fractional);
			fractional += fractionalIncrement;
			phase +=  phaseAdd * shiftToFreq(instantBend);
		}
	}
	
	// Scale back down to a factor, and write the final phase value back to AS3
	phase /= tableSize;
	AS3_Set(settings, AS3_String("phase"), AS3_Number(phase));
	
	return 0;
}