1 /*******************************************************************************
2 * Copyright 2015-2016 Juan Francisco Crespo Galán
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16
17 #include "fx/HRTFLoader.h"
18 #include "file/File.h"
19 #include "Exception.h"
20
21 #include <dirent.h>
22 #include <exception>
23
24 AUD_NAMESPACE_BEGIN
25
loadLeftHRTFs(std::shared_ptr<FFTPlan> plan,const std::string & fileExtension,const std::string & path)26 std::shared_ptr<HRTF> HRTFLoader::loadLeftHRTFs(std::shared_ptr<FFTPlan> plan, const std::string& fileExtension, const std::string& path)
27 {
28 std::shared_ptr<HRTF> hrtfs(std::make_shared<HRTF>(plan));
29 loadHRTFs(hrtfs, 'L', fileExtension, path);
30 return hrtfs;
31 }
32
loadRightHRTFs(std::shared_ptr<FFTPlan> plan,const std::string & fileExtension,const std::string & path)33 std::shared_ptr<HRTF> HRTFLoader::loadRightHRTFs(std::shared_ptr<FFTPlan> plan, const std::string& fileExtension, const std::string& path)
34 {
35 std::shared_ptr<HRTF> hrtfs(std::make_shared<HRTF>(plan));
36 loadHRTFs(hrtfs, 'R', fileExtension, path);
37 return hrtfs;
38 }
39
loadLeftHRTFs(const std::string & fileExtension,const std::string & path)40 std::shared_ptr<HRTF> HRTFLoader::loadLeftHRTFs(const std::string& fileExtension, const std::string& path)
41 {
42 std::shared_ptr<HRTF> hrtfs(std::make_shared<HRTF>());
43 loadHRTFs(hrtfs, 'L', fileExtension, path);
44 return hrtfs;
45 }
46
loadRightHRTFs(const std::string & fileExtension,const std::string & path)47 std::shared_ptr<HRTF> HRTFLoader::loadRightHRTFs(const std::string& fileExtension, const std::string& path)
48 {
49 std::shared_ptr<HRTF> hrtfs(std::make_shared<HRTF>());
50 loadHRTFs(hrtfs, 'R', fileExtension, path);
51 return hrtfs;
52 }
53
loadHRTFs(std::shared_ptr<HRTF> hrtfs,char ear,const std::string & fileExtension,const std::string & path)54 void HRTFLoader::loadHRTFs(std::shared_ptr<HRTF> hrtfs, char ear, const std::string& fileExtension, const std::string& path)
55 {
56 std::string readpath = path;
57 if(path == "")
58 readpath = ".";
59
60 DIR* dir = opendir(path.c_str());
61 if(!dir)
62 return;
63
64 float azim, elev;
65
66 while(dirent* entry = readdir(dir))
67 {
68 std::string filename = entry->d_name;
69 if(filename.front() == ear && filename.length() >= fileExtension.length() && filename.substr(filename.length() - fileExtension.length()) == fileExtension)
70 {
71 try
72 {
73 elev = std::stof(filename.substr(1, filename.find("e") - 1));
74 azim = std::stof(filename.substr(filename.find("e") + 1, filename.find("a") - filename.find("e") - 1));
75 if(ear == 'L')
76 azim = 360 - azim;
77 }
78 catch(std::exception& e)
79 {
80 AUD_THROW(FileException, "The HRTF name doesn't follow the naming scheme: " + filename);
81 }
82 hrtfs->addImpulseResponse(std::make_shared<StreamBuffer>(std::make_shared<File>(readpath + "/" + filename)), azim, elev);
83 }
84 }
85 closedir(dir);
86 return;
87 }
88
89 AUD_NAMESPACE_END