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/BinauralReader.h"
18 #include "Exception.h"
19 
20 #include <cstring>
21 #include <cstdlib>
22 #include <algorithm>
23 
24 #define NUM_OUTCHANNELS 2
25 #define NUM_CONVOLVERS 4
26 #define CROSSFADE_SAMPLES 1024
27 
28 AUD_NAMESPACE_BEGIN
BinauralReader(std::shared_ptr<IReader> reader,std::shared_ptr<HRTF> hrtfs,std::shared_ptr<Source> source,std::shared_ptr<ThreadPool> threadPool,std::shared_ptr<FFTPlan> plan)29 BinauralReader::BinauralReader(std::shared_ptr<IReader> reader, std::shared_ptr<HRTF> hrtfs, std::shared_ptr<Source> source, std::shared_ptr<ThreadPool> threadPool, std::shared_ptr<FFTPlan> plan) :
30 	m_reader(reader), m_hrtfs(hrtfs), m_source(source), m_N(plan->getSize()), m_threadPool(threadPool), m_position(0), m_eosReader(false), m_eosTail(false), m_transition(false), m_transPos(CROSSFADE_SAMPLES*NUM_OUTCHANNELS)
31 {
32 	if(m_hrtfs->isEmpty())
33 		AUD_THROW(StateException, "The provided HRTF object is empty");
34 	if(m_reader->getSpecs().channels != 1)
35 		AUD_THROW(StateException, "The sound must have only one channel");
36 	if(m_reader->getSpecs().rate != m_hrtfs->getSpecs().rate)
37 		AUD_THROW(StateException, "The sound and the HRTFs must have the same rate");
38 	m_M = m_L = m_N / 2;
39 
40 	m_RealAzimuth = m_Azimuth = m_source->getAzimuth();
41 	m_RealElevation = m_Elevation = m_source->getElevation();
42 	auto irs = m_hrtfs->getImpulseResponse(m_RealAzimuth, m_RealElevation);
43 	for(unsigned int i = 0; i < NUM_CONVOLVERS; i++)
44 		if(i%NUM_OUTCHANNELS==0)
45 			m_convolvers.push_back(std::unique_ptr<Convolver>(new Convolver(irs.first->getChannel(0), irs.first->getLength(), m_threadPool, plan)));
46 		else
47 			m_convolvers.push_back(std::unique_ptr<Convolver>(new Convolver(irs.second->getChannel(0), irs.second->getLength(), m_threadPool, plan)));
48 	m_futures.resize(NUM_CONVOLVERS);
49 
50 	m_outBuffer = (sample_t*)std::malloc(m_L*NUM_OUTCHANNELS*sizeof(sample_t));
51 	m_eOutBufLen = m_outBufLen = m_outBufferPos = m_L * NUM_OUTCHANNELS;
52 	m_inBuffer = (sample_t*)std::malloc(m_L * sizeof(sample_t));
53 	for(int i = 0; i < NUM_CONVOLVERS; i++)
54 		m_vecOut.push_back((sample_t*)std::calloc(m_L, sizeof(sample_t)));
55 }
56 
~BinauralReader()57 BinauralReader::~BinauralReader()
58 {
59 	std::free(m_outBuffer);
60 	std::free(m_inBuffer);
61 	for(int i = 0; i < m_vecOut.size(); i++)
62 		std::free(m_vecOut[i]);
63 }
64 
isSeekable() const65 bool BinauralReader::isSeekable() const
66 {
67 	return m_reader->isSeekable();
68 }
69 
seek(int position)70 void BinauralReader::seek(int position)
71 {
72 	m_position = position;
73 	m_reader->seek(position);
74 	for(int i = 0; i < NUM_CONVOLVERS; i++)
75 		m_convolvers[i]->reset();
76 	m_eosTail = false;
77 	m_eosReader = false;
78 	m_outBufferPos = m_eOutBufLen = m_outBufLen;
79 	m_transition = false;
80 	m_transPos = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
81 }
82 
getLength() const83 int BinauralReader::getLength() const
84 {
85 	return m_reader->getLength();
86 }
87 
getPosition() const88 int BinauralReader::getPosition() const
89 {
90 	return m_position;
91 }
92 
getSpecs() const93 Specs BinauralReader::getSpecs() const
94 {
95 	Specs specs = m_reader->getSpecs();
96 	specs.channels = CHANNELS_STEREO;
97 	return specs;
98 }
99 
read(int & length,bool & eos,sample_t * buffer)100 void BinauralReader::read(int& length, bool& eos, sample_t* buffer)
101 {
102 	int samples = 0;
103 	int iteration = 0;
104 	if(length <= 0)
105 	{
106 		length = 0;
107 		eos = (m_eosTail && m_outBufferPos >= m_eOutBufLen);
108 		return;
109 	}
110 
111 	eos = false;
112 	int writePos = 0;
113 	do
114 	{
115 		int bufRest = m_eOutBufLen - m_outBufferPos;
116 		int writeLength = std::min((length*NUM_OUTCHANNELS) - writePos, m_eOutBufLen + bufRest);
117 		if(bufRest < writeLength || (m_eOutBufLen == 0 && m_eosTail))
118 		{
119 			if(bufRest > 0)
120 				std::memcpy(buffer + writePos, m_outBuffer + m_outBufferPos, bufRest*sizeof(sample_t));
121 			if(!m_eosTail)
122 			{
123 				int n = NUM_OUTCHANNELS;
124 				if(m_transition)
125 					n = NUM_CONVOLVERS;
126 				else if(checkSource())
127 					n = NUM_CONVOLVERS;
128 				loadBuffer(n);
129 
130 				int len = std::min(std::abs(writeLength - bufRest), m_eOutBufLen);
131 				std::memcpy(buffer + writePos + bufRest, m_outBuffer, len*sizeof(sample_t));
132 				samples += len;
133 				m_outBufferPos = len;
134 				writeLength = std::min((length*NUM_OUTCHANNELS) - writePos, m_eOutBufLen + bufRest);
135 			}
136 			else
137 			{
138 				m_outBufferPos += bufRest;
139 				length = (writePos+bufRest) / NUM_OUTCHANNELS;
140 				eos = true;
141 				return;
142 			}
143 		}
144 		else
145 		{
146 			std::memcpy(buffer + writePos, m_outBuffer + m_outBufferPos, writeLength*sizeof(sample_t));
147 			m_outBufferPos += writeLength;
148 		}
149 		writePos += writeLength;
150 		iteration++;
151 	} while(writePos < length*NUM_OUTCHANNELS);
152 	m_position += length;
153 }
154 
checkSource()155 bool BinauralReader::checkSource()
156 {
157 	if((m_Azimuth != m_source->getAzimuth() || m_Elevation != m_source->getElevation()) && (!m_eosReader && !m_eosTail))
158 	{
159 		float az = m_Azimuth = m_source->getAzimuth();
160 		float el = m_Elevation = m_source->getElevation();
161 		auto irs = m_hrtfs->getImpulseResponse(az, el);
162 		if(az != m_RealAzimuth || el != m_RealElevation)
163 		{
164 			m_RealAzimuth = az;
165 			m_RealElevation = el;
166 			for(int i = 0; i < NUM_OUTCHANNELS; i++)
167 			{
168 				auto temp = std::move(m_convolvers[i]);
169 				m_convolvers[i] = std::move(m_convolvers[i + NUM_OUTCHANNELS]);
170 				m_convolvers[i + NUM_OUTCHANNELS] = std::move(temp);
171 			}
172 			for(int i = 0; i < NUM_OUTCHANNELS; i++)
173 				if(i%NUM_OUTCHANNELS == 0)
174 					m_convolvers[i]->setImpulseResponse(irs.first->getChannel(0));
175 				else
176 					m_convolvers[i]->setImpulseResponse(irs.second->getChannel(0));
177 
178 			m_transPos = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
179 			m_transition = true;
180 			return true;
181 		}
182 	}
183 	return false;
184 }
185 
loadBuffer(int nConvolvers)186 void BinauralReader::loadBuffer(int nConvolvers)
187 {
188 	m_lastLengthIn = m_L;
189 	m_reader->read(m_lastLengthIn, m_eosReader, m_inBuffer);
190 	if(!m_eosReader || m_lastLengthIn > 0)
191 	{
192 		int len = m_lastLengthIn;
193 		for(int i = 0; i < nConvolvers; i++)
194 			m_futures[i] = m_threadPool->enqueue(&BinauralReader::threadFunction, this, i, true);
195 		for(int i = 0; i < nConvolvers; i++)
196 			len = m_futures[i].get();
197 
198 		joinByChannel(0, len, nConvolvers);
199 		m_eOutBufLen = len*NUM_OUTCHANNELS;
200 	}
201 	else if(!m_eosTail)
202 	{
203 		int len = m_lastLengthIn = m_L;
204 		for(int i = 0; i < nConvolvers; i++)
205 			m_futures[i] = m_threadPool->enqueue(&BinauralReader::threadFunction, this, i, false);
206 		for(int i = 0; i < nConvolvers; i++)
207 			len = m_futures[i].get();
208 
209 		joinByChannel(0, len, nConvolvers);
210 		m_eOutBufLen = len*NUM_OUTCHANNELS;
211 	}
212 }
213 
joinByChannel(int start,int len,int nConvolvers)214 void BinauralReader::joinByChannel(int start, int len, int nConvolvers)
215 {
216 	int k = 0;
217 	float vol = 0;
218 	const int l = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
219 	for(int i = 0; i < len*NUM_OUTCHANNELS; i += NUM_OUTCHANNELS)
220 	{
221 		if(m_transition)
222 		{
223 			vol = (m_transPos - i) / (float)l;
224 			if(vol > 1.0f)
225 				vol = 1.0f;
226 			else if(vol < 0.0f)
227 				vol = 0.0f;
228 		}
229 
230 		for(int j = 0; j < NUM_OUTCHANNELS; j++)
231 			m_outBuffer[i + j + start] = ((m_vecOut[j][k] * (1.0f - vol)) + (m_vecOut[j + NUM_OUTCHANNELS][k] * vol))*m_source->getVolume();
232 		k++;
233 	}
234 	if(m_transition)
235 	{
236 		m_transPos -= len*NUM_OUTCHANNELS;
237 		if(m_transPos <= 0)
238 		{
239 			m_transition = false;
240 			m_transPos = l;
241 		}
242 	}
243 }
244 
threadFunction(int id,bool input)245 int BinauralReader::threadFunction(int id, bool input)
246 {
247 	int l = m_lastLengthIn;
248 	if(input)
249 		m_convolvers[id]->getNext(m_inBuffer, m_vecOut[id], l, m_eosTail);
250 	else
251 		m_convolvers[id]->getNext(nullptr, m_vecOut[id], l, m_eosTail);
252 	return l;
253 }
254 
255 AUD_NAMESPACE_END
256