#include <stdio.h>
#include <stdlib.h>
#include <lame.h>
#define INBUFSIZE 4096
#define MP3BUFSIZE (int)(1.25 * INBUFSIZE) + 7200
int encode(char *inpath, char *outpath)
{
int status = 0;
lame_global_flags *gfp;
int ret_code;
FILE *intfp;
FILE *outfp;
short *input_buffer;
int input_samples;
char *mp3_buffer;
int mp3_bytes;
/* Initialize the library.*/
gfp = lame_init();
if(gfp == NULL)
{
printf("lame_init returned NULL\n");
status = -1;
goto exit;
}
/*Set the encoding parameters.*/
ret_code = lame_init_params(gfp);
if(ret_code < 0)
{
printf("lame_init_params returned %d\n", ret_code);
status = -1;
goto close_lame;
}
/*Open our input and output files.*/
intfp = fopen(inpath, "rb");
outfp = fopen(outpath, "wb");
/*Allocate some buffers.*/
input_buffer = (short*)malloc(INBUFSIZE *2);
mp3_buffer = (char*)malloc(MP3BUFSIZE);
/*Read from the input file, encode, and write to be output file.*/
do{
input_samples = fread(input_buffer, 2, INBUFSIZE, intfp);
if(input_samples > 0)
{
mp3_bytes = lame_encode_buffer_interleaved(
gfp,
input_buffer,
input_samples / 2,
mp3_buffer,
MP3BUFSIZE
);
if(mp3_bytes < 0)
{
printf("lame_encode_buffer_interleaved returned %d\n", mp3_bytes);
status = -1;
goto free_buffers;
}else if(mp3_bytes > 0)
{
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
}
}while(input_samples == INBUFSIZE);
/*Flush the encoder of any remaining bytes.*/
mp3_bytes = lame_encode_flush(gfp, mp3_buffer, sizeof(mp3_buffer));
if(mp3_bytes > 0)
{
printf("writing %d mp3 bytes\n", mp3_bytes);
fwrite(mp3_buffer, 1, mp3_bytes, outfp);
}
/*Clean up.*/
free_buffers:
free(mp3_buffer);
free(input_buffer);
fclose(outfp);
fclose(intfp);
close_lame:
lame_close(gfp);
exit:
return status;
}
int main(int argc, char * argv[])
{
if(argc < 3)
{
printf("usage: clame rewinfile mp3outfile\n");
exit(1);
}
encode(argv[1], argv[2]);
return 0;
}
/*
// unix ro linux:
gcc -I/usr/include/lame clame.c -lmp3lame -o clame
//windows
cl /IC:\lame-3.98.2\include clame.c \
C:\lame-3.98.2\libmp3lame\Release\libmp3lame.lib \
C:\lame-3.98.2\mpglib\Release\mpglib.lib
*/
#include <Python.h>
#include <lame.h>
/*defined in clame.c*/
int encode(char*, char*);
static PyObject *pylame1_encode(PyObject *self, PyObject *args)
{
int status;
char *inpath;
char *outpath;
if(!PyArg_ParseTuple(args,"ss", &inpath, &outpath))
{
return NULL;
}
status = encode(inpath, outpath);
return Py_BuildValue("i", status);
//Py_RETURN_NONE;
}
static PyMethodDef pylame1_methods[] = {
{"encode", (PyCFunction)pylame1_encode, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initpylame1()
{
Py_InitModule3("pylame1", pylame1_methods, "My first LAME module.");
}
/*
// unix ro linux:
gcc -shared -I/usr/include/pyton3.1 -I/usr/include/lame pylame1.c clame.c -lmp3lame -o pylame1.so
//windows
cl /LD /IC:\Pyton31\include /IC:\lame-3.98.2\include pylame1.c clame.c \
C:\Python31\libs\python31.lib \
C:\lame-3.98.2\libmp3lame\Release\libmp3lame.lib \
C:\lame-3.98.2\mpglib\Release\mpglib.lib
*/