Fix for [#37362] Audio strips sometimes are evaluated incorrectly.
[blender.git] / intern / audaspace / intern / AUD_AnimateableProperty.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * Copyright 2009-2011 Jörg Hermann Müller
5  *
6  * This file is part of AudaSpace.
7  *
8  * Audaspace is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * AudaSpace is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Audaspace; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file audaspace/intern/AUD_AnimateableProperty.cpp
26  *  \ingroup audaspaceintern
27  */
28
29
30 #include "AUD_AnimateableProperty.h"
31 #include "AUD_MutexLock.h"
32
33 #include <cstring>
34 #include <cmath>
35
36 AUD_AnimateableProperty::AUD_AnimateableProperty(int count) :
37         AUD_Buffer(count * sizeof(float)), m_count(count), m_isAnimated(false)
38 {
39         memset(getBuffer(), 0, count * sizeof(float));
40
41         pthread_mutexattr_t attr;
42         pthread_mutexattr_init(&attr);
43         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
44
45         pthread_mutex_init(&m_mutex, &attr);
46
47         pthread_mutexattr_destroy(&attr);
48 }
49
50 void AUD_AnimateableProperty::updateUnknownCache(int start, int end)
51 {
52         float* buf = getBuffer();
53
54         for(int i = start; i <= end; i++)
55                 // TODO: maybe first instead of zero order interpolation?
56                 memcpy(buf + i * m_count, buf + (start - 1) * m_count, m_count * sizeof(float));
57 }
58
59 AUD_AnimateableProperty::~AUD_AnimateableProperty()
60 {
61         pthread_mutex_destroy(&m_mutex);
62 }
63
64 void AUD_AnimateableProperty::lock()
65 {
66         pthread_mutex_lock(&m_mutex);
67 }
68
69 void AUD_AnimateableProperty::unlock()
70 {
71         pthread_mutex_unlock(&m_mutex);
72 }
73
74 void AUD_AnimateableProperty::write(const float* data)
75 {
76         AUD_MutexLock lock(*this);
77
78         m_isAnimated = false;
79         m_unknown.clear();
80         memcpy(getBuffer(), data, m_count * sizeof(float));
81 }
82
83 void AUD_AnimateableProperty::write(const float* data, int position, int count)
84 {
85         AUD_MutexLock lock(*this);
86
87         int pos = getSize() / (sizeof(float) * m_count);
88
89         if(!m_isAnimated)
90                 pos = 0;
91
92         m_isAnimated = true;
93
94         assureSize((count + position) * m_count * sizeof(float), true);
95
96         float* buf = getBuffer();
97
98         memcpy(buf + position * m_count, data, count * m_count * sizeof(float));
99
100         // have to fill up space between?
101         if(pos < position)
102         {
103                 m_unknown.push_back(Unknown(pos, position - 1));
104
105                 if(pos == 0)
106                 {
107                         memset(buf, 0, position * m_count * sizeof(float));
108                 }
109                 else
110                         updateUnknownCache(pos, position - 1);
111         }
112         // otherwise it's not at the end, let's check if some unknown part got filled
113         else
114         {
115                 for(std::list<Unknown>::iterator it = m_unknown.begin(); it != m_unknown.end(); it++)
116                 {
117                         // unknown area before position
118                         if(it->end < position)
119                                 continue;
120
121                         // we're after the new area, let's stop
122                         if(it->start >= position + count)
123                                 break;
124
125                         // we have an intersection, now 4 cases:
126                         // the start is included
127                         if(position <= it->start)
128                         {
129                                 // the end is included
130                                 if(position + count > it->end)
131                                 {
132                                         // simply delete
133                                         std::list<Unknown>::iterator it2 = it;
134                                         it++;
135                                         m_unknown.erase(it2);
136                                 }
137                                 // the end is excluded, a second part remains
138                                 else
139                                 {
140                                         // update second part
141                                         it->start = position + count;
142                                         updateUnknownCache(it->start, it->end);
143                                         break;
144                                 }
145                         }
146                         // start is excluded, a first part remains
147                         else
148                         {
149                                 // the end is included
150                                 if(position + count > it->end)
151                                 {
152                                         // update first part
153                                         it->end = position - 1;
154                                 }
155                                 // the end is excluded, a second part remains
156                                 else
157                                 {
158                                         // add another item and update both parts
159                                         m_unknown.insert(it, Unknown(it->start, position - 1));
160                                         it->start = position + count;
161                                         updateUnknownCache(it->start, it->end);
162                                 }
163                         }
164                 }
165         }
166 }
167
168 void AUD_AnimateableProperty::read(float position, float* out)
169 {
170         AUD_MutexLock lock(*this);
171
172         if(!m_isAnimated)
173         {
174                 memcpy(out, getBuffer(), m_count * sizeof(float));
175                 return;
176         }
177
178         int last = getSize() / (sizeof(float) * m_count) - 1;
179         float t = position - floor(position);
180
181         if(position >= last)
182         {
183                 position = last;
184                 t = 0;
185         }
186
187         if(t == 0)
188         {
189                 memcpy(out, getBuffer() + int(floor(position)) * m_count, m_count * sizeof(float));
190         }
191         else
192         {
193                 int pos = int(floor(position)) * m_count;
194                 float t2 = t * t;
195                 float t3 = t2 * t;
196                 float m0, m1;
197                 float* p0;
198                 float* p1 = getBuffer() + pos;
199                 float* p2;
200                 float* p3;
201                 last *= m_count;
202
203                 if(pos == 0)
204                         p0 = p1;
205                 else
206                         p0 = p1 - m_count;
207
208                 p2 = p1 + m_count;
209                 if(pos + m_count == last)
210                         p3 = p2;
211                 else
212                         p3 = p2 + m_count;
213
214                 for(int i = 0; i < m_count; i++)
215                 {
216                         m0 = (p2[i] - p0[i]) / 2.0f;
217                         m1 = (p3[i] - p1[i]) / 2.0f;
218
219                         out[i] = (2 * t3 - 3 * t2 + 1) * p0[i] + (-2 * t3 + 3 * t2) * p1[i] +
220                                          (t3 - 2 * t2 + t) * m0 + (t3 - t2) * m1;
221                 }
222         }
223 }
224
225 bool AUD_AnimateableProperty::isAnimated() const
226 {
227         return m_isAnimated;
228 }