simple program with lame lib
Riccardo Cohen <[email protected]> Thu, 26 Mar 2009 00:35:02 +0100
| Newsgroups | gmane.comp.audio.mp3.encoders |
|---|---|
| Organization | Architecte du Logiciel |
| Message-ID | <[email protected]> |
Hello
I'm discovering Lame. I know C++ quite well but don't know sound
processing at all. This is my question :
I try to do a very simple program : open an mp3, read and copy the first
30 seconds to a new mp3 file.
I'm currently in the first step : read then mp3 and write raw data. I
looked at the frontend sample and tried to simplify it.
The program compiles and runs with no error, but produces a zero bytes
output file. I must be missing something, but I thought that default
parameters would be enough to make it work...
The problem is that "iread" is always zero, and I don't understand why.
Here is my code (below)
I'm running macosx 10.5 and latest source download of lame 398-2
(I compiled lame with no particular options , configure+make)
Thanks for any help.
--
Riccardo Cohen
Architecte du Logiciel
http://www.architectedulogiciel.fr
+33 (0)6.09.83.64.49
#include <lame.h>
#include <stdio.h>
#include <string.h> // memset
mp3data_struct mp3input_data;
void WriteBytes(FILE * fp, char *p, int n)
{
/* No error condition checking */
while (n-- > 0)
putc(*p++, fp);
}
int lame_decode_fromfile(FILE * fd, short pcm_l[], short pcm_r[],
mp3data_struct * mp3data)
{
int ret = 0;
size_t len = 0;
unsigned char buf[1024];
/* first see if we still have data buffered in the decoder: */
ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);
if (ret != 0)
return ret;
/* read until we get a valid output frame */
while (1) {
len = fread(buf, 1, 1024, fd);
if (len == 0) {
/* we are done reading the file, but check for buffered data */
ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);
if (ret <= 0) {
lame_decode_exit(); /* release mp3decoder memory */
return -1; /* done with file */
}
break;
}
ret = lame_decode1_headers(buf, len, pcm_l, pcm_r, mp3data);
if (ret == -1) {
lame_decode_exit(); /* release mp3decoder memory */
return -1;
}
if (ret > 0)
break;
}
return ret;
}
int read_samples_mp3(lame_global_flags * const gfp, FILE * const
musicin, short int mpg123pcm[2][1152])
{
int out;
static const char type_name[] = "MP3 file";
out = lame_decode_fromfile(musicin, mpg123pcm[0], mpg123pcm[1],
&mp3input_data);
/*
* out < 0: error, probably EOF
* out = 0: not possible with lame_decode_fromfile() ???
* out > 0: number of output samples
*/
if (out < 0)
{
memset(mpg123pcm, 0, sizeof(**mpg123pcm) * 2 * 1152);
return 0;
}
return out;
}
int main(int argc, char **argv)
{
printf("start...\n");
short int buffer[2][1152];
lame_global_flags *gfp;
gfp = lame_init();
int ret_code = lame_init_params(gfp);
if (ret_code>=0)
{
FILE *inf=fopen("/Users/rc/zdrop/etoiles.mp3","rb");
FILE *outf=fopen("/Users/rc/zdrop/out.mp3","wb");
int skip_start = 528 + 1;/* mp3 decoder has a 528 sample delay,
plus user supplied "skip" */
if (inf!=NULL)
{
if (outf!=NULL && inf!=NULL)
{
int idx,iread;
do
{
iread=read_samples_mp3(gfp,inf,buffer);
if (iread>=0)
{
for (idx=0;idx<iread;idx++)
{
printf("*");
WriteBytes(outf, (char *) &buffer[0][idx], sizeof(short));
WriteBytes(outf, (char *) &buffer[1][idx], sizeof(short));
}
fflush(outf);
}
}while(iread > 0);
fclose(outf);
}
else
printf("cannot open outf\n");
fclose(inf);
}
else
printf("cannot open inf\n");
}
else
printf("error init\n");
lame_close(gfp);
printf("... end\n");
return(0);
}