ITASampleFrame.cpp 14.2 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2
#include <ITASampleFrame.h>
#include <ITAAudiofileReader.h>
3
#include <ITAAudiofileWriter.h>
Jonas Stienen's avatar
Jonas Stienen committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#include <assert.h>
#include <sstream>

#include <ITAException.h>

ITASampleFrame::ITASampleFrame()
: m_iChannels(0), m_iLength(0) {}

ITASampleFrame::ITASampleFrame(int iChannels, int iLength, bool bZeroinit)
: m_iChannels(0), m_iLength(0)
{
	init( iChannels, iLength, bZeroinit);
}


ITASampleFrame::ITASampleFrame(const ITASampleFrame* pSource)
: m_iChannels(0), m_iLength(0)
{
	*this = *pSource;
}

ITASampleFrame::ITASampleFrame(const ITASampleFrame& sbSource)
: m_iChannels(0), m_iLength(0)
{
	*this = sbSource;
}

ITASampleFrame::ITASampleFrame( const std::string& sFileName )
{
	Load( sFileName );
}

37 38
ITASampleFrame::~ITASampleFrame()
{
Jonas Stienen's avatar
Jonas Stienen committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}

bool ITASampleFrame::empty() const {
	return ((m_iChannels == 0) || (m_iLength == 0));
}

int ITASampleFrame::channels() const {
	return m_iChannels;
}

int ITASampleFrame::length() const {
	return m_iLength;
}

void ITASampleFrame::init( int iChannels, int iLength, bool bZeroinit)
{
	assert( iChannels >= 0 );
	assert( iLength >= 0 );

	// TODO: Mit ein wenig cleverness knnte dies ohne Vollstndiges Abrumen gehen.
	//       Die Frage ist: Wer's braucht?
	if ((m_iChannels > 0) || (m_iLength > 0)) free();

	m_vChannels.resize(iChannels);
	for (int i=0; i<iChannels; i++) {
		m_vChannels[i].m_pParent = NULL;
		m_vChannels[i].Init( iLength, bZeroinit);
		m_vChannels[i].m_pParent = this;
	}

	m_iChannels = iChannels;
	m_iLength = iLength;
}

73 74 75 76 77 78 79
void ITASampleFrame::Load( const std::string& sFilePath )
{
	double dSampleRate;
	Load( sFilePath, dSampleRate );
}

void ITASampleFrame::Load( const std::string& sFilePath, double& dSampleRate )
Jonas Stienen's avatar
Jonas Stienen committed
80
{
81
#ifdef ITABASE_WITH_SNDFILE
Jonas Stienen's avatar
Jonas Stienen committed
82
	ITAAudiofileReader* pReader( NULL );
83 84
	try
	{
Jonas Stienen's avatar
Jonas Stienen committed
85
		// Datei ffnen
86
		pReader = ITAAudiofileReader::create( sFilePath );
Jonas Stienen's avatar
Jonas Stienen committed
87
		ITAAudiofileProperties oProps = pReader->getAudiofileProperties();
88
		dSampleRate = oProps.dSampleRate;
89
		init( ( int ) oProps.iChannels, ( int ) oProps.iLength, false );
Jonas Stienen's avatar
Jonas Stienen committed
90 91

		// Zielzeiger zusammenstellen
92 93
		std::vector<float*> vpfDest( oProps.iChannels );
		for( int i = 0; i < oProps.iChannels; i++ )
Jonas Stienen's avatar
Jonas Stienen committed
94 95 96
			vpfDest[ i ] = m_vChannels[ i ].GetData();

		// Daten einlesen
97
		pReader->read( oProps.iLength, vpfDest );
Jonas Stienen's avatar
Jonas Stienen committed
98 99

	}
100 101
	catch( ... )
	{
Jonas Stienen's avatar
Jonas Stienen committed
102 103 104 105 106 107 108
		// Alle Daten verwerfen
		delete pReader;
		free();
		throw;
	}

	delete pReader;
109 110 111
#else
	ITA_EXCEPT1( NOT_IMPLEMENTED, "ITASampleFrame::Store() function not available without libsndfile" );
#endif
Jonas Stienen's avatar
Jonas Stienen committed
112 113
}

