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); }
////////////////////////////////////////////////////////////////////// // 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; }
/*------------------------------------------------------------------------------ * 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, getInChannel() == 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)); 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)); if ( lowpass ) { 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 ( highpass ) { 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_quality( lameGlobalFlags, 2) ) { throw Exception( __FILE__, __LINE__, "lame lib setting quality error", 2 ); } reportEvent( 5, "set lame quality", lame_get_quality( lameGlobalFlags)); 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; }
/* ======================================================================= */ BOOL EncodeTrack(PWINDOWHANDLES H, DWORD Index, PCHAR BasePath, HANDLE CDHandle, PCDTRACK CDTrackData, PDWORD DiscCurrent, PDWORD DiscTotal, PDWORD TrackCount) { Log(LOG_WRITE, "Encoding track %u/%u; Sector %u-%u/%u", Index+1, *TrackCount, CDTrackData[Index].Address, CDTrackData[Index].Address + CDTrackData[Index].Length - 1, CDTrackData[Index].Length); CHAR MP3FilePath[MAX_PATH]; CHAR MP3FilePathFancy[MAX_PATH]; HANDLE MP3FileHandle = INVALID_HANDLE_VALUE; static lame_global_flags *GFP = NULL; if(OneTrackOnly == 0) { _snprintf(MP3FilePath, MAX_PATH, "%s\\Track %u.mp3", BasePath, Index + 1); MakeFancyPath(MP3FilePath, MP3FilePathFancy, 35); SetLabel(H->WT, "Creating file %s...", MP3FilePath); MP3FileHandle = CreateFile(MP3FilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } else { _snprintf(MP3FilePath, MAX_PATH, "%s\\Audio.mp3", BasePath, Index + 1); MakeFancyPath(MP3FilePath, MP3FilePathFancy, 35); switch(OneTrackOnly) { case 1: SetLabel(H->WT, "Creating solid file %s...", MP3FilePath); MP3FileHandle = CreateFile(MP3FilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); OneTrackOnly = 2; break; case 2: SetLabel(H->WT, "Re-opening file %s...", MP3FilePath); MP3FileHandle = CreateFile(MP3FilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(MP3FileHandle != INVALID_HANDLE_VALUE) SetFilePointer(MP3FileHandle, 0, NULL, FILE_END); break; } } BOOL Success = TRUE; INT EncoderReturnCode = 0; if(MP3FileHandle != INVALID_HANDLE_VALUE) { Log(LOG_WRITE, "Created file %s", MP3FilePath); DWORD DiscLastCurrent = *DiscCurrent; DWORD SectorTotal = CDTrackData[Index].Length/SECTORS_AT_READ; DWORD MP3BytesWritten; SendMessage(H->PO, PBM_SETRANGE32, 0, SectorTotal); SendMessage(H->PO, PBM_SETPOS, 0, 0); SendMessage(H->PA, PBM_SETRANGE32, 0, *DiscTotal); if(GFP == NULL) { GFP = lame_init(); lame_set_preset(GFP, MP3Quality); lame_set_copyright(GFP, 1); lame_set_original(GFP, 1); lame_set_error_protection(GFP, 1); lame_set_extension(GFP, 1); lame_set_quality(GFP, Quality); EncoderReturnCode = lame_init_params(GFP); } if(EncoderReturnCode == 0) { DWORD dwWAVBufferSize=(1152 * lame_get_num_channels(GFP)); DWORD dwMP3BufferSize=(DWORD)(1.25*(dwWAVBufferSize/lame_get_num_channels(GFP))+7200); PBYTE MP3Buffer = new BYTE[dwMP3BufferSize]; PBYTE CDBuffer = new BYTE[SECTORS_AT_READ * RAW_SECTOR_SIZE]; INT nOutputSamples; DWORD CDBytesWritten; RAW_READ_INFO Info; Info.TrackMode = CDDA; Info.SectorCount = SECTORS_AT_READ; DWORD SectorCurrent; TCHAR NumWritten[30]; TCHAR NumWrittenKB[30]; for(SectorCurrent = 0; SectorCurrent < SectorTotal; ++SectorCurrent, ++*DiscCurrent) { Info.DiskOffset.QuadPart = (CDTrackData[Index].Address + SectorCurrent*SECTORS_AT_READ) * CD_SECTOR_SIZE; MP3BytesWritten = SetFilePointer(MP3FileHandle, 0, NULL, FILE_CURRENT); Comma(MP3BytesWritten, NumWritten, sizeof(NumWritten)); Comma(MP3BytesWritten / 1024, NumWrittenKB, sizeof(NumWrittenKB)); SetLabel(H->WT, "Encoding track %u of %u and sector %u of %u\nTo %s\nWritten %s bytes (%s KB) to file", Index + 1, *TrackCount, SectorCurrent, SectorTotal - 1, MP3FilePathFancy, NumWritten, NumWrittenKB); if(DeviceIoControl(CDHandle, IOCTL_CDROM_RAW_READ, &Info, sizeof(Info), CDBuffer, SECTORS_AT_READ*RAW_SECTOR_SIZE, &CDBytesWritten, NULL) != 0) { if(EncodeAudioBuffer(CDBuffer, CDBytesWritten, dwWAVBufferSize, MP3FileHandle, MP3Buffer, GFP) == FALSE) { Log(LOG_WRITE, "Encoding of audio buffer failed"); Success = FALSE; break; } } else { DWORD ErrorCode = GetLastError(); if(ErrorCode == ERROR_INVALID_FUNCTION) { Log(LOG_WRITE, "Track %u is not a valid CDDA track", Index + 1); Success = FALSE; break; } else if(ErrorCode != ERROR_INVALID_PARAMETER) { Log(LOG_WRITE, "Error code %u reading track %u!", ErrorCode, Index + 1); Success = FALSE; break; } } SendMessage(H->PO, PBM_SETPOS, SectorCurrent, 0); SendMessage(H->PA, PBM_SETPOS, *DiscCurrent, 0); Percentage = (float)((float)*DiscCurrent / (float)*DiscTotal) * 100; } if(Success == FALSE) { *DiscTotal -= SectorTotal; SectorTotal = 0; } else if(OneTrackOnly == 0 || Index+1 == *TrackCount) { nOutputSamples = lame_encode_flush_nogap(GFP, MP3Buffer, LAME_MAXMP3BUFFER); if(nOutputSamples > 0) { if(WriteFile(MP3FileHandle, MP3Buffer, nOutputSamples, &MP3BytesWritten, NULL) == FALSE) { Log(LOG_WRITE, "Failed to write %u final bytes to file", nOutputSamples); Success = FALSE; } else if(nOutputSamples != (int)MP3BytesWritten) { Log(LOG_WRITE, "Written %u final bytes instead of %u bytes to file", MP3BytesWritten, nOutputSamples); Success = FALSE; } } else if(nOutputSamples < 0) { Log(LOG_WRITE, "Error code %d flushing encoded audio buffer", nOutputSamples); Success = FALSE; } } *DiscCurrent = DiscLastCurrent + SectorTotal; SendMessage(H->PO, PBM_SETPOS, *DiscCurrent, 0); } else { Log(LOG_WRITE, "Error code %d initialising audio encoder", EncoderReturnCode); Success = FALSE; } MP3BytesWritten = SetFilePointer(MP3FileHandle, 0, NULL, FILE_CURRENT); if(CloseHandle(MP3FileHandle) == FALSE) { Log(LOG_WRITE, "Error code %u closing file handle"); Success = FALSE; } if(OneTrackOnly == 0 && (MP3BytesWritten == 0 || Success == FALSE)) { if(DeleteFile(MP3FilePath) == FALSE) Log(LOG_WRITE, "Error code %u deleting file"); else Log(LOG_WRITE, "Deleted the file due to error"); } else Log(LOG_WRITE, "Written %u bytes to file", MP3BytesWritten); } else { Log(LOG_WRITE, "Error code %u creating %s", GetLastError(), MP3FilePath); Success = FALSE; } if(EncoderReturnCode == 0 && (OneTrackOnly == 0 || Index+1 == *TrackCount)) { lame_close(GFP); GFP = NULL; } return Success; }
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; }
bool OutLame::apply_profile() { if(enc_flags) lame_close(enc_flags); enc_flags = lame_init(); lame_set_errorf(enc_flags,(void (*)(const char*, va_list))error); lame_set_debugf(enc_flags,(void (*)(const char*, va_list))func); lame_set_msgf(enc_flags,(void (*)(const char*, va_list))act); lame_set_num_samples(enc_flags,OUT_CHUNK); lame_set_num_channels(enc_flags,2); // the mixed input stream is stereo lame_set_in_samplerate(enc_flags,SAMPLE_RATE); // the mixed input stream is 44khz lame_set_error_protection(enc_flags,1); // 2 bytes per frame for a CRC checksum lame_set_compression_ratio(enc_flags,0); lame_set_quality(enc_flags,2); // 1..9 1=best 9=worst (suggested: 2, 5, 7) // lame_set_VBR(enc_flags,vbr_abr); /* BITRATE */ lame_set_brate(enc_flags,bps()); Shouter *ice = (Shouter*)icelist.begin(); while(ice) { char tmp[256]; snprintf(tmp,256,"%u",bps()); ice->bps( tmp ); snprintf(tmp,256,"%u",freq()); ice->freq( tmp ); snprintf(tmp,256,"%u",channels()); ice->channels( tmp ); ice = (Shouter*)ice->next; } lame_set_out_samplerate(enc_flags,freq()); /* CHANNELS mode 2 (double channel) is unsupported */ int mode; switch( channels() ) { /* 0,1,2,3 stereo,jstereo,dual channel,mono */ case 1: mode = 3; break; case 2: mode = 1; break; default: mode = 3; break; } lame_set_mode(enc_flags,(MPEG_mode_e)mode); /* in case of VBR func("reversed quality is %i, guessed bps %i",(int)fabs( 10 - quality()),bps()); lame_set_VBR_q(enc_flags,(int)fabs( 10 - quality() )); lame_set_VBR_mean_bitrate_kbps(enc_flags,bps()); */ /* lame chooses for us frequency filtering when values are 0 */ lame_set_lowpassfreq(enc_flags,lowpass()); lame_set_highpassfreq(enc_flags,highpass()); int res = lame_init_params(enc_flags); if(res<0) { error("lame_init_params failed"); lame_close(enc_flags); enc_flags = NULL; } return (res<0)?false:true; }