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()); }
int lame_enc_init(lame_enc *lame) { int rc; char info_buf[256]; lame->gfp = lame_init(); lame_set_num_channels(lame->gfp, lame->channel); lame_set_in_samplerate(lame->gfp, lame->samplerate_in); lame_set_out_samplerate(lame->gfp, lame->samplerate_out); lame_set_brate(lame->gfp, lame->bitrate); if((rc = lame_init_params(lame->gfp)) < 0) { printf("bitrate: %d\n", lame->bitrate); snprintf(info_buf, sizeof(info_buf), "unable to init lame params %d", rc); print_info(info_buf, 1); return 1; } lame->state = LAME_READY; return 0; }
void Java_com_test_lame_TestLameActivity_convertMP3(JNIEnv* env,jobject thiz,jstring inFile ,jstring outFile, jstring progF, int sampleRate, int bitRate){ userCancel=0; totBytes=0; lame_global_flags *gf = lame_init(); lame_set_num_channels(gf, 1); lame_set_in_samplerate(gf,sampleRate); lame_set_brate(gf,bitRate); lame_set_bWriteVbrTag(gf,0); int ipr = lame_init_params(gf); const char* outfs; outfs = (*env)->GetStringUTFChars(env, outFile, NULL); const char* infs; infs = (*env)->GetStringUTFChars(env,inFile, NULL); int enc_delay = -1; int enc_padding = -1; const char* progfs; progfs = (*env)->GetStringUTFChars(env, progF, NULL); //set the global progress file variable to be accessed in lame_encoder() progFile = fopen(progfs,"w"); FILE *outf = init_files(gf,infs,outfs,&enc_delay,&enc_padding); lame_encoder(gf, outf,0 ,infs ,outfs ); }
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; }
int main(int argc, char** argv) { if(argc != 2) { printf("Usage: %s <wav file>\n", argv[0]); exit(0); } lame_global_flags *gfp; //set params gfp = lame_init(); lame_set_num_channels(gfp,2); lame_set_in_samplerate(gfp,44100); lame_set_brate(gfp,128); lame_set_mode(gfp,1); lame_set_quality(gfp,2); int ret_code = lame_init_params(gfp); if(ret_code < 0) { printf("ret_code < 0\n"); exit(-1); } //read input file int read, write; FILE* pcm = fopen(argv[1], "rb"); char* outPath; outPath = strdup(argv[1]); sprintf(outPath+strlen(argv[1])-3, "mp3"); FILE* mp3 = fopen(outPath, "wb"); short int pcm_buffer[PCM_SIZE * 2]; unsigned char mp3_buffer[MP3_SIZE]; do { read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm); if(read == 0) write = lame_encode_flush(gfp, mp3_buffer, MP3_SIZE); else write = lame_encode_buffer_interleaved(gfp, pcm_buffer, read, mp3_buffer, MP3_SIZE); fwrite(mp3_buffer, write, sizeof (unsigned char), mp3); } while (read != 0); lame_close(gfp); fclose(mp3); fclose(pcm); exit(0); }
void ensureInitialized() { if (isOpen()) { if (!m_init_params_called) { m_init_params_called = true; lame_init_params(m_gf); } } }
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); }
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; }
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")); } }
/* * Class: com_example_mp3encodedemo_JNIMp3Encode * Method: init * Signature: (III)V */ JNIEXPORT void JNICALL Java_com_example_soundtouchdemo_JNIMp3Encode_init (JNIEnv *env, jobject obj, jint channel, jint sampleRate, jint brate) { lame = lame_init(); lame_set_num_channels(lame, channel); lame_set_in_samplerate(lame, sampleRate); lame_set_brate(lame, brate); lame_set_mode(lame, 1); lame_set_quality(lame, 2); lame_init_params(lame); }
//this is an attempt to convert raw data frame by frame, if anyone can find a solution, send a patch. void Java_com_test_lame_TestLameActivity_convertMP3Realtime(JNIEnv* env,jobject thiz,jstring aaa,jstring outF, jshortArray inBytes, int sampleRate, int bitRate){ userCancel=0; lame_global_flags *gf = lame_init(); lame_set_num_channels(gf, 1); lame_set_in_samplerate(gf,sampleRate); lame_set_brate(gf,bitRate); lame_set_bWriteVbrTag(gf,0); int ipr = lame_init_params(gf); const char* outfs; outfs = (*env)->GetStringUTFChars(env, outF, NULL); int enc_delay = -1; int enc_padding = -1; FILE * outf; int outFopen=0; if(outFopen == 0) { outf = fopen(outfs,"a+b"); unsigned char mp3buffer[LAME_MAXMP3BUFFER]; int imp3 = lame_get_id3v2_tag(gf, mp3buffer, sizeof(mp3buffer)); fwrite(mp3buffer, 1, imp3, outf); outFopen =1; } jshort* jInBytes = (*env)->GetShortArrayElements(env,inBytes,NULL); jsize sizeIn = (*env)->GetArrayLength(env,inBytes); short inInts[sizeIn]; int totRead=0; int amountRead; while(totRead < sizeIn) { if(sizeIn - totRead < 1152) { amountRead = sizeIn -totRead; } else { amountRead = 1152; } int i; for(i=0;i< amountRead;i++) { inInts[i] = jInBytes[i]; totRead++; } lame_realtime_encoder(gf, outf,0 ,inInts,amountRead); } //failing on JNI: unpinPrimitiveArray() failed to find entry (valid=0) //(*env)->ReleaseByteArrayElements(env,jInBytes,inBytes,0); }
/* initialize the lame library */ static void mp3streamout_tilde_lame_init(t_mp3streamout *x) { int ret; x->lgfp = lame_init(); /* set default parameters for now */ #ifdef _WIN32 /* load lame_enc.dll library */ HINSTANCE dll; dll=LoadLibrary("lame_enc.dll"); if(dll==NULL) { error("mp3streamout~: error loading lame_enc.dll"); closesocket(x->x_fd); x->x_fd = -1; outlet_float(x->x_obj.ob_outlet, 0); post("mp3streamout~: connection closed"); return; } #endif { const char *lameVersion = get_lame_version(); logpost(NULL, 4, "mp3streamout~ : 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 ); /* deprecated in LAME */ ret = lame_init_params( x->lgfp ); if ( ret<0 ) { post( "mp3streamout~ : error : lame params initialization returned : %d", ret ); } else { x->x_lame=1; /* magic formula copied from windows dll for MPEG-I */ x->x_lamechunk = 2*1152; post( "mp3streamout~ : lame initialization done. (%d)", x->x_lame ); } lame_init_bitstream( x->lgfp ); }
JNIEXPORT void JNICALL Java_com_czt_mp3recorder_util_LameUtil_init( JNIEnv *env, jclass cls, jint inSamplerate, jint inChannel, jint outSamplerate, jint outBitrate, jint quality) { if (lame != NULL) { lame_close(lame); lame = NULL; } lame = lame_init(); lame_set_in_samplerate(lame, inSamplerate); lame_set_num_channels(lame, inChannel);//输入流的声道 lame_set_out_samplerate(lame, outSamplerate); lame_set_brate(lame, outBitrate); lame_set_quality(lame, quality); lame_init_params(lame); }
JNIEXPORT void JNICALL Java_com_xiong_richard_greyparrot_SimpleLame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } 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); lame_init_params(glf); }
JNIEXPORT void JNICALL Java_cn_net_xyd_videoaudiodemo_mp3lame_SimpleLame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } 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); lame_init_params(glf); }
void Java_ice_caster_android_shout_Lame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } 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); lame_init_params(glf); }
JNIEXPORT void JNICALL Java_org_proof_recorder_service_jni_SimpleLame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } 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); lame_init_params(glf); }
JNIEXPORT void JNICALL Java_com_anhuioss_crowdroid_util_RecordVoiceToMp3Lame_init( JNIEnv *env, jclass cls, jint inSamplerate, jint outChannel, jint outSamplerate, jint outBitrate, jint quality) { if (glf != NULL) { lame_close(glf); glf = NULL; } 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); lame_init_params(glf); }
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); }
void Java_com_iliayugai_zapp_utils_CommonUtils_initEncoder(JNIEnv *env, jobject jobj, jint in_num_channels, jint in_samplerate, jint in_brate, jint in_mode, jint in_quality) { lame = lame_init(); LOGD("Init parameters:"); lame_set_num_channels(lame, in_num_channels); LOGD("Number of channels: %d", in_num_channels); lame_set_in_samplerate(lame, in_samplerate); LOGD("Sample rate: %d", in_samplerate); lame_set_brate(lame, in_brate); LOGD("Bitrate: %d", in_brate); lame_set_mode(lame, in_mode); LOGD("Mode: %d", in_mode); lame_set_quality(lame, in_quality); LOGD("Quality: %d", in_quality); int res = lame_init_params(lame); LOGD("Init returned: %d", res); }
void Java_mobi_cangol_mobile_utils_LameUtils_initEncoder(JNIEnv *env, jobject jobj, jint in_num_channels, jint in_samplerate, jint in_brate, jint in_mode, jint in_quality) { lame = lame_init(); LOGD("Init parameters:",NULL); lame_set_num_channels(lame, in_num_channels); LOGD("Number of channels: %d", in_num_channels); lame_set_in_samplerate(lame, in_samplerate); LOGD("Sample rate: %d", in_samplerate); lame_set_brate(lame, in_brate); LOGD("Bitrate: %d", in_brate); lame_set_mode(lame, in_mode); LOGD("Mode: %d", in_mode); lame_set_quality(lame, in_quality); LOGD("Quality: %d", in_quality); int res = lame_init_params(lame); LOGD("Init returned: %d", res); }
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; }
bool Mp3Writer::open(const QString &fn, long sr, bool s) { bool b = AudioFileWriter::open(fn + ".mp3", sr, s); if (!b) return false; lame = lame_init(); if (!lame) return false; bitRate = preferences.get(Pref::OutputFormatMp3Bitrate).toInt(); lame_set_in_samplerate(lame, sampleRate); lame_set_num_channels(lame, stereo ? 2 : 1); lame_set_out_samplerate(lame, sampleRate); // TODO: do we need this? lame_set_bWriteVbrTag(lame, 0); lame_set_mode(lame, stereo ? STEREO : MONO); lame_set_brate(lame, bitRate); if (lame_init_params(lame) == -1) return false; return true; }
static switch_status_t shout_file_write(switch_file_handle_t *handle, void *data, size_t *len) { shout_context_t *context; int rlen = 0; int16_t *audio = data; size_t nsamples = *len; if (!handle) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error no handle\n"); return SWITCH_STATUS_FALSE; } if (!(context = handle->private_info)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error no context\n"); return SWITCH_STATUS_FALSE; } if (context->err) { return SWITCH_STATUS_FALSE; } if (context->shout && !context->shout_init) { if (!context->gfp) { return SWITCH_STATUS_FALSE; } context->shout_init++; if (shout_open(context->shout) != SHOUTERR_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening stream: %s\n", shout_get_error(context->shout)); context->err++; return SWITCH_STATUS_FALSE; } launch_write_stream_thread(context); } if (handle->handler && context->audio_mutex) { switch_mutex_lock(context->audio_mutex); if (context->audio_buffer) { if (!switch_buffer_write(context->audio_buffer, data, (nsamples * sizeof(int16_t) * handle->channels))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Buffer error\n"); context->err++; } } else { context->err++; } switch_mutex_unlock(context->audio_mutex); if (context->err) { return SWITCH_STATUS_FALSE; } handle->sample_count += *len; return SWITCH_STATUS_SUCCESS; } if (!context->lame_ready) { lame_init_params(context->gfp); lame_print_config(context->gfp); context->lame_ready = 1; } if (context->mp3buflen < nsamples * 4) { context->mp3buflen = nsamples * 4; context->mp3buf = switch_core_alloc(context->memory_pool, context->mp3buflen); } if (handle->channels == 2) { switch_size_t i, j = 0; if (context->llen < nsamples) { context->l = switch_core_alloc(context->memory_pool, nsamples * 2); context->r = switch_core_alloc(context->memory_pool, nsamples * 2); context->llen = context->rlen = nsamples; } for (i = 0; i < nsamples; i++) { context->l[i] = audio[j++]; context->r[i] = audio[j++]; } if ((rlen = lame_encode_buffer(context->gfp, context->l, context->r, nsamples, context->mp3buf, context->mp3buflen)) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); return SWITCH_STATUS_FALSE; } } else if (handle->channels == 1) { if ((rlen = lame_encode_buffer(context->gfp, audio, NULL, nsamples, context->mp3buf, context->mp3buflen)) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); return SWITCH_STATUS_FALSE; } } else { rlen = 0; } if (rlen) { int ret = fwrite(context->mp3buf, 1, rlen, context->fp); if (ret < 0) { return SWITCH_STATUS_FALSE; } } handle->sample_count += *len; return SWITCH_STATUS_SUCCESS; }
static void *SWITCH_THREAD_FUNC write_stream_thread(switch_thread_t *thread, void *obj) { shout_context_t *context = (shout_context_t *) obj; switch_thread_rwlock_rdlock(context->rwlock); if (context->thread_running) { context->thread_running++; } else { switch_thread_rwlock_unlock(context->rwlock); return NULL; } if (!context->lame_ready) { lame_init_params(context->gfp); lame_print_config(context->gfp); context->lame_ready = 1; } while (!context->err && context->thread_running) { unsigned char mp3buf[8192] = ""; int16_t audio[9600] = { 0 }; switch_size_t audio_read = 0; int rlen = 0; long ret = 0; switch_mutex_lock(context->audio_mutex); if (context->audio_buffer) { audio_read = switch_buffer_read(context->audio_buffer, audio, sizeof(audio)); } else { context->err++; } switch_mutex_unlock(context->audio_mutex); error_check(); if (!audio_read) { audio_read = sizeof(audio); memset(audio, 255, sizeof(audio)); } if (context->channels == 2) { int16_t l[4800] = { 0 }; int16_t r[4800] = { 0 }; int j = 0; switch_size_t i; for (i = 0; i < audio_read / 4; i++) { l[i] = audio[j++]; r[i] = audio[j++]; } if ((rlen = lame_encode_buffer(context->gfp, l, r, audio_read / 4, mp3buf, sizeof(mp3buf))) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); goto error; } } else if (context->channels == 1) { if ((rlen = lame_encode_buffer(context->gfp, (void *) audio, NULL, audio_read / sizeof(int16_t), mp3buf, sizeof(mp3buf))) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); goto error; } } if (rlen) { ret = shout_send(context->shout, mp3buf, rlen); if (ret != SHOUTERR_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Send error: %s\n", shout_get_error(context->shout)); goto error; } } else { memset(mp3buf, 0, 128); ret = shout_send(context->shout, mp3buf, 128); } shout_sync(context->shout); switch_yield(100000); } error: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write Thread Done\n"); switch_thread_rwlock_unlock(context->rwlock); context->thread_running = 0; return NULL; }
void do_broadcast(switch_stream_handle_t *stream) { char *path_info = switch_event_get_header(stream->param_event, "http-path-info"); char *file; lame_global_flags *gfp = NULL; switch_file_handle_t fh = { 0 }; unsigned char mp3buf[TC_BUFFER_SIZE] = ""; uint8_t buf[1024]; int rlen; int is_local = 0; uint32_t interval = 20000; if (strstr(path_info + 7, "://")) { file = strdup(path_info + 7); is_local++; } else { file = switch_mprintf("%s/streamfiles/%s", SWITCH_GLOBAL_dirs.base_dir, path_info + 7); } assert(file); if (switch_core_file_open(&fh, file, 0, 0, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { memset(&fh, 0, sizeof(fh)); stream->write_function(stream, "Content-type: text/html\r\n\r\n<h2>File not found</h2>\n"); goto end; } if (switch_test_flag((&fh), SWITCH_FILE_NATIVE)) { stream->write_function(stream, "Content-type: text/html\r\n\r\n<h2>File format not supported</h2>\n"); goto end; } if (!(gfp = lame_init())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate lame\n"); goto end; } lame_set_num_channels(gfp, fh.channels); lame_set_in_samplerate(gfp, fh.samplerate); lame_set_brate(gfp, 16 * (fh.samplerate / 8000) * fh.channels); lame_set_mode(gfp, 3); lame_set_quality(gfp, 2); lame_set_errorf(gfp, log_error); lame_set_debugf(gfp, log_debug); lame_set_msgf(gfp, log_msg); lame_set_bWriteVbrTag(gfp, 0); lame_mp3_tags_fid(gfp, NULL); lame_init_params(gfp); lame_print_config(gfp); stream->write_function(stream, "Content-type: audio/mpeg\r\n" "Content-Disposition: inline; filename=\"%s.mp3\"\r\n\r\n", path_info + 7); if (fh.interval) { interval = fh.interval * 1000; } for (;;) { switch_size_t samples = sizeof(buf) / 2; switch_core_file_read(&fh, buf, &samples); if (is_local) { switch_yield(interval); } if (!samples) { break; } if ((rlen = lame_encode_buffer(gfp, (void *) buf, NULL, samples, mp3buf, sizeof(mp3buf))) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); goto end; } if (rlen) { if (stream->raw_write_function(stream, mp3buf, rlen)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Disconnected\n"); goto end; } } } while ((rlen = lame_encode_flush(gfp, mp3buf, sizeof(mp3buf))) > 0) { if (stream->raw_write_function(stream, mp3buf, rlen)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Disconnected\n"); goto end; } } end: if (fh.channels) { switch_core_file_close(&fh); } switch_safe_free(file); if (gfp) { lame_close(gfp); gfp = NULL; } }
void do_telecast(switch_stream_handle_t *stream) { char *path_info = switch_event_get_header(stream->param_event, "http-path-info"); char *uuid = strdup(path_info + 4); switch_core_session_t *tsession; char *fname = "stream.mp3"; if ((fname = strchr(uuid, '/'))) { *fname++ = '\0'; } if (!(tsession = switch_core_session_locate(uuid))) { char *ref = switch_event_get_header(stream->param_event, "http-referer"); stream->write_function(stream, "Content-type: text/html\r\n\r\n<h2>Not Found!</h2>\n" "<META http-equiv=\"refresh\" content=\"1;URL=%s\">", ref); } else { switch_media_bug_t *bug = NULL; switch_buffer_t *buffer = NULL; switch_mutex_t *mutex; switch_channel_t *channel = switch_core_session_get_channel(tsession); lame_global_flags *gfp = NULL; switch_codec_implementation_t read_impl = { 0 }; switch_core_session_get_read_impl(tsession, &read_impl); if (switch_channel_test_flag(channel, CF_PROXY_MODE)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Stepping into media path so this will work!\n"); switch_ivr_media(uuid, SMF_REBRIDGE); } if (!(gfp = lame_init())) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not allocate lame\n"); goto end; } lame_set_num_channels(gfp, read_impl.number_of_channels); lame_set_in_samplerate(gfp, read_impl.actual_samples_per_second); lame_set_brate(gfp, 16 * (read_impl.actual_samples_per_second / 8000) * read_impl.number_of_channels); lame_set_mode(gfp, 3); lame_set_quality(gfp, 2); lame_set_errorf(gfp, log_error); lame_set_debugf(gfp, log_debug); lame_set_msgf(gfp, log_msg); lame_set_bWriteVbrTag(gfp, 0); lame_mp3_tags_fid(gfp, NULL); lame_init_params(gfp); lame_print_config(gfp); switch_mutex_init(&mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(tsession)); switch_buffer_create_dynamic(&buffer, 1024, 2048, 0); switch_buffer_add_mutex(buffer, mutex); if (switch_core_media_bug_add(tsession, "telecast", NULL, telecast_callback, buffer, 0, SMBF_READ_STREAM | SMBF_WRITE_STREAM | SMBF_READ_PING, &bug) != SWITCH_STATUS_SUCCESS) { goto end; } stream->write_function(stream, "Content-type: audio/mpeg\r\n" "Content-Disposition: inline; filename=\"%s\"\r\n\r\n", fname); while (switch_channel_ready(channel)) { unsigned char mp3buf[TC_BUFFER_SIZE] = ""; int rlen; uint8_t buf[1024]; switch_size_t bytes = 0; if (switch_buffer_inuse(buffer) >= 1024) { switch_buffer_lock(buffer); bytes = switch_buffer_read(buffer, buf, sizeof(buf)); switch_buffer_unlock(buffer); } else { if (!bytes) { switch_cond_next(); continue; } memset(buf, 0, bytes); } if ((rlen = lame_encode_buffer(gfp, (void *) buf, NULL, bytes / 2, mp3buf, sizeof(mp3buf))) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "MP3 encode error %d!\n", rlen); goto end; } if (rlen) { if (stream->raw_write_function(stream, mp3buf, rlen)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Disconnected\n"); goto end; } } } end: switch_safe_free(uuid); if (gfp) { lame_close(gfp); gfp = NULL; } if (bug) { switch_core_media_bug_remove(tsession, &bug); } if (buffer) { switch_buffer_destroy(&buffer); } switch_core_session_rwunlock(tsession); } }