Created
August 31, 2016 12:07
-
-
Save vanne02135/85848dbeb76c9092fefbfae74959458b to your computer and use it in GitHub Desktop.
Reading raw impulse responses and direction from sofa file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef OLD_HEADER_FILENAME | |
#include <iostream.h> | |
#else | |
#include <iostream> | |
#endif | |
#include <string> | |
#include <vector> | |
#ifndef H5_NO_NAMESPACE | |
#ifndef H5_NO_STD | |
using std::cout; | |
using std::endl; | |
#endif // H5_NO_STD | |
#endif | |
#include "H5Cpp.h" | |
#ifndef H5_NO_NAMESPACE | |
using namespace H5; | |
#endif | |
std::vector<float> getDirections(const char* filename) { | |
// Read SOFA file and | |
// return Ndirections x 3 vector, which has azimuth, elevation and distance | |
// as in sofa convention | |
H5File file( filename, H5F_ACC_RDONLY ); | |
DataSet SourcePosition = file.openDataSet( "SourcePosition" ); | |
if (SourcePosition.getTypeClass() != H5T_FLOAT) | |
throw Exception("We expect float SourcePosition values"); | |
hsize_t spDims[2]; | |
SourcePosition.getSpace().getSimpleExtentDims(spDims, NULL); | |
cout << "Source position array: " << spDims[0] << " x "; | |
cout << spDims[1] << endl; | |
auto spArray = std::vector<float> (spDims[0] * spDims[1]); | |
SourcePosition.read( (void*) &(spArray[0]), H5::PredType::NATIVE_FLOAT); | |
file.close(); | |
return spArray; | |
} | |
std::vector<float> getDataIR(const char *filename) { | |
// Read SOFA file and | |
// return Ndirections x 2 x IRlen vector, which has impulse responses | |
// as in sofa convention | |
H5File file( filename, H5F_ACC_RDONLY ); | |
DataSet hrir = file.openDataSet( "Data.IR" ); | |
if (hrir.getTypeClass() != H5T_FLOAT) | |
throw Exception("We expect float IRs"); | |
hsize_t dims_out[3]; | |
int ndims = hrir.getSpace().getSimpleExtentDims( dims_out, NULL); | |
auto hrirArray = std::vector<float> (dims_out[0] * dims_out[1] * dims_out[2]); | |
hrir.read( (void*) &(hrirArray[0]), H5::PredType::NATIVE_FLOAT ); | |
file.close(); | |
return hrirArray; | |
} | |
std::vector<float> getIR(const std::vector<float> &DataIR, const int Ndirections, const int directionIndex, const int earIndex) { | |
// earIndex = 0 (left) or 1 (right) | |
auto IRlen = DataIR.size() / Ndirections / 2; | |
auto index = directionIndex * 2 * IRlen + earIndex * IRlen; | |
auto IR = std::vector<float>(&(DataIR[index]), &(DataIR[index+IRlen])); | |
return IR; | |
} | |
int main (int argc, char **argv) | |
{ | |
/* | |
* Try block to detect exceptions raised by any of the calls inside it | |
*/ | |
try | |
{ | |
/* | |
* Turn off the auto-printing when failure occurs so that we can | |
* handle the errors appropriately | |
*/ | |
Exception::dontPrint(); | |
auto spArray = getDirections(argv[1]); | |
auto Ndirections = spArray.size() / 3; | |
for (int i = 0; i < Ndirections; i++) { | |
cout << spArray[i*3 + 0] << " " << spArray[i*3 + 1] << " " << spArray[i*3 + 2] << endl; | |
} | |
auto DataIR = getDataIR(argv[1]); | |
auto IRlen = DataIR.size() / Ndirections / 2; | |
cout << "Impulse response length = " << IRlen << endl; | |
auto myIR_left = getIR(DataIR, Ndirections, 433, 0); | |
auto myIR_right = getIR(DataIR, Ndirections, 433, 1); | |
for (int i = 0; i < IRlen; i++) { | |
//cout << myIR_left[i] << endl; | |
} | |
} // end of try block | |
// catch failure caused by the H5File operations | |
catch( FileIException error ) | |
{ | |
error.printError(); | |
return -1; | |
} | |
// catch failure caused by the DataSet operations | |
catch( DataSetIException error ) | |
{ | |
error.printError(); | |
return -1; | |
} | |
// catch failure caused by the DataSpace operations | |
catch( DataSpaceIException error ) | |
{ | |
error.printError(); | |
return -1; | |
} | |
// catch failure caused by the DataSpace operations | |
catch( DataTypeIException error ) | |
{ | |
error.printError(); | |
return -1; | |
} | |
catch (Exception e) { | |
e.printError(); | |
} | |
return 0; // successfully terminated | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment