Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BinauralReader.cpp « fx « src « audaspace « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2792adada8ad3ae7f8fc328a01bfd58b96ca936e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
* Copyright 2015-2016 Juan Francisco Crespo Galán
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

#include "fx/BinauralReader.h"
#include "Exception.h"

#include <cstring>
#include <cstdlib>
#include <algorithm>

#define NUM_OUTCHANNELS 2
#define NUM_CONVOLVERS 4
#define CROSSFADE_SAMPLES 1024

AUD_NAMESPACE_BEGIN
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) :
	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)
{
	if(m_hrtfs->isEmpty())
		AUD_THROW(StateException, "The provided HRTF object is empty");
	if(m_reader->getSpecs().channels != 1) 
		AUD_THROW(StateException, "The sound must have only one channel");
	if(m_reader->getSpecs().rate != m_hrtfs->getSpecs().rate)
		AUD_THROW(StateException, "The sound and the HRTFs must have the same rate");
	m_M = m_L = m_N / 2;
	
	m_RealAzimuth = m_Azimuth = m_source->getAzimuth();
	m_RealElevation = m_Elevation = m_source->getElevation();
	auto irs = m_hrtfs->getImpulseResponse(m_RealAzimuth, m_RealElevation);
	for(unsigned int i = 0; i < NUM_CONVOLVERS; i++)
		if(i%NUM_OUTCHANNELS==0)
			m_convolvers.push_back(std::unique_ptr<Convolver>(new Convolver(irs.first->getChannel(0), irs.first->getLength(), m_threadPool, plan)));
		else
			m_convolvers.push_back(std::unique_ptr<Convolver>(new Convolver(irs.second->getChannel(0), irs.second->getLength(), m_threadPool, plan)));
	m_futures.resize(NUM_CONVOLVERS);

	m_outBuffer = (sample_t*)std::malloc(m_L*NUM_OUTCHANNELS*sizeof(sample_t));
	m_eOutBufLen = m_outBufLen = m_outBufferPos = m_L * NUM_OUTCHANNELS;
	m_inBuffer = (sample_t*)std::malloc(m_L * sizeof(sample_t));
	for(int i = 0; i < NUM_CONVOLVERS; i++)
		m_vecOut.push_back((sample_t*)std::calloc(m_L, sizeof(sample_t)));
}

BinauralReader::~BinauralReader()
{
	std::free(m_outBuffer);
	std::free(m_inBuffer);
	for(int i = 0; i < m_vecOut.size(); i++)
		std::free(m_vecOut[i]);
}

bool BinauralReader::isSeekable() const
{
	return m_reader->isSeekable();
}

void BinauralReader::seek(int position)
{
	m_position = position;
	m_reader->seek(position);
	for(int i = 0; i < NUM_CONVOLVERS; i++)
		m_convolvers[i]->reset();
	m_eosTail = false;
	m_eosReader = false;
	m_outBufferPos = m_eOutBufLen = m_outBufLen;
	m_transition = false;
	m_transPos = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
}

int BinauralReader::getLength() const
{
	return m_reader->getLength();
}

int BinauralReader::getPosition() const
{
	return m_position;
}

Specs BinauralReader::getSpecs() const
{
	Specs specs = m_reader->getSpecs();
	specs.channels = CHANNELS_STEREO;
	return specs;
}

void BinauralReader::read(int& length, bool& eos, sample_t* buffer)
{
	int samples = 0;
	int iteration = 0;
	if(length <= 0)
	{
		length = 0;
		eos = (m_eosTail && m_outBufferPos >= m_eOutBufLen);
		return;
	}

	eos = false;
	int writePos = 0;
	do
	{
		int bufRest = m_eOutBufLen - m_outBufferPos;
		int writeLength = std::min((length*NUM_OUTCHANNELS) - writePos, m_eOutBufLen + bufRest);
		if(bufRest < writeLength || (m_eOutBufLen == 0 && m_eosTail))
		{
			if(bufRest > 0)
				std::memcpy(buffer + writePos, m_outBuffer + m_outBufferPos, bufRest*sizeof(sample_t));
			if(!m_eosTail)
			{
				int n = NUM_OUTCHANNELS;
				if(m_transition)
					n = NUM_CONVOLVERS;
				else if(checkSource())
					n = NUM_CONVOLVERS;
				loadBuffer(n);

				int len = std::min(std::abs(writeLength - bufRest), m_eOutBufLen);
				std::memcpy(buffer + writePos + bufRest, m_outBuffer, len*sizeof(sample_t));
				samples += len;
				m_outBufferPos = len;
				writeLength = std::min((length*NUM_OUTCHANNELS) - writePos, m_eOutBufLen + bufRest);
			}
			else
			{
				m_outBufferPos += bufRest;
				length = (writePos+bufRest) / NUM_OUTCHANNELS;
				eos = true;
				return;
			}
		}
		else
		{
			std::memcpy(buffer + writePos, m_outBuffer + m_outBufferPos, writeLength*sizeof(sample_t));
			m_outBufferPos += writeLength;
		}
		writePos += writeLength;
		iteration++;
	} while(writePos < length*NUM_OUTCHANNELS);
	m_position += length;
}

