~ubuntu-branches/ubuntu/quantal/python-pyo/quantal

« back to all changes in this revision

Viewing changes to src/objects/matrixmodule.c

  • Committer: Package Import Robot
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2012-07-03 23:45:41 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120703234541-jh5jg00lvljnwq8m
Tags: 0.6.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
200
200
static int
201
201
NewMatrix_traverse(NewMatrix *self, visitproc visit, void *arg)
202
202
{
203
 
    Py_VISIT(self->server);
204
 
    Py_VISIT(self->matrixstream);
 
203
    pyo_matrix_VISIT
205
204
    return 0;
206
205
}
207
206
 
208
207
static int 
209
208
NewMatrix_clear(NewMatrix *self)
210
209
{
211
 
    Py_CLEAR(self->server);
212
 
    Py_CLEAR(self->matrixstream);
 
210
    pyo_matrix_CLEAR
213
211
    return 0;
214
212
}
215
213
 
217
215
NewMatrix_dealloc(NewMatrix* self)
218
216
{
219
217
    int i;
220
 
    for (i=0; i<self->height; i++) {
 
218
    for (i=0; i<(self->height+1); i++) {
221
219
        free(self->data[i]);
222
220
    }    
223
221
    free(self->data);
228
226
static PyObject *
229
227
NewMatrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
230
228
{
 
229
    int i, j;
 
230
    PyObject *inittmp=NULL;
231
231
    NewMatrix *self;
232
232
    
233
233
    self = (NewMatrix *)type->tp_alloc(type, 0);
237
237
    self->x_pointer = self->y_pointer = 0;
238
238
    
239
239
    MAKE_NEW_MATRIXSTREAM(self->matrixstream, &MatrixStreamType, NULL);
240
 
    
241
 
    return (PyObject *)self;
242
 
}
243
240
 
244
 
static int
245
 
NewMatrix_init(NewMatrix *self, PyObject *args, PyObject *kwds)
246
 
{    
247
 
    int i, j;
248
 
    PyObject *inittmp=NULL;
249
241
    static char *kwlist[] = {"width", "height", "init", NULL};
250
242
    
251
243
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii|O", kwlist, &self->width, &self->height, &inittmp))
252
 
        return -1; 
253
 
 
 
244
        Py_RETURN_NONE; 
 
245
    
254
246
    self->data = (MYFLT **)realloc(self->data, (self->height + 1) * sizeof(MYFLT));
255
 
 
 
247
    
256
248
    for (i=0; i<(self->height+1); i++) {
257
249
        self->data[i] = (MYFLT *)malloc((self->width + 1) * sizeof(MYFLT));
258
250
    }
259
 
 
 
251
    
260
252
    for(i=0; i<(self->height+1); i++) {
261
253
        for (j=0; j<(self->width+1); j++) {
262
254
            self->data[i][j] = 0.0;
263
255
        }    
264
256
    }
265
 
 
 
257
    
266
258
    MatrixStream_setWidth(self->matrixstream, self->width);
267
259
    MatrixStream_setHeight(self->matrixstream, self->height);
268
 
 
 
260
    
269
261
    if (inittmp) {
270
262
        PyObject_CallMethod((PyObject *)self, "setMatrix", "O", inittmp);
271
263
    }
272
264
    
273
265
    MatrixStream_setData(self->matrixstream, self->data);
274
 
 
275
 
    Py_INCREF(self);
276
 
    return 0;
 
266
    
 
267
    return (PyObject *)self;
277
268
}
278
269
 
279
270
static PyObject * NewMatrix_getServer(NewMatrix* self) { GET_SERVER };
463
454
0,                         /* tp_descr_get */
464
455
0,                         /* tp_descr_set */
465
456
0,                         /* tp_dictoffset */
466
 
(initproc)NewMatrix_init,      /* tp_init */
 
457
0,      /* tp_init */
467
458
0,                         /* tp_alloc */
468
459
NewMatrix_new,                 /* tp_new */
469
460
};
573
564
static void
574
565
MatrixRec_dealloc(MatrixRec* self)
575
566
{
576
 
    free(self->data);
 
567
    pyo_DEALLOC
577
568
    free(self->trigsBuffer);
578
569
    MatrixRec_clear(self);
579
570
    self->ob_type->tp_free((PyObject*)self);
580
571
}
581
572
 
582
 
static PyObject * MatrixRec_deleteStream(MatrixRec *self) { DELETE_STREAM };
583
 
 
584
573
static PyObject *
585
574
MatrixRec_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
586
575
{
587
576
    int i;
 
577
    PyObject *inputtmp, *input_streamtmp, *matrixtmp;
588
578
    MatrixRec *self;
589
579
    self = (MatrixRec *)type->tp_alloc(type, 0);
590
580
    
597
587
    
598
588
    Stream_setFunctionPtr(self->stream, MatrixRec_compute_next_data_frame);
599
589
    Stream_setStreamActive(self->stream, 0);
600
 
    
601
 
    return (PyObject *)self;
602
 
}
603
590
 
604
 
static int
605
 
MatrixRec_init(MatrixRec *self, PyObject *args, PyObject *kwds)
606
 
{
607
 
    int i;
608
 
    PyObject *inputtmp, *input_streamtmp, *matrixtmp;
609
 
    
610
591
    static char *kwlist[] = {"input", "matrix", "fadetime", "delay", NULL};
611
592
    
612
593
    if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_OO_FI, kwlist, &inputtmp, &matrixtmp, &self->fadetime, &self->delay))