114 115 116 117 118 119
void ITASampleFrame::Store( const std::string& sFilePath, double dSamplingRate ) const
{
#ifdef ITABASE_WITH_SNDFILE
	ITAAudiofileWriter* pWriter( NULL );
	try
	{
Jonas Stienen's avatar
Jonas Stienen committed
120
		ITAAudiofileProperties oProps;
121 122 123 124 125 126
		oProps.dSampleRate = dSamplingRate;
		oProps.eDomain = ITADomain::ITA_TIME_DOMAIN;
		oProps.eQuantization = ITAQuantization::ITA_FLOAT;
		oProps.iChannels = (unsigned int) m_iChannels;
		oProps.iLength = (unsigned int) m_iLength;
		pWriter = ITAAudiofileWriter::create( sFilePath, oProps );
Jonas Stienen's avatar
Jonas Stienen committed
127
	
128 129 130
		std::vector< const float* > vpfDest( oProps.iChannels );
		for( int i = 0; i < oProps.iChannels; i++ )
			vpfDest[ i ] = m_vChannels[ i ].data();
Jonas Stienen's avatar
Jonas Stienen committed
131

132
		pWriter->write( oProps.iLength, vpfDest );
Jonas Stienen's avatar
Jonas Stienen committed
133

134 135 136
	}
	catch (...)
	{
Jonas Stienen's avatar
Jonas Stienen committed
137 138 139 140 141
		delete pWriter;
		throw;
	}

	delete pWriter;
142 143 144 145
#else
	ITA_EXCEPT1( NOT_IMPLEMENTED, "ITASampleFrame::Store() function not available without libsndfile" );
#endif

Jonas Stienen's avatar
Jonas Stienen committed
146 147
}

148 149 150 151 152 153 154
void ITASampleFrame::free()
{
	m_vChannels.clear();
	m_iChannels = 0;
	m_iLength = 0;
};

155 156
void ITASampleFrame::fill( float fValue )
{
Jonas Stienen's avatar
Jonas Stienen committed
157 158 159 160 161 162 163 164 165 166 167 168 169
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->Fill(fValue);
}

void ITASampleFrame::fill(int iOffset, int iCount, float fValue) {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->Fill(iOffset, iCount, fValue);	
}

void ITASampleFrame::zero() {
	fill(0);
}

170 171 172
void ITASampleFrame::zero(int iOffset, int iCount )
{
	fill(iOffset, iCount, 0.0f);
Jonas Stienen's avatar
Jonas Stienen committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
}

void ITASampleFrame::identity() {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->Identity();	
}

void ITASampleFrame::fade(int iOffset, int iCount, int iFadeDirection, int iFadeFunction) {
	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].Fade( iOffset, iCount, iFadeDirection, iFadeFunction );
}

void ITASampleFrame::crossfade(const ITASampleFrame* psfSrc, int iOffset, int iCount, int iFadeDirection, int iFadeFunction) {
	if (!psfSrc) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSrc->channels() != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");

	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].Crossfade( (*psfSrc)[i], iOffset, iCount, iFadeDirection, iFadeFunction );
}

void ITASampleFrame::crossfade(const ITASampleFrame& sfSrc, int iOffset, int iCount, int iFadeDirection, int iFadeFunction) {
	crossfade( &sfSrc, iOffset, iCount, iFadeDirection, iFadeFunction );
}

void ITASampleFrame::envelope(float fGain0, float fGain1) {
	if (empty()) return;

	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].Envelope( fGain0, fGain1 );
}

void ITASampleFrame::write(const ITASampleFrame* psfSrc, int iCount, int iSrcOffset, int iDestOffset) {
	if (!psfSrc) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSrc->channels() != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");

	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].write( (*psfSrc)[i], iCount, iSrcOffset, iDestOffset );
}

void ITASampleFrame::write(const ITASampleFrame& sfSrc, int iCount, int iSrcOffset, int iDestOffset) {
	write(&sfSrc, iCount, iSrcOffset, iDestOffset);
}

void ITASampleFrame::cyclic_write(const ITASampleFrame* psfSrc, int iCount, int iSrcOffset, int iDestOffset) {
	if (!psfSrc) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSrc->channels() != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");

	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].cyclic_write( &((*psfSrc)[i]), iCount, iSrcOffset, iDestOffset );
}