bool BinauralReader::checkSource()
{
	if((m_Azimuth != m_source->getAzimuth() || m_Elevation != m_source->getElevation()) && (!m_eosReader && !m_eosTail))
	{
		float az = m_Azimuth = m_source->getAzimuth();
		float el = m_Elevation = m_source->getElevation();
		auto irs = m_hrtfs->getImpulseResponse(az, el);
		if(az != m_RealAzimuth || el != m_RealElevation)
		{
			m_RealAzimuth = az;
			m_RealElevation = el;
			for(int i = 0; i < NUM_OUTCHANNELS; i++)
			{
				auto temp = std::move(m_convolvers[i]);
				m_convolvers[i] = std::move(m_convolvers[i + NUM_OUTCHANNELS]);
				m_convolvers[i + NUM_OUTCHANNELS] = std::move(temp);
			}
			for(int i = 0; i < NUM_OUTCHANNELS; i++)
				if(i%NUM_OUTCHANNELS == 0)
					m_convolvers[i]->setImpulseResponse(irs.first->getChannel(0));
				else
					m_convolvers[i]->setImpulseResponse(irs.second->getChannel(0));

			m_transPos = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
			m_transition = true;
			return true;
		}
	}
	return false;
}

void BinauralReader::loadBuffer(int nConvolvers)
{
	m_lastLengthIn = m_L;
	m_reader->read(m_lastLengthIn, m_eosReader, m_inBuffer);
	if(!m_eosReader || m_lastLengthIn > 0)
	{
		int len = m_lastLengthIn;
		for(int i = 0; i < nConvolvers; i++)
			m_futures[i] = m_threadPool->enqueue(&BinauralReader::threadFunction, this, i, true);
		for(int i = 0; i < nConvolvers; i++)
			len = m_futures[i].get();

		joinByChannel(0, len, nConvolvers);
		m_eOutBufLen = len*NUM_OUTCHANNELS;
	}
	else if(!m_eosTail)
	{
		int len = m_lastLengthIn = m_L;
		for(int i = 0; i < nConvolvers; i++)
			m_futures[i] = m_threadPool->enqueue(&BinauralReader::threadFunction, this, i, false);
		for(int i = 0; i < nConvolvers; i++)
			len = m_futures[i].get();

		joinByChannel(0, len, nConvolvers);
		m_eOutBufLen = len*NUM_OUTCHANNELS;
	}
}

void BinauralReader::joinByChannel(int start, int len, int nConvolvers)
{
	int k = 0;
	float vol = 0;
	const int l = CROSSFADE_SAMPLES*NUM_OUTCHANNELS;
	for(int i = 0; i < len*NUM_OUTCHANNELS; i += NUM_OUTCHANNELS)
	{
		if(m_transition)
		{
			vol = (m_transPos - i) / (float)l;
			if(vol > 1.0f)
				vol = 1.0f;
			else if(vol < 0.0f)
				vol = 0.0f;
		}

		for(int j = 0; j < NUM_OUTCHANNELS; j++)
			m_outBuffer[i + j + start] = ((m_vecOut[j][k] * (1.0f - vol)) + (m_vecOut[j + NUM_OUTCHANNELS][k] * vol))*m_source->getVolume();
		k++;
	}
	if(m_transition)
	{
		m_transPos -= len*NUM_OUTCHANNELS;
		if(m_transPos <= 0)
		{
			m_transition = false;
			m_transPos = l;
		}
	}
}

int BinauralReader::threadFunction(int id, bool input)
{
	int l = m_lastLengthIn;
	if(input)
		m_convolvers[id]->getNext(m_inBuffer, m_vecOut[id], l, m_eosTail);
	else
		m_convolvers[id]->getNext(nullptr, m_vecOut[id], l, m_eosTail);
	return l;
}

AUD_NAMESPACE_END