Пример #1
0
static void
MatrixMorph_compute_next_data_frame(MatrixMorph *self)
{
    int x, y;
    long i, j, width, height, numsamps, index;
    MYFLT input, interp, interp1;
    
    MYFLT *in = Stream_getData((Stream *)self->input_stream);
    width = NewMatrix_getWidth((NewMatrix *)self->matrix);
    height = NewMatrix_getHeight((NewMatrix *)self->matrix);
    numsamps = width * height;
    int len = PyList_Size(self->sources);
    
    input = MatrixMorph_clip(in[0]);
    
    interp = input * (len - 1);
    x = (int)(interp);   
    y = x + 1;
    
    MatrixStream *tab1 = (MatrixStream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->sources, x), "getMatrixStream", "");
    MatrixStream *tab2 = (MatrixStream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->sources, y), "getMatrixStream", "");
    
    interp = MYFMOD(interp, 1.0);
    interp1 = 1. - interp;
    
    for (i=0; i<height; i++) {
        for (j=0; j<width; j++) {
            index = i*width+j;
            self->buffer[index] = MatrixStream_getPointFromPos(tab1, j, i) * interp1 + MatrixStream_getPointFromPos(tab2, j, i) * interp;
        }    
    }    
    
    NewMatrix_recordChunkAllRow((NewMatrix *)self->matrix, self->buffer, numsamps);
}
Пример #2
0
static void
LFO_generates_aa(LFO *self) {
    MYFLT val, inc, freq, sharp, pointer, numh;
    MYFLT v1, v2, inc2, fade;
    MYFLT sharp2 = 0.0;
    int i, maxHarms;

    MYFLT *fr = Stream_getData((Stream *)self->freq_stream);
    MYFLT *sh = Stream_getData((Stream *)self->sharp_stream);

    switch (self->wavetype) {
        case 0: /* Saw up */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverFour/freq);
                numh = sharp * 46.0 + 4.0;
                if (numh > maxHarms)
                    numh = maxHarms;
                pointer = self->pointerPos * 2.0 - 1.0;
                val = pointer - MYTANH(numh * pointer) / MYTANH(numh);
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 1: /* Saw down */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverFour/freq);
                numh = sharp * 46.0 + 4.0;
                if (numh > maxHarms)
                    numh = maxHarms;
                pointer = self->pointerPos * 2.0 - 1.0;
                val = -(pointer - MYTANH(numh * pointer) / MYTANH(numh));
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 2: /* Square */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverEight/freq);
                numh = sharp * 46.0 + 4.0;
                if (numh > maxHarms)
                    numh = maxHarms;
                val = MYATAN(numh * MYSIN(TWOPI*self->pointerPos));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 3: /* Triangle */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverFour/freq);
                if ((sharp * 36.0) > maxHarms)
                    numh = (MYFLT)(maxHarms / 36.0);
                else
                    numh = sharp;
                v1 = MYTAN(MYSIN(TWOPI*self->pointerPos)) * self->oneOverPiOverTwo;
                pointer = self->pointerPos + 0.25;
                if (pointer > 1.0)
                    pointer -= 1.0;
                v2 = 4.0 * (0.5 - MYFABS(pointer - 0.5)) - 1.0;
                val = v1 * (1 - numh) + v2 * numh;
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 4: /* Pulse */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverEight/freq);
                numh = MYFLOOR(sharp * 46.0 + 4.0);
                if (numh > maxHarms)
                    numh = maxHarms;
                if (MYFMOD(numh, 2.0) == 0.0)
                    numh += 1.0;
                val = MYTAN(MYPOW(MYFABS(MYSIN(TWOPI*self->pointerPos)), numh));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 5: /* Bi-Pulse */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                maxHarms = (int)(self->srOverEight/freq);
                numh = MYFLOOR(sharp * 46.0 + 4.0);
                if (numh > maxHarms)
                    numh = maxHarms;
                if (MYFMOD(numh, 2.0) == 0.0)
                    numh += 1.0;
                val = MYTAN(MYPOW(MYSIN(TWOPI*self->pointerPos), numh));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 6: /* SAH */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                numh = 1.0 - sharp;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                inc2 = 1.0 / (int)(1.0 / inc * numh);
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1) {
                    self->pointerPos -= 1.0;
                    self->sahPointerPos = 0.0;
                    self->sahLastValue = self->sahCurrentValue;
                    self->sahCurrentValue = RANDOM_UNIFORM * 2.0 - 1.0;
                }
                if (self->sahPointerPos < 1.0) {
                    fade = 0.5 * MYSIN(PI * (self->sahPointerPos+0.5)) + 0.5;
                    val = self->sahCurrentValue * (1.0 - fade) + self->sahLastValue * fade;
                    self->sahPointerPos += inc2;
                }
                else {
                    val = self->sahCurrentValue;
                }
                self->data[i] = val;
            }
            break;
        case 7: /* Sine-mod */
            for (i=0; i<self->bufsize; i++) {
                sharp = sh[i];
                if (sharp < 0.0)
                    sharp = 0.0;
                else if (sharp > 1.0)
                    sharp = 1.0;
                freq = fr[i];
                if (freq < 0.00001)
                    freq = 0.00001;
                else if (freq > self->srOverFour)
                    freq = self->srOverFour;
                inc = freq * self->oneOverSr;
                inc2 = inc * sharp * 0.99;
                sharp2 = sharp * 0.5;
                self->modPointerPos += inc2;
                if (self->modPointerPos < 0)
                    self->modPointerPos += 1.0;
                else if (self->modPointerPos >= 1)
                    self->modPointerPos -= 1.0;
                val = ((sharp2 * MYCOS(TWOPI*self->modPointerPos) + sharp2) + (1.0 - sharp)) * MYSIN(TWOPI*self->pointerPos);
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        default:
            break;
    }
}
Пример #3
0
static void
LFO_generates_ii(LFO *self) {
    MYFLT val, inc, freq, sharp, pointer, numh;
    MYFLT v1, v2, inc2, fade;
    int i, maxHarms;

    freq = PyFloat_AS_DOUBLE(self->freq);
    if (freq <= 0) {
        return;
    }
    sharp = PyFloat_AS_DOUBLE(self->sharp);
    if (sharp < 0.0)
        sharp = 0.0;
    else if (sharp > 1.0)
        sharp = 1.0;
    inc = freq / self->sr;

    switch (self->wavetype) {
        case 0: /* Saw up */
            maxHarms = (int)(self->srOverFour/freq);
            numh = sharp * 46.0 + 4.0;
            if (numh > maxHarms)
                numh = maxHarms;
            for (i=0; i<self->bufsize; i++) {
                pointer = self->pointerPos * 2.0 - 1.0;
                val = pointer - MYTANH(numh * pointer) / MYTANH(numh);
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 1: /* Saw down */
            maxHarms = (int)(self->srOverFour/freq);
            numh = sharp * 46.0 + 4.0;
            if (numh > maxHarms)
                numh = maxHarms;
            for (i=0; i<self->bufsize; i++) {
                pointer = self->pointerPos * 2.0 - 1.0;
                val = -(pointer - MYTANH(numh * pointer) / MYTANH(numh));
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 2: /* Square */
            maxHarms = (int)(self->srOverEight/freq);
            numh = sharp * 46.0 + 4.0;
            if (numh > maxHarms)
                numh = maxHarms;
            for (i=0; i<self->bufsize; i++) {
                val = MYATAN(numh * MYSIN(TWOPI*self->pointerPos));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 3: /* Triangle */
            maxHarms = (int)(self->srOverFour/freq);
            if ((sharp * 36.0) > maxHarms)
                numh = (MYFLT)(maxHarms / 36.0);
            else
                numh = sharp;
            for (i=0; i<self->bufsize; i++) {
                v1 = MYTAN(MYSIN(TWOPI*self->pointerPos)) * self->oneOverPiOverTwo;
                pointer = self->pointerPos + 0.25;
                if (pointer > 1.0)
                    pointer -= 1.0;
                v2 = 4.0 * (0.5 - MYFABS(pointer - 0.5)) - 1.0;
                val = v1 * (1 - numh) + v2 * numh;
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 4: /* Pulse */
            maxHarms = (int)(self->srOverEight/freq);
            numh = MYFLOOR(sharp * 46.0 + 4.0);
            if (numh > maxHarms)
                numh = maxHarms;
            if (MYFMOD(numh, 2.0) == 0.0)
                numh += 1.0;
            for (i=0; i<self->bufsize; i++) {
                val = MYTAN(MYPOW(MYFABS(MYSIN(TWOPI*self->pointerPos)), numh));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 5: /* Bi-Pulse */
            maxHarms = (int)(self->srOverEight/freq);
            numh = MYFLOOR(sharp * 46.0 + 4.0);
            if (numh > maxHarms)
                numh = maxHarms;
            if (MYFMOD(numh, 2.0) == 0.0)
                numh += 1.0;
            for (i=0; i<self->bufsize; i++) {
                val = MYTAN(MYPOW(MYSIN(TWOPI*self->pointerPos), numh));
                self->data[i] = val * self->oneOverPiOverTwo;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        case 6: /* SAH */
            numh = 1.0 - sharp;
            inc2 = 1.0 / (int)(1.0 / inc * numh);
            for (i=0; i<self->bufsize; i++) {
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1) {
                    self->pointerPos -= 1.0;
                    self->sahPointerPos = 0.0;
                    self->sahLastValue = self->sahCurrentValue;
                    self->sahCurrentValue = rand()/((MYFLT)(RAND_MAX)*0.5) - 1.0;
                }
                if (self->sahPointerPos < 1.0) {
                    fade = 0.5 * MYSIN(PI * (self->sahPointerPos+0.5)) + 0.5;
                    val = self->sahCurrentValue * (1.0 - fade) + self->sahLastValue * fade;
                    self->sahPointerPos += inc2;
                }
                else {
                    val = self->sahCurrentValue;
                }
                self->data[i] = val;
            }
            break;
        case 7: /* Sine-mod */
            inc2 = inc * sharp;
            for (i=0; i<self->bufsize; i++) {
                self->modPointerPos += inc2;
                if (self->modPointerPos < 0)
                    self->modPointerPos += 1.0;
                else if (self->modPointerPos >= 1)
                    self->modPointerPos -= 1.0;
                val = (0.5 * MYCOS(TWOPI*self->modPointerPos) + 0.5) * MYSIN(TWOPI*self->pointerPos);
                self->data[i] = val;
                self->pointerPos += inc;
                if (self->pointerPos < 0)
                    self->pointerPos += 1.0;
                else if (self->pointerPos >= 1)
                    self->pointerPos -= 1.0;
            }
            break;
        default:
            break;
    }
}