svn merge -r39781:39792 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[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 AUD_DoubleReader::AUD_DoubleReader(AUD_Reference<AUD_IReader> reader1,
37                                                                    AUD_Reference<AUD_IReader> reader2) :
38                 m_reader1(reader1), m_reader2(reader2), m_finished1(false)
39 {
40         AUD_Specs s1, s2;
41         s1 = reader1->getSpecs();
42         s2 = reader2->getSpecs();
43 }
44
45 AUD_DoubleReader::~AUD_DoubleReader()
46 {
47 }
48
49 bool AUD_DoubleReader::isSeekable() const
50 {
51         return m_reader1->isSeekable() && m_reader2->isSeekable();
52 }
53
54 void AUD_DoubleReader::seek(int position)
55 {
56         m_reader1->seek(position);
57
58         int pos1 = m_reader1->getPosition();
59
60         if((m_finished1 = (pos1 < position)))
61                 m_reader2->seek(position - pos1);
62         else
63                 m_reader2->seek(0);
64 }
65
66 int AUD_DoubleReader::getLength() const
67 {
68         int len1 = m_reader1->getLength();
69         int len2 = m_reader2->getLength();
70         if(len1 < 0 || len2 < 0)
71                 return -1;
72         return len1 + len2;
73 }
74
75 int AUD_DoubleReader::getPosition() const
76 {
77         return m_reader1->getPosition() + m_reader2->getPosition();
78 }
79
80 AUD_Specs AUD_DoubleReader::getSpecs() const
81 {
82         return m_finished1 ? m_reader1->getSpecs() : m_reader2->getSpecs();
83 }
84
85 void AUD_DoubleReader::read(int& length, bool& eos, sample_t* buffer)
86 {
87         eos = false;
88
89         if(!m_finished1)
90         {
91                 int len = length;
92
93                 m_reader1->read(len, m_finished1, buffer);
94
95                 if(len < length)
96                 {
97                         AUD_Specs specs1, specs2;
98                         specs1 = m_reader1->getSpecs();
99                         specs2 = m_reader2->getSpecs();
100                         if(AUD_COMPARE_SPECS(specs1, specs2))
101                         {
102                                 int len2 = length - len;
103                                 m_reader2->read(len2, eos, buffer + specs1.channels * len);
104                                 length = len + len2;
105                         }
106                         else
107                                 length = len;
108                 }
109         }
110         else
111         {
112                 m_reader2->read(length, eos, buffer);
113         }
114 }