BPNN *bpnn_read(char *filename) { char *mem; BPNN *_new; FILE * fd; int n1, n2, n3, i, j, memcnt; fprintf(stderr,"%s",filename); if ((fd = fopen(filename, "wb+")) == NULL) { return (NULL); } fprintf(stderr,"Reading '%s'\n", filename); fflush(stdout); fread((char *) &n1, sizeof(int),1,fd); fread((char *) &n2, sizeof(int),1,fd); fread((char *) &n3, sizeof(int),1,fd); _new = bpnn_internal_create(n1, n2, n3); fprintf(stderr,"'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3); fprintf(stderr,"Reading input weights..."); fflush(stdout); memcnt = 0; mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double))); fread( mem, (n1+1) * (n2+1) * sizeof(double),1,fd); for (i = 0; i <= n1; i++) { for (j = 0; j <= n2; j++) { fastcopy(&(_new->input_weights[i][j]), &mem[memcnt], sizeof(double)); memcnt += sizeof(double); } } free(mem); fprintf(stderr,"Done\nReading hidden weights..."); fflush(stdout); memcnt = 0; mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double))); fread(mem, (n2+1) * (n3+1) * sizeof(double),1,fd); for (i = 0; i <= n2; i++) { for (j = 0; j <= n3; j++) { fastcopy(&(_new->hidden_weights[i][j]), &mem[memcnt], sizeof(double)); memcnt += sizeof(double); } } free(mem); fclose(fd); fprintf(stderr,"Done\n"); fflush(stdout); bpnn_zero_weights(_new->input_prev_weights, n1, n2); bpnn_zero_weights(_new->hidden_prev_weights, n2, n3); return (_new); }
BPNN *bpnn_read(char *filename) { char *mem; BPNN *new_t; int fd, n1, n2, n3, i, j, memcnt; if ((fd = open(filename, 0, 0644)) == -1) { return (NULL); } printf("Reading '%s'\n", filename); //fflush(stdout); read(fd, (char *) &n1, sizeof(int)); read(fd, (char *) &n2, sizeof(int)); read(fd, (char *) &n3, sizeof(int)); new_t = bpnn_internal_create(n1, n2, n3); printf("'%s' contains a %dx%dx%d network\n", filename, n1, n2, n3); printf("Reading input weights..."); //fflush(stdout); memcnt = 0; mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(float))); read(fd, mem, (n1+1) * (n2+1) * sizeof(float)); for (i = 0; i <= n1; i++) { for (j = 0; j <= n2; j++) { fastcopy(&(new_t->input_weights[i][j]), &mem[memcnt], sizeof(float)); memcnt += sizeof(float); } } free(mem); printf("Done\nReading hidden weights..."); //fflush(stdout); memcnt = 0; mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(float))); read(fd, mem, (n2+1) * (n3+1) * sizeof(float)); for (i = 0; i <= n2; i++) { for (j = 0; j <= n3; j++) { fastcopy(&(new_t->hidden_weights[i][j]), &mem[memcnt], sizeof(float)); memcnt += sizeof(float); } } free(mem); close(fd); printf("Done\n"); //fflush(stdout); bpnn_zero_weights(new_t->input_prev_weights, n1, n2); bpnn_zero_weights(new_t->hidden_prev_weights, n2, n3); return (new_t); }
void bpnn_save(BPNN *net,char *filename) { FILE *fd; int n1, n2, n3, i, j, memcnt; double dvalue, **w; char *mem; char temp[256]; sprintf(temp,"../nets/%s",filename); if ((fd = fopen(temp, "wb+")) == NULL) { fprintf(stderr,"BPNN_SAVE: Cannot create '%s'\n", temp); return; } n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n; fprintf(stderr,"Saving %dx%dx%d network to '%s'\n", n1, n2, n3, temp); fflush(stdout); fwrite( (char *) &n1, sizeof(int),1,fd); fwrite((char *) &n2, sizeof(int),1,fd ); fwrite( (char *) &n3, sizeof(int),1,fd); memcnt = 0; w = net->input_weights; mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(double))); for (i = 0; i <= n1; i++) { for (j = 0; j <= n2; j++) { dvalue = w[i][j]; fastcopy(&mem[memcnt], &dvalue, sizeof(double)); memcnt += sizeof(double); } } fwrite(mem, (n1+1) * (n2+1) * sizeof(double),1,fd); free(mem); memcnt = 0; w = net->hidden_weights; mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(double))); for (i = 0; i <= n2; i++) { for (j = 0; j <= n3; j++) { dvalue = w[i][j]; fastcopy(&mem[memcnt], &dvalue, sizeof(double)); memcnt += sizeof(double); } } fwrite( mem, (n2+1) * (n3+1) * sizeof(double),1,fd); free(mem); fclose(fd); return; }
BOOL FCopy (char *src, char *dst) { HANDLE srcfh, dstfh; BOOL result; ATTRIBUTE_TYPE Attributes; unsigned filedate, filetime; GET_ATTRIBUTES(src, Attributes); if (Attributes == FILE_ATTRIBUTE_DIRECTORY) { fprintf( stderr, "\nUnable to open source"); return FALSE; } if (_dos_creatnew( src, _A_RDONLY, &srcfh) != 0) if (_dos_open( src, O_RDONLY, &srcfh) != 0) { fprintf( stderr, "\nUnable to open source, error code %d", GetLastError() ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); return FALSE; } if (_dos_getftime(srcfh, &filedate, &filetime) != 0) { fprintf( stderr, "\nUnable to get time of source"); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); return FALSE; } if (_dos_creatnew( dst, _A_NORMAL, &dstfh) != 0) if (_dos_open( dst, O_RDWR, &dstfh) != 0) { fprintf( stderr, "\nUnable to create destination, error code %d", GetLastError() ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return FALSE; } result = fastcopy( srcfh, dstfh ); if(!result) { if (dstfh != INVALID_HANDLE_VALUE) { CloseHandle( dstfh ); dstfh = INVALID_HANDLE_VALUE; } DeleteFile( dst ); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); fprintf( stderr, "\nUnable to copy file"); return FALSE; } if (_dos_setftime(dstfh, filedate, filetime != 0)) { fprintf( stderr, "\nUnable to set time of destination"); if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return FALSE; } if (srcfh != INVALID_HANDLE_VALUE) CloseHandle( srcfh ); if (dstfh != INVALID_HANDLE_VALUE) CloseHandle( dstfh ); return TRUE; } // FCopy
char *fappend( char *src, HANDLE dstfh ) { HANDLE srcfh; char *result; if( ( srcfh = CreateFile( src, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL ) ) != (HANDLE)-1 ) { SetFilePointer( dstfh, 0L, 0L, FILE_END ); result = fastcopy( srcfh, dstfh ); CloseHandle( srcfh ); } else { result = "Unable to open source"; } return result; }
static int do_move(char *from, char *to) { struct stat sb; char modep[15]; /* * (1) If the destination path exists, the -f option is not specified * and either of the following conditions are true: * * (a) The permissions of the destination path do not permit * writing and the standard input is a terminal. * (b) The -i option is specified. * * the mv utility shall write a prompt to standard error and * read a line from standard input. If the response is not * affirmative, mv shall do nothing more with the current * source file... */ if (!fflg && !access(to, F_OK)) { int ask = 1; int ch; if (iflg) { if (access(from, F_OK)) { warn("rename %s", from); return (1); } (void)fprintf(stderr, "overwrite %s? ", to); } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) { if (access(from, F_OK)) { warn("rename %s", from); return (1); } strmode(sb.st_mode, modep); (void)fprintf(stderr, "override %s%s%s/%s for %s? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sb.st_uid, 0), group_from_gid(sb.st_gid, 0), to); } else ask = 0; if (ask) { if ((ch = getchar()) != EOF && ch != '\n') { int ch2; while ((ch2 = getchar()) != EOF && ch2 != '\n') continue; } if (ch != 'y' && ch != 'Y') return (0); } } /* * (2) If rename() succeeds, mv shall do nothing more with the * current source file. If it fails for any other reason than * EXDEV, mv shall write a diagnostic message to the standard * error and do nothing more with the current source file. * * (3) If the destination path exists, and it is a file of type * directory and source_file is not a file of type directory, * or it is a file not of type directory, and source file is * a file of type directory, mv shall write a diagnostic * message to standard error, and do nothing more with the * current source file... */ if (!rename(from, to)) { if (vflg) printf("%s -> %s\n", from, to); return (0); } if (errno != EXDEV) { warn("rename %s to %s", from, to); return (1); } /* * (4) If the destination path exists, mv shall attempt to remove it. * If this fails for any reason, mv shall write a diagnostic * message to the standard error and do nothing more with the * current source file... */ if (!lstat(to, &sb)) { if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) { warn("can't remove %s", to); return (1); } } /* * (5) The file hierarchy rooted in source_file shall be duplicated * as a file hierarchy rooted in the destination path... */ if (lstat(from, &sb)) { warn("%s", from); return (1); } return (S_ISREG(sb.st_mode) ? fastcopy(from, to, &sb) : copy(from, to)); }
int do_move(char *from, char *to) { struct stat sb, fsb; char modep[15]; /* Source path must exist (symlink is OK). */ if (lstat(from, &fsb)) { warn("%s", from); return (1); } /* * (1) If the destination path exists, the -f option is not specified * and either of the following conditions are true: * * (a) The permissions of the destination path do not permit * writing and the standard input is a terminal. * (b) The -i option is specified. * * the mv utility shall write a prompt to standard error and * read a line from standard input. If the response is not * affirmative, mv shall do nothing more with the current * source file... */ if (!fflg && !access(to, F_OK)) { int ask = 1; int ch, first; if (iflg && !access(from, F_OK)) { (void)fprintf(stderr, "overwrite %s? ", to); } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) { strmode(sb.st_mode, modep); (void)fprintf(stderr, "override %s%s%s/%s for %s? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sb.st_uid, 0), group_from_gid(sb.st_gid, 0), to); } else ask = 0; if (ask) { first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); if (first != 'y' && first != 'Y') return (0); } } /* * (2) If rename() succeeds, mv shall do nothing more with the * current source file. If it fails for any other reason than * EXDEV, mv shall write a diagnostic message to the standard * error and do nothing more with the current source file. * * (3) If the destination path exists, and it is a file of type * directory and source_file is not a file of type directory, * or it is a file not of type directory, and source file is * a file of type directory, mv shall write a diagnostic * message to standard error, and do nothing more with the * current source file... */ if (!rename(from, to)) return (0); if (errno != EXDEV) { warn("rename %s to %s", from, to); return (1); } /* Disallow moving a mount point. */ if (S_ISDIR(fsb.st_mode)) { struct statfs sfs; char path[MAXPATHLEN]; if (realpath(from, path) == NULL) { warnx("cannot resolve %s", from); return (1); } if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { warnx("cannot rename a mount point"); return (1); } } /* * (4) If the destination path exists, mv shall attempt to remove it. * If this fails for any reason, mv shall write a diagnostic * message to the standard error and do nothing more with the * current source file... */ if (!lstat(to, &sb)) { if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) { warn("can't remove %s", to); return (1); } } /* * (5) The file hierarchy rooted in source_file shall be duplicated * as a file hierarchy rooted in the destination path... */ return (S_ISREG(fsb.st_mode) ? fastcopy(from, to, &fsb) : copy(from, to)); }
void FrameCutter::compute() { const vector<Real>& buffer = _buffer.get(); vector<Real>& frame = _frame.get(); // if we're already lastFrame or the input stream is empty, don't return any frame if (_lastFrame || buffer.empty()) { frame.clear(); return; } // if we're past the end of stream, don't return anything either if (_startIndex >= (int)buffer.size()) { frame.clear(); return; } frame.resize(_frameSize); int idxInFrame = 0; // if we're before the beginning of the buffer, fill the frame with 0 if (_startIndex < 0) { int howmuch = min(-_startIndex, _frameSize); for (; idxInFrame<howmuch; idxInFrame++) { frame[idxInFrame] = (Real)0.0; } } // now, just copy from the buffer to the frame int howmuch = min(_frameSize, (int)buffer.size() - _startIndex) - idxInFrame; fastcopy(&frame[0]+idxInFrame, &buffer[0]+_startIndex+idxInFrame, howmuch); idxInFrame += howmuch; // check if the idxInFrame is below the threshold (this would only happen // for the last frame in the stream) if (idxInFrame < _validFrameThreshold) { frame.clear(); _lastFrame = true; return; } if (_startIndex + idxInFrame >= (int)buffer.size() && _startFromZero && !_lastFrameToEndOfFile) _lastFrame = true; if (idxInFrame < _frameSize) { if (_startFromZero) { if (_lastFrameToEndOfFile) { if (_startIndex >= (int)buffer.size()) _lastFrame = true; } // if we're zero-padding with startFromZero=true, it means we're filling // in the last frame, so we'll have to stop after this one else _lastFrame = true; } else { // if we're zero-padding and the center of the frame is past the end of the // stream, then this is the last frame and we need to stop after this one if (_startIndex + _frameSize/2 >= (int)buffer.size()) { _lastFrame = true; } } // fill in the frame with 0 until the end of the buffer for (; idxInFrame < _frameSize; idxInFrame++) { frame[idxInFrame] = (Real)0.0; } } // advance frame position _startIndex += _hopSize; }
AlgorithmStatus FrameCutter::process() { bool lastFrame = false; EXEC_DEBUG("process()"); // if _streamIndex < _startIndex, we need to advance into the stream until we // arrive at _startIndex if (_streamIndex < _startIndex) { // to make sure we can skip that many, use frameSize (buffer has been resized // to be able to accomodate at least that many sample before starting processing) int skipSize = _frameSize; int howmuch = min(_startIndex - _streamIndex, skipSize); _audio.setAcquireSize(howmuch); _audio.setReleaseSize(howmuch); _frames.setAcquireSize(0); _frames.setReleaseSize(0); if (acquireData() != OK) return NO_INPUT; releaseData(); _streamIndex += howmuch; return OK; } // need to know whether we have to zero-pad on the left: ie, _startIndex < 0 int zeropadSize = 0; int acquireSize = _frameSize; int releaseSize = min(_hopSize, _frameSize); // in case hopsize > framesize int available = _audio.available(); // we need this check anyway because we might be at the very end of the stream and try to acquire 0 // for our last frame, which will unfortunately work, so just get rid of this case right now if (available == 0) return NO_INPUT; if (_startIndex < 0) { // left zero-padding and only acquire as much as _frameSize + startIndex tokens and should release zero acquireSize = _frameSize + _startIndex; releaseSize = 0; zeropadSize = -_startIndex; } // if there are not enough tokens in the stream (howmuch < available): if (acquireSize >= available) { // has to be >= in case the size of the audio fits exactly with frameSize & hopSize if (!shouldStop()) return NO_INPUT; // not end of stream -> return and wait for more data to come acquireSize = available; // need to acquire what's left releaseSize = _startIndex >= 0 ? min(available, _hopSize) : 0; // cannot release more tokens than there are available if (_startFromZero) { if (_lastFrameToEndOfFile) { if (_startIndex >= _streamIndex+available) lastFrame = true; } else lastFrame = true; } else { if (_startIndex + _frameSize/2 >= _streamIndex + available) // center of frame >= end of stream lastFrame = true; } } _frames.setAcquireSize(1); _frames.setReleaseSize(1); _audio.setAcquireSize(acquireSize); _audio.setReleaseSize(releaseSize); /* EXEC_DEBUG("zeropadSize: " << zeropadSize << "\tacquireSize: " << acquireSize << "\treleaseSize: " << releaseSize << "\tavailable: " << available << "\tlast frame: " << lastFrame << "\tstartIndex: " << _startIndex << "\tstreamIndex: " << _streamIndex); */ AlgorithmStatus status = acquireData(); EXEC_DEBUG("data acquired (audio: " << acquireSize << " - frames: 1)"); if (status != OK) { if (status == NO_INPUT) return NO_INPUT; if (status == NO_OUTPUT) return NO_OUTPUT; throw EssentiaException("FrameCutter: something weird happened."); } // some semantic description to not get mixed up between the 2 meanings // of a vector<Real> (which acts both as a stream of Real tokens at the // input and as a single vector<Real> token at the output) typedef vector<AudioSample> Frame; // get the audio input and copy it as a frame to the output const vector<AudioSample>& audio = _audio.tokens(); Frame& frame = _frames.firstToken(); frame.resize(_frameSize); // left zero-padding of the frame int idxInFrame = 0; for (; idxInFrame < zeropadSize; idxInFrame++) { frame[idxInFrame] = (Real)0.0; } fastcopy(frame.begin()+idxInFrame, audio.begin(), acquireSize); idxInFrame += acquireSize; // check if the idxInFrame is below the threshold (this would only happen // for the last frame in the stream) and if so, don't produce data if (idxInFrame < _validFrameThreshold) { E_INFO("FrameCutter: dropping incomplete frame"); // release inputs (advance to next frame), but not the output frame (we didn't produce anything) _audio.release(_audio.releaseSize()); return NO_INPUT; } // right zero-padding on the last frame for (; idxInFrame < _frameSize; idxInFrame++) { frame[idxInFrame] = (Real)0.0; } _startIndex += _hopSize; if (isSilent(frame)) { switch (_silentFrames) { case DROP: E_INFO("FrameCutter: dropping silent frame"); // release inputs (advance to next frame), but not the output frame (we didn't produce anything) _audio.release(_audio.releaseSize()); return OK; case ADD_NOISE: { vector<AudioSample> inputFrame(_frameSize, 0.0); fastcopy(&inputFrame[0]+zeropadSize, &frame[0], acquireSize); _noiseAdder->input("signal").set(inputFrame); _noiseAdder->output("signal").set(frame); _noiseAdder->compute(); break; } // otherwise, don't do nothing... case KEEP: default: ; } } EXEC_DEBUG("produced frame; releasing"); releaseData(); _streamIndex += _audio.releaseSize(); EXEC_DEBUG("released"); if (lastFrame) return PASS; return OK; }
void bpnn_save(BPNN *net, char *filename) { int n1, n2, n3, i, j, memcnt; float dvalue, **w; char *mem; ///add// FILE *pFile; pFile = fopen( filename, "w+" ); /////// /* if ((fd = creat(filename, 0644)) == -1) { printf("BPNN_SAVE: Cannot create '%s'\n", filename); return; } */ n1 = net->input_n; n2 = net->hidden_n; n3 = net->output_n; printf("Saving %dx%dx%d network to '%s'\n", n1, n2, n3, filename); //fflush(stdout); //write(fd, (char *) &n1, sizeof(int)); //write(fd, (char *) &n2, sizeof(int)); //write(fd, (char *) &n3, sizeof(int)); fwrite( (char *) &n1 , sizeof(char), sizeof(char), pFile); fwrite( (char *) &n2 , sizeof(char), sizeof(char), pFile); fwrite( (char *) &n3 , sizeof(char), sizeof(char), pFile); memcnt = 0; w = net->input_weights; mem = (char *) malloc ((unsigned) ((n1+1) * (n2+1) * sizeof(float))); for (i = 0; i <= n1; i++) { for (j = 0; j <= n2; j++) { dvalue = w[i][j]; fastcopy(&mem[memcnt], &dvalue, sizeof(float)); memcnt += sizeof(float); } } //write(fd, mem, (n1+1) * (n2+1) * sizeof(float)); fwrite( mem , (unsigned)(sizeof(float)), (unsigned) ((n1+1) * (n2+1) * sizeof(float)) , pFile); free(mem); memcnt = 0; w = net->hidden_weights; mem = (char *) malloc ((unsigned) ((n2+1) * (n3+1) * sizeof(float))); for (i = 0; i <= n2; i++) { for (j = 0; j <= n3; j++) { dvalue = w[i][j]; fastcopy(&mem[memcnt], &dvalue, sizeof(float)); memcnt += sizeof(float); } } //write(fd, mem, (n2+1) * (n3+1) * sizeof(float)); fwrite( mem , sizeof(float), (unsigned) ((n2+1) * (n3+1) * sizeof(float)) , pFile); free(mem); fclose(pFile); return; }
static int do_move(const char *from, const char *to) { struct stat sb; int ask, ch, first; char modep[15]; /* * Check access. If interactive and file exists, ask user if it * should be replaced. Otherwise if file exists but isn't writable * make sure the user wants to clobber it. */ if (!fflg && !access(to, F_OK)) { /* prompt only if source exist */ if (lstat(from, &sb) == -1) { warn("%s", from); return (1); } #define YESNO "(y/n [n]) " ask = 0; if (nflg) { if (vflg) printf("%s not overwritten\n", to); return (0); } else if (iflg) { (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); ask = 1; } else if (access(to, W_OK) && !stat(to, &sb)) { strmode(sb.st_mode, modep); (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid((unsigned long)sb.st_uid, 0), group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO); ask = 1; } if (ask) { first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); if (first != 'y' && first != 'Y') { (void)fprintf(stderr, "not overwritten\n"); return (0); } } } /* * Rename on FreeBSD will fail with EISDIR and ENOTDIR, before failing * with EXDEV. Therefore, copy() doesn't have to perform the checks * specified in the Step 3 of the POSIX mv specification. */ if (!rename(from, to)) { if (vflg) printf("%s -> %s\n", from, to); return (0); } if (errno == EXDEV) { struct statfs sfs; char path[PATH_MAX]; /* * If the source is a symbolic link and is on another * filesystem, it can be recreated at the destination. */ if (lstat(from, &sb) == -1) { warn("%s", from); return (1); } if (!S_ISLNK(sb.st_mode)) { /* Can't mv(1) a mount point. */ if (realpath(from, path) == NULL) { warn("cannot resolve %s: %s", from, path); return (1); } if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { warnx("cannot rename a mount point"); return (1); } } } else { warn("rename %s to %s", from, to); return (1); } /* * If rename fails because we're trying to cross devices, and * it's a regular file, do the copy internally; otherwise, use * cp and rm. */ if (lstat(from, &sb)) { warn("%s", from); return (1); } return (S_ISREG(sb.st_mode) ? fastcopy(from, to, &sb) : copy(from, to)); }
AlgorithmStatus Resample::process() { EXEC_DEBUG("process()"); EXEC_DEBUG("Trying to acquire data"); AlgorithmStatus status = acquireData(); if (status != OK) { // FIXME: are we sure this still works? // if status == NO_OUTPUT, we should temporarily stop the resampler, // return from this function so its dependencies can process the frames, // and reschedule the framecutter to run when all this is done. if (status == NO_OUTPUT) { EXEC_DEBUG("no more output available for resampling; mark it for rescheduling and return"); //_reschedule = true; return NO_OUTPUT; // if the buffer is full, we need to have produced something! } // if shouldStop is true, that means there is no more audio, so we need // to take what's left to fill in the output, instead of waiting for more // data to come in (which would have done by returning from this function) if (!shouldStop()) return NO_INPUT; int available = input("signal").available(); EXEC_DEBUG("There are " << available << " available tokens"); if (available == 0) return NO_INPUT; input("signal").setAcquireSize(available); input("signal").setReleaseSize(available); output("signal").setAcquireSize((int)(_data.src_ratio * available + 100 + (int)_delay)); _data.end_of_input = 1; return process(); } EXEC_DEBUG("data acquired"); const vector<AudioSample>& signal = _signal.tokens(); vector<AudioSample>& resampled = _resampled.tokens(); EXEC_DEBUG("signal size:" << signal.size()); EXEC_DEBUG("resampled size:" << resampled.size()); _data.data_in = const_cast<float*>(&(signal[0])); _data.input_frames = (long)signal.size(); _data.data_out = &(resampled[0]); _data.output_frames = (long)resampled.size(); if (_data.src_ratio == 1.0) { assert(_data.output_frames >= _data.input_frames); fastcopy(_data.data_out, _data.data_in, _data.input_frames); _data.input_frames_used = _data.input_frames; _data.output_frames_gen = _data.input_frames; } else { int error = src_process(_state, &_data); if (error) { throw EssentiaException("Resample: ", src_strerror(error)); } if (_data.input_frames_used == 0) { throw EssentiaException("Resample: Internal consumption problem while resampling"); } } EXEC_DEBUG("input frames:" << _data.input_frames_used); EXEC_DEBUG("produced:" << _data.output_frames_gen); _delay += (Real)_data.input_frames_used*_data.src_ratio - (Real)_data.output_frames_gen; assert((int)resampled.size() >= _data.output_frames_gen); assert((int)signal.size() >= _data.input_frames_used); _signal.setReleaseSize(_data.input_frames_used); _resampled.setReleaseSize(_data.output_frames_gen); releaseData(); EXEC_DEBUG("released"); return OK; }
static int do_move( const char *from, const char *to ) { struct stat sb; int ask, ch, first; char modep[15]; if ( !fflg && !access( to, F_OK ) ) { if ( lstat( from, &sb ) == -1 ) { warn( "%s", from ); return ( 1 ); } #define YESNO "(y/n [n]) " ask = 0; if ( nflg ) { if ( vflg ) printf( "%s not overwritten\n", to ); return ( 0 ); } else if ( iflg ) { (void) fprintf( stderr, "overwrite %s? %s", to, YESNO ); ask = 1; } else if ( access( to, W_OK ) && !stat( to, &sb ) && isatty( STDIN_FILENO ) ) { strmode( sb.st_mode, modep ); (void) fprintf( stderr, "override %s%s%s/%s for %s? %s", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid( (unsigned long) sb.st_uid, 0 ), group_from_gid( (unsigned long) sb.st_gid, 0 ), to, YESNO ); ask = 1; } if ( ask ) { first = ch = getchar(); while ( ch != '\n' && ch != EOF ) ch = getchar(); if ( first != 'y' && first != 'Y' ) { (void) fprintf( stderr, "not overwritten\n" ); return ( 0 ); } } } if ( !rename( from, to ) ) { if ( vflg ) printf( "%s -> %s\n", from, to ); return ( 0 ); } if ( errno == EXDEV ) { struct statfs sfs; char path[PATH_MAX]; if ( lstat( from, &sb ) == -1 ) { warn( "%s", from ); return ( 1 ); } if ( !S_ISLNK( sb.st_mode ) ) { if ( realpath( from, path ) == NULL ) { warn( "cannot resolve %s: %s", from, path ); return ( 1 ); } if ( !statfs( path, &sfs ) && !strcmp( path, sfs.f_mntonname ) ) { warnx( "cannot rename a mount point" ); return ( 1 ); } } } else { warn( "rename %s to %s", from, to ); return ( 1 ); } if ( lstat( from, &sb ) ) { warn( "%s", from ); return ( 1 ); } return ( S_ISREG( sb.st_mode ) ? fastcopy( from, to, &sb ) : copy( from, to ) ); }
AlgorithmStatus Trimmer::process() { EXEC_DEBUG("process()"); if ((_consumed < _startIndex) && (_consumed + _preferredSize > _startIndex)) { _input.setAcquireSize(_startIndex - _consumed); _input.setReleaseSize(_startIndex - _consumed); } if (_consumed == _startIndex) { _input.setAcquireSize(_preferredSize); _input.setReleaseSize(_preferredSize); } AlgorithmStatus status = acquireData(); if (status != OK) { // if status == NO_OUTPUT, we should temporarily stop the framecutter, // return from this function so its dependencies can process the frames, // and reschedule the framecutter to run when all this is done. if (status == NO_OUTPUT) { EXEC_DEBUG("no more output available for trimmer; mark it for rescheduling and return"); //_reschedule = true; return NO_OUTPUT; // if the buffer is full, we need to have produced something! } // if shouldStop is true, that means there is no more audio, so we need // to take what's left to fill in the output buffer if (!shouldStop()) return NO_INPUT; int available = input("signal").available(); EXEC_DEBUG("Frame could not be fully acquired. Next frame will be incomplete"); EXEC_DEBUG("There are " << available << " available tokens"); if (available == 0) { shouldStop(true); return NO_INPUT; } _input.setAcquireSize(available); _input.setReleaseSize(available); _output.setAcquireSize(available); _output.setReleaseSize(available); _preferredSize = available; return process(); } EXEC_DEBUG("data acquired"); // get the audio input and copy it to the output const vector<Real>& input = _input.tokens(); vector<Real>& output = _output.tokens(); if (_consumed >= _startIndex && _consumed < _endIndex) { assert(input.size() == output.size()); int howMany = min((long long)input.size(), _endIndex - _consumed); fastcopy(output.begin(), input.begin(), howMany); _output.setReleaseSize(howMany); } else { _output.setReleaseSize(0); } EXEC_DEBUG("produced frame"); _consumed += _input.releaseSize(); // optimization: we should also tell the parent (most of the time an // audio loader) to also stop, to avoid decoding an entire mp3 when only // 10 seconds are needed if (_consumed >= _endIndex) { // FIXME: does still still work with the new composites? shouldStop(true); const_cast<SourceBase*>(_input.source())->parent()->shouldStop(true); } EXEC_DEBUG("releasing"); releaseData(); EXEC_DEBUG("released"); return OK; }
AlgorithmStatus Slicer::process() { EXEC_DEBUG("process()"); // 10 first, consume and release tokens until we reach the start of the first slice // 20 produce the first slice, push it, and remove it from the list of slices to be processed // 30 goto 10 // in case we already processed all slices, just gobble up data if (_sliceIdx == int(_slices.size())) { bool ok = _input.acquire(defaultPreferredSize); if (!ok) return NO_INPUT; // TODO: make a special case for end of stream? _input.release(defaultPreferredSize); return OK; } int startIndex = _slices[_sliceIdx].first; int endIndex = _slices[_sliceIdx].second; // arriving there, only consume the tokens left before we reach the beginning of the slice if ((_consumed < startIndex) && (_consumed + _input.acquireSize() > startIndex)) { _input.setAcquireSize(startIndex - _consumed); _input.setReleaseSize(startIndex - _consumed); } // we're at the beginning of a slice, grab it entirely at once if (_consumed == startIndex) { _input.setAcquireSize(endIndex - startIndex); } AlgorithmStatus status = acquireData(); if (status != OK) { // FIXME: what does this return do here, without a comment explaining why we now skip everything after it??? return status; // if shouldStop is true, that means there is no more audio, so we need to stop if (!shouldStop()) return status; EXEC_DEBUG("Slice could not be fully acquired. Creating a partial slice to " "the end of the token stream."); EXEC_DEBUG("There are " << _input.available() << " available tokens left."); // Since there is no more tokens to consume, the last slice will be // partial (not to the end of endIndex) if (_input.available() == 0) return NO_INPUT; _input.setAcquireSize(_input.available()); _input.setReleaseSize(_input.available()); status = acquireData(); // If the last of the tokens could not be acquired, abort if (status != OK) return status; } int acquired = _input.acquireSize(); EXEC_DEBUG("data acquired (in: " << acquired << ")"); // are we dropping the tokens, or are we at the beginning of a slice, in which // case we need to copy it if (_consumed != startIndex) { // we're still dropping tokens to arrive to a slice _input.release(acquired); _consumed += acquired; return OK; } // we are copying a slice, get the audio input and copy it to the output const vector<Real>& input = _input.tokens(); vector<Real>& output = _output.firstToken(); assert((int)input.size() == _input.acquireSize()); output.resize(input.size()); fastcopy(output.begin(), input.begin(), (int)output.size()); EXEC_DEBUG("produced frame"); // set release size in function of next slice to get _sliceIdx++; int toRelease = acquired; // if next slice is very close, be careful not to release too many tokens if (_sliceIdx < (int)_slices.size()) { toRelease = min(toRelease, _slices[_sliceIdx].first - _consumed); } _input.setReleaseSize(toRelease); EXEC_DEBUG("releasing"); releaseData(); _consumed += _input.releaseSize(); EXEC_DEBUG("released"); // reset acquireSize to its default value _input.setAcquireSize(defaultPreferredSize); return OK; }