2

First I want to show my method for converting source .wav files to .mp3 by Lame library:

- (void)convertFromWav:(NSString *)sourceFilePath ToMp3:(NSString *)resultName {
  NSString *mp3FileName = [resultName stringByAppendingString:@".mp3"];
  NSString *mp3FilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:mp3FileName];

  @try {
    int read, write;
    FILE *pcm = fopen([sourceFilePath UTF8String], "rb");  //source
    if (pcm == NULL) {
      perror("fopen");
      return;
    }
    fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header
    FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output

    const int sampleRate = 44100;
    const int bitsPerSample = 16;
    const int numberOfChannels = 2;

    const int PCM_SIZE = 8192*2;
    const int MP3_SIZE = 8192*2;
    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];

    lame_t lame = lame_init();
    lame_set_in_samplerate(lame, sampleRate);
    lame_set_VBR(lame, vbr_default);
    lame_init_params(lame);

    lame_get_num_samples(lame);

    long long fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:sourceFilePath error:nil] objectForKey:NSFileSize] longLongValue];
    long duration = fileSize / (sampleRate * numberOfChannels * bitsPerSample / 8);//(fileSize * 8.0f) / (sampleRate * 2);

    lame_set_num_samples(lame, (duration * sampleRate));
    lame_get_num_samples(lame);

    float percent     = 0.0;
    int totalframes = lame_get_totalframes(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);

      int frameNum    = lame_get_frameNum(lame);
      if (frameNum < totalframes)
        percent = (100. * frameNum / totalframes + 0.5);
      else
        percent = 100;

      if ([_delegate respondsToSelector:@selector(convertingProgressChangedWithPercent:)])
      {
        [_delegate convertingProgressChangedWithPercent:percent];
      }

    } while (read != 0);

    lame_close(lame);
    fclose(mp3);
    fclose(pcm);
  }
  @catch (NSException *exception) {
    NSLog(@"%@",[exception description]);
  }
  @finally {
    if ([_delegate respondsToSelector:@selector(convertingDidFinish:)])
    {
      [_delegate convertingDidFinish:mp3FilePath];
    }
  }
}

It's okay and it's working. As a result I have .mp3 which has 152000 bits per second. But I want to make it 320000 bits per second. How can I change it? I am not good in theory about this stuff so I don't know which values change to what. Thanks.

Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
  • Why not use CBR instead of VBR? A constant bit rate would allow for a set number... – VC.One Mar 01 '16 at 16:57
  • I do not know much about these things and I know that I should study it more but for now it would be great if I could result as 320kbits. So have can I change it to CBR? – Libor Zapletal Mar 01 '16 at 21:03

1 Answers1

2

You want to use lame_set_VBR (lame_t, vbr_off); and then you can use lame_set_brate where you can set the required bitrate amount. Using vbr_off gives you CBR mode as confirmed in the docs (see headers.h) :

*********************************************************************
VBR control
************************************************************************
/* Types of VBR. default = vbr_off = CBR */
int CDECL lame_set_VBR(lame_global_flags *, vbr_mode);
vbr_mode CDECL lame_get_VBR(const lame_global_flags *);

Try this :

//# for constants of settings
const int sampleRate = 44100;
const int bitsPerSample = 16;
const int numberOfChannels = 2;

const int myBitRate = 320;

//# for Lame settings
lame_t lame = lame_init();
lame_set_in_samplerate  (lame_t, sampleRate); //is 44100
lame_set_VBR            (lame_t, vbr_off); //force CBR mode
lame_set_brate          (lame_t, myBitRate); //is 320
lame_init_params        (lame_t);

Also you can probably setup Lame like this :
lame_t lame = lame_init(); instead becomes like this : lame_t = lame_init();

Just saying that if you defined a lame_t I would expect it to require that name for rest of settings. You know like lame_init_params (lame_t); etc.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • I found a problem that I don't have method `lame_set_CBR` (I am using Lame library recompiled for iOS and ObjC). So I must use VBR. I tried `lame_set_brate` but it didn't help. I found method `lame_set_VBR_min_bitrate_kbps` and it looks like it helps. Do you think it's good solution? Is it correct? – Libor Zapletal Mar 02 '16 at 09:51
  • 1
    Do you have a link to the library? I'll check their docs. Setting a `...min_bitrate` and `...max_bitrate` of the same amount could work as it seems so for you. But using VBR makes the worst kind of MP3 file (from a playback systems & bytes handling point of view). Finally try `lame_set_VBR (lame_t, vbr_off);` before using `lame_set_brate` where it will expect either a value of **320** or **320000**. – VC.One Mar 02 '16 at 10:44
  • 1
    Looks like with `lame_set_VBR (lame_t, vbr_off);` and `lame_set_brate` it's working and without `lame_set_VBR_min_bitrate_kbps`. So maybe when vbr_off is used it's CBR? I used this [link](https://github.com/wuqiong/mp3lame-for-iOS) for generate framework library that I need. – Libor Zapletal Mar 02 '16 at 11:00
  • Okay. So you finally have an MP3 of 320 CBR? I just checked the docs and its confirmed that `vbr_off` gives you CBR mode instead. I wish I saw it earlier. I'll update the useful info for future readers. – VC.One Mar 02 '16 at 11:27
  • @VC.One What is that talk about `lame_t`?!?!? That's a type defined by `lame.h` like so `typedef lame_global_flags *lame_t;` and the sample code from OP uses it correctly. Your code, however, is all wrong. – Alexis Wilke Jan 25 '20 at 19:23