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);
}

你可能感兴趣的:(Lame框架 MP3与WAV互转)