ITAAudiofileWriter.cpp 4.53 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3 4 5 6 7 8
#include <ITAAudiofileWriter.h>

#include <ITAException.h>

#include <ITASampleBuffer.h>
#include <ITASampleFrame.h>
#include "libsndfileAudiofileWriter.h"

9 10 11
ITAAudiofileWriter* ITAAudiofileWriter::create( const std::string& sFilePath, const ITAAudiofileProperties& props )
{
	return new libsndfileAudiofileWriter( sFilePath, props );
Jonas Stienen's avatar
Jonas Stienen committed
12 13 14 15
}

ITAAudiofileWriter::~ITAAudiofileWriter() { };

16 17 18
void ITAAudiofileWriter::write( int iLength, std::vector< float* > vpfSource )
{
	write( iLength, *reinterpret_cast< std::vector< const float* >* >( &vpfSource ) );
Jonas Stienen's avatar
Jonas Stienen committed
19 20
}

21 22 23 24
void ITAAudiofileWriter::write( const ITASampleFrame* psfSource )
{
	if( !psfSource )
		ITA_EXCEPT1( INVALID_PARAMETER, "Nullpointer argument" );
Jonas Stienen's avatar
Jonas Stienen committed
25

26
	write( psfSource, psfSource->length(), 0 );
Jonas Stienen's avatar
Jonas Stienen committed
27 28
}

