Audio:
[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                                 handle = new AUD_SequencerHandle(entry, m_device);
111                                 handles.push_front(handle);
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_factory->m_entries.end())
134                 {
135                         handle = new AUD_SequencerHandle(*eit, m_device);
136                         handles.push_front(handle);
137                         eit++;
138                 }
139
140                 m_handles = handles;
141
142                 m_entry_status = m_factory->m_entry_status;
143         }
144
145         AUD_Specs specs = m_factory->m_specs;
146         int pos = 0;
147         float time = float(m_position) / float(specs.rate);
148         float volume, frame;
149         int len, cfra;
150         AUD_Vector3 v, v2;
151         AUD_Quaternion q;
152
153
154         while(pos < length)
155         {
156                 frame = time * m_factory->m_fps;
157                 cfra = int(floor(frame));
158
159                 len = int(ceil((cfra + 1) / m_factory->m_fps * specs.rate)) - m_position;
160                 len = AUD_MIN(length - pos, len);
161                 len = AUD_MAX(len, 1);
162
163                 for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
164                 {
165                         (*it)->update(time, frame, m_factory->m_fps);
166                 }
167
168                 m_factory->m_volume.read(frame, &volume);
169                 m_device.setVolume(volume);
170
171                 m_factory->m_orientation.read(frame, q.get());
172                 m_device.setListenerOrientation(q);
173                 m_factory->m_location.read(frame, v.get());
174                 m_device.setListenerLocation(v);
175                 m_factory->m_location.read(frame + 1, v2.get());
176                 v2 -= v;
177                 m_device.setListenerVelocity(v2 * m_factory->m_fps);
178
179                 m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);
180
181                 pos += len;
182                 time += float(len) / float(specs.rate);
183         }
184
185         m_factory->unlock();
186
187         m_position += length;
188
189         eos = false;
190 }