DSF2FLAC
dsd_sample_reader.cpp
00001 /*
00002  * dsf2flac - http://code.google.com/p/dsf2flac/
00003  *
00004  * A file conversion tool for translating dsf dsd audio files into
00005  * flac pcm audio files.
00006  *
00007  * Copyright (c) 2013 by respective authors.
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  *
00024  * Acknowledgements
00025  *
00026  * Many thanks to the following authors and projects whose work has greatly
00027  * helped the development of this tool.
00028  *
00029  *
00030  * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)
00031  * SACD Ripper (http://code.google.com/p/sacd-ripper/)
00032  * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)
00033  * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)
00034  * Jesus R - www.sonore.us
00035  *
00036  */
00037 
00049 #include <dsd_sample_reader.h>
00050 
00057 DsdSampleReader::DsdSampleReader()
00058 {
00059         bufferLength = defaultBufferLength;
00060         isBufferAllocated = false;
00061 }
00062 
00069 DsdSampleReader::~DsdSampleReader()
00070 {
00071         if (isBufferAllocated)
00072                 delete[] circularBuffers;
00073         isBufferAllocated = false;
00074 }
00075 
00086 boost::circular_buffer<dsf2flac_uint8>* DsdSampleReader::getBuffer()
00087 {
00088         return circularBuffers;
00089 }
00090 
00097 dsf2flac_uint32 DsdSampleReader::getBufferLength()
00098 {
00099         return bufferLength;
00100 }
00101 
00110 bool DsdSampleReader::setBufferLength(dsf2flac_uint32 b)
00111 {
00112         if (b<1) {
00113                 errorMsg = "dsdSampleReader::setBufferLength:buffer length must be >0";
00114                 return false;
00115         }
00116         bufferLength=b;
00117         resizeBuffer();
00118         rewind();
00119         return true;
00120 }
00121 
00128 dsf2flac_float64 DsdSampleReader::getPositionInSeconds()
00129 {
00130         return getPosition() / (dsf2flac_float64) getSamplingFreq();
00131 }
00132 
00139 dsf2flac_float64 DsdSampleReader::getPositionAsPercent()
00140 {
00141         return 100* getPosition() / (dsf2flac_float64) getLength();
00142 }
00143 
00150 dsf2flac_float64 DsdSampleReader::getLengthInSeconds()
00151 {
00152         return getLength() / (dsf2flac_float64) getSamplingFreq();
00153 }
00154 
00161 bool DsdSampleReader::isValid()
00162 {
00163         return valid;
00164 }
00165 
00172 std::string DsdSampleReader::getErrorMsg()
00173 {
00174         return errorMsg;
00175 }
00176 
00183 void DsdSampleReader::allocateBuffer()
00184 {
00185         if (isBufferAllocated)
00186                 return;
00187                 
00188         circularBuffers = new boost::circular_buffer<dsf2flac_uint8> [getNumChannels()];
00189         for (dsf2flac_uint32 i = 0; i<getNumChannels(); i++) {
00190                 boost::circular_buffer<dsf2flac_uint8> cb(getBufferLength());
00191                 circularBuffers[i] = cb;
00192         }
00193         isBufferAllocated = true;
00194         clearBuffer();
00195         return;
00196 }
00197 
00204 void DsdSampleReader::clearBuffer() 
00205 {
00206         if (!isBufferAllocated) {
00207                 allocateBuffer();
00208                 return;
00209         }
00210         
00211         dsf2flac_uint8 c = getIdleSample();
00212         for (dsf2flac_uint32 i = 0; i<getNumChannels(); i++)
00213                 for (dsf2flac_uint32 j=0; j<getBufferLength(); j++)
00214                         circularBuffers[i].push_front(c);
00215 
00216 }
00217 
00224 void DsdSampleReader::resizeBuffer() {
00225         if (!isBufferAllocated) {
00226                 allocateBuffer();
00227                 return;
00228         }
00229         for (dsf2flac_uint32 i = 0; i<getNumChannels(); i++)
00230                 circularBuffers[i].set_capacity(getBufferLength());
00231         clearBuffer();
00232 }
00233 
00234 
 All Classes Files Functions Variables