Fix for an unhandled exception. Campbell: please include in 2.60a.
[blender.git] / intern / audaspace / intern / AUD_SequencerReader.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * Copyright 2009-2011 Jörg Hermann Müller
7  *
8  * This file is part of AudaSpace.
9  *
10  * Audaspace is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * AudaSpace is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Audaspace; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file audaspace/intern/AUD_SequencerReader.cpp
28  *  \ingroup audaspaceintern
29  */
30
31
32 #include "AUD_SequencerReader.h"
33
34 typedef std::list<AUD_Reference<AUD_SequencerHandle> >::iterator AUD_HandleIterator;
35 typedef std::list<AUD_Reference<AUD_SequencerEntry> >::iterator AUD_EntryIterator;
36
37 AUD_SequencerReader::AUD_SequencerReader(AUD_Reference<AUD_SequencerFactory> factory, bool quality) :
38         m_position(0), m_device(factory->m_specs), m_factory(factory), m_status(0), m_entry_status(0)
39 {
40         m_device.setQuality(quality);
41 }
42
43 AUD_SequencerReader::~AUD_SequencerReader()
44 {
45 }
46
47 bool AUD_SequencerReader::isSeekable() const
48 {
49         return true;
50 }
51
52 void AUD_SequencerReader::seek(int position)
53 {
54         m_position = position;
55
56         for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
57         {
58                 (*it)->seek(position / m_factory->m_specs.rate);
59         }
60 }
61
62 int AUD_SequencerReader::getLength() const
63 {
64         return -1;
65 }
66
67 int AUD_SequencerReader::getPosition() const
68 {
69         return m_position;
70 }
71
72 AUD_Specs AUD_SequencerReader::getSpecs() const
73 {
74         return m_factory->m_specs;
75 }
76
77 void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
78 {
79         m_factory->lock();
80
81         if(m_factory->m_status != m_status)
82         {
83                 m_device.changeSpecs(m_factory->m_specs);
84                 m_device.setSpeedOfSound(m_factory->m_speed_of_sound);
85                 m_device.setDistanceModel(m_factory->m_distance_model);
86                 m_device.setDopplerFactor(m_factory->m_doppler_factor);
87
88                 m_status = m_factory->m_status;
89         }
90
91         if(m_factory->m_entry_status != m_entry_status)
92         {
93                 std::list<AUD_Reference<AUD_SequencerHandle> > handles;
94
95                 AUD_HandleIterator hit = m_handles.begin();
96                 AUD_EntryIterator  eit = m_factory->m_entries.begin();
97
98                 int result;
99                 AUD_Reference<AUD_SequencerHandle> handle;
100
101                 while(hit != m_handles.end() && eit != m_factory->m_entries.end())
102                 {
103                         handle = *hit;
104                         AUD_Reference<AUD_SequencerEntry> entry = *eit;
105
106                         result = handle->compare(entry);
107
108                         if(result < 0)
109                         {
110                                 try
111                                 {
112                                         handle = new AUD_SequencerHandle(entry, m_device);
113                                         handles.push_front(handle);
114                                 }
115                                 catch(AUD_Exception&)
116                                 {
117                                 }
118                                 eit++;
119                         }
120                         else if(result == 0)
121                         {
122                                 handles.push_back(handle);
123                                 hit++;
124                                 eit++;
125                         }
126                         else
127                         {
128                                 handle->stop();
129                                 hit++;
130                         }
131                 }
132
133                 while(hit != m_handles.end())
134                 {
135                         (*hit)->stop();
136                         hit++;
137                 }
138
139                 while(eit != m_factory->m_entries.end())
140                 {
141                         try
142                         {
143                                 handle = new AUD_SequencerHandle(*eit, m_device);
144                                 handles.push_front(handle);
145                         }
146                         catch(AUD_Exception&)
147                         {
148                         }
149                         eit++;
150                 }
151
152                 m_handles = handles;
153
154                 m_entry_status = m_factory->m_entry_status;
155         }
156
157         AUD_Specs specs = m_factory->m_specs;
158         int pos = 0;
159         float time = float(m_position) / float(specs.rate);
160         float volume, frame;
161         int len, cfra;
162         AUD_Vector3 v, v2;
163         AUD_Quaternion q;
164
165
166         while(pos < length)
167         {
168                 frame = time * m_factory->m_fps;
169                 cfra = int(floor(frame));
170
171                 len = int(ceil((cfra + 1) / m_factory->m_fps * specs.rate)) - m_position;
172                 len = AUD_MIN(length - pos, len);
173                 len = AUD_MAX(len, 1);
174
175                 for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
176                 {
177                         (*it)->update(time, frame, m_factory->m_fps);
178                 }
179
180                 m_factory->m_volume.read(frame, &volume);
181                 m_device.setVolume(volume);
182
183                 m_factory->m_orientation.read(frame, q.get());
184                 m_device.setListenerOrientation(q);
185                 m_factory->m_location.read(frame, v.get());
186                 m_device.setListenerLocation(v);
187                 m_factory->m_location.read(frame + 1, v2.get());
188                 v2 -= v;
189                 m_device.setListenerVelocity(v2 * m_factory->m_fps);
190
191                 m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);
192
193                 pos += len;
194                 time += float(len) / float(specs.rate);
195         }
196
197         m_factory->unlock();
198
199         m_position += length;
200
201         eos = false;
202 }