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()); }
static void progress_line(const lame_global_flags * gf, int full, int frames) { char rst[20] = "\0"; int barlen_TOT = 0, barlen_COD = 0, barlen_RST = 0; int res = 1; float time_in_sec = 0; unsigned int hour, min, sec; int fsize = lame_get_framesize(gf); int srate = lame_get_out_samplerate(gf); if (full < frames) { full = frames; } if (srate > 0) { time_in_sec = (float)(full - frames); time_in_sec *= fsize; time_in_sec /= srate; } hour = (unsigned int)(time_in_sec / 3600); time_in_sec -= hour * 3600; min = (unsigned int)(time_in_sec / 60); time_in_sec -= min * 60; sec = (unsigned int)time_in_sec; if (full != 0) { if (hour > 0) { sprintf(rst, "%*d:%02u:%02u", digits(hour), hour, min, sec); res += digits(hour) + 1 + 5; } else { sprintf(rst, "%02u:%02u", min, sec); res += 5; } /* some problems when br_hist_TOT \approx br_hist_LR: You can't see that there are still MS frames */ barlen_TOT = (full * (Console_IO.disp_width - res) + full - 1) / full; /* round up */ barlen_COD = (frames * (Console_IO.disp_width - res) + full - 1) / full; /* round up */ barlen_RST = barlen_TOT - barlen_COD; if (barlen_RST == 0) { sprintf(rst, "%.*s", res - 1, brhist.bar_coded); } } else { barlen_TOT = barlen_COD = barlen_RST = 0; } if (Console_IO.str_clreoln[0]) { /* ClearEndOfLine available */ console_printf("\n%.*s%s%.*s%s", barlen_COD, brhist.bar_coded, rst, barlen_RST, brhist.bar_space, Console_IO.str_clreoln); } else { console_printf("\n%.*s%s%.*s%*s", barlen_COD, brhist.bar_coded, rst, barlen_RST, brhist.bar_space, Console_IO.disp_width - res - barlen_TOT, ""); } brhist.hist_printed_lines++; }
void timestatus_klemm ( const lame_global_flags* const gfp ) { static double last_time = 0.; if ( silent <= 0 ) if ( lame_get_frameNum(gfp) == 0 || lame_get_frameNum(gfp) == 9 || GetRealTime () - last_time >= update_interval || GetRealTime () - last_time < 0 ) { #ifdef BRHIST brhist_jump_back(); #endif timestatus ( lame_get_out_samplerate( gfp ), lame_get_frameNum(gfp), lame_get_totalframes(gfp), lame_get_framesize(gfp) ); #ifdef BRHIST if ( brhist ) { brhist_disp ( gfp ); } #endif last_time = GetRealTime (); /* from now! disp_time seconds */ } }
int lame_encoder(lame_global_flags * gf, FILE * outf, int nogap, char *inPath, char *outPath) { unsigned char mp3buffer[LAME_MAXMP3BUFFER]; int Buffer[2][1152]; int iread, imp3; static const char *mode_names[2][4] = { {"stereo", "j-stereo", "dual-ch", "single-ch"}, {"stereo", "force-ms", "dual-ch", "single-ch"} }; int frames; if (silent < 10) { lame_print_config(gf); /* print useful information about options being used */ fprintf(stderr, "Encoding %s%s to %s\n", strcmp(inPath, "-") ? inPath : "<stdin>", strlen(inPath) + strlen(outPath) < 66 ? "" : "\n ", strcmp(outPath, "-") ? outPath : "<stdout>"); fprintf(stderr, "Encoding as %g kHz ", 1.e-3 * lame_get_out_samplerate(gf)); { const char *appendix = ""; switch (lame_get_VBR(gf)) { case vbr_mt: case vbr_rh: case vbr_mtrh: appendix = "ca. "; fprintf(stderr, "VBR(q=%i)", lame_get_VBR_q(gf)); break; case vbr_abr: fprintf(stderr, "average %d kbps", lame_get_VBR_mean_bitrate_kbps(gf)); break; default: fprintf(stderr, "%3d kbps", lame_get_brate(gf)); break; } fprintf(stderr, " %s MPEG-%u%s Layer III (%s%gx) qval=%i\n", mode_names[lame_get_force_ms(gf)][lame_get_mode(gf)], 2 - lame_get_version(gf), lame_get_out_samplerate(gf) < 16000 ? ".5" : "", appendix, 0.1 * (int) (10. * lame_get_compression_ratio(gf) + 0.5), lame_get_quality(gf)); } if (silent <= -10) lame_print_internals(gf); fflush(stderr); } /* encode until we hit eof */ do { /* read in 'iread' samples */ iread = get_audio(gf, Buffer); frames = lame_get_frameNum(gf); /********************** status display *****************************/ if (silent <= 0) { if (update_interval > 0) { timestatus_klemm(gf); } else { if (0 == frames % 50) { #ifdef BRHIST brhist_jump_back(); #endif timestatus(lame_get_out_samplerate(gf), frames, lame_get_totalframes(gf), lame_get_framesize(gf)); #ifdef BRHIST if (brhist) brhist_disp(gf); #endif } } } /* encode */ imp3 = lame_encode_buffer_int(gf, Buffer[0], Buffer[1], iread, mp3buffer, sizeof(mp3buffer)); /* was our output buffer big enough? */ if (imp3 < 0) { if (imp3 == -1) fprintf(stderr, "mp3 buffer is not big enough... \n"); else fprintf(stderr, "mp3 internal error: error code=%i\n", imp3); return 1; } if (fwrite(mp3buffer, 1, imp3, outf) != imp3) { fprintf(stderr, "Error writing mp3 output \n"); return 1; } } while (iread); if (nogap) imp3 = lame_encode_flush_nogap(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */ else imp3 = lame_encode_flush(gf, mp3buffer, sizeof(mp3buffer)); /* may return one more mp3 frame */ if (imp3 < 0) { if (imp3 == -1) fprintf(stderr, "mp3 buffer is not big enough... \n"); else fprintf(stderr, "mp3 internal error: error code=%i\n", imp3); return 1; } if (silent <= 0) { #ifdef BRHIST brhist_jump_back(); #endif frames = lame_get_frameNum(gf); timestatus(lame_get_out_samplerate(gf), frames, lame_get_totalframes(gf), lame_get_framesize(gf)); #ifdef BRHIST if (brhist) { brhist_disp(gf); } brhist_disp_total(gf); #endif timestatus_finish(); } fwrite(mp3buffer, 1, imp3, outf); return 0; }
static void timestatus(const lame_global_flags * const gfp) { timestatus_t* real_time = &global_encoder_progress.real_time; timestatus_t* proc_time = &global_encoder_progress.proc_time; int percent; double tmx, delta; int samp_rate = lame_get_out_samplerate(gfp) , frameNum = lame_get_frameNum(gfp) , totalframes = lame_get_totalframes(gfp) , framesize = lame_get_framesize(gfp) ; if (totalframes < frameNum) { totalframes = frameNum; } if (global_encoder_progress.time_status_init == 0) { real_time->last_time = GetRealTime(); proc_time->last_time = GetCPUTime(); real_time->elapsed_time = 0; proc_time->elapsed_time = 0; } /* we need rollover protection for GetCPUTime, and maybe GetRealTime(): */ tmx = GetRealTime(); delta = tmx - real_time->last_time; if (delta < 0) delta = 0; /* ignore, clock has rolled over */ real_time->elapsed_time += delta; real_time->last_time = tmx; tmx = GetCPUTime(); delta = tmx - proc_time->last_time; if (delta < 0) delta = 0; /* ignore, clock has rolled over */ proc_time->elapsed_time += delta; proc_time->last_time = tmx; if (global_encoder_progress.time_status_init == 0) { console_printf("\r" " Frame | CPU time/estim | REAL time/estim | play/CPU | ETA \n" " 0/ ( 0%%)| 0:00/ : | 0:00/ : | " SPEED_CHAR "| : \r" /* , Console_IO.str_clreoln, Console_IO.str_clreoln */ ); global_encoder_progress.time_status_init = 1; return; } ts_calc_times(real_time, samp_rate, frameNum, totalframes, framesize); ts_calc_times(proc_time, samp_rate, frameNum, totalframes, framesize); if (frameNum < totalframes) { percent = (int) (100. * frameNum / totalframes + 0.5); } else { percent = 100; } console_printf("\r%6i/%-6i", frameNum, totalframes); console_printf(percent < 100 ? " (%2d%%)|" : "(%3.3d%%)|", percent); ts_time_decompose(proc_time->elapsed_time, '/'); ts_time_decompose(proc_time->estimated_time, '|'); ts_time_decompose(real_time->elapsed_time, '/'); ts_time_decompose(real_time->estimated_time, '|'); console_printf(proc_time->speed_index <= 1. ? "%9.4f" SPEED_CHAR "|" : "%#9.5g" SPEED_CHAR "|", SPEED_MULT * proc_time->speed_index); ts_time_decompose((real_time->estimated_time - real_time->elapsed_time), ' '); }