C or C ++ to use the LAME API to convert M4A (MPEG-4 audio) to MP3

I use my own LAME code for part of my Android application. This code should take a line pointing to the input file (M4A) and a line to the output file (MP3). I found code that seems to be from what I can compile to do this. However, when I play MP3 files, all I hear is ZIP! sound. No matter how long the recording takes, I get the same sound.

I thought it was somehow related to the sampling rate, so I tried all the standard ones and got almost the same result (some zip files are smaller ... zippy).

Here is the only C code that I managed to find, however I am open to using a C ++ solution.

#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); 
 }
0
source share
1

M4A ​​LAME.

"PCM audio" .

+3

All Articles