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

#include <stdexcept>

EncoderLame::EncoderLame(RawAudioSource::p source_) : source(source_) {
	gfp = lame_init();

	lame_set_in_samplerate(gfp, source->get_samplerate());
	lame_set_out_samplerate(gfp, 0);
	lame_set_num_channels(gfp, source->get_channels());

	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;
	// TODO: handle non-stereo inputs
	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;
	
}

std::string EncoderLame::get_mime_type() {
	return "audio/mpeg";
}