Blender  V2.59
AUD_SRCResampleReader.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_SRCResampleReader.cpp 35141 2011-02-25 10:21:56Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * Copyright 2009-2011 Jörg Hermann Müller
00007  *
00008  * This file is part of AudaSpace.
00009  *
00010  * Audaspace is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * AudaSpace is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with Audaspace; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include "AUD_SRCResampleReader.h"
00033 
00034 #include <cmath>
00035 #include <cstring>
00036 #include <cstdio>
00037 
00038 static long src_callback(void *cb_data, float **data)
00039 {
00040         return ((AUD_SRCResampleReader*)cb_data)->doCallback(data);
00041 }
00042 
00043 static const char* state_error = "AUD_SRCResampleReader: SRC State couldn't be "
00044                                                                  "created.";
00045 
00046 AUD_SRCResampleReader::AUD_SRCResampleReader(AUD_IReader* reader,
00047                                                                                          AUD_Specs specs) :
00048                 AUD_EffectReader(reader),
00049                 m_sspecs(reader->getSpecs()),
00050                 m_factor(double(specs.rate) / double(m_sspecs.rate)),
00051                 m_tspecs(specs),
00052                 m_position(0)
00053 {
00054         m_tspecs.channels = m_sspecs.channels;
00055 
00056         int error;
00057         m_src = src_callback_new(src_callback,
00058                                                          SRC_SINC_MEDIUM_QUALITY,
00059                                                          m_sspecs.channels,
00060                                                          &error,
00061                                                          this);
00062 
00063         if(!m_src)
00064         {
00065                 // XXX printf("%s\n", src_strerror(error));
00066                 AUD_THROW(AUD_ERROR_SRC, state_error);
00067         }
00068 }
00069 
00070 AUD_SRCResampleReader::~AUD_SRCResampleReader()
00071 {
00072         src_delete(m_src);
00073 }
00074 
00075 long AUD_SRCResampleReader::doCallback(float** data)
00076 {
00077         int length = m_buffer.getSize() / AUD_SAMPLE_SIZE(m_tspecs);
00078         sample_t* buffer;
00079 
00080         m_reader->read(length, buffer);
00081 
00082         *data = buffer;
00083         return length;
00084 }
00085 
00086 void AUD_SRCResampleReader::seek(int position)
00087 {
00088         m_reader->seek(position / m_factor);
00089         src_reset(m_src);
00090         m_position = position;
00091 }
00092 
00093 int AUD_SRCResampleReader::getLength() const
00094 {
00095         return m_reader->getLength() * m_factor;
00096 }
00097 
00098 int AUD_SRCResampleReader::getPosition() const
00099 {
00100         return m_position;
00101 }
00102 
00103 AUD_Specs AUD_SRCResampleReader::getSpecs() const
00104 {
00105         return m_tspecs;
00106 }
00107 
00108 void AUD_SRCResampleReader::read(int & length, sample_t* & buffer)
00109 {
00110         int size = length * AUD_SAMPLE_SIZE(m_tspecs);
00111 
00112         if(m_buffer.getSize() < size)
00113                 m_buffer.resize(size);
00114 
00115         buffer = m_buffer.getBuffer();
00116 
00117         length = src_callback_read(m_src, m_factor, length, buffer);
00118 
00119         m_position += length;
00120 }