Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AUD_LinearResampleReader.cpp « intern « audaspace « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dcd5310575dd9c9a421141feb2183f1bea9457bb (plain)
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
124
125
126
127
128
129
130
131
132
133
/*
 * $Id$
 *
 * ***** BEGIN LGPL LICENSE BLOCK *****
 *
 * Copyright 2009 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 Lesser General Public License as published by
 * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AudaSpace.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***** END LGPL LICENSE BLOCK *****
 */

#include "AUD_LinearResampleReader.h"
#include "AUD_Buffer.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_tspecs = specs;
	m_tspecs.channels = m_sspecs.channels;
	m_factor = (float)m_tspecs.rate / (float)m_sspecs.rate;

	m_position = 0;
	m_sposition = 0;

	m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
	m_cache = new AUD_Buffer(2 * AUD_SAMPLE_SIZE(specs)); AUD_NEW("buffer")
}

AUD_LinearResampleReader::~AUD_LinearResampleReader()
{
	delete m_buffer; AUD_DELETE("buffer")
	delete m_cache; AUD_DELETE("buffer")
}

void AUD_LinearResampleReader::seek(int position)
{
	m_position = position;
	m_sposition = floor(position / m_factor);
	m_reader->seek(m_sposition);
}

int AUD_LinearResampleReader::getLength()
{
	return m_reader->getLength() * m_factor;
}

int AUD_LinearResampleReader::getPosition()
{
	return m_position;
}

AUD_Specs AUD_LinearResampleReader::getSpecs()
{
	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;
}