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
{
}
}