OggFLAC_API OggFLAC__SeekableStreamEncoderState OggFLAC__seekable_stream_encoder_init(OggFLAC__SeekableStreamEncoder *encoder) { FLAC__ASSERT(0 != encoder); if(encoder->protected_->state != OggFLAC__SEEKABLE_STREAM_ENCODER_UNINITIALIZED) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_ALREADY_INITIALIZED; if(0 == encoder->private_->seek_callback || 0 == encoder->private_->tell_callback || 0 == encoder->private_->write_callback) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_INVALID_CALLBACK; if(!OggFLAC__ogg_encoder_aspect_init(&encoder->protected_->ogg_encoder_aspect)) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OGG_ERROR; if(0 != encoder->private_->seek_table && !FLAC__format_seektable_is_legal(encoder->private_->seek_table)) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_INVALID_SEEKTABLE; /* * These must be done before we init the stream encoder because that * calls the write_callback, which uses these values. */ encoder->private_->first_seekpoint_to_check = 0; encoder->private_->samples_written = 0; encoder->protected_->streaminfo_offset = 0; encoder->protected_->seektable_offset = 0; encoder->protected_->audio_offset = 0; FLAC__stream_encoder_set_write_callback(encoder->private_->FLAC_stream_encoder, write_callback_); FLAC__stream_encoder_set_metadata_callback(encoder->private_->FLAC_stream_encoder, metadata_callback_); FLAC__stream_encoder_set_client_data(encoder->private_->FLAC_stream_encoder, encoder); if(FLAC__stream_encoder_init(encoder->private_->FLAC_stream_encoder) != FLAC__STREAM_ENCODER_OK) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR; /* * Initializing the stream encoder writes all the metadata, so we * save the stream offset now. */ if(encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) != FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK) return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_TELL_ERROR; return encoder->protected_->state = OggFLAC__SEEKABLE_STREAM_ENCODER_OK; }
static int flac_output_open(const char *fname, const char *comment) { int fd; int nch; FLAC__StreamMetadata padding; FLAC__StreamMetadata *metadata[4]; int num_metadata = 0; #ifndef LEGACY_FLAC FLAC__StreamEncoderInitStatus init_status; #endif FLAC_ctx *ctx; if (flac_ctx == NULL) flac_session_close(); if (!(flac_ctx = (FLAC_ctx *)calloc(sizeof(FLAC_ctx), 1))) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s", strerror(errno)); return -1; } ctx = flac_ctx; ctx->in_bytes = ctx->out_bytes = 0; if(strcmp(fname, "-") == 0) { fd = 1; /* data to stdout */ if (comment == NULL) comment = "(stdout)"; } else { /* Open the audio file */ fd = open(fname, FILE_OUTPUT_MODE); if(fd < 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", fname, strerror(errno)); return -1; } if(comment == NULL) comment = fname; } dpm.fd = fd; nch = (dpm.encoding & PE_MONO) ? 1 : 2; if (0 < flac_options.padding) { padding.is_last = 0; padding.type = FLAC__METADATA_TYPE_PADDING; padding.length = flac_options.padding; metadata[num_metadata++] = &padding; } #ifdef LEGACY_FLAC #ifdef AU_OGGFLAC if (flac_options.isogg) { if ((ctx->encoder.ogg.stream = OggFLAC__stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC stream"); flac_session_close(); return -1; } OggFLAC__stream_encoder_set_channels(ctx->encoder.ogg.stream, nch); /* 16bps only */ OggFLAC__stream_encoder_set_bits_per_sample(ctx->encoder.ogg.stream, 16); /* set sequential number for serial */ serial_number++; if (serial_number == 1) { srand(time(NULL)); serial_number = rand(); } OggFLAC__stream_encoder_set_serial_number(ctx->encoder.ogg.stream, serial_number); OggFLAC__stream_encoder_set_verify(ctx->encoder.ogg.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; } OggFLAC__stream_encoder_set_sample_rate(ctx->encoder.ogg.stream, dpm.rate); OggFLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision); /* expensive! */ OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { OggFLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.mid_side); OggFLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.adaptive_mid_side); } OggFLAC__stream_encoder_set_max_lpc_order(ctx->encoder.ogg.stream, flac_options.max_lpc_order); OggFLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.ogg.stream, flac_options.min_residual_partition_order); OggFLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.ogg.stream, flac_options.max_residual_partition_order); OggFLAC__stream_encoder_set_blocksize(ctx->encoder.ogg.stream, flac_options.blocksize); OggFLAC__stream_encoder_set_client_data(ctx->encoder.ogg.stream, ctx); if (0 < num_metadata) OggFLAC__stream_encoder_set_metadata(ctx->encoder.ogg.stream, metadata, num_metadata); /* set callback */ OggFLAC__stream_encoder_set_write_callback(ctx->encoder.ogg.stream, ogg_stream_encoder_write_callback); ctx->state.ogg = OggFLAC__stream_encoder_init(ctx->encoder.ogg.stream); if (ctx->state.ogg != OggFLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC state (%s)", OggFLAC__StreamEncoderStateString[ctx->state.ogg]); flac_session_close(); return -1; } } else #endif /* AU_OGGFLAC */ if (flac_options.seekable) { 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); /* */ #if (!defined(__BORLANDC__) && !defined(__POCC__)) 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; } } #else /* !LEGACY_FLAC */ 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; } #ifdef AU_OGGFLAC if (flac_options.isogg) { /* set sequential number for serial */ serial_number++; if (serial_number == 1) { srand(time(NULL)); serial_number = rand(); } FLAC__stream_encoder_set_ogg_serial_number(ctx->encoder.flac.stream, serial_number); } #endif /* AU_OGGFLAC */ 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); if (0 < num_metadata) FLAC__stream_encoder_set_metadata(ctx->encoder.flac.stream, metadata, num_metadata); #ifdef AU_OGGFLAC if (flac_options.isogg) init_status = FLAC__stream_encoder_init_ogg_stream(ctx->encoder.flac.stream, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx); else #endif init_status = FLAC__stream_encoder_init_stream(ctx->encoder.flac.stream, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx); if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC encoder (init status: %s)", FLAC__StreamEncoderInitStatusString[init_status]); flac_session_close(); return -1; } #endif return 0; }
QString & SampleBuffer::toBase64( QString & _dst ) const { #ifdef LMMS_HAVE_FLAC_STREAM_ENCODER_H const f_cnt_t FRAMES_PER_BUF = 1152; FLAC__StreamEncoder * flac_enc = FLAC__stream_encoder_new(); FLAC__stream_encoder_set_channels( flac_enc, DEFAULT_CHANNELS ); FLAC__stream_encoder_set_blocksize( flac_enc, FRAMES_PER_BUF ); /* FLAC__stream_encoder_set_do_exhaustive_model_search( flac_enc, true ); FLAC__stream_encoder_set_do_mid_side_stereo( flac_enc, true );*/ FLAC__stream_encoder_set_sample_rate( flac_enc, engine::mixer()->sampleRate() ); QBuffer ba_writer; ba_writer.open( QBuffer::WriteOnly ); FLAC__stream_encoder_set_write_callback( flac_enc, flacStreamEncoderWriteCallback ); FLAC__stream_encoder_set_metadata_callback( flac_enc, flacStreamEncoderMetadataCallback ); FLAC__stream_encoder_set_client_data( flac_enc, &ba_writer ); if( FLAC__stream_encoder_init( flac_enc ) != FLAC__STREAM_ENCODER_OK ) { printf( "error within FLAC__stream_encoder_init()!\n" ); } f_cnt_t frame_cnt = 0; while( frame_cnt < m_frames ) { f_cnt_t remaining = qMin<f_cnt_t>( FRAMES_PER_BUF, m_frames - frame_cnt ); FLAC__int32 buf[FRAMES_PER_BUF * DEFAULT_CHANNELS]; for( f_cnt_t f = 0; f < remaining; ++f ) { for( ch_cnt_t ch = 0; ch < DEFAULT_CHANNELS; ++ch ) { buf[f*DEFAULT_CHANNELS+ch] = (FLAC__int32)( Mixer::clip( m_data[f+frame_cnt][ch] ) * OUTPUT_SAMPLE_MULTIPLIER ); } } FLAC__stream_encoder_process_interleaved( flac_enc, buf, remaining ); frame_cnt += remaining; } FLAC__stream_encoder_finish( flac_enc ); FLAC__stream_encoder_delete( flac_enc ); printf("%d %d\n", frame_cnt, (int)ba_writer.size() ); ba_writer.close(); base64::encode( ba_writer.buffer().data(), ba_writer.buffer().size(), _dst ); #else /* LMMS_HAVE_FLAC_STREAM_ENCODER_H */ base64::encode( (const char *) m_data, m_frames * sizeof( sampleFrame ), _dst ); #endif /* LMMS_HAVE_FLAC_STREAM_ENCODER_H */ return _dst; }
static int start_write(sox_format_t * const ft) { priv_t * p = (priv_t *)ft->priv; FLAC__StreamEncoderState status; unsigned compression_level = MAX_COMPRESSION; /* Default to "best" */ if (ft->encoding.compression != HUGE_VAL) { compression_level = ft->encoding.compression; if (compression_level != ft->encoding.compression || compression_level > MAX_COMPRESSION) { lsx_fail_errno(ft, SOX_EINVAL, "FLAC compression level must be a whole number from 0 to %i", MAX_COMPRESSION); return SOX_EOF; } } p->encoder = FLAC__stream_encoder_new(); if (p->encoder == NULL) { lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder instance"); return SOX_EOF; } p->decoded_samples = lsx_malloc(sox_globals.bufsiz * sizeof(FLAC__int32)); p->bits_per_sample = ft->encoding.bits_per_sample; lsx_report("encoding at %i bits per sample", p->bits_per_sample); FLAC__stream_encoder_set_channels(p->encoder, ft->signal.channels); FLAC__stream_encoder_set_bits_per_sample(p->encoder, p->bits_per_sample); FLAC__stream_encoder_set_sample_rate(p->encoder, (unsigned)(ft->signal.rate + .5)); { /* Check if rate is streamable: */ static const unsigned streamable_rates[] = {8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000}; size_t i; sox_bool streamable = sox_false; for (i = 0; !streamable && i < array_length(streamable_rates); ++i) streamable = (streamable_rates[i] == ft->signal.rate); if (!streamable) { lsx_report("non-standard rate; output may not be streamable"); FLAC__stream_encoder_set_streamable_subset(p->encoder, sox_false); } } #if FLAC_API_VERSION_CURRENT >= 10 FLAC__stream_encoder_set_compression_level(p->encoder, compression_level); #else { static struct { unsigned blocksize; FLAC__bool do_exhaustive_model_search; FLAC__bool do_mid_side_stereo; FLAC__bool loose_mid_side_stereo; unsigned max_lpc_order; unsigned max_residual_partition_order; unsigned min_residual_partition_order; } const options[MAX_COMPRESSION + 1] = { {1152, sox_false, sox_false, sox_false, 0, 2, 2}, {1152, sox_false, sox_true, sox_true, 0, 2, 2}, {1152, sox_false, sox_true, sox_false, 0, 3, 0}, {4608, sox_false, sox_false, sox_false, 6, 3, 3}, {4608, sox_false, sox_true, sox_true, 8, 3, 3}, {4608, sox_false, sox_true, sox_false, 8, 3, 3}, {4608, sox_false, sox_true, sox_false, 8, 4, 0}, {4608, sox_true, sox_true, sox_false, 8, 6, 0}, {4608, sox_true, sox_true, sox_false, 12, 6, 0}, }; #define SET_OPTION(x) do {\ lsx_report(#x" = %i", options[compression_level].x); \ FLAC__stream_encoder_set_##x(p->encoder, options[compression_level].x);\ } while (0) SET_OPTION(blocksize); SET_OPTION(do_exhaustive_model_search); SET_OPTION(max_lpc_order); SET_OPTION(max_residual_partition_order); SET_OPTION(min_residual_partition_order); if (ft->signal.channels == 2) { SET_OPTION(do_mid_side_stereo); SET_OPTION(loose_mid_side_stereo); } #undef SET_OPTION } #endif if (ft->signal.length != 0) { FLAC__stream_encoder_set_total_samples_estimate(p->encoder, (FLAC__uint64)(ft->signal.length / ft->signal.channels)); p->metadata[p->num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); if (p->metadata[p->num_metadata] == NULL) { lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder seek table template"); return SOX_EOF; } { #if FLAC_API_VERSION_CURRENT >= 8 if (!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(p->metadata[p->num_metadata], (unsigned)(10 * ft->signal.rate + .5), (FLAC__uint64)(ft->signal.length/ft->signal.channels))) { #else size_t samples = 10 * ft->signal.rate; size_t total_samples = ft->signal.length/ft->signal.channels; if (!FLAC__metadata_object_seektable_template_append_spaced_points(p->metadata[p->num_metadata], total_samples / samples + (total_samples % samples != 0), (FLAC__uint64)total_samples)) { #endif lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder seek table points"); return SOX_EOF; } } p->metadata[p->num_metadata]->is_last = sox_false; /* the encoder will set this for us */ ++p->num_metadata; } if (ft->oob.comments) { /* Make the comment structure */ FLAC__StreamMetadata_VorbisComment_Entry entry; int i; p->metadata[p->num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); for (i = 0; ft->oob.comments[i]; ++i) { static const char prepend[] = "Comment="; char * text = lsx_calloc(strlen(prepend) + strlen(ft->oob.comments[i]) + 1, sizeof(*text)); /* Prepend `Comment=' if no field-name already in the comment */ if (!strchr(ft->oob.comments[i], '=')) strcpy(text, prepend); entry.entry = (FLAC__byte *) strcat(text, ft->oob.comments[i]); entry.length = strlen(text); FLAC__metadata_object_vorbiscomment_append_comment(p->metadata[p->num_metadata], entry, /*copy= */ sox_true); free(text); } ++p->num_metadata; } if (p->num_metadata) FLAC__stream_encoder_set_metadata(p->encoder, p->metadata, p->num_metadata); #if FLAC_API_VERSION_CURRENT <= 7 FLAC__stream_encoder_set_write_callback(p->encoder, flac_stream_encoder_write_callback); FLAC__stream_encoder_set_metadata_callback(p->encoder, flac_stream_encoder_metadata_callback); FLAC__stream_encoder_set_client_data(p->encoder, ft); status = FLAC__stream_encoder_init(p->encoder); #else status = FLAC__stream_encoder_init_stream(p->encoder, flac_stream_encoder_write_callback, flac_stream_encoder_seek_callback, flac_stream_encoder_tell_callback, flac_stream_encoder_metadata_callback, ft); #endif if (status != FLAC__STREAM_ENCODER_OK) { lsx_fail_errno(ft, SOX_EINVAL, "%s", FLAC__StreamEncoderStateString[status]); return SOX_EOF; } return SOX_SUCCESS; } static size_t write_samples(sox_format_t * const ft, sox_sample_t const * const sampleBuffer, size_t const len) { priv_t * p = (priv_t *)ft->priv; unsigned i; for (i = 0; i < len; ++i) { long pcm = SOX_SAMPLE_TO_SIGNED_32BIT(sampleBuffer[i], ft->clips); p->decoded_samples[i] = pcm >> (32 - p->bits_per_sample); switch (p->bits_per_sample) { case 8: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_8BIT(sampleBuffer[i], ft->clips); break; case 16: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_16BIT(sampleBuffer[i], ft->clips); break; case 24: p->decoded_samples[i] = /* sign extension: */ SOX_SAMPLE_TO_SIGNED_24BIT(sampleBuffer[i],ft->clips) << 8; p->decoded_samples[i] >>= 8; break; case 32: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_32BIT(sampleBuffer[i],ft->clips); break; } } FLAC__stream_encoder_process_interleaved(p->encoder, p->decoded_samples, (unsigned) len / ft->signal.channels); return FLAC__stream_encoder_get_state(p->encoder) == FLAC__STREAM_ENCODER_OK ? len : 0; }
static bool flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, GError **error) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; unsigned bits_per_sample; encoder->audio_format = *audio_format; /* FIXME: flac should support 32bit as well */ switch (audio_format->format) { case SAMPLE_FORMAT_S8: bits_per_sample = 8; break; case SAMPLE_FORMAT_S16: bits_per_sample = 16; break; case SAMPLE_FORMAT_S24_P32: bits_per_sample = 24; break; default: bits_per_sample = 24; audio_format->format = SAMPLE_FORMAT_S24_P32; } /* allocate the encoder */ encoder->fse = FLAC__stream_encoder_new(); if (encoder->fse == NULL) { g_set_error(error, flac_encoder_quark(), 0, "flac_new() failed"); return false; } if (!flac_encoder_setup(encoder, bits_per_sample, error)) { FLAC__stream_encoder_delete(encoder->fse); return false; } encoder->buffer_length = 0; pcm_buffer_init(&encoder->buffer); pcm_buffer_init(&encoder->expand_buffer); /* this immediately outputs data through callback */ #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 { FLAC__StreamEncoderState init_status; FLAC__stream_encoder_set_write_callback(encoder->fse, flac_write_callback); init_status = FLAC__stream_encoder_init(encoder->fse); if (init_status != FLAC__STREAM_ENCODER_OK) { g_set_error(error, flac_encoder_quark(), 0, "failed to initialize encoder: %s\n", FLAC__StreamEncoderStateString[init_status]); flac_encoder_close(_encoder); return false; } } #else { FLAC__StreamEncoderInitStatus init_status; init_status = FLAC__stream_encoder_init_stream(encoder->fse, flac_write_callback, NULL, NULL, NULL, encoder); if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { g_set_error(error, flac_encoder_quark(), 0, "failed to initialize encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]); flac_encoder_close(_encoder); return false; } } #endif return true; }