PCM to MP3 audio file (with lame library)

Source: Internet
Author: User
Tags goto printf

Pcm2mp3.c

#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);
		printf ("Input_samples is%d./n", input_samples);
		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; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.