613
 
        return -1; 
614
 
    
615
 
    Py_XDECREF(self->input);
616
 
    self->input = inputtmp;
617
 
    input_streamtmp = PyObject_CallMethod((PyObject *)self->input, "_getStream", NULL);
618
 
    Py_INCREF(input_streamtmp);
619
 
    Py_XDECREF(self->input_stream);
620
 
    self->input_stream = (Stream *)input_streamtmp;
621
 
    
 
594
        Py_RETURN_NONE; 
 
595
    
 
596
    INIT_INPUT_STREAM
 
597
 
622
598
    Py_XDECREF(self->matrix);
623
599
    self->matrix = (NewMatrix *)matrixtmp;
624
600
    
625
 
    Py_INCREF(self->stream);
626
601
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
627
602
    
628
603
    self->trigsBuffer = (MYFLT *)realloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT));
640
615
    if ((self->fadetime * self->sr) > (size * 0.5))
641
616
        self->fadetime = size * 0.5 / self->sr;
642
617
    self->fadeInSample = roundf(self->fadetime * self->sr + 0.5);
643
 
        
644
 
    Py_INCREF(self);
645
 
    return 0;
 
618
    
 
619
    return (PyObject *)self;
646
620
}
647
621
 
648
622
static PyObject * MatrixRec_getServer(MatrixRec* self) { GET_SERVER };
691
665
    {"getServer", (PyCFunction)MatrixRec_getServer, METH_NOARGS, "Returns server object."},
692
666
    {"_getStream", (PyCFunction)MatrixRec_getStream, METH_NOARGS, "Returns stream object."},
693
667
    {"_getTriggerStream", (PyCFunction)MatrixRec_getTriggerStream, METH_NOARGS, "Returns trigger stream object."},
