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 ); }
//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); }
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_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; }
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); } }
int mp3_encode(int fd_in, int fd_out) { lame_global_flags *gfp; uint32_t i, j, k, count; int16_t *b0 = 0, *b1 = 0; unsigned char *c; int ret; /* calculate data size*/ k = (uint32_t) lseek(fd_in, 0, SEEK_CUR); i = (uint32_t) lseek(fd_in, 0, SEEK_END); lseek(fd_in, k, SEEK_SET); i -= k; k = 0; gfp = lame_init(); lame_set_errorf(gfp,lame_error_handler); lame_set_debugf(gfp,lame_error_handler); lame_set_msgf(gfp,lame_error_handler); lame_set_num_channels(gfp,2); lame_set_in_samplerate(gfp,8000); #if 0 lame_set_brate(gfp,64); /* compress 1:4 */ lame_set_mode(gfp,0); /* mode = stereo */ lame_set_quality(gfp,2); /* 2=high 5 = medium 7=low */ #else lame_set_quality(gfp,5); /* 2=high 5 = medium 7=low */ lame_set_mode(gfp,3); /* mode = mono */ if(lame_get_VBR(gfp) == vbr_off) lame_set_VBR(gfp, vbr_default); lame_set_VBR_quality(gfp, 7.0); lame_set_findReplayGain(gfp, 0); lame_set_bWriteVbrTag(gfp, 1); lame_set_out_samplerate(gfp,11025); /* lame_set_num_samples(gfp,i/2); */ #endif if(lame_init_params(gfp) < 0) { log_err("encode: failed to init lame"); return 0; } #define CHSZ (512*1024) #define OCHSZ (5*(CHSZ/8)+7200) b0 = (int16_t *) malloc(OCHSZ); b1 = (int16_t *) malloc(CHSZ); if(!b0 || !b1) { log_err("encode: out of memory"); if(b0) free(b0); if(b1) free(b1); return 0; } do { ret = 0; j = 0; count = 0; for(c = (unsigned char*) b1; count < CHSZ; c += ret) { j = (k + RSZ > i) ? i - k : RSZ; ret = read(fd_in,c,j); if(ret < 0) break; k += ret; count += ret; if(k == i) break; } if(ret < 0) break; if(count) { ret = lame_encode_buffer(gfp,b1,b1,count/2,(unsigned char *)b0,OCHSZ); if(ret < 0) break; c = (unsigned char *) b0; count = ret; for(j = 0; j < count; j += RSZ, c += ret) { ret = (j + RSZ > count) ? count - j : RSZ; write(fd_out,c,ret); } } } while(k < i); k = (uint32_t) lame_encode_flush(gfp,(unsigned char *)b0,OCHSZ); if(k) write(fd_out,b0,k); k = lame_get_lametag_frame(gfp,(unsigned char *)b0,OCHSZ); if(k > 0) write(fd_out,b0,k); lame_close(gfp); free(b0); free(b1); return 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; }
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; }
void lame_encoder_impl_internal::set_write_vbr_tag(bool _Val) { int lameRet = lame_set_bWriteVbrTag(m_Lame.get(), (int)_Val); DT_LAME_CHECK_ERR(lameRet); }
void *complete_encode_worker(void* arg) { int ret; ENC_WRK_ARGS *args = (ENC_WRK_ARGS*)arg; // parse argument struct while (true) { #ifdef __VERBOSE_ cout << "Checking for work\n"; #endif // determine which file to process next bool bFoundWork = false; int iFileIdx = -1; pthread_mutex_lock(&mutFilesFinished); for (int i = 0; i < args->iNumFiles; i++) { if (!args->pbFilesFinished[i]) { args->pbFilesFinished[i] = true; // mark as being worked on iFileIdx = i; bFoundWork = true; break; } } pthread_mutex_unlock(&mutFilesFinished); if (!bFoundWork) {// done yet? return NULL; // break } string sMyFile = args->pFilenames->at(iFileIdx); string sMyFileOut = sMyFile.substr(0, sMyFile.length() - 3) + "mp3"; // start working FMT_DATA *hdr = NULL; short *leftPcm = NULL, *rightPcm = NULL; // init encoding params lame_global_flags *gfp = lame_init(); lame_set_brate(gfp, 192); // increase bitrate lame_set_quality(gfp, 3); // increase quality level lame_set_bWriteVbrTag(gfp, 0); // parse wave file #ifdef __VERBOSE_ printf("Parsing %s ...\n", sMyFile.c_str()); #endif int iDataSize = -1; ret = read_wave(sMyFile.c_str(), hdr, leftPcm, rightPcm, iDataSize); if (ret != EXIT_SUCCESS) { printf("Error in file %s. Skipping.\n", sMyFile.c_str()); continue; // see if there's more to do } lame_set_num_channels(gfp, hdr->wChannels); lame_set_num_samples(gfp, iDataSize / hdr->wBlockAlign); // check params ret = lame_init_params(gfp); if (ret != 0) { cerr << "Invalid encoding parameters! Skipping file." << endl; continue; } // encode to mp3 ret = encode_to_file(gfp, hdr, leftPcm, rightPcm, iDataSize, sMyFileOut.c_str()); if (ret != EXIT_SUCCESS) { cerr << "Unable to encode mp3: " << sMyFileOut.c_str() << endl; continue; } printf("[:%i][ok] .... %s\n", args->iThreadId, sMyFile.c_str()); ++args->iProcessedFiles; lame_close(gfp); if (leftPcm != NULL) delete[] leftPcm; if (rightPcm != NULL) delete[] rightPcm; if (hdr != NULL) delete hdr; } pthread_exit((void*)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; }