From 4284187a82e408d1487a1f25e27e6b4eb7590a4e Mon Sep 17 00:00:00 2001 From: Stefan Sayer Date: Sun, 10 Feb 2008 22:05:09 +0000 Subject: [PATCH] fixing buffered output for fpopen, merged fpopen and open functions git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@714 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- core/AmAudioFile.cpp | 89 ++++++++++---------------------------------- core/AmAudioFile.h | 3 ++ 2 files changed, 23 insertions(+), 69 deletions(-) diff --git a/core/AmAudioFile.cpp b/core/AmAudioFile.cpp index e14b1eec..ca35c191 100644 --- a/core/AmAudioFile.cpp +++ b/core/AmAudioFile.cpp @@ -105,27 +105,19 @@ int AmAudioFileFormat::getCodecId() return -1; } - - // returns 0 if everything's OK // return -1 if error int AmAudioFile::open(const string& filename, OpenMode mode, bool is_tmp) { close(); - AmAudioFileFormat* f_fmt = fileName2Fmt(filename); - if(!f_fmt){ - ERROR("while trying to the format of '%s'\n",filename.c_str()); - return -1; - } - fmt.reset(f_fmt); - - open_mode = mode; this->close_on_exit = true; + FILE* n_fp = NULL; + if(!is_tmp){ - fp = fopen(filename.c_str(),mode == AmAudioFile::Read ? "r" : "w+"); - if(!fp){ + n_fp = fopen(filename.c_str(),mode == AmAudioFile::Read ? "r" : "w+"); + if(!n_fp){ if(mode == AmAudioFile::Read) ERROR("file not found: %s\n",filename.c_str()); else @@ -133,70 +125,24 @@ int AmAudioFile::open(const string& filename, OpenMode mode, bool is_tmp) return -1; } } else { - fp = tmpfile(); - if(!fp){ + n_fp = tmpfile(); + if(!n_fp){ ERROR("could not create temporary file: %s\n",strerror(errno)); - } - } - - amci_file_desc_t fd; - memset(&fd, 0, sizeof(amci_file_desc_t)); - - int ret = -1; - - if(open_mode == AmAudioFile::Write){ - - if (f_fmt->channels<0 || f_fmt->rate<0) { - if (f_fmt->channels<0) - ERROR("channel count must be set for output file.\n"); - if (f_fmt->rate<0) - ERROR("sampling rate must be set for output file.\n"); - close(); return -1; } } - fd.subtype = f_fmt->getSubtypeId(); - fd.channels = f_fmt->channels; - fd.rate = f_fmt->rate; - - if( iofmt->open && - !(ret = (*iofmt->open)(fp, &fd, mode, f_fmt->getHCodecNoInit())) ) { - - if (mode == AmAudioFile::Read) { - f_fmt->setSubtypeId(fd.subtype); - f_fmt->channels = fd.channels; - f_fmt->rate = fd.rate; - data_size = fd.data_size; - - setBufferSize(fd.buffer_size, fd.buffer_thresh, fd.buffer_full_thresh); - } - begin = ftell(fp); - } - else { - if(!iofmt->open) - ERROR("no open function\n"); - else - ERROR("open returned %d\n",ret); - close(); - return ret; - } - - // if(open_mode == AmAudioFile::Write){ - - // DBG("After open:\n"); - // DBG("fmt::subtype = %i\n",f_fmt->getSubtypeId()); - // DBG("fmt::sample = %i\n",f_fmt->sample); - // DBG("fmt::channels = %i\n",f_fmt->channels); - // DBG("fmt::rate = %i\n",f_fmt->rate); - // } - - return ret; + return fpopen_int(filename, mode, n_fp); } int AmAudioFile::fpopen(const string& filename, OpenMode mode, FILE* n_fp) { close(); + return fpopen_int(filename, mode, n_fp); +} + +int AmAudioFile::fpopen_int(const string& filename, OpenMode mode, FILE* n_fp) +{ AmAudioFileFormat* f_fmt = fileName2Fmt(filename); if(!f_fmt){ @@ -210,6 +156,8 @@ int AmAudioFile::fpopen(const string& filename, OpenMode mode, FILE* n_fp) fseek(fp,0L,SEEK_SET); amci_file_desc_t fd; + memset(&fd, 0, sizeof(amci_file_desc_t)); + int ret = -1; if(open_mode == AmAudioFile::Write){ @@ -228,16 +176,19 @@ int AmAudioFile::fpopen(const string& filename, OpenMode mode, FILE* n_fp) fd.channels = f_fmt->channels; fd.rate = f_fmt->rate; - if( iofmt->open && !(ret = (*iofmt->open)(fp,&fd,mode, f_fmt->getHCodecNoInit())) ) { + if( iofmt->open && + !(ret = (*iofmt->open)(fp, &fd, mode, f_fmt->getHCodecNoInit())) ) { + if (mode == AmAudioFile::Read) { f_fmt->setSubtypeId(fd.subtype); f_fmt->channels = fd.channels; f_fmt->rate = fd.rate; data_size = fd.data_size; + + setBufferSize(fd.buffer_size, fd.buffer_thresh, fd.buffer_full_thresh); } begin = ftell(fp); - } - else { + } else { if(!iofmt->open) ERROR("no open function\n"); else diff --git a/core/AmAudioFile.h b/core/AmAudioFile.h index e88f09cf..f7b6d228 100644 --- a/core/AmAudioFile.h +++ b/core/AmAudioFile.h @@ -106,6 +106,9 @@ protected: /** @return a file format from file name. (ex: '1234.wav') */ AmAudioFileFormat* fileName2Fmt(const string& name); + /** internal function for opening the file */ + int fpopen_int(const string& filename, OpenMode mode, FILE* n_fp); + public: AmSharedVar loop;