📄 flac_a.c
字号:
if ((ctx->encoder.flac.s_stream = FLAC__seekable_stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream"); flac_session_close(); return -1; } FLAC__seekable_stream_encoder_set_channels(ctx->encoder.flac.s_stream, nch); /* 16bps only */ FLAC__seekable_stream_encoder_set_bits_per_sample(ctx->encoder.flac.s_stream, 16); FLAC__seekable_stream_encoder_set_verify(ctx->encoder.flac.s_stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } FLAC__seekable_stream_encoder_set_sample_rate(ctx->encoder.flac.s_stream, dpm.rate); FLAC__seekable_stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision); /* expensive! */ FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { FLAC__seekable_stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.mid_side); FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.adaptive_mid_side); } FLAC__seekable_stream_encoder_set_max_lpc_order(ctx->encoder.flac.s_stream, flac_options.max_lpc_order); FLAC__seekable_stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.min_residual_partition_order); FLAC__seekable_stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.max_residual_partition_order); FLAC__seekable_stream_encoder_set_blocksize(ctx->encoder.flac.s_stream, flac_options.blocksize); FLAC__seekable_stream_encoder_set_client_data(ctx->encoder.flac.s_stream, ctx); if (0 < num_metadata) FLAC__seekable_stream_encoder_set_metadata(ctx->encoder.flac.s_stream, metadata, num_metadata); /* set callback *//* FLAC__seekable_stream_encoder_set_metadata_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_metadata_callback); /* */#ifndef __BORLANDC__ FLAC__stream_encoder_set_metadata_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_metadata_callback); /* */#endif FLAC__seekable_stream_encoder_set_write_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_write_callback); ctx->state.s_flac = FLAC__seekable_stream_encoder_init(ctx->encoder.flac.s_stream); if (ctx->state.s_flac != FLAC__SEEKABLE_STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)", FLAC__SeekableStreamEncoderStateString[ctx->state.s_flac]); flac_session_close(); return -1; } } else { if ((ctx->encoder.flac.stream = FLAC__stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream"); flac_session_close(); return -1; } FLAC__stream_encoder_set_channels(ctx->encoder.flac.stream, nch); /* 16bps only */ FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.flac.stream, 16); FLAC__stream_encoder_set_verify(ctx->encoder.flac.stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } FLAC__stream_encoder_set_sample_rate(ctx->encoder.flac.stream, dpm.rate); FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision); /* expensive! */ FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.stream, flac_options.mid_side); FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.stream, flac_options.adaptive_mid_side); } FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.flac.stream, flac_options.max_lpc_order); FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.stream, flac_options.min_residual_partition_order); FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.stream, flac_options.max_residual_partition_order); FLAC__stream_encoder_set_blocksize(ctx->encoder.flac.stream, flac_options.blocksize); FLAC__stream_encoder_set_client_data(ctx->encoder.flac.stream, ctx); if (0 < num_metadata) FLAC__stream_encoder_set_metadata(ctx->encoder.flac.stream, metadata, num_metadata); /* set callback */ FLAC__stream_encoder_set_metadata_callback(ctx->encoder.flac.stream, flac_stream_encoder_metadata_callback); FLAC__stream_encoder_set_write_callback(ctx->encoder.flac.stream, flac_stream_encoder_write_callback); ctx->state.flac = FLAC__stream_encoder_init(ctx->encoder.flac.stream); if (ctx->state.flac != FLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)", FLAC__StreamEncoderStateString[ctx->state.flac]); flac_session_close(); return -1; } } return 0;}static int auto_flac_output_open(const char *input_filename, const char *title){ char *output_filename;#ifdef AU_OGGFLAC if (flac_options.isogg) {#ifndef __W32G__ output_filename = create_auto_output_name(input_filename, "ogg", NULL, 0);#else output_filename = create_auto_output_name(input_filename, "ogg", w32g_output_dir, w32g_auto_output_mode);#endif } else#endif /* AU_OGGFLAC */ {#ifndef __W32G__ output_filename = create_auto_output_name(input_filename, "flac", NULL, 0);#else output_filename = create_auto_output_name(input_filename, "flac", w32g_output_dir, w32g_auto_output_mode);#endif } if (output_filename == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "unknown output file name"); return -1; } if ((flac_output_open(output_filename, input_filename)) == -1) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "files open failed %s->%s", output_filename, input_filename); free(output_filename); return -1; } if (dpm.name != NULL) free(dpm.name); dpm.name = output_filename; ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Output %s", dpm.name); return 0;}static int open_output(void){ int include_enc, exclude_enc; #ifdef AU_FLAC_DLL if (g_load_libFLAC_dll("libFLAC.dll")) { return -1; }#endif#ifdef AU_OGGFLAC_DLL if (g_load_libOggFLAC_dll("libOggFLAC.dll")) {#ifdef AU_FLAC_DLL g_free_libFLAC_dll ();#endif return -1; }#endif include_enc = exclude_enc = 0; /* only 16 bit is supported */ include_enc |= PE_16BIT | PE_SIGNED; exclude_enc |= PE_BYTESWAP | PE_24BIT; dpm.encoding = validate_encoding(dpm.encoding, include_enc, exclude_enc);#ifdef AU_OGGFLAC if (flac_options.isogg) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "*** cannot write back seekpoints when encoding to Ogg yet ***"); ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "*** and stream end will not be written. ***"); }#endif#ifndef __W32G__ if(dpm.name == NULL) { dpm.flag |= PF_AUTO_SPLIT_FILE; dpm.name = NULL; } else { dpm.flag &= ~PF_AUTO_SPLIT_FILE; if(flac_output_open(dpm.name, NULL) == -1) return -1; }#else if(w32g_auto_output_mode > 0) { dpm.flag |= PF_AUTO_SPLIT_FILE; dpm.name = NULL; } else { dpm.flag &= ~PF_AUTO_SPLIT_FILE; if (flac_output_open(dpm.name, NULL) == -1) return -1; }#endif return 0;}#ifdef AU_OGGFLACstatic FLAC__StreamEncoderWriteStatusogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data){ FLAC_ctx *ctx = (FLAC_ctx *)client_data; ctx->out_bytes += bytes; if (write(dpm.fd, buffer, bytes) != -1) return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; else return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;}#endifstatic FLAC__StreamEncoderWriteStatusflac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data){ FLAC_ctx *ctx = (FLAC_ctx *)client_data; ctx->out_bytes += bytes; if (write(dpm.fd, buffer, bytes) == bytes) return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; else return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;}static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data){}static FLAC__StreamEncoderWriteStatusflac_seekable_stream_encoder_write_callback(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data){ FLAC_ctx *ctx = (FLAC_ctx *)client_data; ctx->out_bytes += bytes; if (write(dpm.fd, buffer, bytes) == bytes) return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; else return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;}static void flac_seekable_stream_encoder_metadata_callback(const FLAC__SeekableStreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data){}static int output_data(char *buf, int32 nbytes){ FLAC__int32 *oggbuf; FLAC__int16 *s; int i; int nch = (dpm.encoding & PE_MONO) ? 1 : 2; FLAC_ctx *ctx = flac_ctx; if (dpm.fd < 0) return 0; if (ctx == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream is not initialized"); return -1; } oggbuf = (FLAC__int32 *)safe_malloc(nbytes * sizeof(FLAC__int32) / nch); /* packing 16 -> 32 bit sample */ s = (FLAC__int16 *)buf; for (i = 0; i < nbytes / nch; i++) { oggbuf[i] = *s++; }#ifdef AU_OGGFLAC if (flac_options.isogg) { ctx->state.ogg = OggFLAC__stream_encoder_get_state(ctx->encoder.ogg.stream); if (ctx->state.ogg != OggFLAC__STREAM_ENCODER_OK) { if (ctx->state.ogg == OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream verify error (%s)", FLAC__StreamDecoderStateString[OggFLAC__stream_encoder_get_verify_decoder_state(ctx->encoder.ogg.stream)]); } ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode OggFLAC stream (%s)", OggFLAC__StreamEncoderStateString[ctx->state.ogg]); flac_session_close(); return -1; } if (!OggFLAC__stream_encoder_process_interleaved(ctx->encoder.ogg.stream, oggbuf, nbytes / nch / 2)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode OggFLAC stream"); flac_session_close(); return -1; } } else#endif /* AU_OGGFLAC */ if (flac_options.seekable) { ctx->state.s_flac = FLAC__seekable_stream_encoder_get_state(ctx->encoder.flac.s_stream); if (ctx->state.s_flac != FLAC__STREAM_ENCODER_OK) { if (ctx->state.s_flac == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR | FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream verify error (%s)", FLAC__SeekableStreamDecoderStateString[FLAC__seekable_stream_encoder_get_verify_decoder_state(ctx->encoder.flac.s_stream)]); } else { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream (%s)", FLAC__SeekableStreamEncoderStateString[ctx->state.s_flac]); } flac_session_close(); return -1; } if (!FLAC__seekable_stream_encoder_process_interleaved(ctx->encoder.flac.s_stream, oggbuf, nbytes / nch / 2 )) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream"); flac_session_close(); return -1; } } else { ctx->state.flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.stream); if (ctx->state.flac != FLAC__STREAM_ENCODER_OK) { if (ctx->state.flac == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR | FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream verify error (%s)", FLAC__StreamDecoderStateString[FLAC__stream_encoder_get_verify_decoder_state(ctx->encoder.flac.stream)]); } else { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream (%s)", FLAC__StreamEncoderStateString[ctx->state.flac]); } flac_session_close(); return -1; } if (!FLAC__stream_encoder_process_interleaved(ctx->encoder.flac.stream, oggbuf, nbytes / nch / 2 )) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot encode FLAC stream"); flac_session_close(); return -1; } } ctx->in_bytes += nbytes; free(oggbuf); return 0;}static void close_output(void){ FLAC_ctx *ctx; ctx = flac_ctx; if (ctx == NULL) return; if (dpm.fd < 0) { flac_session_close(); return; } if (flac_options.isogg) {#ifdef AU_OGGFLAC if ((ctx->state.ogg = OggFLAC__stream_encoder_get_state(ctx->encoder.ogg.stream)) != OggFLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "OggFLAC stream encoder is invalid (%s)", OggFLAC__StreamEncoderStateString[ctx->state.ogg]); /* fall through */ } } else#endif /* AU_OGGFLAC */ if (flac_options.seekable) { if ((ctx->state.s_flac = FLAC__seekable_stream_encoder_get_state(ctx->encoder.flac.s_stream)) != FLAC__SEEKABLE_STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream encoder is invalid (%s)", FLAC__SeekableStreamEncoderStateString[ctx->state.s_flac]); /* fall through */ } } else { if ((ctx->state.flac = FLAC__stream_encoder_get_state(ctx->encoder.flac.stream)) != FLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "FLAC stream encoder is invalid (%s)", FLAC__StreamEncoderStateString[ctx->state.flac]); /* fall through */ } } ctl->cmsg(CMSG_INFO, VERB_NORMAL, "Wrote %lu/%lu bytes(%g%% compressed)", ctx->out_bytes, ctx->in_bytes, ((double)ctx->out_bytes / (double)ctx->in_bytes) * 100.); flac_session_close();#ifdef AU_FLAC_DLL g_free_libFLAC_dll ();#endif#ifdef AU_OGGFLAC_DLL g_free_libOggFLAC_dll ();#endif}static int acntl(int request, void *arg){ switch(request) { case PM_REQ_PLAY_START: if(dpm.flag & PF_AUTO_SPLIT_FILE) return auto_flac_output_open(current_file_info->filename, current_file_info->seq_name); break; case PM_REQ_PLAY_END: if(dpm.flag & PF_AUTO_SPLIT_FILE) { close_output(); return 0; } break; case PM_REQ_DISCARD: return 0; } return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -