svn merge -r39781:39792 https://svn.blender.org/svnroot/bf-blender/trunk/blender...
[blender-staging.git] / intern / audaspace / FX / AUD_LimiterReader.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_LimiterReader.cpp
28  *  \ingroup audfx
29  */
30
31
32 #include "AUD_LimiterReader.h"
33 #include "AUD_Buffer.h"
34
35 AUD_LimiterReader::AUD_LimiterReader(AUD_Reference<AUD_IReader> reader,
36                                                                          float start, float end) :
37                 AUD_EffectReader(reader),
38                 m_start(start),
39                 m_end(end)
40 {
41         if(m_start > 0)
42         {
43                 AUD_Specs specs = m_reader->getSpecs();
44                 AUD_Specs specs2;
45
46                 if(m_reader->isSeekable())
47                         m_reader->seek(m_start * specs.rate);
48                 else
49                 {
50                         // skip first m_start samples by reading them
51                         int length = AUD_DEFAULT_BUFFER_SIZE;
52                         AUD_Buffer buffer(AUD_DEFAULT_BUFFER_SIZE * AUD_SAMPLE_SIZE(specs));
53                         bool eos = false;
54                         for(int len = m_start * specs.rate;
55                                 length > 0 && !eos;
56                                 len -= length)
57                         {
58                                 if(len < AUD_DEFAULT_BUFFER_SIZE)
59                                         length = len;
60
61                                 m_reader->read(length, eos, buffer.getBuffer());
62
63                                 specs2 = m_reader->getSpecs();
64                                 if(specs2.rate != specs.rate)
65                                 {
66                                         len = len * specs2.rate / specs.rate;
67                                         specs.rate = specs2.rate;
68                                 }
69
70                                 if(specs2.channels != specs.channels)
71                                 {
72                                         specs = specs2;
73                                         buffer.assureSize(AUD_DEFAULT_BUFFER_SIZE * AUD_SAMPLE_SIZE(specs));
74                                 }
75                         }
76                 }
77         }
78 }
79
80 void AUD_LimiterReader::seek(int position)
81 {
82         m_reader->seek(position + m_start * m_reader->getSpecs().rate);
83 }
84
85 int AUD_LimiterReader::getLength() const
86 {
87         int len = m_reader->getLength();
88         AUD_SampleRate rate = m_reader->getSpecs().rate;
89         if(len < 0 || (len > m_end * rate && m_end >= 0))
90                 len = m_end * rate;
91         return len - m_start * rate;
92 }
93
94 int AUD_LimiterReader::getPosition() const
95 {
96         int pos = m_reader->getPosition();
97         AUD_SampleRate rate = m_reader->getSpecs().rate;
98         return AUD_MIN(pos, m_end * rate) - m_start * rate;
99 }
100
101 void AUD_LimiterReader::read(int& length, bool& eos, sample_t* buffer)
102 {
103         eos = false;
104         if(m_end >= 0)
105         {
106                 int position = m_reader->getPosition();
107                 AUD_SampleRate rate = m_reader->getSpecs().rate;
108
109                 if(position + length > m_end * rate)
110                 {
111                         length = m_end * rate - position;
112                         eos = true;
113                 }
114
115                 if(position < m_start * rate)
116                 {
117                         int len2 = length;
118                         for(int len = m_start * rate - position;
119                                 len2 == length && !eos;
120                                 len -= length)
121                         {
122                                 if(len < length)
123                                         len2 = len;
124
125                                 m_reader->read(len2, eos, buffer);
126                                 position += len2;
127                         }
128
129                         if(position < m_start * rate)
130                         {
131                                 length = 0;
132                                 return;
133                         }
134                 }
135
136                 if(length < 0)
137                 {
138                         length = 0;
139                         return;
140                 }
141         }
142         if(eos)
143         {
144                 m_reader->read(length, eos, buffer);
145                 eos = true;
146         }
147         else
148                 m_reader->read(length, eos, buffer);
149 }