ITAFileDataSource.cpp 1.66 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
#include "ITAFileDataSource.h"
#include <ITAAudiofileReader.h>

#ifndef WIN32
#include <memory.h>
#endif

ITAFileDatasource::ITAFileDatasource(std::string sFilename,
							        unsigned int uiBlocklength,
							        bool bLoopMode)
: ITABufferDatasource()
{
	m_sFilename = sFilename;
	m_uiFileCapacity = 0;

	assert(uiBlocklength > 0);

	// Audiodatei vollstndig in den Speicher laden
	// TODO: Ausnahme auslsen, falls Spektraldaten!
	ITAAudiofileReader* pReader = ITAAudiofileReader::create(sFilename);
	ITAAudiofileProperties props = pReader->getAudiofileProperties();
22
	m_uiFileCapacity = props.iLength;
Jonas Stienen's avatar
Jonas Stienen committed
23 24 25 26 27 28 29 30

	if (m_uiFileCapacity > 0) {
		// Lnge auf Vielfaches der Blocklnge erweitern
		unsigned int k = (m_uiFileCapacity / uiBlocklength);
		if (m_uiFileCapacity %  uiBlocklength) k++;
		unsigned int uiCapacity = k * uiBlocklength;

		// Datenpuffer allozieren und Daten hinein kopieren
31
		for ( int i=0; i<props.iChannels; i++) {
Jonas Stienen's avatar
Jonas Stienen committed
32 33 34 35 36 37 38 39 40
			// TODO: Fehlerbehandlung!
			float* pfData = new float[uiCapacity];

			// Den letzten Block mit Nullen vorinitialisieren, falls Capacity > FileCapacity
			memset(pfData + (k-1)*uiBlocklength, 0, uiBlocklength * sizeof(float));

			vpfData.push_back(pfData);
		}

41
		pReader->read(props.iLength, vpfData);
Jonas Stienen's avatar
Jonas Stienen committed
42 43

		// Unterliegende Puffer-Datenquelle initialisieren
44
		Init(vpfData, uiCapacity, props.dSampleRate, uiBlocklength, bLoopMode);
Jonas Stienen's avatar
Jonas Stienen committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	}

	delete pReader;
}

ITAFileDatasource::~ITAFileDatasource() {
	// Puffer freigeben
	for (unsigned int i=0; i<vpfData.size(); i++) delete[] vpfData[i];
}

unsigned int ITAFileDatasource::GetFileCapacity() const { return m_uiFileCapacity; }
std::string ITAFileDatasource::GetFileName() const { return m_sFilename; }