int enclameInit( hb_work_object_t * w, hb_job_t * job ) { hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) ); hb_audio_t * audio = w->audio; w->private_data = pv; pv->job = job; hb_log( "enclame: opening libmp3lame" ); pv->lame = lame_init(); // use ABR lame_set_scale( pv->lame, 32768.0 ); if( audio->config.out.compression_level >= 0 ) { lame_set_quality( pv->lame, audio->config.out.compression_level ); } if( audio->config.out.bitrate > 0 ) { lame_set_VBR( pv->lame, vbr_abr ); lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate ); } else if( audio->config.out.quality >= 0 ) { lame_set_brate( pv->lame, 0 ); lame_set_VBR( pv->lame, vbr_default ); lame_set_VBR_quality( pv->lame, audio->config.out.quality ); } lame_set_in_samplerate( pv->lame, audio->config.out.samplerate ); lame_set_out_samplerate( pv->lame, audio->config.out.samplerate ); pv->out_discrete_channels = hb_mixdown_get_discrete_channel_count( audio->config.out.mixdown ); // Lame's default encoding mode is JOINT_STEREO. This subtracts signal // that is "common" to left and right (within some threshold) and encodes // it separately. This improves quality at low bitrates, but hurts // imaging (channel separation) at higher bitrates. So if the bitrate // is suffeciently high, use regular STEREO mode. if ( pv->out_discrete_channels == 1 ) { lame_set_mode( pv->lame, MONO ); lame_set_num_channels( pv->lame, 1 ); } else if ( audio->config.out.bitrate >= 128 ) { lame_set_mode( pv->lame, STEREO ); } lame_init_params( pv->lame ); pv->input_samples = 1152 * pv->out_discrete_channels; pv->output_bytes = LAME_MAXMP3BUFFER; pv->buf = malloc( pv->input_samples * sizeof( float ) ); audio->config.out.samples_per_frame = 1152; pv->list = hb_list_init(); pv->pts = -1; return 0; }
bool LameAudioWriter::open_private() { m_fid = fopen(m_fileName.toUtf8().data(), "w+"); if (!m_fid) { return false; } m_lameInfo->flags = lame_init(); if (m_lameInfo->flags == 0) { PERROR("lame_init failed."); return false; } if (m_channels == 1) { lame_set_mode(m_lameInfo->flags, MONO); } lame_set_num_channels(m_lameInfo->flags, m_channels); lame_set_in_samplerate(m_lameInfo->flags, m_rate); lame_set_out_samplerate(m_lameInfo->flags, m_rate); if(m_method == 0) { // Constant Bitrate lame_set_VBR(m_lameInfo->flags, vbr_off); lame_set_brate(m_lameInfo->flags, m_maxBitrate); } else if (m_method == 1) { // Average Bitrate lame_set_VBR(m_lameInfo->flags, vbr_abr); lame_set_VBR_mean_bitrate_kbps(m_lameInfo->flags, m_maxBitrate); } else if (m_method == 2) { // Variable Bitrate (old) lame_set_VBR(m_lameInfo->flags, vbr_default); lame_set_VBR_min_bitrate_kbps(m_lameInfo->flags, m_minBitrate); lame_set_VBR_max_bitrate_kbps(m_lameInfo->flags, m_maxBitrate); } else if (m_method == 3) { // Variable Bitrate (new) lame_set_VBR(m_lameInfo->flags, vbr_default); lame_set_VBR_min_bitrate_kbps(m_lameInfo->flags, m_minBitrate); lame_set_VBR_max_bitrate_kbps(m_lameInfo->flags, m_maxBitrate); } lame_set_quality(m_lameInfo->flags, m_quality); // // file options // lame_set_copyright(m_lameInfo->flags, false); lame_set_original(m_lameInfo->flags, true); lame_set_strict_ISO(m_lameInfo->flags, false); lame_set_error_protection(m_lameInfo->flags, false); return (lame_init_params(m_lameInfo->flags ) != -1); }
int doInit(LameConf* conf) { int result; conf->gf=lame_init(); if (conf->gf==NULL) { //throwRuntimeException(env, "out of memory"); return org_tritonus_lowlevel_lame_Lame_OUT_OF_MEMORY; } lame_set_num_channels(conf->gf, conf->channels); lame_set_in_samplerate(conf->gf, conf->sampleRate); if (conf->mode!=org_tritonus_lowlevel_lame_Lame_CHANNEL_MODE_AUTO) { lame_set_mode(conf->gf, conf->mode); } if (conf->VBR) { lame_set_VBR(conf->gf, vbr_default); lame_set_VBR_q(conf->gf, conf->quality); } else { if (conf->bitrate!=org_tritonus_lowlevel_lame_Lame_BITRATE_AUTO) { lame_set_brate(conf->gf, conf->bitrate); } } lame_set_quality(conf->gf, conf->quality); result=lame_init_params(conf->gf); // return effective values conf->sampleRate=lame_get_out_samplerate(conf->gf); conf->bitrate=lame_get_brate(conf->gf); conf->mode=lame_get_mode(conf->gf); conf->VBR=lame_get_VBR(conf->gf); conf->quality=(conf->VBR)?lame_get_VBR_q(conf->gf):lame_get_quality(conf->gf); conf->mpegVersion=lame_get_version(conf->gf); return result; }
MP3Encoder(UINT bitRate) { curBitRate = bitRate; lgf = lame_init(); if(!lgf) CrashError(TEXT("Unable to open mp3 encoder")); lame_set_in_samplerate(lgf, App->GetSampleRateHz()); lame_set_out_samplerate(lgf, App->GetSampleRateHz()); lame_set_num_channels(lgf, 2); lame_set_mode(lgf, STEREO); lame_set_disable_reservoir(lgf, TRUE); //bit reservoir has to be disabled for seamless streaming lame_set_VBR(lgf, vbr_off); lame_set_brate(lgf, bitRate); lame_init_params(lgf); outputFrameSize = lame_get_framesize(lgf); //1152 usually dwMP3MaxSize = DWORD(1.25*double(outputFrameSize*audioBlockSize) + 7200.0); MP3OutputBuffer.SetSize(dwMP3MaxSize+1); MP3OutputBuffer[0] = 0x2f; bFirstPacket = true; Log(TEXT("------------------------------------------")); Log(TEXT("%s"), GetInfoString().Array()); }
JNIEXPORT jint JNICALL JNIDEFINE(nativeOpenEncoder)(JNIEnv* env, jclass clz) { lame = lame_init(); lame_set_in_samplerate(lame, 44100); //lame_set_num_channels(lame, 1); lame_set_VBR(lame, vbr_default); lame_init_params(lame); return 0; }
static bool lame_encoder_setup(struct lame_encoder *encoder, GError **error) { if (encoder->quality >= -1.0) { /* a quality was configured (VBR) */ if (0 != lame_set_VBR(encoder->gfp, vbr_rh)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame VBR mode"); return false; } if (0 != lame_set_VBR_q(encoder->gfp, encoder->quality)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame VBR quality"); return false; } } else { /* a bit rate was configured */ if (0 != lame_set_brate(encoder->gfp, encoder->bitrate)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame bitrate"); return false; } } if (0 != lame_set_num_channels(encoder->gfp, encoder->audio_format.channels)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame num channels"); return false; } if (0 != lame_set_in_samplerate(encoder->gfp, encoder->audio_format.sample_rate)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame sample rate"); return false; } if (0 != lame_set_out_samplerate(encoder->gfp, encoder->audio_format.sample_rate)) { g_set_error(error, lame_encoder_quark(), 0, "error setting lame out sample rate"); return false; } if (0 > lame_init_params(encoder->gfp)) { g_set_error(error, lame_encoder_quark(), 0, "error initializing lame params"); return false; } return true; }
void AudioReader::exportAudioDataToMP3(const char* data) { QSettings settings; emit TCMessage("Exporting MP3..."); int read, write; FILE *pcm = fopen("TCTemp.wav", "wb"); fwrite(data,1,format.chunkSize,pcm); fflush(pcm); pcm = fopen("TCTemp.wav", "rb"); f = fopen(this->outfile.toLocal8Bit().constData(), "wb"); const int PCM_SIZE = 8192; const int MP3_SIZE = 8192*2; short int pcm_buffer[PCM_SIZE*2]; unsigned char mp3_buffer[MP3_SIZE]; lame_t lame = lame_init(); lame_set_in_samplerate(lame, format.SampleRate); lame_set_VBR(lame, (vbr_mode)settings.value("vbr",0).toInt()); if(lame_get_VBR(lame)) { lame_set_VBR_min_bitrate_kbps(lame,settings.value("min",128).toInt()); lame_set_VBR_max_bitrate_kbps(lame,settings.value("max",192).toInt()); } else { lame_set_brate(lame,settings.value("min",128).toInt()); } lame_set_num_channels(lame,(int)format.Channels); lame_set_quality(lame,0); if(format.Channels==1)lame_set_mode(lame,MONO); else lame_set_mode(lame,STEREO); lame_init_params(lame); do { if(!knock->running)break; read = fread(pcm_buffer, 2*byte_size, PCM_SIZE, pcm); fflush(f); 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, f); fflush(f); } while (read != 0); lame_close(lame); fclose(pcm); if(unlink("TCTemp.wav")!=0) { emit TCColor("color: red;"); emit TCMessage(QString("Error while removing TCTemp.wav")); } }
void Encoder(char* src, char* dst){ //printf("Inside Encoder : %s \n",src); //printf("Inside Encoder : %s \n",dst); if(src == NULL || dst == NULL) return; int read, write; FILE* wav; FILE* mp3; wav = fopen(src, "rb"); if(wav == NULL){ printf(" Unable to open the file %s \n", src); return; } mp3 = fopen(dst, "wb"); if(mp3 == NULL){ printf(" Unable to open the file %s \n", dst); return; } const int PCM_SIZE = 8192; const int MP3_SIZE = 8192; short int PCM_BUFFER[2*PCM_SIZE]; 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, wav); 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(wav); fclose(mp3); }
////////////////////////////////////////////////////////////////////// // Init - initialized or reiniyialized encoder SDK with given input // and output settings ////////////////////////////////////////////////////////////////////// HRESULT CEncoder::Init() { CAutoLock l(&m_lock); m_outOffset = 0; m_outReadOffset = 0; m_bFinished = FALSE; m_frameCount = 0; if (!pgf) { if (!m_bInpuTypeSet || !m_bOutpuTypeSet) return E_UNEXPECTED; // Init Lame library // note: newer, safer interface which doesn't // allow or require direct access to 'gf' struct is being written // see the file 'API' included with LAME. if (pgf = lame_init()) { lame_set_num_channels(pgf, m_wfex.nChannels); lame_set_in_samplerate(pgf, m_wfex.nSamplesPerSec); lame_set_out_samplerate(pgf, m_mabsi.dwSampleRate); if ((lame_get_out_samplerate(pgf) >= 32000) && (m_mabsi.dwBitrate < 32)) lame_set_brate(pgf, 32); else lame_set_brate(pgf, m_mabsi.dwBitrate); lame_set_VBR(pgf, m_mabsi.vmVariable); lame_set_VBR_min_bitrate_kbps(pgf, m_mabsi.dwVariableMin); lame_set_VBR_max_bitrate_kbps(pgf, m_mabsi.dwVariableMax); lame_set_copyright(pgf, m_mabsi.bCopyright); lame_set_original(pgf, m_mabsi.bOriginal); lame_set_error_protection(pgf, m_mabsi.bCRCProtect); lame_set_bWriteVbrTag(pgf, m_mabsi.dwXingTag); lame_set_strict_ISO(pgf, m_mabsi.dwStrictISO); lame_set_VBR_hard_min(pgf, m_mabsi.dwEnforceVBRmin); if (lame_get_num_channels(pgf) == 2 && !m_mabsi.bForceMono) { //int act_br = pgf->VBR ? pgf->VBR_min_bitrate_kbps + pgf->VBR_max_bitrate_kbps / 2 : pgf->brate; // Disabled. It's for user's consideration now //int rel = pgf->out_samplerate / (act_br + 1); //pgf->mode = rel < 200 ? m_mabsi.ChMode : JOINT_STEREO; lame_set_mode(pgf, m_mabsi.ChMode); } else lame_set_mode(pgf, MONO); if (lame_get_mode(pgf) == JOINT_STEREO) lame_set_force_ms(pgf, m_mabsi.dwForceMS); else lame_set_force_ms(pgf, 0); // pgf->mode_fixed = m_mabsi.dwModeFixed; if (m_mabsi.dwVoiceMode != 0) { lame_set_lowpassfreq(pgf,12000); ///pgf->VBR_max_bitrate_kbps = 160; } if (m_mabsi.dwKeepAllFreq != 0) { ///pgf->lowpassfreq = -1; ///pgf->highpassfreq = -1; /// not available anymore } lame_set_quality(pgf, m_mabsi.dwQuality); lame_set_VBR_q(pgf, m_mabsi.dwVBRq); lame_init_params(pgf); // encoder delay compensation { int const nch = lame_get_num_channels(pgf); short * start_padd = (short *)calloc(48, nch * sizeof(short)); int out_bytes = 0; if (nch == 2) out_bytes = lame_encode_buffer_interleaved(pgf, start_padd, 48, m_outFrameBuf, OUT_BUFFER_SIZE); else out_bytes = lame_encode_buffer(pgf, start_padd, start_padd, 48, m_outFrameBuf, OUT_BUFFER_SIZE); if (out_bytes > 0) m_outOffset += out_bytes; free(start_padd); } return S_OK; } return E_FAIL; } return S_OK; }
Mp3OutputStream::Mp3OutputStream() : global_fags(), bytes_per_sample(), bytes_per_second() #ifdef Mp3OutputStream_Buffering m_inBuffer(), m_inBufferLenght(), m_inBufferPos(), #endif ,m_outBuffer(), m_outBufferLenght() { } Mp3OutputStream::~Mp3OutputStream() { Close(); #ifdef Mp3OutputStream_Buffering delete [] m_inBuffer; #endif delete [] m_outBuffer; } bool Mp3OutputStream::Initialize(int samplerate, int numchannels, int bitspersamp) { global_fags = lame_init(); if(global_fags == nullptr) return false; //lame_set_errorf(global_fags, lame_debugger); lame_set_analysis(global_fags, 0); lame_set_in_samplerate(global_fags, samplerate); lame_set_bWriteVbrTag( global_fags, 0 ); lame_set_error_protection( global_fags, 0 ); lame_set_extension( global_fags, 0 ); lame_set_original( global_fags, 0 ); lame_set_VBR(global_fags, vbr_off); lame_set_num_channels(global_fags, numchannels); lame_set_mode( global_fags, STEREO); lame_set_out_samplerate(global_fags, 44100); // 9 = LQP_LOW_QUALITY // 2 = LQP_HIGH_QUALITY: // 0 = LQP_VERYHIGH_QUALITY //lame_set_quality(global_fags, 0 ); //lame_set_preset( global_fags, STANDARD_FAST); Settings& s = Global::Settings; #ifdef STREAMER_PAYD_VERSION lame_set_brate(global_fags, std::min(Mp3OutputStreamConstants::Presets[s.encoder_preset], 320)); #else lame_set_brate(global_fags, 64); #endif if(lame_init_params(global_fags) != 0) { OutputDebugString(_T("lame_init_params failed")); lame_close(global_fags); global_fags = nullptr; return false; } bytes_per_second = lame_get_brate(global_fags) / 8; #ifdef Mp3OutputStream_Buffering if ( 0 == lame_get_version( global_fags ) ) { // For MPEG-II, only 576 samples per frame per channel m_inBufferLenght = 576 * lame_get_num_channels( global_fags ); } else { // For MPEG-I, 1152 samples per frame per channel m_inBufferLenght = 1152 * lame_get_num_channels( global_fags ); } // delete buffers if(m_inBuffer != nullptr) delete [] m_inBuffer; m_inBuffer = new BYTE[m_inBufferLenght * 2]; #endif m_outBufferLenght = static_cast<int>( ( 1.25 * ( samplerate / numchannels ) + 7200 ) * 2.0); if(m_outBuffer != nullptr) delete [] m_outBuffer; m_outBuffer = new BYTE[m_outBufferLenght]; bytes_per_sample = bitspersamp / 8; return true; }
/*------------------------------------------------------------------------------ * Open an encoding session *----------------------------------------------------------------------------*/ bool LameLibEncoder :: open ( void ) throw ( Exception ) { if ( isOpen() ) { close(); } lameGlobalFlags = lame_init(); // ugly lame returns -1 in a pointer on allocation errors if ( !lameGlobalFlags || ((int)lameGlobalFlags) == -1 ) { throw Exception( __FILE__, __LINE__, "lame lib init error", (int) lameGlobalFlags); } if ( 0 > lame_set_num_channels( lameGlobalFlags, getInChannel()) ) { throw Exception( __FILE__, __LINE__, "lame lib setting channels error", getInChannel() ); } if ( 0 > lame_set_mode( lameGlobalFlags, getOutChannel() == 1 ? MONO : JOINT_STEREO) ) { throw Exception( __FILE__, __LINE__, "lame lib setting mode error", JOINT_STEREO ); } reportEvent( 5, "set lame mode", lame_get_mode( lameGlobalFlags)); reportEvent( 5, "set lame channels", lame_get_num_channels( lameGlobalFlags)); if ( 0 > lame_set_in_samplerate( lameGlobalFlags, getInSampleRate()) ) { throw Exception( __FILE__, __LINE__, "lame lib setting input sample rate error", getInSampleRate() ); } reportEvent( 5, "set lame in sample rate", lame_get_in_samplerate( lameGlobalFlags)); if ( 0 > lame_set_out_samplerate( lameGlobalFlags, getOutSampleRate()) ) { throw Exception( __FILE__, __LINE__, "lame lib setting output sample rate error", getOutSampleRate() ); } reportEvent( 5, "set lame out sample rate", lame_get_out_samplerate( lameGlobalFlags)); switch ( getOutBitrateMode() ) { case cbr: { if ( 0 > lame_set_brate( lameGlobalFlags, getOutBitrate()) ) { throw Exception( __FILE__, __LINE__, "lame lib setting output bit rate error", getOutBitrate() ); } reportEvent( 5, "set lame bit rate", lame_get_brate( lameGlobalFlags)); double d = (1.0 - getOutQuality()) * 10.0; int q = int (d + 0.499999); if ( 0 > lame_set_quality( lameGlobalFlags, q) ) { throw Exception( __FILE__, __LINE__, "lame lib setting quality error", q); } reportEvent( 5, "set lame quality", lame_get_quality( lameGlobalFlags)); } break; case abr: if ( 0 > lame_set_VBR( lameGlobalFlags,vbr_abr)) { throw Exception( __FILE__, __LINE__, "lame lib setting abr error", vbr_abr); } reportEvent( 5, "set lame abr bitrate", lame_get_VBR( lameGlobalFlags)); if ( 0 > lame_set_VBR_mean_bitrate_kbps( lameGlobalFlags, getOutBitrate())) { throw Exception( __FILE__, __LINE__, "lame lib setting abr mean bitrate error", getOutBitrate()); } reportEvent( 5, "set lame abr mean bitrate", lame_get_VBR_mean_bitrate_kbps( lameGlobalFlags)); break; case vbr: { if ( 0 > lame_set_VBR( lameGlobalFlags, vbr_mtrh)) { throw Exception( __FILE__, __LINE__, "lame lib setting vbr error", vbr_mtrh ); } reportEvent( 5, "set lame vbr bitrate", lame_get_VBR( lameGlobalFlags)); double d = (1.0 - getOutQuality()) * 10.0; int q = int (d + 0.499999); if ( 0 > lame_set_VBR_q( lameGlobalFlags, q) ) { throw Exception( __FILE__, __LINE__, "lame lib setting vbr quality error", q); } reportEvent( 5, "set lame vbr quality", lame_get_VBR_q( lameGlobalFlags)); } break; } if ( 0 > lame_set_lowpassfreq( lameGlobalFlags, lowpass) ) { throw Exception( __FILE__, __LINE__, "lame lib setting lowpass frequency error", lowpass ); } reportEvent( 5, "set lame lowpass frequency", lame_get_lowpassfreq( lameGlobalFlags)); if ( 0 > lame_set_highpassfreq( lameGlobalFlags, highpass) ) { throw Exception( __FILE__, __LINE__, "lame lib setting highpass frequency error", lowpass ); } reportEvent( 5, "set lame highpass frequency", lame_get_highpassfreq( lameGlobalFlags)); // not configurable lame settings if ( 0 > lame_set_exp_nspsytune( lameGlobalFlags, 1) ) { throw Exception( __FILE__, __LINE__, "lame lib setting psycho acoustic model error"); } reportEvent( 5, "set lame psycho acoustic model", lame_get_exp_nspsytune( lameGlobalFlags)); if ( 0 > lame_set_error_protection( lameGlobalFlags, 1) ) { throw Exception( __FILE__, __LINE__, "lame lib setting error protection error", 1 ); } reportEvent( 5, "set lame error protection", lame_get_error_protection( lameGlobalFlags)); // let lame init its own params based on our settings if ( 0 > lame_init_params( lameGlobalFlags) ) { throw Exception( __FILE__, __LINE__, "lame lib initializing params error" ); } lame_print_config( lameGlobalFlags); // open the underlying sink if ( !sink->open() ) { throw Exception( __FILE__, __LINE__, "lame lib opening underlying sink error"); } return true; }
bool ACMStream::open(const AEncodeProperties & the_Properties) { bool bResult = false; // Init the MP3 Stream // Init the global flags structure gfp = lame_init(); // Set input sample frequency lame_set_in_samplerate( gfp, my_SamplesPerSec ); // Set output sample frequency lame_set_out_samplerate( gfp, my_OutBytesPerSec ); lame_set_num_channels( gfp, my_Channels ); if (my_Channels == 1) lame_set_mode( gfp, MONO ); else lame_set_mode( gfp, (MPEG_mode_e)the_Properties.GetChannelModeValue()) ; /// \todo Get the mode from the default configuration // lame_set_VBR( gfp, vbr_off ); /// \note VBR not supported for the moment lame_set_VBR( gfp, my_VBRMode ); /// \note VBR not supported for the moment if (my_VBRMode == vbr_abr) { lame_set_VBR_q( gfp, 1 ); lame_set_VBR_mean_bitrate_kbps( gfp, (my_AvgBytesPerSec * 8 + 500) / 1000 ); if (24000 > lame_get_in_samplerate( gfp )) { // For MPEG-II lame_set_VBR_min_bitrate_kbps( gfp, 8); lame_set_VBR_max_bitrate_kbps( gfp, 160); } else { // For MPEG-I lame_set_VBR_min_bitrate_kbps( gfp, 32); lame_set_VBR_max_bitrate_kbps( gfp, 320); } } // Set bitrate lame_set_brate( gfp, my_AvgBytesPerSec * 8 / 1000 ); /// \todo Get the mode from the default configuration // Set copyright flag? lame_set_copyright( gfp, the_Properties.GetCopyrightMode()?1:0 ); // Do we have to tag it as non original lame_set_original( gfp, the_Properties.GetOriginalMode()?1:0 ); // Add CRC? lame_set_error_protection( gfp, the_Properties.GetCRCMode()?1:0 ); // Set private bit? lame_set_extension( gfp, the_Properties.GetPrivateMode()?1:0 ); // INFO tag support not possible in ACM - it requires rewinding // output stream to the beginning after encoding is finished. lame_set_bWriteVbrTag( gfp, 0 ); if (0 == lame_init_params( gfp )) { //LAME encoding call will accept any number of samples. if ( 0 == lame_get_version( gfp ) ) { // For MPEG-II, only 576 samples per frame per channel my_SamplesPerBlock = 576 * lame_get_num_channels( gfp ); } else { // For MPEG-I, 1152 samples per frame per channel my_SamplesPerBlock = 1152 * lame_get_num_channels( gfp ); } } my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "version =%d",lame_get_version( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Layer =3"); switch ( lame_get_mode( gfp ) ) { case STEREO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Stereo" ); break; case JOINT_STEREO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Joint-Stereo" ); break; case DUAL_CHANNEL: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Forced Stereo" ); break; case MONO: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Mono" ); break; case NOT_SET: /* FALLTROUGH */ default: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "mode =Error (unknown)" ); break; } my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "sampling frequency =%.1f kHz", lame_get_in_samplerate( gfp ) /1000.0 ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "bitrate =%d kbps", lame_get_brate( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Vbr Min bitrate =%d kbps", lame_get_VBR_min_bitrate_kbps( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Vbr Max bitrate =%d kbps", lame_get_VBR_max_bitrate_kbps( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Quality Setting =%d", lame_get_quality( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Low pass frequency =%d", lame_get_lowpassfreq( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Low pass width =%d", lame_get_lowpasswidth( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "High pass frequency =%d", lame_get_highpassfreq( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "High pass width =%d", lame_get_highpasswidth( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "No Short Blocks =%d", lame_get_no_short_blocks( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "de-emphasis =%d", lame_get_emphasis( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "private flag =%d", lame_get_extension( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "copyright flag =%d", lame_get_copyright( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "original flag =%d", lame_get_original( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "CRC =%s", lame_get_error_protection( gfp ) ? "on" : "off" ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Fast mode =%s", ( lame_get_quality( gfp ) )? "enabled" : "disabled" ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Force mid/side stereo =%s", ( lame_get_force_ms( gfp ) )?"enabled":"disabled" ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Padding Type =%d", lame_get_padding_type( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Disable Resorvoir =%d", lame_get_disable_reservoir( gfp ) ); my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "VBR =%s, VBR_q =%d, VBR method =", ( lame_get_VBR( gfp ) !=vbr_off ) ? "enabled": "disabled", lame_get_VBR_q( gfp ) ); switch ( lame_get_VBR( gfp ) ) { case vbr_off: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_off" ); break; case vbr_mt : my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_mt" ); break; case vbr_rh : my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_rh" ); break; case vbr_mtrh: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_mtrh" ); break; case vbr_abr: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "vbr_abr (average bitrate %d kbps)", lame_get_VBR_mean_bitrate_kbps( gfp ) ); break; default: my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "error, unknown VBR setting"); break; } my_debug->OutPut(DEBUG_LEVEL_FUNC_DEBUG, "Write VBR Header =%s\n", ( lame_get_bWriteVbrTag( gfp ) ) ?"Yes":"No"); #ifdef FROM_DLL beConfig.format.LHV1.dwReSampleRate = my_OutBytesPerSec; // force the user resampling #endif // FROM_DLL bResult = true; return bResult; }
static gint mp3_open(void) { int imp3; gfp = lame_init(); if (gfp == NULL) return 0; /* setup id3 data */ id3tag_init(gfp); if (tuple) { /* XXX write UTF-8 even though libmp3lame does id3v2.3. --yaz */ lameid3.track_name = tuple_get_str (tuple, FIELD_TITLE, NULL); id3tag_set_title(gfp, lameid3.track_name); lameid3.performer = tuple_get_str (tuple, FIELD_ARTIST, NULL); id3tag_set_artist(gfp, lameid3.performer); lameid3.album_name = tuple_get_str (tuple, FIELD_ALBUM, NULL); id3tag_set_album(gfp, lameid3.album_name); lameid3.genre = tuple_get_str (tuple, FIELD_GENRE, NULL); id3tag_set_genre(gfp, lameid3.genre); lameid3.year = str_printf ("%d", tuple_get_int (tuple, FIELD_YEAR, NULL)); id3tag_set_year(gfp, lameid3.year); lameid3.track_number = str_printf ("%d", tuple_get_int (tuple, FIELD_TRACK_NUMBER, NULL)); id3tag_set_track(gfp, lameid3.track_number); if (force_v2_val) { id3tag_add_v2(gfp); } if (only_v1_val) { id3tag_v1_only(gfp); } if (only_v2_val) { id3tag_v2_only(gfp); } } /* input stream description */ lame_set_in_samplerate(gfp, input.frequency); lame_set_num_channels(gfp, input.channels); /* Maybe implement this? */ /* lame_set_scale(lame_global_flags *, float); */ lame_set_out_samplerate(gfp, out_samplerate_val); /* general control parameters */ lame_set_bWriteVbrTag(gfp, toggle_xing_val); lame_set_quality(gfp, algo_quality_val); if (audio_mode_val != 4) { AUDDBG("set mode to %d\n", audio_mode_val); lame_set_mode(gfp, audio_mode_val); } lame_set_errorf(gfp, lame_debugf); lame_set_debugf(gfp, lame_debugf); lame_set_msgf(gfp, lame_debugf); if (enc_toggle_val == 0 && vbr_on == 0) lame_set_brate(gfp, bitrate_val); else if (vbr_on == 0) lame_set_compression_ratio(gfp, compression_val); /* frame params */ lame_set_copyright(gfp, mark_copyright_val); lame_set_original(gfp, mark_original_val); lame_set_error_protection(gfp, error_protect_val); lame_set_strict_ISO(gfp, enforce_iso_val); if (vbr_on != 0) { if (vbr_type == 0) lame_set_VBR(gfp, 2); else lame_set_VBR(gfp, 3); lame_set_VBR_q(gfp, vbr_quality_val); lame_set_VBR_mean_bitrate_kbps(gfp, abr_val); lame_set_VBR_min_bitrate_kbps(gfp, vbr_min_val); lame_set_VBR_max_bitrate_kbps(gfp, vbr_max_val); lame_set_VBR_hard_min(gfp, enforce_min_val); } /* not to write id3 tag automatically. */ lame_set_write_id3tag_automatic(gfp, 0); if (lame_init_params(gfp) == -1) return 0; /* write id3v2 header */ imp3 = lame_get_id3v2_tag(gfp, encbuffer, sizeof(encbuffer)); if (imp3 > 0) { write_output(encbuffer, imp3); id3v2_size = imp3; } else { id3v2_size = 0; } write_buffer = NULL; write_buffer_size = 0; return 1; }
//core function to encide WAV to MP3, relying on LAME library void encode(char* wavFilePath, char* mp3FilePath) { FILE* wavFile = fopen(wavFilePath, "rb"); FILE* mp3File = fopen(mp3FilePath, "wb+"); int readPCMSampleCount; short int pcmBuffer[PCM_BUF_SIZE]; unsigned char mp3Buffer[MP3_BUF_SIZE]; unsigned short channel; unsigned long sampleRate; unsigned short blockAlign; lame_t lame = lame_init(); if((wavFile == NULL) || (mp3File == NULL)) { printErrorMessage(FILE_IO_EXCEPTION); return; } if(lame == NULL) { printErrorMessage(LAME_API_ERROR); return; } //Set number of channels else if(fseek(wavFile, 22, SEEK_SET) != 0) { printErrorMessage(FILE_IO_EXCEPTION); return; } else if(fread(&channel, 2, 1, wavFile) < 1) { printErrorMessage(FILE_IO_EXCEPTION); return; } else { lame_set_num_channels(lame, channel); lame_set_mode(lame, (channel == 1) ? MONO : JOINT_STEREO); } //Set number of sample rate if(fseek(wavFile, 24, SEEK_SET) != 0) { printErrorMessage(FILE_IO_EXCEPTION); return; } else if(fread(&sampleRate, 4, 1, wavFile) < 1) { printErrorMessage(FILE_IO_EXCEPTION); return; } else { lame_set_in_samplerate(lame, sampleRate); lame_set_out_samplerate(lame, sampleRate); } //Set block align (byte/sample) if(fseek(wavFile, 32, SEEK_SET) != 0) { printErrorMessage(FILE_IO_EXCEPTION); return; } else if(fread(&blockAlign, 4, 1, wavFile) < 1) { printErrorMessage(FILE_IO_EXCEPTION); return; } //Look for data chank in the WAV file if(fseek(wavFile, 36, SEEK_SET) != 0) { printErrorMessage(FILE_IO_EXCEPTION); return; } else { char data[5]; data[4] = '\0'; while(fread(data, 4, 1, wavFile) == 1) { if(strcmp(data, "data") != 0) { //Search data chank by 1 byte fseek(wavFile, -3, SEEK_CUR); continue; } else if(fseek(wavFile, 4, SEEK_CUR)) { //data chank was found, then skip next 4 byte(indicating data size) to proceed printErrorMessage(FILE_IO_EXCEPTION); return; } else { //Now we are pointing the head of the PCM data, let's proceed break; } } //If data chank cannot be detected if(feof(wavFile)) { printErrorMessage(NOT_WAV_HEADER); return; } } //Encoding conficuration with "good" level quality lame_set_quality(lame, 5); lame_set_VBR(lame, vbr_default); lame_set_VBR_q(lame, 5); if(lame_init_params(lame) == -1) { printErrorMessage(LAME_API_ERROR); return; } //Perfoem encoding do { int encodedSampleCount; readPCMSampleCount = fread(pcmBuffer, blockAlign, PCM_BUF_SIZE / blockAlign, wavFile); if(readPCMSampleCount != 0) { if(channel == 1) { //For monoral case short int pcmBufferRight[PCM_BUF_SIZE]; short int pcmBufferLeft[PCM_BUF_SIZE]; int i; for(i = 0; i < PCM_BUF_SIZE; i++) { pcmBufferRight[i] = pcmBuffer[i]; pcmBufferLeft[i] = pcmBuffer[i]; } encodedSampleCount = lame_encode_buffer(lame, pcmBufferLeft, pcmBufferRight, readPCMSampleCount, mp3Buffer, MP3_BUF_SIZE); } else { //For stereo case encodedSampleCount = lame_encode_buffer_interleaved(lame, pcmBuffer, readPCMSampleCount, mp3Buffer, MP3_BUF_SIZE); } } else { encodedSampleCount = lame_encode_flush(lame, mp3Buffer, MP3_BUF_SIZE); } if(encodedSampleCount < 0) { printErrorMessage(LAME_API_ERROR); } else { fwrite(mp3Buffer, encodedSampleCount, 1, mp3File); } } while(readPCMSampleCount != 0); lame_close(lame); fclose(wavFile); fclose(mp3File); }
static int apply_abr_preset(lame_global_flags * gfp, int preset, int enforce) { int k; typedef struct { int abr_kbps; int quant_comp; int quant_comp_s; int safejoint; FLOAT nsmsfix; FLOAT st_lrm; /*short threshold */ FLOAT st_s; FLOAT nsbass; FLOAT scale; FLOAT masking_adj; FLOAT ath_lower; FLOAT ath_curve; FLOAT interch; int sfscale; } abr_presets_t; /* *INDENT-OFF* */ /* * Switch mappings for ABR mode */ const abr_presets_t abr_switch_map[] = { /* kbps quant q_s safejoint nsmsfix st_lrm st_s ns-bass scale msk ath_lwr ath_curve interch , sfscale */ { 8, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -30.0, 11, 0.0012, 1}, /* 8, impossible to use in stereo */ { 16, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -25.0, 11, 0.0010, 1}, /* 16 */ { 24, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -20.0, 11, 0.0010, 1}, /* 24 */ { 32, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -15.0, 11, 0.0010, 1}, /* 32 */ { 40, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1}, /* 40 */ { 48, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -10.0, 11, 0.0009, 1}, /* 48 */ { 56, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -6.0, 11, 0.0008, 1}, /* 56 */ { 64, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, -2.0, 11, 0.0008, 1}, /* 64 */ { 80, 9, 9, 0, 0, 6.60, 145, 0, 0.95, 0, .0, 8, 0.0007, 1}, /* 80 */ { 96, 9, 9, 0, 2.50, 6.60, 145, 0, 0.95, 0, 1.0, 5.5, 0.0006, 1}, /* 96 */ {112, 9, 9, 0, 2.25, 6.60, 145, 0, 0.95, 0, 2.0, 4.5, 0.0005, 1}, /* 112 */ {128, 9, 9, 0, 1.95, 6.40, 140, 0, 0.95, 0, 3.0, 4, 0.0002, 1}, /* 128 */ {160, 9, 9, 1, 1.79, 6.00, 135, 0, 0.95, -2, 5.0, 3.5, 0, 1}, /* 160 */ {192, 9, 9, 1, 1.49, 5.60, 125, 0, 0.97, -4, 7.0, 3, 0, 0}, /* 192 */ {224, 9, 9, 1, 1.25, 5.20, 125, 0, 0.98, -6, 9.0, 2, 0, 0}, /* 224 */ {256, 9, 9, 1, 0.97, 5.20, 125, 0, 1.00, -8, 10.0, 1, 0, 0}, /* 256 */ {320, 9, 9, 1, 0.90, 5.20, 125, 0, 1.00, -10, 12.0, 0, 0, 0} /* 320 */ }; /* *INDENT-ON* */ /* Variables for the ABR stuff */ int r; int actual_bitrate = preset; r = nearestBitrateFullIndex(preset); (void) lame_set_VBR(gfp, vbr_abr); (void) lame_set_VBR_mean_bitrate_kbps(gfp, (actual_bitrate)); (void) lame_set_VBR_mean_bitrate_kbps(gfp, min_int(lame_get_VBR_mean_bitrate_kbps(gfp), 320)); (void) lame_set_VBR_mean_bitrate_kbps(gfp, max_int(lame_get_VBR_mean_bitrate_kbps(gfp), 8)); (void) lame_set_brate(gfp, lame_get_VBR_mean_bitrate_kbps(gfp)); /* parameters for which there is no proper set/get interface */ if (abr_switch_map[r].safejoint > 0) (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2); /* safejoint */ if (abr_switch_map[r].sfscale > 0) (void) lame_set_sfscale(gfp, 1); /* ns-bass tweaks */ if (fabs(abr_switch_map[r].nsbass) > 0) { k = (int) (abr_switch_map[r].nsbass * 4); if (k < 0) k += 64; (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 2)); } SET_OPTION(quant_comp, abr_switch_map[r].quant_comp, -1); SET_OPTION(quant_comp_short, abr_switch_map[r].quant_comp_s, -1); SET_OPTION(msfix, abr_switch_map[r].nsmsfix, -1); SET_OPTION(short_threshold_lrm, abr_switch_map[r].st_lrm, -1); SET_OPTION(short_threshold_s, abr_switch_map[r].st_s, -1); /* ABR seems to have big problems with clipping, especially at low bitrates */ /* so we compensate for that here by using a scale value depending on bitrate */ SET_OPTION(scale, abr_switch_map[r].scale, -1); SET_OPTION(maskingadjust, abr_switch_map[r].masking_adj, 0); if (abr_switch_map[r].masking_adj > 0) { SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj * .9, 0); } else { SET_OPTION(maskingadjust_short, abr_switch_map[r].masking_adj * 1.1, 0); } SET_OPTION(ATHlower, abr_switch_map[r].ath_lower, 0); SET_OPTION(ATHcurve, abr_switch_map[r].ath_curve, -1); SET_OPTION(interChRatio, abr_switch_map[r].interch, -1); return preset; }
int mp3_encode(int fd_in, int fd_out) { lame_global_flags *gfp; uint32_t i, j, k, count; int16_t *b0 = 0, *b1 = 0; unsigned char *c; int ret; /* calculate data size*/ k = (uint32_t) lseek(fd_in, 0, SEEK_CUR); i = (uint32_t) lseek(fd_in, 0, SEEK_END); lseek(fd_in, k, SEEK_SET); i -= k; k = 0; gfp = lame_init(); lame_set_errorf(gfp,lame_error_handler); lame_set_debugf(gfp,lame_error_handler); lame_set_msgf(gfp,lame_error_handler); lame_set_num_channels(gfp,2); lame_set_in_samplerate(gfp,8000); #if 0 lame_set_brate(gfp,64); /* compress 1:4 */ lame_set_mode(gfp,0); /* mode = stereo */ lame_set_quality(gfp,2); /* 2=high 5 = medium 7=low */ #else lame_set_quality(gfp,5); /* 2=high 5 = medium 7=low */ lame_set_mode(gfp,3); /* mode = mono */ if(lame_get_VBR(gfp) == vbr_off) lame_set_VBR(gfp, vbr_default); lame_set_VBR_quality(gfp, 7.0); lame_set_findReplayGain(gfp, 0); lame_set_bWriteVbrTag(gfp, 1); lame_set_out_samplerate(gfp,11025); /* lame_set_num_samples(gfp,i/2); */ #endif if(lame_init_params(gfp) < 0) { log_err("encode: failed to init lame"); return 0; } #define CHSZ (512*1024) #define OCHSZ (5*(CHSZ/8)+7200) b0 = (int16_t *) malloc(OCHSZ); b1 = (int16_t *) malloc(CHSZ); if(!b0 || !b1) { log_err("encode: out of memory"); if(b0) free(b0); if(b1) free(b1); return 0; } do { ret = 0; j = 0; count = 0; for(c = (unsigned char*) b1; count < CHSZ; c += ret) { j = (k + RSZ > i) ? i - k : RSZ; ret = read(fd_in,c,j); if(ret < 0) break; k += ret; count += ret; if(k == i) break; } if(ret < 0) break; if(count) { ret = lame_encode_buffer(gfp,b1,b1,count/2,(unsigned char *)b0,OCHSZ); if(ret < 0) break; c = (unsigned char *) b0; count = ret; for(j = 0; j < count; j += RSZ, c += ret) { ret = (j + RSZ > count) ? count - j : RSZ; write(fd_out,c,ret); } } } while(k < i); k = (uint32_t) lame_encode_flush(gfp,(unsigned char *)b0,OCHSZ); if(k) write(fd_out,b0,k); k = lame_get_lametag_frame(gfp,(unsigned char *)b0,OCHSZ); if(k > 0) write(fd_out,b0,k); lame_close(gfp); free(b0); free(b1); return 1; }
int apply_preset(lame_global_flags * gfp, int preset, int enforce) { /*translate legacy presets */ switch (preset) { case R3MIX: { preset = V3; (void) lame_set_VBR(gfp, vbr_mtrh); break; } case MEDIUM: { preset = V4; (void) lame_set_VBR(gfp, vbr_rh); break; } case MEDIUM_FAST: { preset = V4; (void) lame_set_VBR(gfp, vbr_mtrh); break; } case STANDARD: { preset = V2; (void) lame_set_VBR(gfp, vbr_rh); break; } case STANDARD_FAST: { preset = V2; (void) lame_set_VBR(gfp, vbr_mtrh); break; } case EXTREME: { preset = V0; (void) lame_set_VBR(gfp, vbr_rh); break; } case EXTREME_FAST: { preset = V0; (void) lame_set_VBR(gfp, vbr_mtrh); break; } case INSANE: { preset = 320; gfp->preset = preset; (void) apply_abr_preset(gfp, preset, enforce); lame_set_VBR(gfp, vbr_off); return preset; } } gfp->preset = preset; { switch (preset) { case V9: apply_vbr_preset(gfp, 9, enforce); return preset; case V8: apply_vbr_preset(gfp, 8, enforce); return preset; case V7: apply_vbr_preset(gfp, 7, enforce); return preset; case V6: apply_vbr_preset(gfp, 6, enforce); return preset; case V5: apply_vbr_preset(gfp, 5, enforce); return preset; case V4: apply_vbr_preset(gfp, 4, enforce); return preset; case V3: apply_vbr_preset(gfp, 3, enforce); return preset; case V2: apply_vbr_preset(gfp, 2, enforce); return preset; case V1: apply_vbr_preset(gfp, 1, enforce); return preset; case V0: apply_vbr_preset(gfp, 0, enforce); return preset; default: break; } } if (8 <= preset && preset <= 320) { return apply_abr_preset(gfp, preset, enforce); } gfp->preset = 0; /*no corresponding preset found */ return preset; }
int WMp3Encoder::Initialize(unsigned int nSampleRate, unsigned int nNumberOfChannels, unsigned int nBitPerSample, unsigned int custom_value, TEncoderReadCallback read_callback, TEncoderWriteCallback write_callback, TEncoderSeekCallback seek_callback, TEncoderTellCallback tell_callback) { c_nSampleRate = nSampleRate; c_nNumberOfChannels = nNumberOfChannels; c_nBitBerSample = nBitPerSample; c_read_calllback = read_callback; c_write_callback = write_callback; c_seek_callback = seek_callback; c_tell_callback = tell_callback; c_user_data = (void*) custom_value; // init encoder gfp = lame_init(); if(gfp == NULL) { err(ENCODER_INIT_ERROR); return 0; } lame_set_num_channels(gfp, c_nNumberOfChannels); lame_set_in_samplerate(gfp, c_nSampleRate); lame_set_brate(gfp, 128); lame_set_mode(gfp, JOINT_STEREO); lame_set_quality(gfp, 7); /* 2=high 5 = medium 7=low */ lame_set_VBR(gfp, vbr_off); if(lame_init_params(gfp) < 0) { lame_close(gfp); err(ENCODER_INIT_ERROR); return 0; } //LAME encoding call will accept any number of samples. if ( 0 == lame_get_version( gfp ) ) { // For MPEG-II, only 576 samples per frame per channel c_NumberOfInputSamples = 576 * lame_get_num_channels( gfp ); } else { // For MPEG-I, 1152 samples per frame per channel c_NumberOfInputSamples = 1152 * lame_get_num_channels( gfp ); } // Set MP3 buffer size, conservative estimate c_nSamplesBufferSizeInBytes = (DWORD)( 1.25 * ( c_NumberOfInputSamples / lame_get_num_channels( gfp ) ) + 7200 ); // allocate working buffer c_pWorkingBuffer = (unsigned char*) malloc(c_nSamplesBufferSizeInBytes); if(c_pWorkingBuffer == NULL) { lame_close(gfp); err(ENCODER_MEMORY_ALLOCATION_FAIL); return 0; } c_fReady = 1; return 1; }
void lame_encoder_impl_internal::set_vbr_mode(lame_encoder_impl_internal::VbrMode _Mode) { int lameRet = lame_set_VBR(m_Lame.get(), (vbr_mode)_Mode); DT_LAME_CHECK_ERR(lameRet); }
bool AudioEncoderMP3::initialize() { if(lame_flags) { STREAMER_ERROR("error: cannot initialize AudioEncoder because lame_flags has been setup already.\n"); return false; } if(settings.bitsize == AV_AUDIO_BITSIZE_UNKNOWN) { STREAMER_ERROR("error: cannot initialize AudioEncoder because the set bitsize is invalid.\n"); return false; } if(settings.in_bitsize == AV_AUDIO_BITSIZE_UNKNOWN) { settings.in_bitsize = settings.bitsize; } if(settings.mode == AV_AUDIO_MODE_UNKNOWN) { STREAMER_ERROR("error: cannot initialize AudioEncoder because the mode (mono/stereo) is invalid.\n"); return false; } if(settings.samplerate == AV_AUDIO_SAMPLERATE_UNKNOWN) { STREAMER_ERROR("error: cannot intialize the AudioEncoder because the samplerate is invalid.\n"); return false; } if(!settings.bitrate) { STREAMER_ERROR("error: cannot initialize the AudioEncoder because the bitrate was not set.\n"); return false; } if(settings.samplerate == AV_AUDIO_SAMPLERATE_44100) { samplerate = 44100; } else if(settings.samplerate == AV_AUDIO_SAMPLERATE_22050) { samplerate = 22050; } else { STREAMER_ERROR("error: invalid samplerate given for the AudioEncoder.\n"); return false; } if(settings.mode == AV_AUDIO_MODE_STEREO) { mode = STEREO; nchannels = 2; } else if(settings.mode == AV_AUDIO_MODE_MONO) { mode = MONO; nchannels = 1; STREAMER_ERROR("error: for now we only implement stereo audio.\n"); return false; } else { STREAMER_ERROR("error: invalid mode given for the AudioEncoder.\n"); return false; } if(settings.quality > 9) { STREAMER_ERROR("error: invalid quality given for the AudioEncoder, use a value between 0 (best) and 9 (worst).\n"); return false; } lame_flags = lame_init(); if(!lame_flags) { STREAMER_ERROR("error: cannot initalize lame.\n"); return false; } lame_set_num_channels(lame_flags, (mode == STEREO) ? 2 : 1); lame_set_in_samplerate(lame_flags, samplerate); lame_set_brate(lame_flags, settings.bitrate); lame_set_VBR(lame_flags, vbr_off); lame_set_mode(lame_flags, mode); lame_set_quality(lame_flags, settings.quality); if(lame_init_params(lame_flags) < 0) { STREAMER_ERROR("error: cannot initialize the lame parameters.\n"); return false; } #if !defined(NDEBUG) lame_print_config(lame_flags); lame_print_internals(lame_flags); STREAMER_VERBOSE("lame: bitrate: %d\n", lame_get_brate(lame_flags)); #endif bitrate_time_started = uv_hrtime(); bitrate_timeout = bitrate_time_started + bitrate_delay; return true; }