예제 #1
0
EmErrorCode LameEncoder::OpenOutput(const std::string& path)
{
    m_OutputFile = ::fopen(path.c_str(), "wb+");

    if (m_OutputFile == nullptr)
        return ErrorCode::EncoderFailedToOpen;

    // init lame
    m_gfp = ::lame_init();

    ::lame_set_quality(m_gfp, m_Quality.userVal);
    ::lame_set_brate(m_gfp, m_BitRate.enumedVal[m_BitRate.userChoice]);
    ::lame_set_mode(m_gfp, ::JOINT_STEREO);
    ::lame_set_findReplayGain(m_gfp, m_ReplayGain.userChoice ? 1 : 0);
    ::lame_set_asm_optimizations(m_gfp, MMX, 1);
    ::lame_set_asm_optimizations(m_gfp, SSE, 1);
    if (m_MediaTag != nullptr) {
        lame_set_write_id3tag_automatic(m_gfp, 1);
        id3tag_init(m_gfp);
        id3tag_v2_only(m_gfp);
        id3tag_set_title(m_gfp, m_MediaTag->title.c_str());
        id3tag_set_artist(m_gfp, m_MediaTag->artist.c_str());
        id3tag_set_album(m_gfp, m_MediaTag->album.c_str());
        id3tag_set_comment(m_gfp, m_MediaTag->comment.c_str());
        id3tag_set_genre(m_gfp, m_MediaTag->genre.c_str());
        id3tag_set_year(m_gfp, scx::NumToStr(m_MediaTag->year).c_str());
        id3tag_set_track(m_gfp, scx::NumToStr(m_MediaTag->track).c_str());
    }
    int ret = ::lame_init_params(m_gfp);
    if (ret < 0)
        return ErrorCode::EncoderFailedToInit;

    return ErrorCode::Ok;
}
예제 #2
0
long MP3_create(const char* format_parameters, amci_codec_fmt_info_t* format_description) {
  mp3_coder_state* coder_state;
  int ret_code;
  
  coder_state = malloc(sizeof(mp3_coder_state));
  if (!coder_state) {
    ERROR("no memory for allocating mp3 coder state\n");
    return -1;
  }
  
  DBG("MP3: creating lame %s\n", get_lame_version());
  format_description[0].id = 0; 
  coder_state->gfp = lame_init(); 
    
  if (!coder_state->gfp) {
    ERROR("initialiting lame\n");
    free(coder_state);
    return -1;
  }

  lame_set_errorf(coder_state->gfp, &no_output);
  lame_set_debugf(coder_state->gfp, &no_output);
  lame_set_msgf(coder_state->gfp, &no_output);
  
  lame_set_num_channels(coder_state->gfp,1);
  lame_set_in_samplerate(coder_state->gfp,8000);
  lame_set_brate(coder_state->gfp,16);
  lame_set_mode(coder_state->gfp,3); // mono
  lame_set_quality(coder_state->gfp,2);   /* 2=high  5 = medium  7=low */ 
  
  id3tag_init(coder_state->gfp);
  id3tag_set_title(coder_state->gfp, "mp3 voicemail by iptel.org");
  ret_code = lame_init_params(coder_state->gfp);
  
  if (ret_code < 0) {
    ERROR("lame encoder init failed: return code is %d\n", ret_code);
    free(coder_state);
    return -1;
  }
  

#ifdef WITH_MPG123DECODER
  coder_state->mpg123_h = mpg123_new(NULL, NULL);
  if (!coder_state->mpg123_h) {
    ERROR("initializing mpg123 decoder instance\n");
    return -1;
  }

  /* suppress output */
  mpg123_param(coder_state->mpg123_h, MPG123_FLAGS, MPG123_QUIET /* | MPG123_FORCE_MONO */,0);

/*   mpg123_param(coder_state->mpg123_h, MPG123_VERBOSE, 0, 0); */

#endif

  return (long)coder_state;
}
예제 #3
0
bool KRecExport_MP3::initialize( const QString &filename ) {
kdDebug( 60005 ) << k_funcinfo << endl;
	if ( !_file &&
	     !( bits()!=16 && channels()!=2 &&
	        KMessageBox::warningContinueCancel( KRecGlobal::the()->mainWidget(),
	          i18n( "At this time MP3-Export only supports files in stereo and 16bit." )
	        ) == KMessageBox::Cancel
	     )
	) {
		KMessageBox::information( KRecGlobal::the()->mainWidget(),
		  i18n( "Please note that this plugin takes its qualitysettings from" \
		        " the corresponding section of the Audio CDs Control Center module" \
			" configuration. Make use" \
		        " of the Control Center to configure these settings." ),
		  i18n( "Quality Configuration" ), "qualityinfo_mp3" );
		_file = new QFile( filename );
		if ( _file->open( IO_Raw|IO_WriteOnly ) ) {
			if ( ! init_done ) {
				gfp = lame_init();
				setLameParameters();
				if ( write_id3 ) {
					id3tag_init( gfp );
					id3tag_v1_only ( gfp );
					id3tag_set_album  ( gfp, "" );
					id3tag_set_artist ( gfp, "" );
					id3tag_set_title  ( gfp, "" );
					id3tag_set_comment( gfp, "krec" );
				}
				/// Set input parameters right
				lame_set_in_samplerate( gfp, this->samplingRate() );
				lame_set_num_channels( gfp, this->channels() );
				lame_init_params( gfp );
			}
			if ( _file->size() >= 128 )
				_file->at( _file->size() - 128 );
			else
				_file->at( _file->size() );
		} else return false;
		return true;
	} else return false;
}
예제 #4
0
static switch_status_t shout_file_open(switch_file_handle_t *handle, const char *path)
{
	shout_context_t *context;
	char *host, *file;
	char *username, *password, *port;
	char *err = NULL;
	const char *mpg123err = NULL;
	int portno = 0;

	if ((context = switch_core_alloc(handle->memory_pool, sizeof(*context))) == 0) {
		return SWITCH_STATUS_MEMERR;
	}

	if (!handle->samplerate) {
		handle->samplerate = 8000;
	}

	context->memory_pool = handle->memory_pool;
	context->samplerate = handle->samplerate;
	context->handle = handle;

	switch_thread_rwlock_create(&(context->rwlock), context->memory_pool);

	switch_thread_rwlock_rdlock(context->rwlock);

	switch_mutex_init(&context->audio_mutex, SWITCH_MUTEX_NESTED, context->memory_pool);

	if (switch_test_flag(handle, SWITCH_FILE_FLAG_READ)) {
		if (switch_buffer_create_dynamic(&context->audio_buffer, TC_BUFFER_SIZE, TC_BUFFER_SIZE * 2, 0) != SWITCH_STATUS_SUCCESS) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
			goto error;
		}

		context->mh = our_mpg123_new(NULL, NULL);
		if (mpg123_format_all(context->mh) != MPG123_OK) {
			MPGERROR();
		}
		if (mpg123_param(context->mh, MPG123_FORCE_RATE, context->samplerate, 0) != MPG123_OK) {
			MPGERROR();
		}

		if (handle->handler) {
			if (mpg123_param(context->mh, MPG123_FLAGS, MPG123_SEEKBUFFER | MPG123_MONO_MIX, 0) != MPG123_OK) {
				MPGERROR();
			}
			if (mpg123_open_feed(context->mh) != MPG123_OK) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening mpg feed\n");
				mpg123err = mpg123_strerror(context->mh);
				goto error;
			}
			context->stream_url = switch_core_sprintf(context->memory_pool, "http://%s", path);
			context->prebuf = handle->prebuf;
			launch_read_stream_thread(context);
		} else {
			handle->seekable = 1;
			if (mpg123_param(context->mh, MPG123_FLAGS, MPG123_MONO_MIX, 0) != MPG123_OK) {
				MPGERROR();
			}
			if (mpg123_open(context->mh, path) != MPG123_OK) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening %s\n", path);
				mpg123err = mpg123_strerror(context->mh);
				goto error;
			}

		}
	} else if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
		if (switch_test_flag(handle, SWITCH_FILE_WRITE_APPEND)) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Appending to MP3 not supported.\n");
		}
		if (!(context->gfp = lame_init())) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate lame\n");
			goto error;
		}

		if (!handle->handler) {
			id3tag_init(context->gfp);
			id3tag_v2_only(context->gfp);
			id3tag_pad_v2(context->gfp);

		}
		context->channels = handle->channels;
		lame_set_brate(context->gfp, 16 * (handle->samplerate / 8000) * handle->channels);
		lame_set_num_channels(context->gfp, handle->channels);
		lame_set_in_samplerate(context->gfp, handle->samplerate);
		lame_set_out_samplerate(context->gfp, handle->samplerate);

		if (handle->channels == 2) {
			lame_set_mode(context->gfp, STEREO);
		} else {
			lame_set_mode(context->gfp, MONO);
		}
		lame_set_quality(context->gfp, 2);	/* 2=high  5 = medium  7=low */

		lame_set_errorf(context->gfp, log_error);
		lame_set_debugf(context->gfp, log_debug);
		lame_set_msgf(context->gfp, log_msg);

		if (handle->handler) {
			if (switch_buffer_create_dynamic(&context->audio_buffer, MY_BLOCK_SIZE, MY_BUF_LEN, 0) != SWITCH_STATUS_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
				goto error;
			}
			lame_set_bWriteVbrTag(context->gfp, 0);
			lame_mp3_tags_fid(context->gfp, NULL);

			username = switch_core_strdup(handle->memory_pool, path);
			if (!(password = strchr(username, ':'))) {
				err = "invalid url";
				goto error;
			}
			*password++ = '\0';

			if (!(host = strchr(password, '@'))) {
				err = "invalid url";
				goto error;
			}
			*host++ = '\0';

			if ((file = strchr(host, '/'))) {
				*file++ = '\0';
			} else {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid URL: %s\n", path);
				goto error;
			}

			if ((port = strchr(host, ':'))) {
				*port++ = '\0';
				if (port) {
					portno = atoi(port);
				}
			}

			if (!portno) {
				portno = 8000;
			}

			if (!(context->shout = shout_new())) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate shout_t\n");
				goto error;
			}

			if (shout_set_host(context->shout, host) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting hostname: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_protocol(context->shout, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting protocol: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_port(context->shout, portno) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting port: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_password(context->shout, password) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting password: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_mount(context->shout, file) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting mount: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_user(context->shout, username) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting user: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_url(context->shout, "http://www.freeswitch.org") != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting name: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_description(context->shout, "FreeSWITCH mod_shout Broadcasting Module") != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting description: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_audio_info(context->shout, "bitrate", "24000") != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting bitrate: %s\n", shout_get_error(context->shout));
				goto error;
			}

			if (shout_set_format(context->shout, SHOUT_FORMAT_MP3) != SHOUTERR_SUCCESS) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error setting format: %s\n", shout_get_error(context->shout));
				goto error;
			}

		} else {
			/* lame being lame and all has FILE * coded into it's API for some functions so we gotta use it */
			if (!(context->fp = fopen(path, "wb+"))) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening %s\n", path);
				goto error;
			}
		}
	}

	handle->samples = 0;
	handle->format = 0;
	handle->sections = 0;
	handle->speed = 0;
	handle->private_info = context;
	switch_thread_rwlock_unlock(context->rwlock);

	return SWITCH_STATUS_SUCCESS;

  error:
	switch_thread_rwlock_unlock(context->rwlock);
	if (err) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: %s\n", err);
	}
	if (mpg123err) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error from mpg123: %s\n", mpg123err);
	}
	free_context(context);
	return SWITCH_STATUS_GENERR;

}
예제 #5
0
파일: mp3write~.c 프로젝트: Angeldude/pd
    /* initialize the lame library */
