/**
    \fn initialize

*/
bool AUDMEncoder_Twolame::initialize(void)
{
  int ret;
  TWOLAME_MPEG_mode mmode;
  uint32_t frequence;
  int channels=wavheader.channels;

  _twolameOptions = twolame_init();
  if (_twolameOptions == NULL)
    return 0;

  if(channels>2)
  {
    printf("[TwoLame]Too many channels\n");
    return 0;
  }
  wavheader.byterate=(_config.bitrate*1000)>>3;


  _chunk = 1152*channels;


  printf("[TwoLame]Incoming :fq : %"PRIu32", channel : %"PRIu32" bitrate: %"PRIu32" \n",
        wavheader.frequency,channels,_config.bitrate);


  twolame_set_in_samplerate(OPTIONS, wavheader.frequency);
  twolame_set_out_samplerate (OPTIONS, wavheader.frequency);
  twolame_set_num_channels(OPTIONS, channels);
  if(channels==1) mmode=TWOLAME_MONO;
  else
      mmode = TWOLAME_STEREO;
  twolame_set_mode(OPTIONS,mmode);
  twolame_set_error_protection(OPTIONS,TRUE);
    	//toolame_setPadding (options,TRUE);
  twolame_set_bitrate (OPTIONS,_config.bitrate);
  twolame_set_verbosity(OPTIONS, 2);
  if(twolame_init_params(OPTIONS))
  {
    printf("[TwoLame]Twolame init failed\n");
    return false;
  }
  printf("[TwoLame]Libtoolame successfully initialized\n");
  return true;
}
Ejemplo n.º 2
0
/* 
  parse_args() 
  Parse the command line arguments
*/
void
parse_args(int argc, char **argv, twolame_options * encopts )
{
    int ch=0;

    // process args
    struct option longopts[] = {
    
        // Input
        { "raw-input",      no_argument,            NULL,       'r' },
        { "byte-swap",      no_argument,            NULL,       'x' },
        { "samplerate",     required_argument,      NULL,       's' },
        { "channels",       required_argument,      NULL,       'N' },
        { "swap-channels",  no_argument,            NULL,       'g' },
        { "scale",          required_argument,      NULL,       1000 },
        { "scale-l",        required_argument,      NULL,       1001 },
        { "scale-r",        required_argument,      NULL,       1002 },
        
        // Output
        { "mode",           required_argument,      NULL,       'm' },
        { "downmix",        no_argument,            NULL,       'a' },
        { "bitrate",        required_argument,      NULL,       'b' },
        { "psyc-mode",      required_argument,      NULL,       'P' },
        { "vbr",            no_argument,  		    NULL,       'v' },
        { "vbr-level",      required_argument,      NULL,       'V' },
        { "max-bitrate",    required_argument,      NULL,       'B' },
        { "ath",            required_argument,      NULL,       'l' },
        { "quick",          required_argument,      NULL,       'q' },
        { "single-frame",   no_argument,            NULL,       'S' },
        
        // Misc
        { "copyright",      no_argument,            NULL,       'c' },
        { "non-original",   no_argument,            NULL,       'o' },
        { "original",   	no_argument,            NULL,       1003 },
        { "protect", 		no_argument,            NULL,       'p' },
        { "padding",        no_argument,            NULL,       'd' },
        { "reserve-bits",   required_argument,      NULL,       'R' },
        { "deemphasis",     required_argument,      NULL,       'e' },
        { "energy",         no_argument,            NULL,       'E' },
        
        // Verbosity
        { "talkativity",    required_argument,      NULL,       't' },
        { "quiet",          no_argument,            NULL,       1004 },
        { "brief",          no_argument,            NULL,       1005 },
        { "verbose",        no_argument,            NULL,       1006 },
        { "help",           no_argument,            NULL,       'h' },
        
        { NULL,             0,                      NULL,       0 }
    };

    
    // Create a short options structure from the long one
    char* shortopts = build_shortopt_string( longopts );
    //printf("shortopts: %s\n", shortopts);
    
    
    while( (ch = getopt_long( argc, argv,  shortopts, longopts, NULL )) != -1) 
    {
        switch(ch) {
        
        // Input
            case 'r':
                sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
                break;

            case 'x':
                byteswap = TRUE;
                break;

            case 's':
                twolame_set_out_samplerate(encopts, atoi(optarg));
                sfinfo.samplerate = atoi(optarg);
                break;

            case 'N':
                sfinfo.channels = atoi(optarg);
                break;
                
            case 'g':
                channelswap = TRUE;
                break;
                
            case 1000:  // --scale
                twolame_set_scale( encopts, atof(optarg) );
                break;

            case 1001:  // --scale-l
                twolame_set_scale_left( encopts, atof(optarg) );
                break;

            case 1002:  // --scale-r
                twolame_set_scale_right( encopts, atof(optarg) );
                break;



        // Output
            case 'm':
                if (*optarg == 's') {
                    twolame_set_mode(encopts, TWOLAME_STEREO);
                } else if (*optarg == 'd') {
                    twolame_set_mode(encopts, TWOLAME_DUAL_CHANNEL);
                } else if (*optarg == 'j') {
                    twolame_set_mode(encopts, TWOLAME_JOINT_STEREO);
                } else if (*optarg == 'm') {
                    twolame_set_mode(encopts, TWOLAME_MONO);
                } else if (*optarg == 'a') {
                    twolame_set_mode(encopts, TWOLAME_AUTO_MODE);
                } else {
                    fprintf(stderr, "Error: mode must be a/s/d/j/m not '%s'\n\n", optarg);
                    usage_long();
                }
                break;

            case 'a':	// downmix
                twolame_set_mode(encopts, TWOLAME_MONO);
                 break;
                
            case 'b':
                twolame_set_bitrate(encopts, atoi(optarg));
                break;

            case 'P':
                twolame_set_psymodel(encopts, atoi(optarg));
                break;
                
            case 'v':
                twolame_set_VBR(encopts, TRUE);
                break;

            case 'V':
                twolame_set_VBR(encopts, TRUE);
                twolame_set_VBR_level(encopts, atof(optarg));
                break;

            case 'B':
                twolame_set_VBR_max_bitrate_kbps(encopts, atoi(optarg));
                break;

            case 'l':
                twolame_set_ATH_level(encopts, atof(optarg));
                break;
                
            case 'q':
                twolame_set_quick_mode(encopts, TRUE);
                twolame_set_quick_count(encopts, atoi(optarg));
                break;

            case 'S':
                single_frame_mode = TRUE;
                break;
                
                
    // Miscellaneous            
            case 'c':
                twolame_set_copyright(encopts, TRUE);
                break;
            case 'o':	// --non-original
                twolame_set_original(encopts, FALSE);
                break;
            case 1003:  // --original
                twolame_set_original(encopts, TRUE);
                break;
            case 'p':
                twolame_set_error_protection(encopts, TRUE);
                break;
            case 'd':
                twolame_set_padding(encopts, TWOLAME_PAD_ALL);
                break;
            case 'R':
                twolame_set_num_ancillary_bits(encopts, atoi(optarg));
                break;
            case 'e':
                if (*optarg == 'n')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_N);
                else if (*optarg == '5')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_5);
                else if (*optarg == 'c')
                    twolame_set_emphasis(encopts, TWOLAME_EMPHASIS_C);
                else {
                    fprintf(stderr, "Error: emphasis must be n/5/c not '%s'\n\n", optarg);
                    usage_long();
                }
                break;
            case 'E':
                twolame_set_energy_levels(encopts, TRUE);
                break;
        

    // Verbosity
            case 't':
                twolame_set_verbosity(encopts, atoi(optarg));
                break;

            case 1004:  // --quiet
                twolame_set_verbosity(encopts, 0);
                break;
                
            case 1005: // --brief
                twolame_set_verbosity(encopts, 1);
                break;
                
            case 1006: // --verbose
                twolame_set_verbosity(encopts, 4);
                break;
                
            case 'h':
                usage_long();
            break;
        
            default:
                usage_short();
            break;
        }
    }
    

    // Look for the input and output file names
    argc -= optind;
    argv += optind;
    while( argc ) {
        if (inputfilename[0] == '\0')
            strncpy(inputfilename, *argv, MAX_NAME_SIZE);
        else if (outputfilename[0] == '\0')
            strncpy(outputfilename, *argv, MAX_NAME_SIZE);
        else {
            fprintf(stderr, "excess argument: %s\n", *argv);
            usage_short();
        }
    
        argv++;
        argc--;
    }
    
    
    // Check that we now have input and output file names ok
    if ( inputfilename[0] == '\0') {
        fprintf(stderr, "Missing input filename.\n");
        usage_short();
    }
    if ( outputfilename[0] == '\0' && strcmp(inputfilename, "-")!=0 ) {
        // Create output filename from the inputfilename 
        // and change the suffix
        new_extension( inputfilename, OUTPUT_SUFFIX, outputfilename );
    }
    if ( outputfilename[0] == '\0') {
        fprintf(stderr, "Missing output filename.\n");
        usage_short();
    }
        
}
//________________________________________________
//   Init lame encoder
// frequence    : Impose frequency , 0 means reuse the incoming fq
// mode                         : ADM_STEREO etc...
// bitrate              : Bitrate in kbps (96,192...)
// return 0 : init failed
//                              1 : init succeeded
//_______________________________________________
uint8_t AUDMEncoder_Twolame::init(ADM_audioEncoderDescriptor *config)
{
  int ret;
  TWOLAME_MPEG_mode mmode;    	
  uint32_t frequence;
  TWOLAME_encoderParam *lameConf=(TWOLAME_encoderParam *)config->param;
  ADM_assert(config->paramSize==sizeof(TWOLAME_encoderParam));

  _twolameOptions = twolame_init();
  if (_twolameOptions == NULL)
    return 0;
      
  if(_wavheader->channels>2)
  {
    printf("[TwoLame]Too many channels\n");
    return 0; 
  }
  _wavheader->byterate=(config->bitrate*1000)>>3;         
      
 
  _chunk = 1152*_wavheader->channels;

 
  printf("[TwoLame]Incoming :fq : %lu, channel : %lu bitrate: %lu \n",
        _wavheader->frequency,_wavheader->channels,config->bitrate);
		
 
  twolame_set_in_samplerate(OPTIONS, _wavheader->frequency);
  twolame_set_out_samplerate (OPTIONS, _wavheader->frequency);
  twolame_set_num_channels(OPTIONS, _wavheader->channels);
  if(_wavheader->channels==1) mmode=TWOLAME_MONO;
  else
    switch (lameConf->mode)
  {
    case ADM_STEREO:
      mmode = TWOLAME_STEREO;
      break;
    case ADM_JSTEREO:
      mmode = TWOLAME_JOINT_STEREO;
      break;
    case ADM_MONO:
      mmode=TWOLAME_MONO;
      break;
				
    default:
      printf("\n **** unknown mode, going stereo ***\n");
      mmode = TWOLAME_STEREO;
      break;

  }
  twolame_set_mode(OPTIONS,mmode);
  twolame_set_error_protection(OPTIONS,TRUE);	
    	//toolame_setPadding (options,TRUE);
  twolame_set_bitrate (OPTIONS,config->bitrate);
  twolame_set_verbosity(OPTIONS, 2);
  if(twolame_init_params(OPTIONS))
  {
    printf("[TwoLame]Twolame init failed\n");
    return 0;
  }
	
 

  printf("[TwoLame]Libtoolame successfully initialized\n");
  return 1;       
}
Ejemplo n.º 4
0
int mpae_init_twolame(audio_encoder_t *encoder)
{
	int mode;
	mpae_twolame_ctx *ctx = NULL;
	
	if(encoder->params.channels == 1)
	{
		mp_msg(MSGT_MENCODER, MSGL_INFO, "ae_twolame, 1 audio channel, forcing mono mode\n");
		mode = TWOLAME_MONO;
	}
	else if(encoder->params.channels == 2)
	{
		if(! strcasecmp(param_mode, "dual"))
			mode = TWOLAME_DUAL_CHANNEL;
		else if(! strcasecmp(param_mode, "jstereo"))
			mode = TWOLAME_JOINT_STEREO;
		else if(! strcasecmp(param_mode, "stereo"))
			mode = TWOLAME_STEREO;
		else
		{
			mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, unknown mode %s, exiting\n", param_mode);
		}
	}
	else
		mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, Twolame can't encode > 2 channels, exiting\n");
	
	ctx = (mpae_twolame_ctx *) calloc(1, sizeof(mpae_twolame_ctx));
	if(ctx == NULL)
	{
		mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't alloc a %d bytes context, exiting\n", sizeof(mpae_twolame_ctx));
		return 0;
	}
	
	ctx->twolame_ctx = twolame_init();
	if(ctx->twolame_ctx == NULL)
	{
		mp_msg(MSGT_MENCODER, MSGL_ERR, "ae_twolame, couldn't initial parameters from libtwolame, exiting\n");
		free(ctx);
		return 0;
	}
	ctx->vbr = 0;

	if(twolame_set_num_channels(ctx->twolame_ctx, encoder->params.channels) != 0)
		return 0;
	if(twolame_set_mode(ctx->twolame_ctx, mode) != 0)
		return 0;
		
	if(twolame_set_in_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
		return 0;
		
	if(twolame_set_out_samplerate(ctx->twolame_ctx, encoder->params.sample_rate) != 0)
		return 0;
	
	if(encoder->params.sample_rate < 32000)
		twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG2);
	else
		twolame_set_version(ctx->twolame_ctx, TWOLAME_MPEG1);
	
	if(twolame_set_psymodel(ctx->twolame_ctx, param_psy) != 0)
		return 0;
	
	if(twolame_set_bitrate(ctx->twolame_ctx, param_bitrate) != 0)
		return 0;
	
	if(param_errprot)
		if(twolame_set_error_protection(ctx->twolame_ctx, TRUE) != 0)
			return 0;
	
	if(param_vbr != 0)
	{
		if(twolame_set_VBR(ctx->twolame_ctx, TRUE) != 0)
			return 0;
		if(twolame_set_VBR_q(ctx->twolame_ctx, param_vbr) != 0)
			return 0;
		if(twolame_set_padding(ctx->twolame_ctx, FALSE) != 0)
			return 0;
		if(param_maxvbr)
		{
			if(twolame_set_VBR_max_bitrate_kbps(ctx->twolame_ctx, param_maxvbr) != 0)
				return 0;
		}
		ctx->vbr = 1;
	}
	
	if(twolame_set_verbosity(ctx->twolame_ctx, param_debug) != 0)
		return 0;
	
	if(twolame_init_params(ctx->twolame_ctx) != 0)
		return 0;
	
	encoder->params.bitrate = param_bitrate * 1000;
	encoder->params.samples_per_frame = 1152;
	encoder->priv = ctx;
	encoder->decode_buffer_size = 1152 * 2 * encoder->params.channels;
	
	encoder->bind = bind_twolame;
	encoder->get_frame_size = get_frame_size;
	encoder->encode = encode_twolame;
	encoder->close = close_twolame;
	
	return 1;
}