summaryrefslogtreecommitdiff
path: root/encoders/lame_encoder.cpp
blob: 7bf2ebc95bcd5c09c8299af313375fe9ad9c57a7 (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
#include "lame_encoder.h"

#include <stdexcept>

EncoderLame::EncoderLame() {
	gfp = lame_init();
	int error = lame_init_params(gfp);

	if(error < 0) {
		throw std::runtime_error("failed to initialize LAME");
	}
}

EncoderLame::~EncoderLame() {
	lame_close(gfp);
}

size_t EncoderLame::encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) {
	return lame_encode_buffer_interleaved(gfp, (short*)input, input_size / 4, output, output_size);
}

size_t EncoderLame::flush(uint8_t *output, size_t output_size) {
	return lame_encode_flush(gfp, output, output_size);
}

size_t EncoderLame::encode(ReadFunc read, uint8_t *output, size_t output_size) {
	char src_data[0x2000];
	std::streamsize src_read = read(src_data, 0x2000);
	if(src_read < 0)
		src_read = 0;
	std::streamsize size = encode((const uint8_t*)src_data, src_read, (uint8_t*)output, output_size);
	// no more data, flush encoder
	if(src_read == 0 && size == 0) {
		size = flush((uint8_t*)output, output_size);
	}
	return size;
	
}