static int mp3write_tilde_lame_init(t_mp3write *x)
{
  time_t now;

    int    ret;
    x->lgfp = lame_init(); /* set default parameters for now */

#ifndef UNIX
    /* load lame_enc.dll library */
    HINSTANCE dll;
    dll=LoadLibrary("lame_enc.dll");
    if(dll==NULL)
    {
        error("mp3write~: error loading lame_enc.dll");
        closesocket(x->x_fd);
        x->x_fd = -1;
        post("mp3write~: connection closed");
        return -1;
    }
#endif
    {
       const char *lameVersion = get_lame_version();
       post( "mp3write~ : using lame version : %s", lameVersion );
    }

        /* setting lame parameters */
    lame_set_num_channels( x->lgfp, 2);
    lame_set_in_samplerate( x->lgfp, sys_getsr() );
    lame_set_out_samplerate( x->lgfp, x->x_samplerate );
    lame_set_brate( x->lgfp, x->x_bitrate );
    lame_set_mode( x->lgfp, x->x_mp3mode );
    lame_set_quality( x->lgfp, x->x_mp3quality );
    lame_set_emphasis( x->lgfp, 1 );
    lame_set_original( x->lgfp, 1 );
    lame_set_copyright( x->lgfp, 1 ); /* viva free music societies !!! */
    lame_set_disable_reservoir( x->lgfp, 0 );
    lame_set_padding_type( x->lgfp, PAD_NO );
    ret = lame_init_params( x->lgfp );
    if ( ret<0 ) {
       post( "mp3write~ : error : lame params initialization returned : %d", ret );
       return -1;
    } else {
       x->x_lame=1;
       /* magic formula copied from windows dll for MPEG-I */
       x->x_lamechunk = 2*1152;

       post( "mp3write~ : lame initialization done. (%d)", x->x_lame );
    }
    lame_init_bitstream( x->lgfp );

    /* setting tag information */
    id3tag_init(x->lgfp);
    id3tag_v1_only(x->lgfp);
    id3tag_space_v1(x->lgfp);
    id3tag_set_artist(x->lgfp, "Pd Session");
    now=time(NULL);
    sprintf( x->x_title, "Started at %s", ctime(&now) );
    id3tag_set_title(x->lgfp, x->x_title );
    
    return 0;

}
lame_global_flags* simple_lame_lib_init(
		JNIEnv* env,
		jint inSamplerate, jint outChannel,
		jint outSamplerate, jint outBitrate, jint quality,
		jstring id3tagTitle, jstring id3tagArtist, jstring id3tagAlbum,
		jstring id3tagYear, jstring id3tagComment) {
	if (local_log) {
		__android_log_print(ANDROID_LOG_VERBOSE, TAG, "Start lame init.");
	}

	lame_global_flags* glf = lame_init();
	lame_set_in_samplerate(glf, inSamplerate);
	lame_set_num_channels(glf, outChannel);
	lame_set_out_samplerate(glf, outSamplerate);
	lame_set_brate(glf, outBitrate);
	lame_set_quality(glf, quality);

	const jchar* title = NULL;
	const jchar* artist = NULL;
	const jchar* album = NULL;
	const jchar* year = NULL;
	const jchar* comment = NULL;
	if (id3tagTitle) {
		title = (*env)->GetStringChars(env, id3tagTitle, NULL);
	}
	if (id3tagArtist) {
		artist = (*env)->GetStringChars(env, id3tagArtist, NULL);
	}
	if (id3tagAlbum) {
		album = (*env)->GetStringChars(env, id3tagAlbum, NULL);
	}
	if (id3tagYear) {
		year = (*env)->GetStringChars(env, id3tagYear, NULL);
	}
	if (id3tagComment) {
		comment = (*env)->GetStringChars(env, id3tagComment, NULL);
	}

	if (title || artist || album || year || comment) {
		id3tag_init(glf);

		if (title) {
			id3tag_set_title(glf, (const char*)title);
			(*env)->ReleaseStringChars(env, id3tagTitle, title);
		}
		if (artist) {
			id3tag_set_artist(glf, (const char*)artist);
			(*env)->ReleaseStringChars(env, id3tagArtist, artist);
		}
		if (album) {
			id3tag_set_album(glf, (const char*)album);
			(*env)->ReleaseStringChars(env, id3tagAlbum, album);
		}
		if (year) {
			id3tag_set_year(glf, (const char*)year);
			(*env)->ReleaseStringChars(env, id3tagYear, year);
		}
		if (comment) {
			id3tag_set_comment(glf, (const char*)comment);
			(*env)->ReleaseStringChars(env, id3tagComment, comment);
		}
	}

	lame_init_params(glf);

	if (local_log) {
		__android_log_print(ANDROID_LOG_VERBOSE, TAG, "End lame init.");
	}
	
	return glf;
}
예제 #7
0
int CEncoderLame::prepare(
    const sp<IMediaSource>& pMediaSource_in,
    const sp<IAudioSink>&  pAudioSink_out,
    const sp<AMessage>&    pOption_in
)
{
    AUTO_LOG();

    if (m_pGobalFlags != NULL) {
        RETURN(ALREADY_EXISTS);
    }

    CHECK_PTR_EXT(pMediaSource_in, BAD_VALUE);
    CHECK_PTR_EXT(pAudioSink_out,  BAD_VALUE);

    sp<MetaData>  pMeta = pMediaSource_in->getFormat();
    CHECK_PTR_EXT(pMeta,  BAD_VALUE);

    m_pGobalFlags = lame_init();
    CHECK_PTR_EXT(m_pGobalFlags, BAD_VALUE);

    lame_set_errorf(m_pGobalFlags, CEncoderLame::errorf);
    lame_set_debugf(m_pGobalFlags, CEncoderLame::debugf);
    lame_set_msgf(m_pGobalFlags, CEncoderLame::msgf);

    id3tag_init(m_pGobalFlags);

    // TO DO
    // pass the user option to encoder
    //if (pOption_in != NULL) {
    //}

    int  ret = OK;
    bool chk = false;

    int32_t iChannelNum = 0;
    chk = pMeta->findInt32(kKeyChannelCount, &iChannelNum);
    CHECK_IS_EXT((true == chk), UNKNOWN_ERROR);
    ret = lame_set_num_channels(m_pGobalFlags, iChannelNum);
    CHECK_IS_EXT((ret == OK), ret);

    int32_t iSampleRate = 0;
    chk = pMeta->findInt32(kKeySampleRate, &iSampleRate);
    CHECK_IS_EXT((true == chk), UNKNOWN_ERROR);
    ret = lame_set_in_samplerate(m_pGobalFlags, iSampleRate);
    CHECK_IS_EXT((ret == OK), ret);

    int32_t iBitsPerSample = 0;
    chk = pMeta->findInt32(kKeyBitsPerSample, &iBitsPerSample);
    CHECK_IS_EXT((true == chk), UNKNOWN_ERROR);
    int32_t iDataSize = 0;
    chk = pMeta->findInt32(kKeyDataSize, &iDataSize);
    CHECK_IS_EXT((true == chk), UNKNOWN_ERROR);
    ret = lame_set_num_samples(m_pGobalFlags, iDataSize / (iChannelNum * ((iBitsPerSample + 7) / 8)));
    CHECK_IS_EXT((ret == OK), ret);

    lame_set_write_id3tag_automatic(m_pGobalFlags, 0);
    ret = lame_init_params(m_pGobalFlags);
    CHECK_IS_EXT((ret == OK), ret);

    size_t id3v2_size = lame_get_id3v2_tag(m_pGobalFlags, 0, 0);

    if (id3v2_size > 0) {
        unsigned char *id3v2tag = new unsigned char[id3v2_size];

        if (id3v2tag != 0) {
            int iTagSz = lame_get_id3v2_tag(m_pGobalFlags, id3v2tag, id3v2_size);
            int iWrite = (int) pAudioSink_out->write(id3v2tag, iTagSz);
            delete(id3v2tag);
            CHECK_IS_EXT((iTagSz == iWrite), UNKNOWN_ERROR);
        }
    }

    RETURN(OK);
}
예제 #8
0
파일: MP3Writer.cpp 프로젝트: roxlu/roxlu
bool MP3Writer::begin() {
  if(!is_setup) {
    RX_ERROR(MP3_WRERR_NOT_SETUP);
    return false;
  }

  if(is_started) {
    return false;
  }

  int samplerate = 0;
  MPEG_mode mode = MONO;
  
  if(config.mode == MP3_WR_MODE_STEREO) {
    mode = STEREO;
  }
  else {
    mode = MONO;
  }

  samplerate = config.samplerateToInt();

  lame_flags = lame_init();
  if(!lame_flags) {
    RX_ERROR(MP3_WRERR_LAME_INIT);
    return false;
  }

  lame_set_num_channels(lame_flags, config.num_channels);
  lame_set_in_samplerate(lame_flags, samplerate);
  lame_set_brate(lame_flags, config.bitrate);
  lame_set_mode(lame_flags, mode);
  lame_set_quality(lame_flags, config.quality); 

  if(config.hasID3()) {
    id3tag_init(lame_flags);
  }

  if(config.id3_title.size()) {
    id3tag_set_title(lame_flags, config.id3_title.c_str());
  }

  if(config.id3_artist.size()) {
    id3tag_set_artist(lame_flags, config.id3_artist.c_str());
  }
  
  if(config.id3_album.size()) {
    id3tag_set_album(lame_flags, config.id3_album.c_str());
  }

  if(config.id3_year.size()) {
    id3tag_set_year(lame_flags, config.id3_year.c_str());
  }

  if(config.id3_comment.size()) {
    id3tag_set_comment(lame_flags, config.id3_comment.c_str());
  }

  if(config.id3_track.size()) {
    int r = id3tag_set_track(lame_flags, config.id3_track.c_str());
    if(r == -1) {
      RX_ERROR(MP3_WRERR_ID3_TRACK_OOR);
    }
    else if(r == -2) {
      RX_ERROR(MP3_WRERR_ID3_TRACK_INVALID);
    }
  }

  if(lame_init_params(lame_flags) < 0) {
    RX_ERROR(MP3_WRERR_LAME_PARAMS);
    return false;
  }

  is_started = true;

  config.cb_open(config.user);
  return true;
}
예제 #9
0
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;
}