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 <windows.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 	WIN32_FIND_DATA entry;
61 	bool found_file = true;
62 	std::string search = readpath + "\\*";
63 	HANDLE dir = FindFirstFile(search.c_str(), &entry);
64 	if(dir == INVALID_HANDLE_VALUE)
65 		return;
66 
67 	float azim, elev;
68 
69 	while(found_file)
70 	{
71 		std::string filename = entry.cFileName;
72 		if(filename.front() == ear && filename.length() >= fileExtension.length() && filename.substr(filename.length() - fileExtension.length()) == fileExtension)
73 		{
74 			try
75 			{
76 				elev = std::stof(filename.substr(1, filename.find("e") - 1));
77 				azim = std::stof(filename.substr(filename.find("e") + 1, filename.find("a") - filename.find("e") - 1));
78 				if(ear == 'L')
79 					azim = 360 - azim;
80 			}
81 			catch(std::exception& e)
82 			{
83 				AUD_THROW(FileException, "The HRTF name doesn't follow the naming scheme: " + filename);
84 			}
85 			hrtfs->addImpulseResponse(std::make_shared<StreamBuffer>(std::make_shared<File>(readpath + "/" + filename)), azim, elev);
86 		}
87 		found_file = FindNextFile(dir, &entry);
88 	}
89 	FindClose(dir);
90 	return;
91 }
92 
93 AUD_NAMESPACE_END