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

AUD_SequencerReader.cpp « intern « audaspace « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 23d50c785089d5cb9e6fec834fca1d0538f959a7 (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
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * Copyright 2009-2011 Jörg Hermann Müller
 *
 * This file is part of AudaSpace.
 *
 * Audaspace is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * AudaSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Audaspace; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file audaspace/intern/AUD_SequencerReader.cpp
 *  \ingroup audaspaceintern
 */


#include "AUD_SequencerReader.h"

typedef std::list<AUD_Reference<AUD_SequencerHandle> >::iterator AUD_HandleIterator;
typedef std::list<AUD_Reference<AUD_SequencerEntry> >::iterator AUD_EntryIterator;

AUD_SequencerReader::AUD_SequencerReader(AUD_Reference<AUD_SequencerFactory> factory, bool quality) :
	m_position(0), m_device(factory->m_specs), m_factory(factory), m_status(0), m_entry_status(0)
{
	m_device.setQuality(quality);
}

AUD_SequencerReader::~AUD_SequencerReader()
{
}

bool AUD_SequencerReader::isSeekable() const
{
	return true;
}

void AUD_SequencerReader::seek(int position)
{
	m_position = position;

	for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
	{
		(*it)->seek(position / m_factory->m_specs.rate);
	}
}

int AUD_SequencerReader::getLength() const
{
	return -1;
}

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

AUD_Specs AUD_SequencerReader::getSpecs() const
{
	return m_factory->m_specs;
}

void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
{
	m_factory->lock();

	if(m_factory->m_status != m_status)
	{
		m_device.changeSpecs(m_factory->m_specs);
		m_device.setSpeedOfSound(m_factory->m_speed_of_sound);
		m_device.setDistanceModel(m_factory->m_distance_model);
		m_device.setDopplerFactor(m_factory->m_doppler_factor);

		m_status = m_factory->m_status;
	}

	if(m_factory->m_entry_status != m_entry_status)
	{
		std::list<AUD_Reference<AUD_SequencerHandle> > handles;

		AUD_HandleIterator hit = m_handles.begin();
		AUD_EntryIterator  eit = m_factory->m_entries.begin();

		int result;
		AUD_Reference<AUD_SequencerHandle> handle;

		while(hit != m_handles.end() && eit != m_factory->m_entries.end())
		{
			handle = *hit;
			AUD_Reference<AUD_SequencerEntry> entry = *eit;

			result = handle->compare(entry);

			if(result < 0)
			{
				try
				{
					handle = new AUD_SequencerHandle(entry, m_device);
					handles.push_front(handle);
				}
				catch(AUD_Exception&)
				{
				}
				eit++;
			}
			else if(result == 0)
			{
				handles.push_back(handle);
				hit++;
				eit++;
			}
			else
			{
				handle->stop();
				hit++;
			}
		}

		while(hit != m_handles.end())
		{
			(*hit)->stop();
			hit++;
		}

		while(eit != m_factory->m_entries.end())
		{
			try
			{
				handle = new AUD_SequencerHandle(*eit, m_device);
				handles.push_front(handle);
			}
			catch(AUD_Exception&)
			{
			}
			eit++;
		}

		m_handles = handles;

		m_entry_status = m_factory->m_entry_status;
	}

	AUD_Specs specs = m_factory->m_specs;
	int pos = 0;
	float time = float(m_position) / float(specs.rate);
	float volume, frame;
	int len, cfra;
	AUD_Vector3 v, v2;
	AUD_Quaternion q;


	while(pos < length)
	{
		frame = time * m_factory->m_fps;
		cfra = int(floor(frame));

		len = int(ceil((cfra + 1) / m_factory->m_fps * specs.rate)) - m_position;
		len = AUD_MIN(length - pos, len);
		len = AUD_MAX(len, 1);

		for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
		{
			(*it)->update(time, frame, m_factory->m_fps);
		}

		m_factory->m_volume.read(frame, &volume);
		m_device.setVolume(volume);

		m_factory->m_orientation.read(frame, q.get());
		m_device.setListenerOrientation(q);
		m_factory->m_location.read(frame, v.get());
		m_device.setListenerLocation(v);
		m_factory->m_location.read(frame + 1, v2.get());
		v2 -= v;
		m_device.setListenerVelocity(v2 * m_factory->m_fps);

		m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);

		pos += len;
		time += float(len) / float(specs.rate);
	}

	m_factory->unlock();

	m_position += length;

	eos = false;
}