C或C ++ code使用LAME API到M4A(MPEG-4音频)转换成MP3 [英] C, or C++ code for using LAME API to convert an M4A (MPEG-4 audio) to MP3

查看:1033
本文介绍了C或C ++ code使用LAME API到M4A(MPEG-4音频)转换成MP3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是本地LAME code为我的Andr​​oid应用程序的一部分。此code为应该采取指向输入文件(M4A)和一个串到输出文件(MP3)的字符串。我发现了一些code,这似乎从我可以收集,做这样的事情。然而,当我播放的MP3文件,我听到的是一个ZIP!声音。无论是录制多久,我得到了同样的声音。

我在想,这有什么做的采样率,所以我尝试了所有的标准的人,并得到几乎相同的结果(有的拉链不太...比比)。

下面是唯一的C ​​code,它我已经能够找到,但是,我愿意用C ++解决方案,以及

 的#include< jni.h>
#包括LT&;&string.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;机器人/ log.h>
#包括lame.h#定义DEBUG_TAGWBA虚空Java_net_smartnotes_media_RawAudioRecorder_trans code(JNIEnv的* ENV,jobject这一点,从的jstring,为的jstring){
    jboolean isCopy;
    为const char * szFrom =(* ENV) - GT; GetStringUTFChars(ENV,从,&安培; isCopy);
    为const char * szTo =(* ENV) - GT; GetStringUTFChars(ENV,于,安培; isCopy);    __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,NDK:LC:[%S],szFrom);
    __android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,NDK:LC:[%S],szTo);    诠释读,写;    FILE * PCM = FOPEN(szFrom,RB);
    FILE * MP3 = FOPEN(szTo,WB);    const int的PCM_SIZE = 8192;
    const int的MP3_SIZE = 8192;    短整型pcm_buffer [PCM_SIZE * 2];
    unsigned char型mp3_buffer [MP3_SIZE]    lame_t跛= lame_init();
    lame_set_in_samplerate(跛脚,44100);
    lame_set_VBR(跛脚,vbr_default);
    lame_init_params(跛脚);    做{
        读= FREAD(pcm_buffer,2 * sizeof的(短整型),PCM_SIZE,PCM);
        如果(阅读== 0)
            写= lame_en code_flush(瘸子,mp3_buffer,MP3_SIZE);
        其他
            写= lame_en code_buffer_interleaved(瘸子,pcm_buffer,读,mp3_buffer,MP3_SIZE);
        FWRITE(mp3_buffer,写1,MP3);
    }而(读!= 0);    lame_close(跛脚);
    FCLOSE(MP3);
    FCLOSE(PCM);    (* ENV) - GT; ReleaseStringUTFChars(ENV,从,szFrom);
    (* ENV) - GT; ReleaseStringUTFChars(ENV,于szTo);
 }


解决方案

您没有发送到它之前LAME解码M4A等音频为原始音频数据。

PCM音频是指没有COM pression。

I'm using native LAME code for a part of my Android application. This code is supposed to take a string pointing to the input file (M4A) and a string to the output file (MP3). I found some code that seems, from what I can gather, to do such a thing. However, when I play back the MP3 files, all I hear is a ZIP! sound. No matter how long the recording is, I get the same sound.

I was thinking that this had something to do with the sample rate, so I've tried all of the standard ones, and get nearly the same result (some zips are less... zippy).

Here is the the only C code that I've been able to find, however, I'm open to using a C++ solution, as well.

#include <jni.h>  
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include "lame.h"

#define DEBUG_TAG "WBA"  

void Java_net_smartnotes_media_RawAudioRecorder_transcode(JNIEnv * env, jobject this, jstring from, jstring to) {
    jboolean isCopy;  
    const char * szFrom = (*env)->GetStringUTFChars(env, from, &isCopy);
    const char * szTo = (*env)->GetStringUTFChars(env, to, &isCopy);

    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szFrom);
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szTo);

    int read, write;

    FILE *pcm = fopen(szFrom, "rb");
    FILE *mp3 = fopen(szTo, "wb");

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

    (*env)->ReleaseStringUTFChars(env, from, szFrom);
    (*env)->ReleaseStringUTFChars(env, to, szTo); 
 }

解决方案

You are not decoding the M4A audio into raw audio data before sending it into LAME.

"PCM audio" means no compression.

这篇关于C或C ++ code使用LAME API到M4A(MPEG-4音频)转换成MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