void ITASampleFrame::cyclic_write(const ITASampleFrame& sfSrc, int iCount, int iSrcOffset, int iDestOffset) {
	cyclic_write(&sfSrc, iCount, iSrcOffset, iDestOffset);
}

void ITASampleFrame::CyclicShift( int iCount)
{
	for (int i = 0; i < m_iChannels; i++)
		m_vChannels[i].CyclicShift(iCount);
}

void ITASampleFrame::add_scalar(float fValue) {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->add_scalar(fValue);
}

void ITASampleFrame::sub_scalar(float fValue) {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->sub_scalar(fValue);
}

void ITASampleFrame::mul_scalar(float fValue) {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->mul_scalar(fValue);
}

void ITASampleFrame::div_scalar(float fValue) {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->div_scalar(fValue);
}

void ITASampleFrame::add_buf(const ITASampleBuffer* psbSource) {
	if (!psbSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psbSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->add_buf(psbSource);
}

void ITASampleFrame::sub_buf(const ITASampleBuffer* psbSource) {
	if (!psbSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psbSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->sub_buf(psbSource);
}

void ITASampleFrame::mul_buf(const ITASampleBuffer* psbSource) {
	if (!psbSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psbSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->sub_buf(psbSource);
}

void ITASampleFrame::div_buf(const ITASampleBuffer* psbSource) {
	if (!psbSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psbSource->GetLength() != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->div_buf(psbSource);
}

void ITASampleFrame::add_buf(const ITASampleBuffer& sbSource) { add_buf(&sbSource); }
void ITASampleFrame::sub_buf(const ITASampleBuffer& sbSource) { sub_buf(&sbSource); }
void ITASampleFrame::mul_buf(const ITASampleBuffer& sbSource) { mul_buf(&sbSource); }
void ITASampleFrame::div_buf(const ITASampleBuffer& sbSource) { div_buf(&sbSource); }

void ITASampleFrame::add_frame(const ITASampleFrame* psfSource) {
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");
	if (psfSource->m_iLength != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (int i=0; i<m_iChannels; ++i)
		m_vChannels[i].add_buf( &(psfSource->m_vChannels[i]) );
}

void ITASampleFrame::add_frame(const ITASampleFrame* psfSource, int iPos)
{
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");
	if ((psfSource->m_iLength+iPos) >= m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Source length + delay exceed length of filter");
	
	for (int i=0; i<m_iChannels; ++i)
		m_vChannels[i].add_buf_pos(&(psfSource->m_vChannels[i]),iPos);
}



void ITASampleFrame::sub_frame(const ITASampleFrame* psfSource) {
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");
	if (psfSource->m_iLength != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (int i=0; i<m_iChannels; ++i)
		m_vChannels[i].sub_buf( &(psfSource->m_vChannels[i]) );
}

void ITASampleFrame::mul_frame(const ITASampleFrame* psfSource) {
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");
	if (psfSource->m_iLength != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (int i=0; i<m_iChannels; ++i)
		m_vChannels[i].mul_buf( &(psfSource->m_vChannels[i]) );
}

void ITASampleFrame::div_frame(const ITASampleFrame* psfSource) {
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");
	if (psfSource->m_iLength != m_iLength)  ITA_EXCEPT1(INVALID_PARAMETER, "Lengths do not match");

	for (int i=0; i<m_iChannels; ++i)
		m_vChannels[i].div_buf( &(psfSource->m_vChannels[i]) );
}

void ITASampleFrame::add_frame(const ITASampleFrame& sfSource) { add_frame(&sfSource); }
void ITASampleFrame::sub_frame(const ITASampleFrame& sfSource) { sub_frame(&sfSource); }
void ITASampleFrame::mul_frame(const ITASampleFrame& sfSource) { mul_frame(&sfSource); }
void ITASampleFrame::div_frame(const ITASampleFrame& sfSource) { div_frame(&sfSource); }

void ITASampleFrame::muladd_frame(const ITASampleFrame* psfSource, float fScalar, int iSrcOffset, int iDestOffset, int iCount) {
	if (!psfSource) ITA_EXCEPT1(INVALID_PARAMETER, "Nullpointer passed");
	if (psfSource->m_iChannels != m_iChannels)  ITA_EXCEPT1(INVALID_PARAMETER, "Number of channels do not match");

	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].MulAdd( (*psfSource)[i], fScalar, iSrcOffset, iDestOffset, iCount );
}

void ITASampleFrame::muladd_frame(const ITASampleFrame& sfSource, float fScalar, int iSrcOffset, int iDestOffset, int iCount) {
	muladd_frame( &sfSource, fScalar, iSrcOffset, iDestOffset, iCount );
}

356 357 358 359 360 361 362 363 364
float ITASampleFrame::findPeak(int* piChannel, int* piPeakIndex)
{
	if ((m_iChannels == 0) || (m_iLength == 0))
	{
		if (piChannel)
			*piChannel = 0;
		if (piPeakIndex)
			*piPeakIndex = 0;

Jonas Stienen's avatar
Jonas Stienen committed
365 366 367 368 369
		return 0;
	}

	float fPeak = 0;
	int iChannel = 0;
370
	int iPeakIndex = 0;
Jonas Stienen's avatar
Jonas Stienen committed
371

372 373
	for( int i = 0; i < m_iChannels; i++ )
	{
Jonas Stienen's avatar
Jonas Stienen committed
374
		int k;
375 376 377
		float x = m_vChannels[ i ].FindPeak( &k );
		if( x > fPeak )
		{
Jonas Stienen's avatar
Jonas Stienen committed
378 379 380 381 382 383
			fPeak = x;
			iChannel = i;
			iPeakIndex = k;
		}
	}

384 385 386 387 388
	if (piChannel)
		*piChannel = iChannel;

	if (piPeakIndex)
		*piPeakIndex = iPeakIndex;
Jonas Stienen's avatar
Jonas Stienen committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

	return fPeak;
}

void ITASampleFrame::negate() {
	for (ch_it it=m_vChannels.begin(); it!=m_vChannels.end(); ++it)
		it->Negate();
};

ITASampleBuffer& ITASampleFrame::operator[](int iChannel) {
	return m_vChannels[iChannel];
}

const ITASampleBuffer& ITASampleFrame::operator[](int iChannel) const {
	return m_vChannels[iChannel];
}

406 407 408 409 410
ITASampleFrame& ITASampleFrame::operator=( const ITASampleFrame& rhs )
{
	// No self assignment
	if( &rhs == this )
		return *this;
Jonas Stienen's avatar
Jonas Stienen committed
411

412
	init( rhs.channels(), rhs.length(), false );
Jonas Stienen's avatar
Jonas Stienen committed
413

414 415
	for( int i = 0; i < m_iChannels; i++ )
		( *this )[ i ].write( rhs[ i ], this->GetLength() );
Jonas Stienen's avatar
Jonas Stienen committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

	return *this;
}

ITASampleFrame& ITASampleFrame::operator+=(const float rhs) {
	add_scalar(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator-=(const float rhs) {
	sub_scalar(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator*=(const float rhs) {
	mul_scalar(rhs);
	return *this;

}

ITASampleFrame& ITASampleFrame::operator/=(const float rhs) {
	div_scalar(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator+=(const ITASampleBuffer& rhs) {
	add_buf(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator-=(const ITASampleBuffer& rhs) {
	sub_buf(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator*=(const ITASampleBuffer& rhs) {
	mul_buf(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator/=(const ITASampleBuffer& rhs) {
	div_buf(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator+=(const ITASampleFrame& rhs) {
	add_frame(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator-=(const ITASampleFrame& rhs) {
	sub_frame(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator*=(const ITASampleFrame& rhs) {
	mul_frame(rhs);
	return *this;
}

ITASampleFrame& ITASampleFrame::operator/=(const ITASampleFrame& rhs) {
	div_frame(rhs);
	return *this;
}

std::string ITASampleFrame::toString() const {
	std::stringstream ss;
	ss << "Sample frame { ";
	if (empty())
		ss << "emtpy";
	else
		ss << m_iChannels << " channels of " << m_iLength << " samples";
	ss << " }";
	return ss.str();
}

float ITASampleFrame::normalize()
{
	int c,s;
	findPeak( &c, &s );
	float fP = m_vChannels[c][s];
	for (int i=0; i<m_iChannels; i++)
		m_vChannels[i].div_scalar( fP );

	return fP;
}