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

AUD_SDLMixerReader.cpp « SDL « audaspace « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec61f99f02deac0b2c3f89b20d82f495506f0def (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * $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_SDLMixerReader.h"
#include "AUD_Buffer.h"

#include <cstring>

inline Uint16 AUD_TO_SDL(AUD_SampleFormat format)
{
	// SDL only supports 8 and 16 bit audio
	switch(format)
	{
	case AUD_FORMAT_U8:
		return AUDIO_U8;
	case AUD_FORMAT_S16:
		return AUDIO_S16SYS;
	default:
		AUD_THROW(AUD_ERROR_SDL);
	}
}

// greatest common divisor
inline int gcd(int a, int b)
{
	int c;

	// make sure a is the bigger
	if(b > a)
	{
		c = b;
		b = a;
		a = c;
	}

	// greetings from Euclides
	while(b != 0)
	{
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}

AUD_SDLMixerReader::AUD_SDLMixerReader(AUD_IReader* reader,
											 AUD_Specs specs)
{
	if(reader == NULL)
		AUD_THROW(AUD_ERROR_READER);

	m_reader = reader;
	m_tspecs = specs;
	m_sspecs = reader->getSpecs();

	try
	{
		// SDL only supports 8 and 16 bit sample formats
		if(SDL_BuildAudioCVT(&m_cvt,
							 AUD_TO_SDL(m_sspecs.format),
							 m_sspecs.channels,
							 m_sspecs.rate,
							 AUD_TO_SDL(specs.format),
							 specs.channels,
							 specs.rate) == -1)
			AUD_THROW(AUD_ERROR_SDL);
	}
	catch(AUD_Exception e)
	{
		delete m_reader; AUD_DELETE("reader")
		throw;
	}

	m_eor = false;
	m_rsposition = 0;
	m_rssize = 0;
	m_ssize = m_sspecs.rate / gcd(specs.rate, m_sspecs.rate);
	m_tsize = m_tspecs.rate * m_ssize / m_sspecs.rate;

	m_buffer = new AUD_Buffer(); AUD_NEW("buffer")
	m_rsbuffer = new AUD_Buffer(); AUD_NEW("buffer")
}

AUD_SDLMixerReader::~AUD_SDLMixerReader()
{
	delete m_reader; AUD_DELETE("reader")
	delete m_buffer; AUD_DELETE("buffer")
	delete m_rsbuffer; AUD_DELETE("buffer")
}

bool AUD_SDLMixerReader::isSeekable()
{
	return m_reader->isSeekable();
}

void AUD_SDLMixerReader::seek(int position)
{
	m_reader->seek(position * m_ssize / m_tsize);
	m_eor = false;
}

int AUD_SDLMixerReader::getLength()
{
	return m_reader->getLength() * m_tsize / m_ssize;
}

int AUD_SDLMixerReader::getPosition()
{
	return m_reader->getPosition() * m_tsize / m_ssize;
}

AUD_Specs AUD_SDLMixerReader::getSpecs()
{
	return m_tspecs;
}

AUD_ReaderType AUD_SDLMixerReader::getType()
{
	return m_reader->getType();
}

bool AUD_SDLMixerReader::notify(AUD_Message &message)
{
	return m_reader->notify(message);
}

void AUD_SDLMixerReader::read(int & length, sample_t* & buffer)
{
	// sample count for the target buffer without getting a shift
	int tns = length + m_tsize - length % m_tsize;
	// sample count for the source buffer without getting a shift
	int sns = tns * m_ssize / m_tsize;
	// target sample size
	int tss = AUD_SAMPLE_SIZE(m_tspecs);
	// source sample size
	int sss = AUD_SAMPLE_SIZE(m_sspecs);

	// input is output buffer
	int buf_size = AUD_MAX(tns*tss, sns*sss);

	// resize if necessary
	if(m_rsbuffer->getSize() < buf_size)
		m_rsbuffer->resize(buf_size, true);

	if(m_buffer->getSize() < length*tss)
		m_buffer->resize(length*tss);

	buffer = m_buffer->getBuffer();
	int size;
	int index = 0;
	sample_t* buf;

	while(index < length)
	{
		if(m_rsposition == m_rssize)
		{
			// no more data
			if(m_eor)
				length = index;
			// mix
			else
			{
				// read from source
				size = sns;
				m_reader->read(size, buf);

				// prepare
				m_cvt.buf = m_rsbuffer->getBuffer();
				m_cvt.len = size*sss;
				memcpy(m_cvt.buf, buf, size*sss);

				// convert
				SDL_ConvertAudio(&m_cvt);

				// end of reader
				if(size < sns)
					m_eor = true;

				m_rsposition = 0;
				m_rssize = size * m_tsize / m_ssize;
			}
		}

		// size to copy
		size = AUD_MIN(m_rssize-m_rsposition, length-index);

		// copy
		memcpy(m_buffer->getBuffer() + index * tss,
			   m_rsbuffer->getBuffer() + m_rsposition * tss,
			   size*tss);
		m_rsposition += size;
		index += size;
	}
}