694
 
    {"deleteStream", (PyCFunction)MatrixRec_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
695
668
    {"setMatrix", (PyCFunction)MatrixRec_setMatrix, METH_O, "Sets a new Matrix."},
696
669
    {"play", (PyCFunction)MatrixRec_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
697
670
    {"stop", (PyCFunction)MatrixRec_stop, METH_NOARGS, "Stops computing."},
735
708
    0,                         /* tp_descr_get */
736
709
    0,                         /* tp_descr_set */
737
710
    0,                         /* tp_dictoffset */
738
 
    (initproc)MatrixRec_init,      /* tp_init */
 
711
    0,      /* tp_init */
739
712
    0,                         /* tp_alloc */
740
713
    MatrixRec_new,                 /* tp_new */
741
714
};
742
715
 
743
716
/******************************/
 
717
/* MatrixRecLoop object definition */
 
718
/******************************/
 
719
typedef struct {
 
720
    pyo_audio_HEAD
 
721
    PyObject *input;
 
722
    Stream *input_stream;
 
723
    NewMatrix *matrix;
 
724
    int pointer;
 
725
    MYFLT *trigsBuffer;
 
726
    TriggerStream *trig_stream;
 
727
} MatrixRecLoop;
 
728
 
 
729
static void
 
730
MatrixRecLoop_compute_next_data_frame(MatrixRecLoop *self)
 
731
{
 
732
    int i;
 
733
    int width = NewMatrix_getWidth((NewMatrix *)self->matrix);
 
734
    int height = NewMatrix_getHeight((NewMatrix *)self->matrix);
 
735
    int size = width * height;
 
736
 
 
737
    MYFLT buffer[self->bufsize];
 
738
    memset(&buffer, 0, sizeof(buffer));
 
739
    MYFLT *in = Stream_getData((Stream *)self->input_stream);
 
740
        
 
741
    for (i=0; i<self->bufsize; i++) {
 
742
        self->trigsBuffer[i] = 0.0;
 
743
        buffer[i] = in[i];
 
744
        if (self->pointer++ >= size) {
 
745
            self->pointer = 0;
 
746
            self->trigsBuffer[i] = 1.0;
 
747
        }
 
748
    }
 
749
    NewMatrix_recordChunkAllRow((NewMatrix *)self->matrix, buffer, self->bufsize);
 
750
}
 
751
 
 
752
static int
 
753
MatrixRecLoop_traverse(MatrixRecLoop *self, visitproc visit, void *arg)
 
754
{
 
755
    pyo_VISIT
 
756
    Py_VISIT(self->input);
 
757
    Py_VISIT(self->input_stream);
 
758
    Py_VISIT(self->matrix);
 
759
    Py_VISIT(self->trig_stream);    
 
760
    return 0;
 
761
}
 
762
 
 
763
static int 
 
764
MatrixRecLoop_clear(MatrixRecLoop *self)
 
765
{
 
766
    pyo_CLEAR
 
767
    Py_CLEAR(self->input);
 
768
    Py_CLEAR(self->input_stream);
 
769
    Py_CLEAR(self->matrix);
 
770
    Py_CLEAR(self->trig_stream);    
 
771
    return 0;
 
772
}
 
773
 
 
774
static void
 
775
MatrixRecLoop_dealloc(MatrixRecLoop* self)
 
776
{
 
777
    pyo_DEALLOC
 
778
    free(self->trigsBuffer);
 
779
    MatrixRecLoop_clear(self);
 
780
    self->ob_type->tp_free((PyObject*)self);
 
781
}
 
782
 
 
783
static PyObject *
 
784
MatrixRecLoop_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
785
{
 
786
    int i;
 
787
    PyObject *inputtmp, *input_streamtmp, *matrixtmp;
 
788
    MatrixRecLoop *self;
 
789
    self = (MatrixRecLoop *)type->tp_alloc(type, 0);
 
790
    
 
791
    self->pointer = 0;
 
792
    
 
793
    INIT_OBJECT_COMMON
 
794
    
 
795
    Stream_setFunctionPtr(self->stream, MatrixRecLoop_compute_next_data_frame);
 
796
    
 
797
    static char *kwlist[] = {"input", "matrix", NULL};
 
798
    
 
799
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &inputtmp, &matrixtmp))
 
800
        Py_RETURN_NONE; 
 
801
    
 
802
    INIT_INPUT_STREAM
 
803
 
 
804
    Py_XDECREF(self->matrix);
 
805
    self->matrix = (NewMatrix *)matrixtmp;
 
806
    
 
807
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
808
    
 
809
    self->trigsBuffer = (MYFLT *)realloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT));
 
810
    
 
811
    for (i=0; i<self->bufsize; i++) {
 
812
        self->trigsBuffer[i] = 0.0;
 
813
    }    
 
814
    
 
815
    MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL);
 
816
    TriggerStream_setData(self->trig_stream, self->trigsBuffer);
 
817
 
 
818
    return (PyObject *)self;
 
819
}
 
820
 
 
821
static PyObject * MatrixRecLoop_getServer(MatrixRecLoop* self) { GET_SERVER };
 
822
static PyObject * MatrixRecLoop_getStream(MatrixRecLoop* self) { GET_STREAM };
 
823
static PyObject * MatrixRecLoop_getTriggerStream(MatrixRecLoop* self) { GET_TRIGGER_STREAM };
 
824
 
 
825
static PyObject * MatrixRecLoop_play(MatrixRecLoop *self, PyObject *args, PyObject *kwds) { PLAY };
 
826
static PyObject * MatrixRecLoop_stop(MatrixRecLoop *self) { STOP };
 
827
 
 
828
static PyObject *
 
