SDIFresult SDIFmem_AddMatrix(SDIFmem_Frame f, SDIFmem_Matrix m) { int sz; SDIFmem_Matrix p, last; assert(f != NULL); assert(m != NULL); m->next = NULL; p = f->matrices; if (p == NULL) { f->matrices = m; } else { while (p != NULL) { if (SDIF_Char4Eq(p->header.matrixType, m->header.matrixType)) { return ESDIF_DUPLICATE_MATRIX_TYPE_IN_FRAME; } last = p; p = p->next; } last->next = m; } sz = SDIF_GetMatrixDataSize(&(m->header)); f->header.size += sz + sizeof(SDIF_MatrixHeader); f->header.matrixCount++; return ESDIF_SUCCESS; }
int Read1NVTFrame(t_sdif_fileinfo *x, FILE *f, char *name, SDIF_FrameHeader *fhp) { // Just read the frame header, so go through the matrices looking for 1NVTs to print SDIFresult r; int i, sz; SDIF_MatrixHeader mh; char *buf; for (i = 0; i < fhp->matrixCount; ++i) { if (r = SDIF_ReadMatrixHeader(&mh, f)) { object_error((t_object *)x, NAME ": error reading matrix header: %s", SDIF_GetErrorString(r)); return 0; } if (SDIF_Char4Eq("1NVT", mh.matrixType) && mh.matrixDataType == SDIF_UTF8) { sz = SDIF_GetMatrixDataSize(&mh); buf = (char *) getbytes(sz); if (buf == 0) { object_error((t_object *)x, NAME ": out of memory; can't read name/value table"); return 0; } if (r = SDIF_ReadMatrixData((void *) buf, f, &mh)) { object_error((t_object *)x, NAME ": error reading 1NVT matrix data: %s", SDIF_GetErrorString(r)); return 0; } //post("Name/value table:"); //post("%s", buf); SDIFfileinfo_output1NVT(x, buf, fhp); freebytes(buf, sz); } else { if (SDIF_Char4Eq("1NVT", mh.matrixType)) { object_post((t_object *)x, NAME ": 1NVT matrix has unexpected matrix data type 0x%x; skipping", mh.matrixDataType); } if (r = SDIF_SkipMatrix(&mh, f)) { object_error((t_object *)x, NAME ": error skipping 1NVT matrix: %s", SDIF_GetErrorString(r)); return 0; } } } }
SDIFresult SDIF_BeginRead(FILE *input) { SDIF_GlobalHeader sgh; SDIFresult r; /* make sure the header is OK. */ if ((r = SDIF_Read1(sgh.SDIF, 4, input))!=ESDIF_SUCCESS) return r; if (!SDIF_Char4Eq(sgh.SDIF, "SDIF")) return ESDIF_BAD_SDIF_HEADER; if ((r = SDIF_Read4(&sgh.size, 1, input))!=ESDIF_SUCCESS) return r; if (sgh.size % 8 != 0) return ESDIF_BAD_SDIF_HEADER; if (sgh.size < 8) return ESDIF_BAD_SDIF_HEADER; if ((r = SDIF_Read4(&sgh.SDIFversion, 1, input))!=ESDIF_SUCCESS) return r; if ((r = SDIF_Read4(&sgh.SDIFStandardTypesVersion, 1, input))!=ESDIF_SUCCESS) return r; if (sgh.SDIFversion != 3) { /*RWD was < 3 */ return ESDIF_OBSOLETE_FILE_VERSION; } if (sgh.SDIFStandardTypesVersion < 1) { return ESDIF_OBSOLETE_TYPES_VERSION; } /* skip size-8 bytes. (We already read the first two version numbers, but maybe there's more data in the header frame.) */ if (sgh.size == 8) { return ESDIF_SUCCESS; } if (SkipBytes(input, sgh.size-8)!=ESDIF_SUCCESS) { return ESDIF_BAD_SDIF_HEADER; } return ESDIF_SUCCESS; }
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); }
void do_scan(t_sdif_fileinfo *x, FILE *f, char *name) { SDIFresult r; SDIF_FrameHeader fh; int result, i, sawStreamAlready, needToSkip; char frameTypeBuffer[5]; x->x_ns = 0; while ((r = SDIF_ReadFrameHeader(&fh, f)) == ESDIF_SUCCESS) { /* post("** Read frame header: ID %d, time %g, type %c%c%c%c", fh.streamID, fh.time, fh.frameType[0], fh.frameType[1], fh.frameType[2], fh.frameType[3]); */ needToSkip = 1; if (x->print_NVT_matrices) { if (SDIF_Char4Eq(fh.frameType, "1NVT")) { needToSkip = 0; // Will read the matrices in this frame rather than skipping it if (Read1NVTFrame(x, f, name, &fh) == 0) return; } } sawStreamAlready = 0; for (i = 0; i < x->x_ns; ++i) { if (x->x_streamID[i] == fh.streamID) { sawStreamAlready = 1; // Already saw this stream, so just make sure type is OK if (!SDIF_Char4Eq(fh.frameType, x->x_frameType[i])) { object_post((t_object *)x, "¥ streamlist: Warning: First frame for stream %ld", fh.streamID); post("¥ had type %c%c%c%c, but frame at time %g has type %c%c%c%c", x->x_frameType[i][0], x->x_frameType[i][1], x->x_frameType[i][2], x->x_frameType[i][3], fh.time, fh.frameType[0], fh.frameType[1], fh.frameType[2], fh.frameType[3]); } break; } } if (!sawStreamAlready) { if (x->x_ns >= MAX_STREAMS) { object_error((t_object *)x, NAME ": SDIF file has more than %ld streams!", MAX_STREAMS); return; } ++(x->x_ns); x->x_streamID[i] = fh.streamID; SDIF_Copy4Bytes(x->x_frameType[i], fh.frameType); SDIF_Copy4Bytes(frameTypeBuffer, fh.frameType); frameTypeBuffer[4] = '\0'; x->x_frameTypeSymbol[i] = gensym(frameTypeBuffer); x->x_starttime[i] = fh.time; x->x_numframes[i] = 0; } x->x_endtime[i] = fh.time; ++(x->x_numframes[i]); if (needToSkip) { if (r = SDIF_SkipFrame(&fh, f)) { object_post((t_object *)x, NAME ": error skipping frame in SDIF file %s:", name); object_post((t_object *)x, " %s", SDIF_GetErrorString(r)); return; } } } if (r != ESDIF_END_OF_DATA) { object_post((t_object *)x, NAME ": error reading SDIF file %s:", name); object_post((t_object *)x, "%s", SDIF_GetErrorString(r)); } outlet_bang(x->outlet2); return; }