⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aflibdata.cc

📁 一个共享源码的音频库2
💻 CC
📖 第 1 页 / 共 3 页
字号:
/* * Copyright: (C) 1999-2001 Bruce W. Forsberg * *   This library is free software; you can redistribute it and/or *   modify it under the terms of the GNU Lesser General Public *   License as published by the Free Software Foundation; either *   version 2.1 of the License, or any later version. * *   This library is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *   Lesser General Public License for more details. * *   You should have received a copy of the GNU Lesser General Public *   License along with this library; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA * *   Bruce Forsberg  forsberg@tns.net * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <iostream>#include <string>using std::cerr;using std::endl;#include "aflibData.h"#ifdef HAVE_BYTESWAP_H#include <byteswap.h>#define  swab_short(x)  bswap_16(x) #define  swab_int(x)    bswap_32(x) #else#define  swab_short(x)  (((unsigned short)(x) << 8) | ((unsigned short)(x) >> 8))#define  swab_int(x)   ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \                        (((x) & 0x0000ff00) << 8)  | (((x) & 0x000000ff) << 24))#endif/*Should be faster  */#define RIGHT(datum,bits) ((datum) >> bits)#define LEFT(datum,bits) ((datum) << bits)/*! \brief Constructor.*/aflibData::aflibData(long length){   // _data is not initialized yet   _initialized = FALSE;   _data = NULL;   _length = length;   _adj_length = length;   _total_length = 0;   _byte_inc = 0;   setHostEndian();}/*! \brief Constructor.*/aflibData::aflibData(   const aflibConfig& config,   long length){   _config = config;   _length = length;   _adj_length = length;   _data   = NULL;   setHostEndian();   _byte_inc = _config.getBitsPerSample() / 8;   init();}/*! \brief Copy Constructor.    This copy constructor will create another data object with the same data as the    other one. It will have newly allocated memory with an identical configuration and    data.*/aflibData::aflibData(const aflibData& data){   _data = NULL;   _config = data.getConfig();   data.getLength(_adj_length);   data.getOrigLength(_length);   _byte_inc = _config.getBitsPerSample() / 8;   setHostEndian();   init();   if (data.getDataPointer() != NULL)   {      memcpy(_data, data.getDataPointer(), _total_length);      }}/*! \brief Assignment operator.    This will set one data object with the same data as the    other one. It will have newly allocated memory with an identical configuration and    data.*/aflibData&aflibData::operator=(const aflibData& data){   _config = data.getConfig();   data.getOrigLength(_length);   data.getLength(_adj_length);   _byte_inc = _config.getBitsPerSample() / 8;   setHostEndian();   init();   if (data.getDataPointer() != NULL)   {      memcpy(_data, data.getDataPointer(), _total_length);      }   return (*this);}/*! \brief Destructor.*/aflibData::~aflibData(){   delete [] (unsigned char *) _data;}/*! \brief Set the audio configuration of the data.    This allows one to set the audio configuration of this data class. This can    be used when it was not set in the constructor or one needs to change it. Any    audio data stored will be lost and a new array allocated.*/voidaflibData::setConfig(const aflibConfig& config){   _config = config;   _byte_inc = _config.getBitsPerSample() / 8;   init();}/*! \brief Gets audio configuration data.*/const aflibConfig&aflibData::getConfig() const{   return (_config);}/*! \brief Gets length of data.    This returns the number of samples. It does not matter how many channels.    It only means samples no matter how many channels.*/voidaflibData::getLength(long& length) const{   length = _adj_length;}/*! \brief Same as getLength(length);.*/longaflibData::getLength() const{   return (_adj_length);}/*! \brief Get the original length.    This returns the original number of samples that was used to allocate    space. If the samples are adjusted down then this is the original value    and not the adjusted value. It does not matter how many channels.    It only means samples no matter how many channels.*/voidaflibData::getOrigLength(long& length) const{   length = _length;}/*! \brief Same as getOrigLength(length).*/longaflibData::getOrigLength() const{   return (_length);}/*! \brief Adjust size of data array.    This allows one to adjust the size of the data array downward without allocating    new data. This is useful when a data object is passed to a read file class with a    certain size. If we are at the end of the file and only a portion of the data    requested is available then the size of the data object can be changed downward.*/voidaflibData::adjustLength(long length){   if (length < _length)   {      _adj_length = length;   }}/*! \brief Adjust total size of data array.    This allows one to adjust the size of the data array downward without allocating    new data. This is useful when a data object is passed to a read file class with a    certain size. If we are at the end of the file and only a portion of the data    requested is available then the size of the data object can be changed downward.    The parameter passed is in total bytes not samples*/voidaflibData::adjustTotalLength(long length){   long sample_length;   sample_length = length / (_config.getChannels() * _byte_inc);   adjustLength(sample_length);}/*! \brief Gets the total length of memory that is allocated.*/longaflibData::getTotalLength(){   return _total_length;}/*! \brief Gets the total length of memory that is adjusted.*/longaflibData::getTotalAdjustLength(){   return (_adj_length * _config.getChannels() * _byte_inc);}/*! \brief Get a pointer to the internal data.    This will return a void pointer to the internally allocated data array. One should    use the setSample and getSample routines if at all possible instead of this one.    Possible exceptions are the start of an audio chain such as a device or file. In    that case we determine the data layout so we can just get a pointer to the data and    copy the data. getTotalLength will tell the total size of the allocated memory. One    should not read or write beyond this value.*/void *aflibData::getDataPointer() const{   return _data;}/*! \brief Sets a sample of audio data.    This stores a single sample value into the data array based on the audio configuration    data. First position begins at 0.  First channel begins at 0.*/voidaflibData::setSample(   int sample,   long position,   int channel){   // This function needs a much more efficient algorithm and needs to be   // an inline function if at all possible.   // TBD only AFLIB_INTERLEAVE supported currently   if (_config.getDataEndian() == _endian)   {      long data_position = _config.getChannels() * position +                           channel;#if 0      if ((data_position * _byte_inc + _byte_inc) > _total_length)      {         cerr << "Buffer Overrun! ERROR!" << endl;         return;      }#endif      if (_config.getSampleSize() == AFLIB_DATA_16S)         ((signed short int *)_data)[data_position] = (signed short int)sample;      else if (_config.getSampleSize() == AFLIB_DATA_8U)         ((unsigned char *)_data)[data_position] = (unsigned char)sample;      else if (_config.getSampleSize() == AFLIB_DATA_8S)         ((signed char *)_data)[data_position] = (signed char)sample;      else if (_config.getSampleSize() == AFLIB_DATA_16U)         ((unsigned short int *)_data)[data_position] = (unsigned short int)sample;      else if (_config.getSampleSize() == AFLIB_DATA_32S)         ((signed int *)_data)[data_position] = (signed int)sample;   }   else   {      long data_position = _config.getChannels() * position +                           channel;      if (_config.getSampleSize() == AFLIB_DATA_16S)      {          sample = (0xff & sample) << 8 | (0xff00 & sample) >> 8;         ((signed short int *)_data)[data_position] = (signed short int)sample;      }      else if (_config.getSampleSize() == AFLIB_DATA_8U)         ((unsigned char *)_data)[data_position] = (unsigned char)sample;      else if (_config.getSampleSize() == AFLIB_DATA_8S)         ((signed char *)_data)[data_position] = (signed char)sample;      else if (_config.getSampleSize() == AFLIB_DATA_16U)      {          sample = (0xff & sample) << 8 | (0xff00 & sample) >> 8;         ((unsigned short int *)_data)[data_position] = (unsigned short int)sample;      }      else if (_config.getSampleSize() == AFLIB_DATA_32S)      {          sample = (0xff & sample) << 24 | (0xff00 & sample) << 8 |                   (0xff0000 & sample) >> 8 | (0xff000000 & sample) >> 24;         ((signed int *)_data)[data_position] = (signed int)sample;      }   }}/*! \brief Gets a sample of audio data.    This will retrieve a single data value from interval memory and return it as an int.    First position begins at 0.  First channel begins at 0.*/intaflibData::getSample(   long position,   int channel){   int ret_value = 0;   // TBD only AFLIB_INTERLEAVE supported currently   if (_config.getDataEndian() == _endian)   {      long data_position = _config.getChannels() * position +                           channel;#if 0      if ((data_position * _byte_inc + _byte_inc) > _total_length)      {         cerr << "Buffer Overrun! ERROR!" << endl;         return 0;      }#endif      if (_config.getSampleSize() == AFLIB_DATA_16S)         ret_value = (int) ((signed short int *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_8U)         ret_value = (int) ((unsigned char *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_8S)         ret_value = (int) ((signed char *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_16U)         ret_value = (int)((unsigned short int *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_32S)         ret_value = (int)((signed int *)_data)[data_position];   }   else   {      long data_position = _config.getChannels() * position +                           channel;      if (_config.getSampleSize() == AFLIB_DATA_16S)      {         ret_value = (int) ((signed short int *)_data)[data_position];	/*FIXME use swab_short */         ret_value = (int)((signed short int)((0xff & ret_value) << 8 | (0xff00 & ret_value) >> 8));      }      else if (_config.getSampleSize() == AFLIB_DATA_8U)         ret_value = (int) ((unsigned char *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_8S)         ret_value = (int) ((signed char *)_data)[data_position];      else if (_config.getSampleSize() == AFLIB_DATA_16U)      {         ret_value = (int)((unsigned short int *)_data)[data_position];	/*FIXME use swab_short */         ret_value = (0xff & ret_value) << 8 | (0xff00 & ret_value) >> 8;      }      else if (_config.getSampleSize() == AFLIB_DATA_32S)      {         ret_value = (int)((signed int *)_data)[data_position];	/*FIXME use swab_int */         ret_value = (int)((signed int)                   ((0xff & ret_value) << 24 | (0xff00 & ret_value) << 8 |                   (0xff0000 & ret_value) >> 8 | (0xff000000 & ret_value) >> 24));      }   }   return (ret_value);}/*! \brief Returns absolute min and max values of data type selected.*/voidaflibData::getMinMax(   int& min_value,   int& max_value) const{   switch (_config.getSampleSize())   {      case AFLIB_DATA_8S:         min_value = -127;         max_value = 128;      break;      case AFLIB_DATA_8U:         min_value = 0;         max_value = 255;      break;      case AFLIB_DATA_16S:         min_value = -32767;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -