ITASoundSampleImpl.cpp 3.22 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 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
/*
 *      +------------------+
 *  --->| ITA Sampler      |--->
 *  --->| o o o o      /// |--->
 *      +------------------+
 *
 *  ITASampler ist eine C++ Bibliothek die einen Sound sampler zur wavetable-basierten
 *  Klangsynthese bereitstellt. Der Begriff "sampler" bezieht sich hier auf den Terminus
 *  aus der Musikproduktion und nicht auf Abtastwerte.
 *
 *  Datei:		ITASoundSamplerImpl.cpp
 *  Zweck:		Implementierungsklasse des sound samplers
 *  Autoren:	Frank Wefers (Frank.Wefers@akustik.rwth-aachen.de)
 *  CVS-Datum:	$Id: ITASoundSampleImpl.cpp,v 1.1 2008-12-10 14:15:03 fwefers Exp $
 *
 *  (c) Copyright Institut fr Technische Akustik (ITA) RWTH Aachen, 2008
 */
#include "ITASoundSampleImpl.h"

#include <ITAAudiofileReader.h>
#include <ITAException.h>
#include <ITAFunctors.h>

#include <algorithm>

ITASoundSampleImpl::ITASoundSampleImpl(const std::string& sFilename, int iMaxNumberOfChannels, double dRequiredSamplerate, std::string sName)
: m_sName(sName), m_sFilename(sFilename), m_iLength(0), m_iPlaybackRefCount(0), m_bOriginFile(true) {
	ITAAudiofileReader* pReader = ITAAudiofileReader::create(sFilename);

30
	if (pReader->getDomain() != ITADomain::ITA_TIME_DOMAIN) {
Jonas Stienen's avatar
Jonas Stienen committed
31 32 33 34
		delete pReader;
		ITA_EXCEPT1(INVALID_PARAMETER, "Audiofile is not in time domain");
	}

35
	if (pReader->getNumberOfChannels() > iMaxNumberOfChannels) {
Jonas Stienen's avatar
Jonas Stienen committed
36 37 38 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		delete pReader;
		ITA_EXCEPT1(INVALID_PARAMETER, "Audiofile has too much channels");
	}

	if (pReader->getSamplerate() != dRequiredSamplerate) {
		delete pReader;
		ITA_EXCEPT1(INVALID_PARAMETER, "Audiofile has invalid sampling rate");
	}

	m_iLength = (signed) pReader->getLength();
	m_vpData = pReader->read(pReader->getLength());
	
	delete pReader;
}

ITASoundSampleImpl::ITASoundSampleImpl(const float** ppfChannelData, int iNumberOfChannels, int iLength, std::string sName)
: m_sName(sName), m_iLength(iLength), m_iPlaybackRefCount(0), m_bOriginFile(false) {
	
	// Kopie der Daten erzeugen
	m_vpData.resize(iNumberOfChannels, NULL);
	for (int i=0; i<iNumberOfChannels; i++) {
		m_vpData[i] = new float[iLength];
		memcpy(m_vpData[i], ppfChannelData[i], iLength*sizeof(float));
	}
}

ITASoundSampleImpl::~ITASoundSampleImpl() {
	std::for_each(m_vpData.begin(), m_vpData.end(), arrayDeleteFunctor<float>);
}

bool ITASoundSampleImpl::OriginFile() const {
	return m_bOriginFile;
}

std::string ITASoundSampleImpl::GetName() const {
	return m_sName;
}

void ITASoundSampleImpl::SetName(const std::string& sName) {
	m_sName = sName;
}

std::string ITASoundSampleImpl::GetFilename() const {
	return m_sFilename;
}

int ITASoundSampleImpl::GetNumberOfChannels() const {
	return (int) m_vpData.size();
}

int ITASoundSampleImpl::GetLength() const {
	return m_iLength;
}

const float* ITASoundSampleImpl::GetChannelData(int iChannel) const {
	return m_vpData[iChannel];
}

bool ITASoundSampleImpl::HasPlaybackReferences() {
	m_csPlaybackRefCount.enter();
	bool bResult = (m_iPlaybackRefCount > 0);
	m_csPlaybackRefCount.leave();
	return bResult;
}

void ITASoundSampleImpl::AddPlaybackReference() {
	m_csPlaybackRefCount.enter();
	m_iPlaybackRefCount++;
	m_csPlaybackRefCount.leave();
}

void ITASoundSampleImpl::RemovePlaybackReference() {
	m_csPlaybackRefCount.enter();
	if (m_iPlaybackRefCount>0) m_iPlaybackRefCount--;
	m_csPlaybackRefCount.leave();
}