1

I'm trying to convert a WAV file into MP3 file using LAME.

I am using this code. I want to do this in background (or in a queue). As input file is large, it can take the full control to it till finishing. Can anybody help me to do so?

int read, write;
FILE *pcm = fopen([mergeFile cStringUsingEncoding:1], "rb");  //source
fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output

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, 44100);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);

do {
    read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
    NSLog(@"");
    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);

} while (read != 0);

lame_close(lame);
fclose(mp3);
fclose(pcm);
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Duni Chand
  • 13
  • 2

1 Answers1

0

I would use a serial NSOperationQueue to do the encoding.

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;
#ifdef __IPHONE_8_0
queue.qualityOfService = /* a NSQualityOfService */;
#endif

You can add jobs like this:

[queue addOperationWithBlock:^{

    /* Encoding code goes here */

    dispatch_async(dispatch_get_main_queue(), ^{

        /* Report operation status on completion (delegate, notification, etc...)

    }];

}];
Matteo Pacini
  • 21,796
  • 7
  • 67
  • 74