829
MatrixRecLoop_setMatrix(MatrixRecLoop *self, PyObject *arg)
 
830
{
 
831
        PyObject *tmp;
 
832
        
 
833
        if (arg == NULL) {
 
834
                Py_INCREF(Py_None);
 
835
                return Py_None;
 
836
        }
 
837
    
 
838
        tmp = arg;
 
839
    Py_INCREF(tmp);
 
840
        Py_DECREF(self->matrix);
 
841
    self->matrix = (NewMatrix *)tmp;
 
842
    
 
843
        Py_INCREF(Py_None);
 
844
        return Py_None;
 
845
}       
 
846
 
 
847
static PyMemberDef MatrixRecLoop_members[] = {
 
848
    {"server", T_OBJECT_EX, offsetof(MatrixRecLoop, server), 0, "Pyo server."},
 
849
    {"stream", T_OBJECT_EX, offsetof(MatrixRecLoop, stream), 0, "Stream object."},
 
850
    {"trig_stream", T_OBJECT_EX, offsetof(MatrixRecLoop, trig_stream), 0, "Trigger Stream object."},
 
851
    {"input", T_OBJECT_EX, offsetof(MatrixRecLoop, input), 0, "Input sound object."},
 
852
    {"matrix", T_OBJECT_EX, offsetof(MatrixRecLoop, matrix), 0, "matrix to record in."},
 
853
    {NULL}  /* Sentinel */
 
854
};
 
