rna polygon access to loop vertex indicies.
[blender-staging.git] / intern / audaspace / FX / AUD_DelayReader.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/FX/AUD_DelayReader.cpp
28  *  \ingroup audfx
29  */
30
31
32 #include "AUD_DelayReader.h"
33
34 #include <cstring>
35
36 AUD_DelayReader::AUD_DelayReader(AUD_IReader* reader, float delay) :
37                 AUD_EffectReader(reader),
38                 m_delay(int(delay * reader->getSpecs().rate)),
39                 m_remdelay(int(delay * reader->getSpecs().rate)),
40                 m_empty(true)
41 {
42 }
43
44 void AUD_DelayReader::seek(int position)
45 {
46         if(position < m_delay)
47         {
48                 m_remdelay = m_delay - position;
49                 m_reader->seek(0);
50         }
51         else
52         {
53                 m_remdelay = 0;
54                 m_reader->seek(position - m_delay);
55         }
56 }
57
58 int AUD_DelayReader::getLength() const
59 {
60         int len = m_reader->getLength();
61         if(len < 0)
62                 return len;
63         return len + m_delay;
64 }
65
66 int AUD_DelayReader::getPosition() const
67 {
68         if(m_remdelay > 0)
69                 return m_delay - m_remdelay;
70         return m_reader->getPosition() + m_delay;
71 }
72
73 void AUD_DelayReader::read(int & length, sample_t* & buffer)
74 {
75         if(m_remdelay > 0)
76         {
77                 AUD_Specs specs = m_reader->getSpecs();
78                 int samplesize = AUD_SAMPLE_SIZE(specs);
79
80                 if(m_buffer.getSize() < length * samplesize)
81                 {
82                         m_buffer.resize(length * samplesize);
83                         m_empty = false;
84                 }
85
86                 buffer = m_buffer.getBuffer();
87
88                 if(length > m_remdelay)
89                 {
90                         if(!m_empty)
91                                 memset(buffer, 0, m_remdelay * samplesize);
92
93                         int len = length - m_remdelay;
94                         sample_t* buf;
95                         m_reader->read(len, buf);
96
97                         memcpy(buffer + m_remdelay * specs.channels,
98                                    buf, len * samplesize);
99
100                         if(len < length-m_remdelay)
101                                 length = m_remdelay + len;
102
103                         m_remdelay = 0;
104                         m_empty = false;
105                 }
106                 else
107                 {
108                         if(!m_empty)
109                         {
110                                 memset(buffer, 0, length * samplesize);
111                                 m_empty = true;
112                         }
113                         m_remdelay -= length;
114                 }
115         }
116         else
117                 m_reader->read(length, buffer);
118 }