Lame框架 MP3与WAV互转

WAV转换成MP3

- (BOOL)convertWAV:(NSString *)wavFilePath toMP3:(NSString *)mp3FilePath {
    @try {
        int read, write;
        
        FILE *pcm = fopen([wavFilePath cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置
        fseek(pcm, 4*1024, SEEK_CUR);                                  
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置
        
        
        
        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, 16000);   // 采样率,必须与录制时的相同,并且要转换成MP3的话,必须双通道录制
        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) {
        return NO;
    }
    @finally {
        if([[NSFileManager defaultManager] fileExistsAtPath:mp3FilePath]) {
            return YES;
        }else{
            return NO;
        }
    }
}

MP3转换成WAV

- (BOOL)convertMP3:(NSString *)mp3FilePath toPCM:(NSString *)wavFilePath {
    int read, i, samples;
    long wavsize = 0;
    long cumulative_read = 0;
    
    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;
    
    // 输出左右声道
    short int pcm_l[PCM_SIZE], pcm_r[PCM_SIZE];
    unsigned char mp3_buffer[MP3_SIZE];
    
    //input输入MP3文件
    FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "rb");
    fseek(mp3, 0, SEEK_SET);
    
    
    FILE *pcm = fopen([wavFilePath cStringUsingEncoding:1], "wb");  //source 被转换的音频文件位置
    
    
    lame_t lame = lame_init();
    lame_set_decode_only(lame, 1);
    
    hip_t hip = hip_decode_init();
    
    mp3data_struct mp3data;
    memset(&mp3data, 0, sizeof(mp3data));
    
    int nChannels = -1;
    int nSampleRate = -1;
    int mp3_len;
    
    while ((read = fread(mp3_buffer, sizeof(char), MP3_SIZE, mp3)) > 0) {
        mp3_len = read;
        cumulative_read += read * sizeof(char);
        do
        {
            samples = hip_decode1_headers(hip, mp3_buffer, mp3_len, pcm_l, pcm_r, &mp3data);
            wavsize += samples;
            
            if(mp3data.header_parsed == 1)//header is gotten
            {
                if(nChannels < 0)//reading for the first time
                { 
                    [self writeWaveHeader:pcm bytes:0x7FFFFFFF freq:mp3data.samplerate channels:mp3data.stereo bites:16];
                }
                nChannels = mp3data.stereo;
                nSampleRate = mp3data.samplerate;
            }
            
            
            if(samples > 0)
            {
                for(i = 0 ; i < samples; i++)
                {
                    fwrite((char*)&pcm_l[i], sizeof(char), sizeof(pcm_l[i]), pcm);
                    if(nChannels == 2)
                    {
                        fwrite((char*)&pcm_r[i], sizeof(char), sizeof(pcm_r[i]), pcm);
                    }
                }
            }
            mp3_len = 0;
        }while(samples>0);
    }
    
    i = (16 / 8) * mp3data.stereo;
    if (wavsize <= 0)
    {
        wavsize = 0;
    }
    else if (wavsize > 0xFFFFFFD0 / i)
    {
        wavsize = 0xFFFFFFD0;
    }
    else
    {
        wavsize *= i;
    }
    
    if(!fseek(pcm, 0l, SEEK_SET)) {
        [self writeWaveHeader:pcm bytes:(int) wavsize freq:mp3data.samplerate channels:mp3data.stereo bites:16];
    } else {
    }
    
    hip_decode_exit(hip);
    lame_close(lame);
    fclose(mp3);
    fclose(pcm);
    
    return YES;
}

- (void)writeWaveHeader:(FILE *)fp bytes:(int)pcmbytes freq:(int)freq channels:(int)channels bites:(int)bits {
    int     bytes = (bits + 7) / 8;
    fwrite("RIFF", 1, 4, fp); 
    [self write_32_bits_low_high:fp val:pcmbytes + 44 - 8];

    fwrite("WAVEfmt ", 2, 4, fp); 
    [self write_32_bits_low_high:fp val:2 + 2 + 4 + 4 + 2 + 2]; 
    [self write_16_bits_low_high:fp val:1];
    [self write_16_bits_low_high:fp val:channels]; 
    [self write_32_bits_low_high:fp val:freq];
    [self write_32_bits_low_high:fp val:freq * channels * bytes]; 
    [self write_16_bits_low_high:fp val:channels * bytes]; 
    [self write_16_bits_low_high:fp val:bits]; 
    fwrite("data", 1, 4, fp);
    [self write_32_bits_low_high:fp val:pcmbytes]; 
}

- (void)write_16_bits_low_high:(FILE *)fp val:(int)val {
    unsigned char bytes[2];
    bytes[0] = (val & 0xff);
    bytes[1] = ((val >> 8) & 0xff);
    fwrite(bytes, 1, 2, fp);
}

- (void)write_32_bits_low_high:(FILE *)fp val:(int)val {
    unsigned char bytes[4];
    bytes[0] = (val & 0xff);
    bytes[1] = ((val >> 8) & 0xff);
    bytes[2] = ((val >> 16) & 0xff);
    bytes[3] = ((val >> 24) & 0xff);
    fwrite(bytes, 1, 4, fp);
}

推荐阅读更多精彩内容

  • 简介 基于wav音频文件太大,因项目上传作业录音,可能一次上传几十上百个wav 文件,就产生了将wav格式的录音...
    砖加阅读 1,923评论 2 2
  • 要在计算机内播放或是处理音频文件,也就是要对声音文件进行数、模转换,这个过程同样由采样和量化构成,人耳所能听到的声...
    Viking_Den阅读 3,734评论 1 8
  • 无损压缩是在保证不损失源文件所有码率的前提下,将音频文件压缩的更小,也就是说这两种音频格式都能保证源文件码率的无损...
    弓三水阅读 3,541评论 1 2
  • 1、最近在项目遇到上传音频到服务端处理错误问题;当然一般情况下如果双端商量好格式,通过iOS系统的录音框架,上传A...
    浮海_2015阅读 524评论 4 2
  • 1、最近在项目遇到上传音频到服务端处理错误问题;当然一般情况下如果双端商量好格式,通过iOS系统的录音框架,上传A...
    浮海_2015阅读 1,029评论 0 1