855
 
 
856
static PyMethodDef MatrixRecLoop_methods[] = {
 
857
    {"getServer", (PyCFunction)MatrixRecLoop_getServer, METH_NOARGS, "Returns server object."},
 
858
    {"_getStream", (PyCFunction)MatrixRecLoop_getStream, METH_NOARGS, "Returns stream object."},
 
859
    {"_getTriggerStream", (PyCFunction)MatrixRecLoop_getTriggerStream, METH_NOARGS, "Returns trigger stream object."},
 
860
    {"setMatrix", (PyCFunction)MatrixRecLoop_setMatrix, METH_O, "Sets a new Matrix."},
 
861
    {"play", (PyCFunction)MatrixRecLoop_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
862
    {"stop", (PyCFunction)MatrixRecLoop_stop, METH_NOARGS, "Stops computing."},
 
863
    {NULL}  /* Sentinel */
 
864
};
 
865
 
 
866
PyTypeObject MatrixRecLoopType = {
 
867
    PyObject_HEAD_INIT(NULL)
 
868
    0,                         /*ob_size*/
 
869
    "_pyo.MatrixRecLoop_base",         /*tp_name*/
 
870
    sizeof(MatrixRecLoop),         /*tp_basicsize*/
 
871
    0,                         /*tp_itemsize*/
 
872
    (destructor)MatrixRecLoop_dealloc, /*tp_dealloc*/
 
873
    0,                         /*tp_print*/
 
874
    0,                         /*tp_getattr*/
 
875
    0,                         /*tp_setattr*/
 
876
    0,                         /*tp_compare*/
 
877
    0,                         /*tp_repr*/
 
878
    0,             /*tp_as_number*/
 
879
    0,                         /*tp_as_sequence*/
 
880
    0,                         /*tp_as_mapping*/
 
881
    0,                         /*tp_hash */
 
882
    0,                         /*tp_call*/
 
883
    0,                         /*tp_str*/
 
884
    0,                         /*tp_getattro*/
 
885
    0,                         /*tp_setattro*/
 
886
    0,                         /*tp_as_buffer*/
 
887
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
888
    "MatrixRecLoop objects. Record circular audio input in a Matrix object.",           /* tp_doc */
 
889
    (traverseproc)MatrixRecLoop_traverse,   /* tp_traverse */
 
890
    (inquiry)MatrixRecLoop_clear,           /* tp_clear */
 
891
    0,                         /* tp_richcompare */
 
892
    0,                         /* tp_weaklistoffset */
 
893
    0,                         /* tp_iter */
 
894
    0,                         /* tp_iternext */
 
895
    MatrixRecLoop_methods,             /* tp_methods */
 
896
    MatrixRecLoop_members,             /* tp_members */
 
897
    0,                      /* tp_getset */
 
898
    0,                         /* tp_base */
 
899
    0,                         /* tp_dict */
 
900
    0,                         /* tp_descr_get */
 
901
    0,                         /* tp_descr_set */
 
902
    0,                         /* tp_dictoffset */
 
903
    0,      /* tp_init */
 
904
    0,                         /* tp_alloc */
 
905
    MatrixRecLoop_new,                 /* tp_new */
 
906
};
 
907
 
 
908
/******************************/
744
909
/* MatrixMorph object definition */
745
910
/******************************/
746
911
typedef struct {
822
987
static void
823
988
MatrixMorph_dealloc(MatrixMorph* self)
824
989
{
825
 
    free(self->data);
 
990
    pyo_DEALLOC
826
991
    free(self->buffer);
827
992
    MatrixMorph_clear(self);
828
993
    self->ob_type->tp_free((PyObject*)self);
829
994
}
830
995
 
831
 
static PyObject * MatrixMorph_deleteStream(MatrixMorph *self) { DELETE_STREAM };
832
 
 
833
996
static PyObject *
834
997
MatrixMorph_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
835
998
{
836
999
    int i;
 
1000
    long width, height, numsamps;
 
1001
    PyObject *inputtmp, *input_streamtmp, *matrixtmp, *sourcestmp;
837
1002
    MatrixMorph *self;
838
1003
    self = (MatrixMorph *)type->tp_alloc(type, 0);
839
1004
    
840
1005
    INIT_OBJECT_COMMON
841
1006
    
842
1007
    Stream_setFunctionPtr(self->stream, MatrixMorph_compute_next_data_frame);
843
 
    
844
 
    return (PyObject *)self;
845
 
}
846
1008
 
847
 
static int
848
 
MatrixMorph_init(MatrixMorph *self, PyObject *args, PyObject *kwds)
849
 
{
850
 
    long width, height, numsamps;
851
 
    PyObject *inputtmp, *input_streamtmp, *matrixtmp, *sourcestmp;
852
 
    
853
1009
    static char *kwlist[] = {"input", "matrix", "sources", NULL};
854
1010
    
855
1011
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist, &inputtmp, &matrixtmp, &sourcestmp))
856
 
        return -1; 
857
 
    
858
 
    Py_XDECREF(self->input);
859
 
    self->input = inputtmp;
860
 
    input_streamtmp = PyObject_CallMethod((PyObject *)self->input, "_getStream", NULL);
861
 
    Py_INCREF(input_streamtmp);
862
 
    Py_XDECREF(self->input_stream);
863
 
    self->input_stream = (Stream *)input_streamtmp;
864
 
    
 
1012
        Py_RETURN_NONE; 
 
1013
    
 
1014
    INIT_INPUT_STREAM
 
1015
 
865
1016
    Py_XDECREF(self->matrix);
866
1017
    self->matrix = (PyObject *)matrixtmp;
867
1018
 
873
1024
    Py_XDECREF(self->sources);
874
1025
    self->sources = (PyObject *)sourcestmp;
875
1026
    
876
 
    Py_INCREF(self->stream);
877
1027
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
878
1028
    
879
 
    Py_INCREF(self);
880
 
    return 0;
 
1029
    return (PyObject *)self;
881
1030
}
882
1031
 
883
1032
static PyObject * MatrixMorph_getServer(MatrixMorph* self) { GET_SERVER };
938
1087
static PyMethodDef MatrixMorph_methods[] = {
939
1088
    {"getServer", (PyCFunction)MatrixMorph_getServer, METH_NOARGS, "Returns server object."},
940
1089
    {"_getStream", (PyCFunction)MatrixMorph_getStream, METH_NOARGS, "Returns stream object."},
941
 
    {"deleteStream", (PyCFunction)MatrixMorph_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
942
1090
    {"setMatrix", (PyCFunction)MatrixMorph_setMatrix, METH_O, "Sets a new matrix."},
943
1091
    {"setSources", (PyCFunction)MatrixMorph_setSources, METH_O, "Changes the sources matrixs."},
944
1092
    {"play", (PyCFunction)MatrixMorph_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
983
1131
    0,                         /* tp_descr_get */
984
1132
    0,                         /* tp_descr_set */
985
1133
    0,                         /* tp_dictoffset */
986
 
    (initproc)MatrixMorph_init,      /* tp_init */
 
1134
    0,      /* tp_init */
987
1135
    0,                         /* tp_alloc */
988
1136
    MatrixMorph_new,                 /* tp_new */
989
1137
};