* clean out some warnings (unrefenced vars mainly)
[blender.git] / intern / audaspace / FX / AUD_DoubleReader.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN LGPL LICENSE BLOCK *****
5  *
6  * Copyright 2009 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 Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * ***** END LGPL LICENSE BLOCK *****
24  */
25
26 #include "AUD_DoubleReader.h"
27 #include "AUD_Buffer.h"
28
29 #include <cstring>
30
31 AUD_DoubleReader::AUD_DoubleReader(AUD_IReader* reader1,
32                                                                    AUD_IReader* reader2) :
33                 m_reader1(reader1), m_reader2(reader2)
34 {
35         try
36         {
37                 if(!reader1)
38                         AUD_THROW(AUD_ERROR_READER);
39
40                 if(!reader2)
41                         AUD_THROW(AUD_ERROR_READER);
42
43                 AUD_Specs s1, s2;
44                 s1 = reader1->getSpecs();
45                 s2 = reader2->getSpecs();
46                 if(memcmp(&s1, &s2, sizeof(AUD_Specs)) != 0)
47                         AUD_THROW(AUD_ERROR_READER);
48         }
49
50         catch(AUD_Exception)
51         {
52                 if(reader1)
53                 {
54                         delete reader1; AUD_DELETE("reader")
55                 }
56                 if(reader2)
57                 {
58                         delete reader2; AUD_DELETE("reader")
59                 }
60
61                 throw;
62         }
63
64         m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
65         m_finished1 = false;
66 }
67
68 AUD_DoubleReader::~AUD_DoubleReader()
69 {
70         delete m_reader1; AUD_DELETE("reader")
71         delete m_reader2; AUD_DELETE("reader")
72         delete m_buffer; AUD_DELETE("buffer")
73 }
74
75 bool AUD_DoubleReader::isSeekable()
76 {
77         return false;
78 }
79
80 void AUD_DoubleReader::seek(int position)
81 {
82         int length1 = m_reader1->getLength();
83
84         if(position < 0)
85                 position = 0;
86
87         if(position < length1)
88         {
89                 m_reader1->seek(position);
90                 m_reader2->seek(0);
91                 m_finished1 = false;
92         }
93         else
94         {
95                 m_reader2->seek(position-length1);
96                 m_finished1 = true;
97         }
98 }
99
100 int AUD_DoubleReader::getLength()
101 {
102         int len1 = m_reader1->getLength();
103         int len2 = m_reader2->getLength();
104         if(len1 < 0 || len2 < 0)
105                 return -1;
106         return len1 + len2;
107 }
108
109 int AUD_DoubleReader::getPosition()
110 {
111         return m_reader1->getPosition() + m_reader2->getPosition();
112 }
113
114 AUD_Specs AUD_DoubleReader::getSpecs()
115 {
116         return m_reader1->getSpecs();
117 }
118
119 AUD_ReaderType AUD_DoubleReader::getType()
120 {
121         if(m_reader1->getType() == AUD_TYPE_BUFFER &&
122            m_reader2->getType() == AUD_TYPE_BUFFER)
123                 return AUD_TYPE_BUFFER;
124         return AUD_TYPE_STREAM;
125 }
126
127 bool AUD_DoubleReader::notify(AUD_Message &message)
128 {
129         return m_reader1->notify(message) | m_reader2->notify(message);
130 }
131
132 void AUD_DoubleReader::read(int & length, sample_t* & buffer)
133 {
134         if(!m_finished1)
135         {
136                 int len = length;
137                 m_reader1->read(len, buffer);
138                 if(len < length)
139                 {
140                         int samplesize = AUD_SAMPLE_SIZE(m_reader1->getSpecs());
141                         if(m_buffer->getSize() < length * samplesize)
142                                 m_buffer->resize(length * samplesize);
143                         memcpy(m_buffer->getBuffer(), buffer, len*samplesize);
144                         len = length - len;
145                         length -= len;
146                         m_reader2->read(len, buffer);
147                         memcpy(m_buffer->getBuffer() + length*samplesize,
148                                    buffer, len*samplesize);
149                         length += len;
150                         buffer = m_buffer->getBuffer();
151                         m_finished1 = true;
152                 }
153         }
154         else
155         {
156                 m_reader2->read(length, buffer);
157         }
158 }