29 30 31 32
void ITAAudiofileWriter::write( const ITASampleFrame* psfSource, int iNumSamples, int iOffset )
{
	if( !psfSource )
		ITA_EXCEPT1( INVALID_PARAMETER, "Nullpointer argument" );
Jonas Stienen's avatar
Jonas Stienen committed
33

34 35
	if( ( iOffset + iNumSamples ) > psfSource->length() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Number of samples and offset exceed sample frame range" );
Jonas Stienen's avatar
Jonas Stienen committed
36 37

	// Datenzeiger aufsetzen
38 39 40
	std::vector< const float* > vpfData( psfSource->channels() );
	for( int i = 0; i < psfSource->channels(); i++ )
		vpfData[ i ] = ( *psfSource )[ i ].GetData() + iOffset;
Jonas Stienen's avatar
Jonas Stienen committed
41 42

	// Schreiben mit der Variante std::vector<const float*>
43
	write( iNumSamples, vpfData );
Jonas Stienen's avatar
Jonas Stienen committed
44 45
}

46 47 48 49 50 51 52 53 54
void writeAudiofile( const std::string& sFilePath, const ITAAudiofileProperties& props, std::vector< const float* >& vpfSource )
{
	ITAAudiofileWriter* pWriter = ITAAudiofileWriter::create( sFilePath, props );
	try
	{
		pWriter->write( props.iLength, vpfSource );
	}
	catch( ... )
	{
Jonas Stienen's avatar
Jonas Stienen committed
55 56 57 58 59 60
		delete pWriter;
		throw;
	}
	delete pWriter;
}

61 62 63
void writeAudiofile( const std::string& sFilePath, const ITAAudiofileProperties& props, std::vector<float*>& vpfSource )
{
	writeAudiofile( sFilePath, props, reinterpret_cast< std::vector<const float*>& >( vpfSource ) );
Jonas Stienen's avatar
Jonas Stienen committed
64 65
}

66
void writeAudiofile( std::string sFilename, const float* pfData, unsigned int uiLength, double dSampleRate, ITAQuantization eQuantization )
67
{
Jonas Stienen's avatar
Jonas Stienen committed
68
	ITAAudiofileProperties props;
69 70
	props.iChannels = 1;
	props.iLength = uiLength;
71
	props.dSampleRate = dSampleRate;
72
	props.eDomain = ITADomain::ITA_TIME_DOMAIN;
Jonas Stienen's avatar
Jonas Stienen committed
73 74 75
	props.eQuantization = eQuantization;
	props.sComment = "";

76 77
	std::vector<const float*> data( 1 );
	data[ 0 ] = pfData;
Jonas Stienen's avatar
Jonas Stienen committed
78

79
	ITAAudiofileWriter* writer = ITAAudiofileWriter::create( sFilename, props );
Jonas Stienen's avatar
Jonas Stienen committed
80
	try {
81 82 83
		writer->write( props.iLength, data );
	}
	catch( ... ) {
Jonas Stienen's avatar
Jonas Stienen committed
84 85 86 87 88 89
		delete writer;
		throw;
	}
	delete writer;
}

90
void writeAudiofile( const std::string& sFilePath, const float* pfDataCh0, const float* pfDataCh1, int iLength, double dSampleRate, ITAQuantization eQuantization )
91
{
Jonas Stienen's avatar
Jonas Stienen committed
92
	ITAAudiofileProperties props;
93 94
	props.iChannels = 2;
	props.iLength = iLength;
95
	props.dSampleRate = dSampleRate;
96
	props.eDomain = ITADomain::ITA_TIME_DOMAIN;
Jonas Stienen's avatar
Jonas Stienen committed
97 98 99
	props.eQuantization = eQuantization;
	props.sComment = "";

100 101 102
	std::vector<const float*> data( 2 );
	data[ 0 ] = pfDataCh0;
	data[ 1 ] = pfDataCh1;
Jonas Stienen's avatar
Jonas Stienen committed
103

104 105 106 107 108 109
	ITAAudiofileWriter* writer = ITAAudiofileWriter::create( sFilePath, props );
	try
	{
		writer->write( props.iLength, data );
	}
	catch( ... ) {
Jonas Stienen's avatar
Jonas Stienen committed
110 111 112 113 114 115
		delete writer;
		throw;
	}
	delete writer;
}

116
void writeAudiofile( const std::string& sFilePath, const ITASampleBuffer* psbData, double dSampleRate, ITAQuantization eQuantization )
117
{
Jonas Stienen's avatar
Jonas Stienen committed
118
	ITAAudiofileProperties props;
119 120
	props.iChannels = 1;
	props.iLength = ( unsigned int ) psbData->GetLength();
121
	props.dSampleRate = dSampleRate;
122
	props.eDomain = ITADomain::ITA_TIME_DOMAIN;
Jonas Stienen's avatar
Jonas Stienen committed
123 124 125
	props.eQuantization = eQuantization;
	props.sComment = "";

126 127
	std::vector<const float*> data( 1 );
	data[ 0 ] = psbData->GetData();
Jonas Stienen's avatar
Jonas Stienen committed
128

129 130 131 132 133 134 135
	ITAAudiofileWriter* writer = ITAAudiofileWriter::create( sFilePath, props );
	try
	{
		writer->write( props.iLength, data );
	}
	catch( ... )
	{
Jonas Stienen's avatar
Jonas Stienen committed
136 137 138 139 140 141
		delete writer;
		throw;
	}
	delete writer;
}

142
void writeAudiofile( const std::string& sFilePath, const ITASampleFrame* psfData, double dSampleRate, ITAQuantization eQuantization )
143
{
Jonas Stienen's avatar
Jonas Stienen committed
144
	ITAAudiofileProperties props;
145 146
	props.iChannels = ( unsigned int ) psfData->channels();
	props.iLength = ( unsigned int ) psfData->length();
147
	props.dSampleRate = dSampleRate;
148
	props.eDomain = ITADomain::ITA_TIME_DOMAIN;
Jonas Stienen's avatar
Jonas Stienen committed
149 150 151
	props.eQuantization = eQuantization;
	props.sComment = "";

152 153 154
	std::vector<const float*> data( psfData->channels() );
	for( int i = 0; i < psfData->channels(); i++ ) 
		data[ i ] = const_cast< float* >( ( *psfData )[ i ].GetData() );
Jonas Stienen's avatar
Jonas Stienen committed
155

156 157 158 159 160 161 162 163
	ITAAudiofileWriter* writer = ITAAudiofileWriter::create( sFilePath, props );

	try
	{
		writer->write( props.iLength, data );
	}
	catch( ... )
	{
Jonas Stienen's avatar
Jonas Stienen committed
164 165 166
		delete writer;
		throw;
	}
167

Jonas Stienen's avatar
Jonas Stienen committed
168 169
	delete writer;
}