Esempio n. 1
0
CDEX_ERR CEncoderLameDll::OpenStream(CUString strOutFileName,DWORD dwSampleRate,WORD nChannels)
{
	CDEX_ERR bReturn = CDEX_OK;

	CUString strLang;
    CUStringConvert strCnv;

	ENTRY_TRACE( _T( "CEncoderLameDll::OpenStream( %s, %ld, %d" ),
				strCnv.ToT( strOutFileName + _W( "." ) + GetExtension() ),
				dwSampleRate,
				nChannels );


	DWORD dwInSampleRate = dwSampleRate;

	m_strStreamFileName = strOutFileName;

	// clear all config settings
	memset( &m_beConfig, 0, sizeof(m_beConfig) );

	// set the number of input channels
	m_nInputChannels = nChannels;

	// Divide sample rate by two for MPEG2
	if ( ( GetVersion() >=1 ) && ( dwInSampleRate >= 32000 ) )
	{
		m_dResampleRatio = 2.0;
		dwSampleRate = dwInSampleRate / 2;
	}

	// mask mode, just to be sure (due to an old hack)
	m_nMode &= 0x0F;

	// Do we have to downmix/upmix ?
	if ( BE_MP3_MODE_MONO == m_nMode )
	{
		if ( 2 == nChannels )
		{
			m_bDownMixToMono = TRUE;
		}
		else
		{
			m_bDownMixToMono = FALSE;
		}
	}
	else 
	{
		if ( 1 == nChannels )
		{
			m_bUpMixToStereo = TRUE;
		}
		else
			m_bUpMixToStereo=FALSE;
	}

	int	nCRC		= m_bCRC;
	int nVBR		= (nCRC>>12)&0x0F;
	int nVbrMethod	= (nCRC>>16)&0x0F;

	m_beConfig.dwConfig						= BE_CONFIG_LAME;
	m_beConfig.format.LHV1.dwStructVersion	= 1;
	m_beConfig.format.LHV1.dwStructSize		= sizeof(m_beConfig);
	m_beConfig.format.LHV1.dwSampleRate		= dwSampleRate;
	m_beConfig.format.LHV1.dwReSampleRate	= m_nOutSampleRate;
	m_beConfig.format.LHV1.nMode			= m_nMode; 
	m_beConfig.format.LHV1.dwBitrate		= m_nBitrate;
	m_beConfig.format.LHV1.dwMaxBitrate		= m_nMaxBitrate;
	m_beConfig.format.LHV1.dwMpegVersion	= (dwSampleRate>=32000)?MPEG1:MPEG2;
	m_beConfig.format.LHV1.dwPsyModel		= 0;
	m_beConfig.format.LHV1.dwEmphasis		= 0;
	m_beConfig.format.LHV1.bCRC				= nCRC&0x01;;
	m_beConfig.format.LHV1.bCopyright		= m_bCopyRight;
	m_beConfig.format.LHV1.bPrivate			= m_bPrivate;
	m_beConfig.format.LHV1.bOriginal		= m_bOriginal;

	// allways write the VBR header, even for CBR file
	m_beConfig.format.LHV1.bWriteVBRHeader	= TRUE;

	if ( nVbrMethod > 0 )
	{
		m_beConfig.format.LHV1.bEnableVBR		= TRUE;
		m_beConfig.format.LHV1.nVbrMethod		= (VBRMETHOD)(nVbrMethod-1) ;

		// is this the ABR method ?
		if ( ( VBR_METHOD_ABR + 1 ) == nVbrMethod )
		{
			// get the average bit rate
			m_beConfig.format.LHV1.dwVbrAbr_bps= LOWORD( GetUserN1() )*1000;
			m_beConfig.format.LHV1.nVBRQuality = 0 ;
		}
		else
		{
			// no ABR selected
			m_beConfig.format.LHV1.dwVbrAbr_bps = 0;
			m_beConfig.format.LHV1.nVBRQuality = nVBR ;
		}
	}

	// Get Quality from third nibble
	m_beConfig.format.LHV1.nPreset=( (nCRC >> 8 ) & 0x0F );

	m_fpOut= CID3Tag::SaveInitialV2Tag( strOutFileName + _W( "." ) + GetExtension(), GetId3V2PadSize() );
    //m_fpOut = CDexOpenFile( strOutFileName + _W( "." ) + GetExtension(), _W( "wb+" ) );

	if ( NULL == m_fpOut )
	{
		CUString strErr;

		strLang = g_language.GetString( IDS_ENCODER_ERROR_COULDNOTOPENFILE );

		strErr.Format( strLang, (LPCWSTR)CUString( strOutFileName + _W( "." ) + GetExtension() ) );

        CDexMessageBox( strErr );

		bReturn  = CDEX_FILEOPEN_ERROR;
	}


	if ( CDEX_OK == bReturn )
	{
		int nReturn = m_InitStream(	&m_beConfig,
									&m_dwInBufferSize,
									&m_dwOutBufferSize,
									&m_hbeStream );

		switch ( nReturn )
		{
			case CDEX_OK:
				// do nothing
			break;
			case -1:
				strLang = g_language.GetString( IDS_ENCODER_ERROR_SAMPLERATEBITRATEMISMATCH );
				CDexMessageBox( strLang );
				bReturn = CDEX_ERROR;
			break;
			case -2:
				strLang = g_language.GetString( IDS_ENCODER_ERROR_INVALIDINPUTSTREAM );
				CDexMessageBox( strLang );
				bReturn = CDEX_ERROR;
			break;
			default:
				strLang = g_language.GetString( IDS_ENCODER_ERROR_INVALIDINPUTSTREAM );
				CDexMessageBox( strLang );
				bReturn = CDEX_ERROR;
			break;

		}
	}

	if ( CDEX_OK == bReturn )
	{

		if ( ( GetVersion() >= 1 ) && ( dwInSampleRate >= 32000 ) )
		{
			// Add multiplcation factor for resampling
			m_dwInBufferSize *= 2;
		}

		if ( m_bDownMixToMono )
		{
			m_dwInBufferSize *= 2;
		}


		// Allocate Output Buffer size
		m_pbOutputStream = new BYTE[ m_dwOutBufferSize ];

		// Only expect halve the number of samples
		// in case we have to upsample
		if ( m_bUpMixToStereo )
		{
			// Allocate upsample Buffer size
			m_psInputStream = new SHORT[ m_dwInBufferSize ];

			m_dwInBufferSize /= 2;

		}
	}

	if ( CDEX_OK == bReturn ) 
	{
		// Initialize the input stream
		bReturn = InitInputStream();
	}


	EXIT_TRACE( _T( "CEncoderLameDll::OpenStream, return value %d" ), bReturn );

	return bReturn;
}
Esempio n. 2
0
CDEX_ERR CEncoderFaacDll::OpenStream(CUString strOutFileName,DWORD dwSampleRate,WORD nChannels)
{
	CDEX_ERR	bReturn		= CDEX_OK;
	bool		bStereo		= true;
	CUString		strLang;

    CUStringConvert strCnv;

	ENTRY_TRACE( _T( "CEncoderFaacDll::OpenStream( %s, %d %d" ),
                strCnv.ToT( strOutFileName + _W( "." ) + GetExtension() ),
				dwSampleRate,
				nChannels );


	// setup number of input channels
	m_nInputChannels = nChannels;

	// setup number of output channels
	if ( ( m_nMode & 0xFFFF ) == BE_MP3_MODE_MONO )
	{
		bStereo = false;
	}

	// mixer setup
	if ( ( false == bStereo ) && ( 2 == nChannels ) )
	{
		m_bDownMixToMono  = TRUE;
	}
	else
	{
		m_bDownMixToMono = FALSE;
	}

	if (  ( true == bStereo ) && ( 1 == nChannels ) )
	{
		m_bUpMixToStereo = TRUE;
	}
	else
	{
		m_bUpMixToStereo = FALSE;
	}

		
	// try to get a handle
	m_handle = faacEncOpen( dwSampleRate,
							(bStereo == true )? 2 : 1,
							&m_dwInBufferSize,
							&m_dwOutBufferSize );


	if ( NULL == m_handle )
	{
		CUString strMsg;

		strLang = g_language.GetString( IDS_LOADLIBRARY_FAILED );

		strMsg.Format(	strLang , (LPCWSTR)CUString( ( g_config.GetAppPath() + _W( "\\" ) + m_strEncoderPath ) ) );

		CDexMessageBox( strMsg );
		
		LTRACE( _T( "CEncoderFaacDll::OpenStream Failed to get handle" ) );

		bReturn = CDEX_ERROR;
	}

	if ( CDEX_OK == bReturn )
	{
		// get current config
		m_pConfig = faacEncGetCurrentConfiguration( m_handle );

		// set settings
		if ( GetUserN1() & 0x08 )
		{
			m_pConfig->mpegVersion = FAAC_MPEG4;
		}
		else
		{
			m_pConfig->mpegVersion = FAAC_MPEG2;
		}

		m_pConfig->bitRate = m_nBitrate * 1000;

		m_pConfig->allowMidside = ( GetUserN1() & 0x02 ) ? TRUE : FALSE;

		m_pConfig->useLfe = ( GetUserN1() & 0x04 ) ? TRUE : FALSE;; /* ? */
		m_pConfig->useTns = ( GetUserN1() & 0x01 ) ? TRUE : FALSE;
		m_pConfig->inputFormat = FAAC_INPUT_16BIT;
		m_pConfig->aacObjectType = ( GetUserN2() & 0x07 );
		m_pConfig->shortctl = SHORTCTL_NORMAL;

		switch ( ( GetUserN1() >> 8 ) & 0x0F )
		{
			case 0: m_pConfig->bandWidth = 16000; break;
			case 1: m_pConfig->bandWidth = 18000; break;
			case 2: m_pConfig->bandWidth = 19500; break;
			default: m_pConfig->bandWidth = 19500; break;
		}

		// set new config
		if ( FALSE == faacEncSetConfiguration( m_handle, m_pConfig ) )
		{
			CUString strMsg;

			strLang = g_language.GetString( IDS_LOADLIBRARY_FAILED );

			strMsg.Format(	strLang , (LPCWSTR)CUString( g_config.GetAppPath() + _W( "\\" ) + m_strEncoderPath ) );

			CDexMessageBox( strMsg );
			
			LTRACE( _T( "CEncoderFaacDll::OpenStream Failed to set config" ) );

			bReturn = CDEX_ERROR;
		}

	}