Use lame in xcode to convert caf files to MP3 files

Source: Internet
Author: User

You must first reference lamp. H, libmp 3lame.

-(Void) audio_pcmtomp3 :( nsstring *) caffilepath tomp3file :( nsstring *) mp3filepath

{

Nsfilemanager * filemanager = [nsfilemanager defaultmanager];

If ([filemanager removeitematpath: mp3filepath error: Nil])

{

Nslog (@ "delete ");

}

@ Try {

Int read, write;

File * PCM = fopen ([caffilepath cstringusingencoding: 1], "rb"); // location of the audio file to which the source is converted

Fseek (PCM, 4*1024, seek_cur); // skip File Header

File * MP3 = fopen ([mp3filepath cstringusingencoding: 1], "WB"); // output the location of the generated MP3 file

Const int pcm_size = 8192;

Const int mp3_size = 8192;

Short int pcm_buffer [pcm_size * 2];

Unsigned char mp3_buffer [mp3_size];

Lame_t lame = lame_init ();

Lame_set_in_samplerate (lame, 11025.0 );

Lame_set_vbr (lame, vbr_default );

Lame_init_params (lame );

Do {

Read = fread (pcm_buffer, 2 * sizeof (short INT), pcm_size, PCM );

If (read = 0)

Write = lame_encode_flush (lame, mp3_buffer, mp3_size );

Else

Write = lame_encode_buffer_interleaved (lame, pcm_buffer, read, mp3_buffer, mp3_size );

Fwrite (mp3_buffer, write, 1, MP3 );

} While (read! = 0 );

Lame_close (lame );

Fclose (mp3 );

Fclose (PCM );

}

@ Catch (nsexception * exception ){

Nslog (@ "% @", [exception description]);

}

@ Finally

{

}

}

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.