rna polygon access to loop vertex indicies.
[blender-staging.git] / intern / audaspace / FX / AUD_DoubleReader.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_DoubleReader.cpp
28  *  \ingroup audfx
29  */
30
31
32 #include "AUD_DoubleReader.h"
33
34 #include <cstring>
35
36 static const char* specs_error = "AUD_DoubleReader: Both readers have to have "
37                                                                  "the same specs.";
38
39 AUD_DoubleReader::AUD_DoubleReader(AUD_IReader* reader1,
40                                                                    AUD_IReader* reader2) :
41                 m_reader1(reader1), m_reader2(reader2), m_finished1(false)
42 {
43         AUD_Specs s1, s2;
44         s1 = reader1->getSpecs();
45         s2 = reader2->getSpecs();
46         if(memcmp(&s1, &s2, sizeof(AUD_Specs)) != 0)
47         {
48                 delete reader1;
49                 delete reader2;
50                 AUD_THROW(AUD_ERROR_SPECS, specs_error);
51         }
52 }
53
54 AUD_DoubleReader::~AUD_DoubleReader()
55 {
56         delete m_reader1;
57         delete m_reader2;
58 }
59
60 bool AUD_DoubleReader::isSeekable() const
61 {
62         return m_reader1->isSeekable() && m_reader2->isSeekable();
63 }
64
65 void AUD_DoubleReader::seek(int position)
66 {
67         m_reader1->seek(position);
68
69         int pos1 = m_reader1->getPosition();
70
71         if((m_finished1 = (pos1 < position)))
72                 m_reader2->seek(position - pos1);
73         else
74                 m_reader2->seek(0);
75 }
76
77 int AUD_DoubleReader::getLength() const
78 {
79         int len1 = m_reader1->getLength();
80         int len2 = m_reader2->getLength();
81         if(len1 < 0 || len2 < 0)
82                 return -1;
83         return len1 + len2;
84 }
85
86 int AUD_DoubleReader::getPosition() const
87 {
88         return m_reader1->getPosition() + m_reader2->getPosition();
89 }
90
91 AUD_Specs AUD_DoubleReader::getSpecs() const
92 {
93         return m_reader1->getSpecs();
94 }
95
96 void AUD_DoubleReader::read(int & length, sample_t* & buffer)
97 {
98         if(!m_finished1)
99         {
100                 int len = length;
101                 m_reader1->read(len, buffer);
102
103                 if(len < length)
104                 {
105                         AUD_Specs specs = m_reader1->getSpecs();
106                         int samplesize = AUD_SAMPLE_SIZE(specs);
107
108                         if(m_buffer.getSize() < length * samplesize)
109                                 m_buffer.resize(length * samplesize);
110
111                         sample_t* buf = buffer;
112                         buffer = m_buffer.getBuffer();
113
114                         memcpy(buffer, buf, len * samplesize);
115
116                         len = length - len;
117                         length -= len;
118                         m_reader2->read(len, buf);
119
120                         memcpy(buffer + length * specs.channels, buf,
121                                    len * samplesize);
122
123                         length += len;
124
125                         m_finished1 = true;
126                 }
127         }
128         else
129         {
130                 m_reader2->read(length, buffer);
131         }
132 }