1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* Copyright 2009-2011 Jörg Hermann Müller
*
* This file is part of AudaSpace.
*
* Audaspace is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* AudaSpace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Audaspace; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "AUD_LinearResampleReader.h"
#include <cmath>
#include <cstring>
#define CC channels + channel
AUD_LinearResampleReader::AUD_LinearResampleReader(AUD_IReader* reader,
AUD_Specs specs) :
AUD_EffectReader(reader),
m_sspecs(reader->getSpecs()),
m_factor(float(specs.rate) / float(m_sspecs.rate)),
m_tspecs(specs),
m_position(0),
m_sposition(0)
{
m_tspecs.channels = m_sspecs.channels;
m_cache.resize(2 * AUD_SAMPLE_SIZE(m_tspecs));
}
void AUD_LinearResampleReader::seek(int position)
{
m_position = position;
m_sposition = floor(position / m_factor);
m_reader->seek(m_sposition);
}
int AUD_LinearResampleReader::getLength() const
{
return m_reader->getLength() * m_factor;
}
int AUD_LinearResampleReader::getPosition() const
{
return m_position;
}
AUD_Specs AUD_LinearResampleReader::getSpecs() const
{
return m_tspecs;
}
void AUD_LinearResampleReader::read(int & length, sample_t* & buffer)
{
int samplesize = AUD_SAMPLE_SIZE(m_tspecs);
int size = length * samplesize;
if(m_buffer.getSize() < size)
m_buffer.resize(size);
int need = ceil((m_position + length) / m_factor) + 1 - m_sposition;
int len = need;
sample_t* buf;
buffer = m_buffer.getBuffer();
m_reader->read(len, buf);
if(len < need)
length = floor((m_sposition + len - 1) * m_factor) - m_position;
float spos;
sample_t low, high;
int channels = m_sspecs.channels;
for(int channel = 0; channel < channels; channel++)
{
for(int i = 0; i < length; i++)
{
spos = (m_position + i) / m_factor - m_sposition;
if(floor(spos) < 0)
{
low = m_cache.getBuffer()[(int)(floor(spos) + 2) * CC];
if(ceil(spos) < 0)
high = m_cache.getBuffer()[(int)(ceil(spos) + 2) * CC];
else
high = buf[(int)ceil(spos) * CC];
}
else
{
low = buf[(int)floor(spos) * CC];
high = buf[(int)ceil(spos) * CC];
}
buffer[i * CC] = low + (spos - floor(spos)) * (high - low);
}
}
if(len > 1)
memcpy(m_cache.getBuffer(),
buf + (len - 2) * channels,
2 * samplesize);
else if(len == 1)
memcpy(m_cache.getBuffer() + 1 * channels, buf, samplesize);
m_sposition += len;
m_position += length;
}
|