Lame框架 MP3與WAV互轉(zhuǎn)

WAV轉(zhuǎn)換成MP3

- (BOOL)convertWAV:(NSString *)wavFilePath toMP3:(NSString *)mp3FilePath {
    @try {
        int read, write;
        
        FILE *pcm = fopen([wavFilePath cStringUsingEncoding:1], "rb");  //source 被轉(zhuǎn)換的音頻文件位置
        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);   // 采樣率,必須與錄制時的相同,并且要轉(zhuǎn)換成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轉(zhuǎn)換成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 被轉(zhuǎn)換的音頻文件位置
    
    
    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);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 簡介 基于wav音頻文件太大,因項目上傳作業(yè)錄音,可能一次上傳幾十上百個wav 文件,就產(chǎn)生了將wav格式的錄音...
    磚加閱讀 4,503評論 2 2
  • 要在計算機內(nèi)播放或是處理音頻文件,也就是要對聲音文件進(jìn)行數(shù)、模轉(zhuǎn)換,這個過程同樣由采樣和量化構(gòu)成,人耳所能聽到的聲...
    Viking_Den閱讀 10,589評論 1 10
  • 無損壓縮是在保證不損失源文件所有碼率的前提下,將音頻文件壓縮的更小,也就是說這兩種音頻格式都能保證源文件碼率的無損...
    風(fēng)起云涌Hal閱讀 9,070評論 1 3
  • 1、最近在項目遇到上傳音頻到服務(wù)端處理錯誤問題;當(dāng)然一般情況下如果雙端商量好格式,通過iOS系統(tǒng)的錄音框架,上傳A...
    浮海_2015閱讀 1,509評論 4 2
  • 1、最近在項目遇到上傳音頻到服務(wù)端處理錯誤問題;當(dāng)然一般情況下如果雙端商量好格式,通過iOS系統(tǒng)的錄音框架,上傳A...
    浮海_2015閱讀 1,922評論 0 1

友情鏈接更多精彩內(nèi)容