3D Audio GSoC:
[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) :
38         m_position(0), m_device(factory->m_specs), m_factory(factory), m_status(0), m_entry_status(0)
39 {
40 }
41
42 AUD_SequencerReader::~AUD_SequencerReader()
43 {
44 }
45
46 bool AUD_SequencerReader::isSeekable() const
47 {
48         return true;
49 }
50
51 void AUD_SequencerReader::seek(int position)
52 {
53         m_position = position;
54
55         for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
56         {
57                 (*it)->seek(position / m_factory->m_specs.rate);
58         }
59 }
60
61 int AUD_SequencerReader::getLength() const
62 {
63         return -1;
64 }
65
66 int AUD_SequencerReader::getPosition() const
67 {
68         return m_position;
69 }
70
71 AUD_Specs AUD_SequencerReader::getSpecs() const
72 {
73         return m_factory->m_specs;
74 }
75
76 void AUD_SequencerReader::read(int& length, bool& eos, sample_t* buffer)
77 {
78         if(m_factory->m_status != m_status)
79         {
80                 m_device.changeSpecs(m_factory->m_specs);
81                 m_device.setSpeedOfSound(m_factory->m_speed_of_sound);
82                 m_device.setDistanceModel(m_factory->m_distance_model);
83                 m_device.setDopplerFactor(m_factory->m_doppler_factor);
84
85                 m_status = m_factory->m_status;
86         }
87
88         if(m_factory->m_entry_status != m_entry_status)
89         {
90                 std::list<AUD_Reference<AUD_SequencerHandle> > handles;
91
92                 AUD_HandleIterator hit = m_handles.begin();
93                 AUD_EntryIterator  eit = m_factory->m_entries.begin();
94
95                 int result;
96                 AUD_Reference<AUD_SequencerHandle> handle;
97
98                 while(hit != m_handles.end() && eit != m_factory->m_entries.end())
99                 {
100                         handle = *hit;
101                         AUD_Reference<AUD_SequencerEntry> entry = *eit;
102
103                         result = handle->compare(entry);
104
105                         if(result < 0)
106                         {
107                                 handle = new AUD_SequencerHandle(entry, m_device);
108                                 handles.push_front(handle);
109                                 eit++;
110                         }
111                         else if(result == 0)
112                         {
113                                 handles.push_back(handle);
114                                 hit++;
115                                 eit++;
116                         }
117                         else
118                         {
119                                 handle->stop();
120                                 hit++;
121                         }
122                 }
123
124                 while(hit != m_handles.end())
125                 {
126                         (*hit)->stop();
127                         hit++;
128                 }
129
130                 while(eit != m_factory->m_entries.end())
131                 {
132                         handle = new AUD_SequencerHandle(*eit, m_device);
133                         handles.push_front(handle);
134                         eit++;
135                 }
136
137                 m_handles = handles;
138
139                 m_entry_status = m_factory->m_entry_status;
140         }
141
142         // AUD_XXX: TODO: animation data
143
144         AUD_Specs specs = m_factory->m_specs;
145         int pos = 0;
146         float time = float(m_position) / float(specs.rate);
147         int len;
148
149         while(pos < length)
150         {
151                 len = int(ceil((int(floor(time * m_factory->m_fps)) + 1) / m_factory->m_fps * specs.rate)) - m_position;
152                 len = AUD_MIN(length - pos, len);
153                 len = AUD_MAX(len, 1);
154
155                 for(AUD_HandleIterator it = m_handles.begin(); it != m_handles.end(); it++)
156                 {
157                         (*it)->update(time);
158                 }
159
160                 m_device.read(reinterpret_cast<data_t*>(buffer + specs.channels * pos), len);
161
162                 pos += len;
163                 time += float(len) / float(specs.rate);
164         }
165
166         m_position += length;
167
168         eos = false;
169 }