#include "sounddata.h"
//------------------------------------------- Dependencies:
#include "cplus_support.h" // cppx::throwX, cppx::S
#include <algorithm> // std::swap
#include <assert.h> // assert
#include <fstream> // std::ifstream
#include <math.h> // sqrt
using cppx::throwX;
using cppx::S;
using std::istream;
namespace {
using std::string;
int intFrom( istream& stream, string const& streamName, string const& intName )
{
int result;
stream >> result
|| throwX( S() << "Error reading " << intName << " from " << streamName );
return result;
}
// The following is just support for detecting indexing range errors.
template< class EType >
inline int countOf( std::vector< EType > const& v )
{
return v.size();
}
template< class ContainerType > struct ElementType;
template< class EType >
struct ElementType< std::vector< EType > >
{
typedef EType T;
};
template< class Container >
inline typename ElementType< Container >::T const& element(
int i,
Container const& v
)
{
assert( 0 <= i && i < countOf( v ) );
return v[i];
}
template< class Container >
inline typename ElementType< Container >::T& element(
int i,
Container& v
)
{
assert( 0 <= i && i < countOf( v ) );
return v[i];
}
}
namespace soundData{
//-------------------------- FeatureVector:
int FeatureVector::count()const
{
return values_.size();
}
double FeatureVector::operator[](int i)const
{
return element(i, values_);
}
FeatureVector::FeatureVector( int n )
: values_( n )
{}
/*==================Frame====================================*/
Frame::Frame( int nFeatures )
: features( nFeatures )
{}
/*===================Frames==========================*/
int Frames::count() const
{
return frames_.size();
}
int Frames::nFeaturesPerFrame() const
{
return nFeaturesPerFrame_;
}
Frame const& Frames::operator[]( int i ) const
{
return element( i, frames_ );
}
Frames::Frames( int n )
: nFeaturesPerFrame_( n )
{}
/*============loading the frames ===============*/
void loadFromFile(
string const& fileName,
Frames& frames,
ostream& log
)
{
using std::endl;
using std::ifstream;
string const streamDescription = "text data file " + fileName;
log << "Opening " << streamDescription << " for reading..." << endl;
ifstream stream( fileName.c_str() );
(!stream.fail())
|| throwX( S() << "Error opening " << streamDescription << "." );
loadFrom( stream, frames, streamDescription, log );
}
} // namespace sounddata