static void SDIFlistpoke_listpoke(SDIFlistpoke *x, t_symbol *dummy, short argc, t_atom *argv) { int i; SDIFmem_Frame f; SDIFmem_Matrix m; float *mdata; char myFrameType[4]; char myMatrixType[4]; SDIFresult r; // post("* SDIFlistpoke_listpoke: x %p, argc %ld, argv %p", x, argc, argv); /* Check that arguments are all numbers */ for (i = 0; i < argc; ++i) { if (argv[i].a_type != A_FLOAT && argv[i].a_type != A_LONG) { object_post((t_object *)x, "¥ SDIF-listpoke: only numbers allowed in list."); return; } } /* Check that the number of arguments is a multiple of x->t_num_columns */ if (argc % x->t_num_columns != 0) { post("¥ SDIF-listpoke: %d element list doesn't go into %ld columns. Ignoring.", argc, x->t_num_columns); return; } /* Get the frame */ LookupMyBuffer(x); if (x->t_buffer == 0) { object_post((t_object *)x, "¥ SDIF-listpoke: no buffer!"); return; } // decide the frame type if(f = SDIFbuf_GetFirstFrame(x->t_buf)) SDIF_Copy4Bytes(myFrameType, f->header.frameType); else // we are about to create first frame in the buffer // (give it the same type as the matrix we are adding) SDIF_Copy4Bytes(myFrameType, x->t_matrixType); // decide the matrix type if (x->t_mainMatrix) { SDIF_Copy4Bytes(myMatrixType, myFrameType); } else { SDIF_Copy4Bytes(myMatrixType, x->t_matrixType); } /* post("** myMatrixType: %c%c%c%c", myMatrixType[0], myMatrixType[1], myMatrixType[2], myMatrixType[3]); */ f = (*(x->t_buffer->FrameLookup))(x->t_buffer, x->t_time, 0); if (f == 0) { /* There was no frame at the given time */ // post("** There was no frame at that time"); f = SDIFmem_CreateEmptyFrame(); if (f == 0) { object_post((t_object *)x, "¥ SDIF-listpoke: out of memory for new frame in SDIF-buffer!"); return; } SDIF_Copy4Bytes(f->header.frameType, myFrameType); f->header.time = x->t_time; f->header.streamID = x->t_buffer->streamID; i = (*(x->t_buffer->FrameInsert))(f, x->t_buffer); if (i) { object_post((t_object *)x, "¥ SDIF-listpoke: FrameInsert returned %d", i); } } else { /* There was already a frame at the given time */ for (m = f->matrices; m != 0; m = m->next) { if (SDIF_Char4Eq(myMatrixType, m->header.matrixType)) { post("SDIF-listpoke: deleting old matrix %c%c%c%c at time %f", m->header.matrixType[0], m->header.matrixType[1], m->header.matrixType[2], m->header.matrixType[3], x->t_time); r = SDIFmem_RemoveMatrix(f, m); if (r != ESDIF_SUCCESS) { object_post((t_object *)x, "¥ SDIF-listpoke: Problem removing matrix: %s", SDIF_GetErrorString(r)); } } } } /* Now we know f is a frame in x->t_buffer, at the right time, with no matrix of the type we want to write into, and that we have to call SDIFmem_RepairFrameHeader(f) */ m = SDIFmem_CreateEmptyMatrix(); if (m == 0) { object_post((t_object *)x, "¥ SDIF-listpoke: out of memory for new matrix in SDIF-buffer!"); SDIFmem_FreeFrame(f); return; } SDIF_Copy4Bytes(m->header.matrixType, myMatrixType); m->header.matrixDataType = SDIF_FLOAT32; m->header.columnCount = x->t_num_columns; m->header.rowCount = argc / x->t_num_columns; // We checked above that it divides evenly // post("** about to getbytes() for the matrix data"); m->data = getbytes(argc * sizeof(float)); if (m->data == 0) { object_post((t_object *)x, "¥ SDIF-listpoke: out of memory for matrix data in SDIF-buffer!"); SDIFmem_RemoveMatrix(f, m); SDIFmem_FreeMatrix(m); SDIFmem_RepairFrameHeader(f); return; } mdata = m->data; for (i = 0; i < argc; ++i) { mdata[i] = asfloat(argv[i]); } /* { SDIFmem_Matrix p; for (p = f->matrices; p != NULL; p = p->next) { post("Matrix: %p, type %c%c%c%c", p, p->header.matrixType[0], p->header.matrixType[1], p->header.matrixType[2], p->header.matrixType[3]); } } */ // post("** about to SDIFmem_AddMatrix"); SDIFmem_AddMatrix(f, m); // post("** about to SDIFmem_RepairFrameHeader"); SDIFmem_RepairFrameHeader(f); }
SDIFresult SDIFmem_ReadFrameContents(SDIF_FrameHeader *head, FILE *f, SDIFmem_Frame *putithere) { /* The user has just read the header for this frame; now we have to read all the frame's matrices, put them in an SDIFmem_Matrix linked list, and then stuff everything into an SDIFmem_Frame. */ SDIFresult r; SDIFmem_Frame result; SDIFmem_Matrix matrix; int i, sz; SDIFmem_Matrix *prevNextPtr; result = (SDIFmem_Frame) (*my_malloc)(sizeof(*result)); if (result == 0) { return ESDIF_OUT_OF_MEMORY; } result->header = *head; result->prev = 0; result->next = 0; result->matrices = 0; prevNextPtr = &(result->matrices); for (i = 0; i < head->matrixCount; ++i) { matrix = SDIFmem_CreateEmptyMatrix(); if (matrix == 0) { SDIFmem_FreeFrame(result); return ESDIF_OUT_OF_MEMORY; } /* Get the linked list pointers right. Now if we bomb out and call SDIFmem_FreeFrame(result) it will free the matrix we just allocated */ *(prevNextPtr) = matrix; prevNextPtr = &(matrix->next); matrix->next = 0; if ((r = SDIF_ReadMatrixHeader(&(matrix->header), f))!=ESDIF_SUCCESS) { SDIFmem_FreeFrame(result); return r; } sz = SDIF_GetMatrixDataSize(&(matrix->header)); if (sz == 0) { matrix->data = NULL; } else { matrix->data = (*my_malloc)(sz); if (matrix->data == NULL) { SDIFmem_FreeFrame(result); return ESDIF_OUT_OF_MEMORY; } } /* COVERITY: what if sz=0? dereferences null */ if (sz != 0) if ((r = SDIF_ReadMatrixData(matrix->data, f, &(matrix->header)))!=ESDIF_SUCCESS) { SDIFmem_FreeFrame(result); return r; } } *putithere = result; return ESDIF_SUCCESS; }