Adding ITA audio sample class with sample rate conversion, currently under dev

parent 21d2c16c
......@@ -8,6 +8,7 @@ include( VistaCommon )
# dependencies
vista_use_package( VistaCoreLibs REQUIRED COMPONENTS VistaBase VistaInterProcComm VistaTools FIND_DEPENDENCIES )
vista_use_package( sndfile )
vista_use_package( samplerate )
vista_use_package( IPP QUIET )
vista_use_package( PCRE QUIET )
vista_use_package( SimpleIni QUIET )
......@@ -31,6 +32,10 @@ if( NOT DEFINED ITA_BASE_WITH_SNDFILE )
set( ITA_BASE_WITH_SNDFILE ON CACHE BOOL "Build with libsndfile to read/write audio samples for ITASampleFrame" )
endif( )
if( NOT DEFINED ITA_BASE_WITH_SAMPLERATE )
set( ITA_BASE_WITH_SAMPLERATE ON CACHE BOOL "Build with libsamplerate to auto-convert between samplerates" )
endif( )
if( NOT DEFINED ITA_BASE_WITH_OLD_ATOMICS )
set( ITA_BASE_WITH_OLD_ATOMICS OFF CACHE BOOL "Build with old atomic code for non-C++11 compatible compilers" )
endif( )
......@@ -56,6 +61,7 @@ include_directories( "include" )
set( ITABaseHeader
"include/ITAASCIITable.h"
"include/ITAAmplitudeSpectrum.h"
"include/ITAAudioSample.h"
"include/ITABaseDefinitions.h"
"include/ITABlockMath.h"
"include/ITABufferedAudioFileWriter.h"
......@@ -152,6 +158,11 @@ if( VSNDFILE_FOUND AND ITA_BASE_WITH_SNDFILE )
add_definitions( -DITABASE_WITH_SNDFILE )
endif( )
if( VSAMPLERATE_FOUND AND ITA_BASE_WITH_SAMPLERATE )
set( ITABaseHeader "${ITABaseHeader}" "include/ITAAudioSample.h" )
set( ITABaseSources "${ITABaseSources}" "src/ITAAudioSample.cpp" )
endif( )
if( VIPP_FOUND AND ITA_BASE_WITH_FASTMATH_IPP )
set( ITABaseSources "${ITABaseSources}" "src/ITAFastMathImplIPP.cpp" )
elseif( ITA_BASE_WITH_FASTMATH_ASSEMBLER )
......
/*
* ----------------------------------------------------------------
*
* ITA core libs
* (c) Copyright Institute of Technical Acoustics (ITA)
* RWTH Aachen University, Germany, 2015-2017
*
* ----------------------------------------------------------------
* ____ __________ _______
* // / //__ ___/ // _ |
* // / // / // /_| |
* // / // / // ___ |
* //__/ //__/ //__/ |__|
*
* ----------------------------------------------------------------
*
*/
#ifndef INCLUDE_WATCHER_ITA_AUDIO_SAMPLE
#define INCLUDE_WATCHER_ITA_AUDIO_SAMPLE
// ITABase
#include <ITABaseDefinitions.h>
#include <ITASampleFrame.h>
#include <string>
//! Multi-channel audio sample with floating point quantization
/**
* ITASampleFrame with a sampling rate. Also converts from other sampling rates.
*/
class ITA_BASE_API CITAAudioSample : public ITASampleFrame
{
public:
inline CITAAudioSample() {};
//! Create empty audio sample
/**
* Requires initialization or load to be used, otherwise methods will throw ITAException.
*/
CITAAudioSample( const float fSampleRate = 44100.0f );
//! Create (empty) audio sample with given parameters
/**
* @param[in] iNumChannels Number of channels
* @param[in] iLengthSamples Number of new samples
* @param[in] fSampleRate Sampling rate of audio sample
* @param[in] bZeroInit Init with zeros
*/
explicit CITAAudioSample( const int iChannels, const int iLength, const float fSampleRate, const bool bZeroInit = true );
//! Copy constructor as pointer
/**
* \param pSource Pointer to source audio sample
*/
CITAAudioSample( const CITAAudioSample* pSource );
//! Copy constructor as reference
/**
* \param pbSource Reference to source audio sample
*/
CITAAudioSample( const CITAAudioSample& sbSource );
virtual inline ~CITAAudioSample() {};
//! Sampling frequency of audio sample
float GetSampleRate() const;
//! Initialize
/**
* (Re-)Initialize an audio sample
*
* @param[in] iNumChannels Number of channels
* @param[in] iLengthSamples Number of new samples
* @param[in] fSampleRate Sampling rate of audio sample
* @param[in] bZeroInit Init with zeros
*/
void Init( const int iNumChannels, const int iLengthSamples, const float fSampleRate, const bool bZeroInit = true );
//! Read from sample buffer with sample rate conversion
void Load( const ITASampleFrame& sfSource, const float fSourceSampleRate );
//! Read from other audio sample with sample rate conversion
void Load( const CITAAudioSample& oSource );
//! Load audio sample from file and inherit it's sample rate
void Load( const std::string& sFilePath );
//! Load audio sample from file and convert sample rate, if necessary
void LoadWithSampleTypeConversion( const std::string& sFilePath );
private:
//! Disable this Init method from sample buffer
void Init( int, int, bool );
//! Disable load without sample rate
void Load( const std::string&, const double& );
float m_fSampleRate; //!< Sampling rate
};
#endif // INCLUDE_WATCHER_ITA_AUDIO_SAMPLE
#include <ITAAudioSample.h>
#include <ITAException.h>
CITAAudioSample::CITAAudioSample( const float fSampleRate )
: m_fSampleRate( fSampleRate )
{
}
CITAAudioSample::CITAAudioSample( const int iChannels, const int iLength, const float fSampleRate, const bool bZeroInit /*= true */ )
: ITASampleFrame( iChannels, iLength, bZeroInit )
, m_fSampleRate( fSampleRate )
{
}
CITAAudioSample::CITAAudioSample( const CITAAudioSample* pSource )
{
Init( pSource->GetNumChannels(), pSource->GetLength(), pSource->GetSampleRate() );
ITASampleFrame::write( pSource, pSource->GetLength() );
}
CITAAudioSample::CITAAudioSample( const CITAAudioSample& sbSource )
{
Init( sbSource.GetNumChannels(), sbSource.GetLength(), sbSource.GetSampleRate() );
ITASampleFrame::write( sbSource, sbSource.GetLength() );
}
void CITAAudioSample::Init( const int iNumChannels, const int iLength, const float fSampleRate, const bool bZeroInit /*= true */ )
{
m_fSampleRate = fSampleRate;
ITASampleFrame::Init( iNumChannels, iLength, bZeroInit );
}
void CITAAudioSample::LoadWithSampleTypeConversion( const std::string& sFilePath )
{
if( m_fSampleRate <= 0.0f )
ITA_EXCEPT_INVALID_PARAMETER( "Invalid internal audio sample sampling rate" );
double dSampleRate;
ITASampleFrame sfRaw;
sfRaw.Load( sFilePath, dSampleRate );
if( double( GetSampleRate() ) != dSampleRate )
Load( sfRaw, GetSampleRate() );
}
float CITAAudioSample::GetSampleRate() const
{
return m_fSampleRate;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment