linux下lame编程:wav转MP3示例代码

1. wav转MP3示例程序代码:

/*
gcc -I /usr/include/lame/ lame_test.c -lmp3lame -o lame_test -lm
*/#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* infp;
      FILE* outfp;
      short* input_buffer;
      int input_samples;
      char* mp3_buffer;
      int mp3_bytes;

      gfp = lame_init();
      if (gfp == NULL)

      {
            printf("lame_init failed/n");
            status = -1;
            goto exit;
      }

      ret_code = lame_init_params(gfp);
      if (ret_code < 0)

      {
           printf("lame_init_params returned %d/n",ret_code);
            status = -1;
            goto close_lame;
      }

      infp = fopen(inPath, "rb");
      outfp = fopen(outPath, "wb");

      input_buffer = (short*)malloc(INBUFSIZE*2);
      mp3_buffer = (char*)malloc(MP3BUFSIZE);

      do

      {
            input_samples = fread(input_buffer, 2, INBUFSIZE, infp);
            //fprintf(stderr, "input_samples is %d./n", input_samples);
            mp3_bytes = lame_encode_buffer_interleaved(gfp, input_buffer,input_samples/2, mp3_buffer, MP3BUFSIZE);
            //fprintf(stderr, "mp3_bytes is %d./n", mp3_bytes);
            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);

      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);
      }
      free_buffers:
      free(mp3_buffer);
      free(input_buffer);

      fclose(outfp);
      fclose(infp);
      close_lame:
      lame_close(gfp);
      exit:
      return status;
}

int main(int argc, char** argv)

{
      if (argc < 3)

      {
            printf("usage: lame_test rawinfile mp3outfile/n");
      }
      encode(argv[1], argv[2]);
      return 0;
}

2. 录音成MP3格式程序代码:

/*
gcc -I /usr/include/lame/ -lmp3lame -o mp3_record mp3_record.c -lm
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <memory.h>
#include <linux/soundcard.h>

#include "lame.h"

#define BUFF_SIZE 512
#define INBUFF_SIZE 4096
#define MP3BUFF_SIZE (int) (1.25 * INBUFF_SIZE) + 7200

static int run=1;

static void stop(int signum){
fprintf(stderr, "exit/n");
run=0;
}

int SetFormat(int fd, unsigned int bit, unsigned int channel, unsigned int hz)
{
int ioctl_val;

/* set bit format */
ioctl_val = bit;
if(ioctl(fd, SNDCTL_DSP_SETFMT, &ioctl_val) < 0){
fprintf(stderr, "Set fmt to bit %d failed:%s/n", bit, strerror(errno));
return -1;
}
if (ioctl_val != bit) {
fprintf(stderr, "do not support bit %d, supported %d/n", bit,ioctl_val);
return (-1);
}

/*set channel */
ioctl_val = channel;
if ((ioctl(fd, SNDCTL_DSP_CHANNELS, &ioctl_val)) == -1) {
fprintf(stderr, "Set Audio Channels %d failed:%s/n", channel, strerror(errno));
return (-1);
}
if (ioctl_val != channel) {
fprintf(stderr, "do not support channel %d,supported %d/n", channel, ioctl_val);
return (-1);
}

/*set speed */
ioctl_val = hz;
if (ioctl(fd, SNDCTL_DSP_SPEED, &ioctl_val) == -1) {
fprintf(stderr, "Set speed to %d failed:%s/n", hz, strerror(errno));
return (-1);
}
if (ioctl_val != hz) {
fprintf(stderr, "do not support speed %d,supported is %d/n", hz,ioctl_val);
return (-1);
}

return 0;

}

int main(int argc, char **argv)
{
int status =0;
int snd_f;
int fd_f;
lame_global_flags* gfp;
short *input_buff;
char *mp3_buff;

if(argc !=2){
fprintf(stderr, "useage: ./mp3_record test.mp3/n");
return -1;
}
signal(SIGINT,stop);

if((snd_f = open("/dev/dsp", O_RDONLY)) < 0){
fprintf(stderr, "open audio device error: %s", strerror(errno));
status = -1;
goto exit;
}

if((fd_f = open(argv[1], O_CREAT | O_WRONLY)) < 0){
fprintf(stderr, "open file error: %s", strerror(errno));
status = -1;
goto exit;
}

if (SetFormat(snd_f, 16, 2, 44100) < 0) {
fprintf(stderr, "cannot set /dev/dsp in bit 16, channel 2, speed 44100/n");
status = -1;
goto exit;
}

gfp = lame_init();
if (gfp == NULL) {
printf("lame_init failed/n");
status = -1;
goto exit;
}

int ret_code = lame_init_params(gfp);
if (ret_code < 0) {
printf("lame_init_params returned %d/n",ret_code);
status = -1;
goto close_lame;
}

input_buff = (short *)malloc(INBUFF_SIZE*2);
mp3_buff = (char *)malloc(MP3BUFF_SIZE);

int samples;
int mp3_bytes;
int write_bytes;
int n=100;
while(run){
//while(n>0){
//n--;
//fprintf(stderr, "n is %d./n", n);
memset(input_buff, 0 , INBUFF_SIZE*2);
memset(mp3_buff, 0 , MP3BUFF_SIZE);
samples = read(snd_f, input_buff, INBUFF_SIZE*2);
if (samples < 0) {
perror("read sound device failed");
status = -1;
goto free_buffers;
}
// fprintf(stderr, "samples is %d./n", samples);
mp3_bytes = lame_encode_buffer_interleaved(gfp, input_buff, samples/4, mp3_buff, MP3BUFF_SIZE);
//fprintf(stderr, "mp3_bytes is %d./n", mp3_bytes);
if (mp3_bytes < 0) {
printf("lame_encode_buffer_interleaved returned %d/n", mp3_bytes);
status = -1;
goto free_buffers;
}
write_bytes = write(fd_f, mp3_buff, mp3_bytes);
//fprintf(stderr, "write_bytes is %d./n", write_bytes);
if(write_bytes < 0){
perror("write sound data file failed");
status = -1;
goto free_buffers;
}
}


mp3_bytes = lame_encode_flush(gfp, mp3_buff, sizeof(mp3_buff));
if (mp3_bytes > 0) {
fprintf(stderr, "writing %d mp3 bytes/n", mp3_bytes);
if(write(fd_f, mp3_buff, mp3_bytes) <0)
fprintf(stderr, "'writing mp3 bytes error/n");
}
else
fprintf(stderr, "writing mp3 bytes 0/n");
free_buffers:
free(mp3_buff);
mp3_buff = NULL;
free(input_buff);
input_buff = NULL;

close(snd_f);
close(fd_f);
close_lame:
lame_close(gfp);
exit:
return status;
}

你可能感兴趣的:(mp3,lame,wav)