2b28bc7d6791edad0f6b2f75bb68fa4d67e052d5
[blender-staging.git] / intern / audaspace / FX / AUD_DoubleReader.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/FX/AUD_DoubleReader.cpp
26  *  \ingroup audfx
27  */
28
29
30 #include "AUD_DoubleReader.h"
31
32 #include <cstring>
33
34 AUD_DoubleReader::AUD_DoubleReader(AUD_Reference<AUD_IReader> reader1,
35                                                                    AUD_Reference<AUD_IReader> reader2) :
36                 m_reader1(reader1), m_reader2(reader2), m_finished1(false)
37 {
38         AUD_Specs s1, s2;
39         s1 = reader1->getSpecs();
40         s2 = reader2->getSpecs();
41 }
42
43 AUD_DoubleReader::~AUD_DoubleReader()
44 {
45 }
46
47 bool AUD_DoubleReader::isSeekable() const
48 {
49         return m_reader1->isSeekable() && m_reader2->isSeekable();
50 }
51
52 void AUD_DoubleReader::seek(int position)
53 {
54         m_reader1->seek(position);
55
56         int pos1 = m_reader1->getPosition();
57
58         if((m_finished1 = (pos1 < position)))
59                 m_reader2->seek(position - pos1);
60         else
61                 m_reader2->seek(0);
62 }
63
64 int AUD_DoubleReader::getLength() const
65 {
66         int len1 = m_reader1->getLength();
67         int len2 = m_reader2->getLength();
68         if(len1 < 0 || len2 < 0)
69                 return -1;
70         return len1 + len2;
71 }
72
73 int AUD_DoubleReader::getPosition() const
74 {
75         return m_reader1->getPosition() + m_reader2->getPosition();
76 }
77
78 AUD_Specs AUD_DoubleReader::getSpecs() const
79 {
80         return m_finished1 ? m_reader1->getSpecs() : m_reader2->getSpecs();
81 }
82
83 void AUD_DoubleReader::read(int& length, bool& eos, sample_t* buffer)
84 {
85         eos = false;
86
87         if(!m_finished1)
88         {
89                 int len = length;
90
91                 m_reader1->read(len, m_finished1, buffer);
92
93                 if(len < length)
94                 {
95                         AUD_Specs specs1, specs2;
96                         specs1 = m_reader1->getSpecs();
97                         specs2 = m_reader2->getSpecs();
98                         if(AUD_COMPARE_SPECS(specs1, specs2))
99                         {
100                                 int len2 = length - len;
101                                 m_reader2->read(len2, eos, buffer + specs1.channels * len);
102                                 length = len + len2;
103                         }
104                         else
105                                 length = len;
106                 }
107         }
108         else
109         {
110                 m_reader2->read(length, eos, buffer);
111         }
112 }