int afSetVirtualChannels (AFfilehandle file, int trackid, int channelCount) { if (!_af_filehandle_ok(file)) return -1; Track *track = file->getTrack(trackid); if (!track) return -1; track->v.channelCount = channelCount; track->ms->setDirty(); if (track->channelMatrix) free(track->channelMatrix); track->channelMatrix = NULL; return 0; }
static _Loop *getLoop (AFfilehandle handle, int instid, int loopid, bool mustWrite) { int loopno, instno; if (!_af_filehandle_ok(handle)) return NULL; if (mustWrite && !_af_filehandle_can_write(handle)) return NULL; if ((instno = _af_handle_instrument_index_from_id(handle, instid)) == -1) return NULL; if ((loopno = _af_handle_loop_index_from_id(handle, instno, loopid)) == -1) return NULL; return &handle->instruments[instno].loops[loopno]; }
AFframecount afSeekFrame (AFfilehandle file, int trackid, AFframecount frame) { if (!_af_filehandle_ok(file)) return -1; if (!file->checkCanRead()) return -1; Track *track = file->getTrack(trackid); if (!track) return -1; if (track->ms->isDirty() && track->ms->setup(file, track) == AF_FAIL) return -1; if (frame < 0) return track->nextvframe; /* Optimize the case of seeking to the current position. */ if (frame == track->nextvframe) return track->nextvframe; /* Limit request to the number of frames in the file. */ if (track->totalvframes != -1) if (frame > track->totalvframes) frame = track->totalvframes - 1; /* Now that the modules are not dirty and frame represents a valid virtual frame, we call _AFsetupmodules again after setting track->nextvframe. _AFsetupmodules will look at track->nextvframe and compute track->nextfframe in clever and mysterious ways. */ track->nextvframe = frame; if (track->ms->setup(file, track) == AF_FAIL) return -1; return track->nextvframe; }
int afSetVirtualPCMMapping (AFfilehandle file, int trackid, double slope, double intercept, double minClip, double maxClip) { _Track *track; if (!_af_filehandle_ok(file)) return -1; if ((track = _af_filehandle_get_track(file, trackid)) == NULL) return -1; track->v.pcm.slope = slope; track->v.pcm.intercept = intercept; track->v.pcm.minClip = minClip; track->v.pcm.maxClip = maxClip; track->ms.modulesdirty = AF_TRUE; return 0; }
void afGetVirtualPCMMapping (AFfilehandle file, int trackid, double *slope, double *intercept, double *minClip, double *maxClip) { _Track *track; if (!_af_filehandle_ok(file)) return; if ((track = _af_filehandle_get_track(file, trackid)) == NULL) return; if (slope) *slope = track->v.pcm.slope; if (intercept) *intercept = track->v.pcm.intercept; if (minClip) *minClip = track->v.pcm.minClip; if (maxClip) *maxClip = track->v.pcm.maxClip; }
int afSetVirtualRate (AFfilehandle file, int trackid, double rate) { if (!_af_filehandle_ok(file)) return -1; Track *track = file->getTrack(trackid); if (!track) return -1; if (rate < 0) { _af_error(AF_BAD_RATE, "invalid sampling rate %.30g", rate); return -1; } track->v.sampleRate = rate; track->ms->setDirty(); return 0; }
int afSetVirtualByteOrder (AFfilehandle file, int trackid, int byteorder) { if (!_af_filehandle_ok(file)) return AF_FAIL; Track *track = file->getTrack(trackid); if (!track) return AF_FAIL; if (byteorder != AF_BYTEORDER_BIGENDIAN && byteorder != AF_BYTEORDER_LITTLEENDIAN) { _af_error(AF_BAD_BYTEORDER, "invalid byte order %d", byteorder); return AF_FAIL; } track->v.byteOrder = byteorder; track->ms->setDirty(); return AF_SUCCEED; }
int afWriteFrames (AFfilehandle file, int trackid, const void *samples, int nvframes2write) { _AFmoduleinst *firstmod; _AFchunk *userc; _Track *track; int bytes_per_vframe; AFframecount vframe; if (!_af_filehandle_ok(file)) return -1; if (!_af_filehandle_can_write(file)) return -1; if ((track = _af_filehandle_get_track(file, trackid)) == NULL) return -1; if (track->ms.modulesdirty) { if (_AFsetupmodules(file, track) != AF_SUCCEED) return -1; } /*if (file->seekok) {*/ if (af_fseek(file->fh, track->fpos_next_frame, SEEK_SET) < 0) { _af_error(AF_BAD_LSEEK, "unable to position write pointer at next frame"); return -1; } /* } */ bytes_per_vframe = _af_format_frame_size(&track->v, AF_TRUE); firstmod = &track->ms.module[0]; userc = &track->ms.chunk[0]; track->filemodhappy = AF_TRUE; vframe = 0; #ifdef UNLIMITED_CHUNK_NVFRAMES /* OPTIMIZATION: see the comment at the very end of arrangemodules() in modules.c for an explanation of this: */ if (!trk->ms.mustuseatomicnvframes) { userc->buf = (char *)buf; userc->nframes = nvframes2write; (*firstmod->mod->run_push)(firstmod); /* Count this chunk if there was no i/o error. */ if (trk->filemodhappy) vframe += userc->nframes; } else #else /* Optimization must be off. */ assert(track->ms.mustuseatomicnvframes); #endif { while (vframe < nvframes2write) { userc->buf = (char *) samples + bytes_per_vframe * vframe; if (vframe <= nvframes2write - _AF_ATOMIC_NVFRAMES) userc->nframes = _AF_ATOMIC_NVFRAMES; else userc->nframes = nvframes2write - vframe; (*firstmod->mod->run_push)(firstmod); if (track->filemodhappy == AF_FALSE) break; vframe += userc->nframes; } } track->nextvframe += vframe; track->totalvframes += vframe; return vframe; }
int afReadFrames (AFfilehandle file, int trackid, void *samples, int nvframeswanted) { _Track *track; _AFmoduleinst *firstmod; _AFchunk *userc; AFframecount nvframesleft, nvframes2read; int bytes_per_vframe; AFframecount vframe; if (!_af_filehandle_ok(file)) return -1; if (!_af_filehandle_can_read(file)) return -1; if ((track = _af_filehandle_get_track(file, trackid)) == NULL) return -1; if (track->ms.modulesdirty) { if (_AFsetupmodules(file, track) != AF_SUCCEED) return -1; } /*if (file->seekok) {*/ if (af_fseek(file->fh, track->fpos_next_frame, SEEK_SET) < 0) { _af_error(AF_BAD_LSEEK, "unable to position read pointer at next frame"); return -1; } /* } */ if (track->totalvframes == -1) nvframes2read = nvframeswanted; else { nvframesleft = track->totalvframes - track->nextvframe; nvframes2read = (nvframeswanted > nvframesleft) ? nvframesleft : nvframeswanted; } bytes_per_vframe = _af_format_frame_size(&track->v, AF_TRUE); firstmod = &track->ms.module[track->ms.nmodules-1]; userc = &track->ms.chunk[track->ms.nmodules]; track->filemodhappy = AF_TRUE; vframe = 0; if (!track->ms.mustuseatomicnvframes) { assert(track->frames2ignore == 0); userc->buf = samples; userc->nframes = nvframes2read; (*firstmod->mod->run_pull)(firstmod); if (track->filemodhappy) vframe += userc->nframes; } else { bool eof = AF_FALSE; if (track->frames2ignore != 0) { userc->nframes = track->frames2ignore; userc->buf = _af_malloc(track->frames2ignore * bytes_per_vframe); if (userc->buf == AF_NULL) return 0; (*firstmod->mod->run_pull)(firstmod); /* Have we hit EOF? */ if (userc->nframes < track->frames2ignore) eof = AF_TRUE; track->frames2ignore = 0; free(userc->buf); userc->buf = NULL; } /* Now start reading useful frames, until EOF or premature EOF. */ while (track->filemodhappy && !eof && vframe < nvframes2read) { AFframecount nvframes2pull; userc->buf = (char *) samples + bytes_per_vframe * vframe; if (vframe <= nvframes2read - _AF_ATOMIC_NVFRAMES) nvframes2pull = _AF_ATOMIC_NVFRAMES; else nvframes2pull = nvframes2read - vframe; userc->nframes = nvframes2pull; (*firstmod->mod->run_pull)(firstmod); if (track->filemodhappy) { vframe += userc->nframes; if (userc->nframes < nvframes2pull) eof = AF_TRUE; } } } track->nextvframe += vframe; return vframe; }