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

#include <stdexcept>

EncoderLame::EncoderLame(RawAudioSource::p source_) : source(source_) {
	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);
}

std::size_t EncoderLame::read(char* buf, std::size_t buf_size) {
	char src_data[0x30000];
	std::streamsize src_read = source->read(src_data, 0x30000);
	if(src_read < 0)
		src_read = 0;
	std::size_t size = lame_encode_buffer_interleaved(gfp, (short*)src_data, src_read / 4, (unsigned char*)buf, buf_size);
	// no more data, flush encoder
	if(src_read == 0 && size == 0) {
		size = lame_encode_flush(gfp, (unsigned char*)buf, buf_size);
	}
	return size;
	
}