1 /*******************************************************************************
2 * Copyright 2009-2016 Jörg Müller
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 "sequence/SequenceReader.h"
18 #include "sequence/SequenceData.h"
19 #include "Exception.h"
20 #include "SequenceHandle.h"
21
22 #include <algorithm>
23 #include <mutex>
24 #include <cmath>
25
26 AUD_NAMESPACE_BEGIN
27
SequenceReader(std::shared_ptr<SequenceData> sequence,bool quality)28 SequenceReader::SequenceReader(std::shared_ptr<SequenceData> sequence, bool quality) :
29 m_position(0), m_device(sequence->m_specs), m_sequence(sequence), m_status(0), m_entry_status(0)
30 {
31 m_device.setQuality(quality);
32 }
33
~SequenceReader()34 SequenceReader::~SequenceReader()
35 {
36 }
37
isSeekable() const38 bool SequenceReader::isSeekable() const
39 {
40 return true;
41 }
42
seek(int position)43 void SequenceReader::seek(int position)
44 {
45 if(position < 0)
46 return;
47
48 m_position = position;
49
50 for(auto& handle : m_handles)
51 {
52 handle->seek(position / (double)m_sequence->m_specs.rate);
53 }
54 }
55
getLength() const56 int SequenceReader::getLength() const
57 {
58 return -1;
59 }
60
getPosition() const61 int SequenceReader::getPosition() const
62 {
63 return m_position;
64 }
65
getSpecs() const66 Specs SequenceReader::getSpecs() const
67 {
68 return m_sequence->m_specs;
69 }
70
read(int & length,bool & eos,sample_t * buffer)71 void SequenceReader::read(int& length, bool& eos, sample_t* buffer)
72 {
73 std::lock_guard<ILockable> lock(*m_sequence);
74
75 if(m_sequence->m_status != m_status)
76 {
77 m_device.changeSpecs(m_sequence->m_specs);
78 m_device.setSpeedOfSound(m_sequence->m_speed_of_sound);
79 m_device.setDistanceModel(m_sequence->m_distance_model);
80 m_device.setDopplerFactor(m_sequence->m_doppler_factor);
81
82 m_status = m_sequence->m_status;
83 }
84
85 if(m_sequence->m_entry_status != m_entry_status)
86 {
87 std::list<std::shared_ptr<SequenceHandle> > handles;
88
89 auto hit = m_handles.begin();
90 auto eit = m_sequence->m_entries.begin();
91
92 int result;
93 std::shared_ptr<SequenceHandle> handle;
94
95 while(hit != m_handles.end() && eit != m_sequence->m_entries.end())
96 {
97 handle = *hit;
98 std::shared_ptr<SequenceEntry> entry = *eit;
99
100 result = handle->compare(entry);
101
102 if(result < 0)
103 {
104 try
105 {
106 handle = std::shared_ptr<SequenceHandle>(new SequenceHandle(entry, m_device));
107 handles.push_back(handle);
108 }
109 catch(Exception&)
110 {
111 }
112 eit++;
113 }
114 else if(result == 0)
115 {
116 handles.push_back(handle);
117 hit++;
118 eit++;
119 }
120 else
121 {
122 handle->stop();
123 hit++;
124 }
125 }
126
127 while(hit != m_handles.end())
128 {
129 (*hit)->stop();
130 hit++;
131 }
132
133 while(eit != m_sequence->m_entries.end())
134 {
135 try
136 {
137 handle = std::shared_ptr<SequenceHandle>(new SequenceHandle(*eit, m_device));
138 handles.push_back(handle);
139 }
140 catch(Exception&)
141 {
142 }
143 eit++;
144 }
145
146 m_handles = handles;
147
148 m_entry_status = m_sequence->m_entry_status;
149 }
150
151 Specs specs = m_sequence->m_specs;
152 int pos = 0;
153 double time = double(m_position) / double(specs.rate);
154 float volume, frame;
155 int len, cfra;
156 Vector3 v, v2;
157 Quaternion q;
158
159 while(pos < length)
160 {
161 frame = time * m_sequence->m_fps;
162 cfra = int(std::floor(frame));
163
164 len = int(std::ceil((cfra + 1) / m_sequence->m_fps * specs.rate)) - m_position;
165 len = std::min(length - pos, len);
166 len = std::max(len, 1);
167
168 for(auto& handle : m_handles)
169 {
170 handle->update(time, frame, m_sequence->m_fps);
171 }
172
173 m_sequence->m_volume.read(frame, &volume);
174 if(m_sequence->m_muted)
175 volume = 0.0f;
176 m_device.setVolume(volume);
177
178 m_sequence->m_orientation.read(frame, q.get());
179 m_device.setListenerOrientation(q);
180 m_sequence->m_location.read(frame, v.get());
181 m_device.setListenerLocation(v);
182 m_sequence->m_location.read(frame + 1, v2.get());
183 v2 -= v;
184 m_device.setListenerVelocity(v2 * m_sequence->m_fps);
185
186 m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);
187
188 pos += len;
189 time += double(len) / double(specs.rate);
190 }
191
192 m_position += length;
193
194 eos = false;
195 }
196
197 AUD_NAMESPACE_END
198