初始化内容
This commit is contained in:
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
add_subdirectory("c/decode/file")
|
||||
add_subdirectory("c/encode/file")
|
||||
|
||||
if(BUILD_CXXLIBS)
|
||||
add_subdirectory("cpp/decode/file")
|
||||
add_subdirectory("cpp/encode/file")
|
||||
endif()
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
if FLaC__WITH_CPPLIBS
|
||||
CPPLIBS_DIRS = cpp
|
||||
endif
|
||||
|
||||
SUBDIRS = c $(CPPLIBS_DIRS)
|
||||
|
||||
EXTRA_DIST = \
|
||||
CMakeLists.txt \
|
||||
README
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
Here are several small example programs that use the libraries in different
|
||||
ways.
|
||||
|
||||
The "c" directory has programs that are all in C and use libFLAC.
|
||||
|
||||
The "cpp" directory has analogous programs that are all in C++ and use libFLAC++.
|
||||
|
||||
The programs are:
|
||||
c/decode/file/ - example_c_decode_file - Simple FLAC file decoder using libFLAC
|
||||
c/encode/file/ - example_c_encode_file - Simple FLAC file encoder using libFLAC
|
||||
cpp/decode/file/ - example_cpp_decode_file - Simple FLAC file decoder using libFLAC++
|
||||
cpp/encode/file/ - example_cpp_encode_file - Simple FLAC file encoder using libFLAC++
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = decode encode
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = file
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(decode_file main.c)
|
||||
target_link_libraries(decode_file FLAC)
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# example_c_decode_file - Simple FLAC file decoder using libFLAC
|
||||
# Copyright (C) 2007-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
EXTRA_DIST = \
|
||||
CMakeLists.txt
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include
|
||||
noinst_PROGRAMS = example_c_decode_file
|
||||
example_c_decode_file_LDADD = \
|
||||
$(top_builddir)/src/libFLAC/libFLAC.la
|
||||
|
||||
example_c_decode_file_SOURCES = main.c
|
||||
|
||||
CLEANFILES = example_c_decode_file.exe
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
/* example_c_decode_file - Simple FLAC file decoder using libFLAC
|
||||
* Copyright (C) 2007-2009 Josh Coalson
|
||||
* Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows how to use libFLAC to decode a FLAC file to a WAVE
|
||||
* file. It only supports 16-bit stereo files.
|
||||
*
|
||||
* Complete API documentation can be found at:
|
||||
* http://xiph.org/flac/api/
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "share/compat.h"
|
||||
#include "FLAC/stream_decoder.h"
|
||||
|
||||
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
|
||||
static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
|
||||
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
|
||||
|
||||
static FLAC__uint64 total_samples = 0;
|
||||
static unsigned sample_rate = 0;
|
||||
static unsigned channels = 0;
|
||||
static unsigned bps = 0;
|
||||
|
||||
static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
|
||||
{
|
||||
return
|
||||
fputc(x, f) != EOF &&
|
||||
fputc(x >> 8, f) != EOF
|
||||
;
|
||||
}
|
||||
|
||||
static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
|
||||
{
|
||||
return write_little_endian_uint16(f, (FLAC__uint16)x);
|
||||
}
|
||||
|
||||
static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
|
||||
{
|
||||
return
|
||||
fputc(x, f) != EOF &&
|
||||
fputc(x >> 8, f) != EOF &&
|
||||
fputc(x >> 16, f) != EOF &&
|
||||
fputc(x >> 24, f) != EOF
|
||||
;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamDecoder *decoder = 0;
|
||||
FLAC__StreamDecoderInitStatus init_status;
|
||||
FILE *fout;
|
||||
|
||||
if(argc != 3) {
|
||||
fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if((fout = fopen(argv[2], "wb")) == NULL) {
|
||||
fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if((decoder = FLAC__stream_decoder_new()) == NULL) {
|
||||
fprintf(stderr, "ERROR: allocating decoder\n");
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
|
||||
|
||||
init_status = FLAC__stream_decoder_init_file(decoder, argv[1], write_callback, metadata_callback, error_callback, /*client_data=*/fout);
|
||||
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
|
||||
fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if(ok) {
|
||||
ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
|
||||
fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
|
||||
fprintf(stderr, " state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
|
||||
}
|
||||
|
||||
FLAC__stream_decoder_delete(decoder);
|
||||
fclose(fout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
|
||||
{
|
||||
FILE *f = (FILE*)client_data;
|
||||
const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
|
||||
size_t i;
|
||||
|
||||
(void)decoder;
|
||||
|
||||
if(total_samples == 0) {
|
||||
fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(channels != 2 || bps != 16) {
|
||||
fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(frame->header.channels != 2) {
|
||||
fprintf(stderr, "ERROR: This frame contains %u channels (should be 2)\n", frame->header.channels);
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(buffer [0] == NULL) {
|
||||
fprintf(stderr, "ERROR: buffer [0] is NULL\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(buffer [1] == NULL) {
|
||||
fprintf(stderr, "ERROR: buffer [1] is NULL\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
|
||||
/* write WAVE header before we write the first frame */
|
||||
if(frame->header.number.sample_number == 0) {
|
||||
if(
|
||||
fwrite("RIFF", 1, 4, f) < 4 ||
|
||||
!write_little_endian_uint32(f, total_size + 36) ||
|
||||
fwrite("WAVEfmt ", 1, 8, f) < 8 ||
|
||||
!write_little_endian_uint32(f, 16) ||
|
||||
!write_little_endian_uint16(f, 1) ||
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)channels) ||
|
||||
!write_little_endian_uint32(f, sample_rate) ||
|
||||
!write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)bps) ||
|
||||
fwrite("data", 1, 4, f) < 4 ||
|
||||
!write_little_endian_uint32(f, total_size)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: write error\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
}
|
||||
|
||||
/* write decoded PCM samples */
|
||||
for(i = 0; i < frame->header.blocksize; i++) {
|
||||
if(
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
|
||||
) {
|
||||
fprintf(stderr, "ERROR: write error\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
}
|
||||
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
|
||||
{
|
||||
(void)decoder, (void)client_data;
|
||||
|
||||
/* print some stats */
|
||||
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
||||
/* save for later */
|
||||
total_samples = metadata->data.stream_info.total_samples;
|
||||
sample_rate = metadata->data.stream_info.sample_rate;
|
||||
channels = metadata->data.stream_info.channels;
|
||||
bps = metadata->data.stream_info.bits_per_sample;
|
||||
|
||||
fprintf(stderr, "sample rate : %u Hz\n", sample_rate);
|
||||
fprintf(stderr, "channels : %u\n", channels);
|
||||
fprintf(stderr, "bits per sample: %u\n", bps);
|
||||
fprintf(stderr, "total samples : %" PRIu64 "\n", total_samples);
|
||||
}
|
||||
}
|
||||
|
||||
void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
|
||||
{
|
||||
(void)decoder, (void)client_data;
|
||||
|
||||
fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = file
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(encode_file main.c)
|
||||
target_link_libraries(encode_file FLAC)
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
# example_c_encode_file - Simple FLAC file encoder using libFLAC
|
||||
# Copyright (C) 2007-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
EXTRA_DIST = \
|
||||
CMakeLists.txt
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include
|
||||
noinst_PROGRAMS = example_c_encode_file
|
||||
example_c_encode_file_LDADD = \
|
||||
$(top_builddir)/src/libFLAC/libFLAC.la \
|
||||
@OGG_LIBS@ \
|
||||
-lm
|
||||
|
||||
example_c_encode_file_SOURCES = main.c
|
||||
|
||||
CLEANFILES = example_c_encode_file.exe
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
/* example_c_encode_file - Simple FLAC file encoder using libFLAC
|
||||
* Copyright (C) 2007-2009 Josh Coalson
|
||||
* Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows how to use libFLAC to encode a WAVE file to a FLAC
|
||||
* file. It only supports 16-bit stereo files in canonical WAVE format.
|
||||
*
|
||||
* Complete API documentation can be found at:
|
||||
* http://xiph.org/flac/api/
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "share/compat.h"
|
||||
#include "FLAC/metadata.h"
|
||||
#include "FLAC/stream_encoder.h"
|
||||
|
||||
static void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
|
||||
|
||||
#define READSIZE 1024
|
||||
|
||||
static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
|
||||
static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
|
||||
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FLAC__bool ok = true;
|
||||
FLAC__StreamEncoder *encoder = 0;
|
||||
FLAC__StreamEncoderInitStatus init_status;
|
||||
FLAC__StreamMetadata *metadata[2];
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
FILE *fin;
|
||||
unsigned sample_rate = 0;
|
||||
unsigned channels = 0;
|
||||
unsigned bps = 0;
|
||||
|
||||
if(argc != 3) {
|
||||
fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if((fin = fopen(argv[1], "rb")) == NULL) {
|
||||
fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* read wav header and validate it */
|
||||
if(
|
||||
fread(buffer, 1, 44, fin) != 44 ||
|
||||
memcmp(buffer, "RIFF", 4) ||
|
||||
memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
|
||||
memcmp(buffer+32, "\004\000\020\000data", 8)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
|
||||
fclose(fin);
|
||||
return 1;
|
||||
}
|
||||
sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
|
||||
channels = 2;
|
||||
bps = 16;
|
||||
total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
|
||||
|
||||
/* allocate the encoder */
|
||||
if((encoder = FLAC__stream_encoder_new()) == NULL) {
|
||||
fprintf(stderr, "ERROR: allocating encoder\n");
|
||||
fclose(fin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ok &= FLAC__stream_encoder_set_verify(encoder, true);
|
||||
ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
|
||||
ok &= FLAC__stream_encoder_set_channels(encoder, channels);
|
||||
ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
|
||||
ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
|
||||
ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
|
||||
|
||||
/* now add some metadata; we'll add some tags and a padding block */
|
||||
if(ok) {
|
||||
if(
|
||||
(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: out of memory or tag error\n");
|
||||
ok = false;
|
||||
} else {
|
||||
|
||||
metadata[1]->length = 1234; /* set the padding length */
|
||||
|
||||
ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* initialize encoder */
|
||||
if(ok) {
|
||||
init_status = FLAC__stream_encoder_init_file(encoder, argv[2], progress_callback, /*client_data=*/NULL);
|
||||
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
|
||||
fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
/* read blocks of samples from WAVE file and feed to encoder */
|
||||
if(ok) {
|
||||
size_t left = (size_t)total_samples;
|
||||
while(ok && left) {
|
||||
size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
|
||||
if(fread(buffer, channels*(bps/8), need, fin) != need) {
|
||||
fprintf(stderr, "ERROR: reading from WAVE file\n");
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
|
||||
size_t i;
|
||||
for(i = 0; i < need*channels; i++) {
|
||||
/* inefficient but simple and works on big- or little-endian machines */
|
||||
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
|
||||
}
|
||||
/* feed samples to encoder */
|
||||
ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
|
||||
}
|
||||
left -= need;
|
||||
}
|
||||
}
|
||||
|
||||
ok &= FLAC__stream_encoder_finish(encoder);
|
||||
|
||||
fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
|
||||
fprintf(stderr, " state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
|
||||
|
||||
/* now that encoding is finished, the metadata can be freed */
|
||||
FLAC__metadata_object_delete(metadata[0]);
|
||||
FLAC__metadata_object_delete(metadata[1]);
|
||||
|
||||
FLAC__stream_encoder_delete(encoder);
|
||||
fclose(fin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
|
||||
{
|
||||
(void)encoder, (void)client_data;
|
||||
|
||||
fprintf(stderr, "wrote %" PRIu64 " bytes, %" PRIu64 "/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = decode encode
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = file
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(decode_file_cxx main.cpp)
|
||||
target_link_libraries(decode_file_cxx FLAC++)
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
# example_cpp_decode_file - Simple FLAC file decoder using libFLAC
|
||||
# Copyright (C) 2007-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
EXTRA_DIST = \
|
||||
CMakeLists.txt
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include
|
||||
noinst_PROGRAMS = example_cpp_decode_file
|
||||
example_cpp_decode_file_LDADD = \
|
||||
$(top_builddir)/src/libFLAC++/libFLAC++.la \
|
||||
$(top_builddir)/src/libFLAC/libFLAC.la \
|
||||
@OGG_LIBS@ \
|
||||
-lm
|
||||
|
||||
example_cpp_decode_file_SOURCES = main.cpp
|
||||
|
||||
CLEANFILES = example_cpp_decode_file.exe
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
/* example_cpp_decode_file - Simple FLAC file decoder using libFLAC
|
||||
* Copyright (C) 2007-2009 Josh Coalson
|
||||
* Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows how to use libFLAC++ to decode a FLAC file to a WAVE
|
||||
* file. It only supports 16-bit stereo files.
|
||||
*
|
||||
* Complete API documentation can be found at:
|
||||
* http://xiph.org/flac/api/
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "FLAC++/decoder.h"
|
||||
#include "share/compat.h"
|
||||
|
||||
static FLAC__uint64 total_samples = 0;
|
||||
static uint32_t sample_rate = 0;
|
||||
static uint32_t channels = 0;
|
||||
static uint32_t bps = 0;
|
||||
|
||||
static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
|
||||
{
|
||||
return
|
||||
fputc(x, f) != EOF &&
|
||||
fputc(x >> 8, f) != EOF
|
||||
;
|
||||
}
|
||||
|
||||
static bool write_little_endian_int16(FILE *f, FLAC__int16 x)
|
||||
{
|
||||
return write_little_endian_uint16(f, (FLAC__uint16)x);
|
||||
}
|
||||
|
||||
static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
|
||||
{
|
||||
return
|
||||
fputc(x, f) != EOF &&
|
||||
fputc(x >> 8, f) != EOF &&
|
||||
fputc(x >> 16, f) != EOF &&
|
||||
fputc(x >> 24, f) != EOF
|
||||
;
|
||||
}
|
||||
|
||||
class OurDecoder: public FLAC::Decoder::File {
|
||||
public:
|
||||
OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { }
|
||||
protected:
|
||||
FILE *f;
|
||||
|
||||
virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
|
||||
virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
|
||||
virtual void error_callback(::FLAC__StreamDecoderErrorStatus status);
|
||||
private:
|
||||
OurDecoder(const OurDecoder&);
|
||||
OurDecoder&operator=(const OurDecoder&);
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool ok = true;
|
||||
FILE *fout;
|
||||
|
||||
if(argc != 3) {
|
||||
fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if((fout = fopen(argv[2], "wb")) == NULL) {
|
||||
fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
OurDecoder decoder(fout);
|
||||
|
||||
if(!decoder) {
|
||||
fprintf(stderr, "ERROR: allocating decoder\n");
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
(void)decoder.set_md5_checking(true);
|
||||
|
||||
FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]);
|
||||
if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
|
||||
fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if(ok) {
|
||||
ok = decoder.process_until_end_of_stream();
|
||||
fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
|
||||
fprintf(stderr, " state: %s\n", decoder.get_state().resolved_as_cstring(decoder));
|
||||
}
|
||||
|
||||
fclose(fout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
|
||||
{
|
||||
const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
|
||||
size_t i;
|
||||
|
||||
// Update data
|
||||
channels = OurDecoder::get_channels();
|
||||
bps = OurDecoder::get_bits_per_sample();
|
||||
|
||||
if(total_samples == 0) {
|
||||
fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
if(channels != 2 || bps != 16) {
|
||||
fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
|
||||
/* write WAVE header before we write the first frame */
|
||||
if(frame->header.number.sample_number == 0) {
|
||||
if(
|
||||
fwrite("RIFF", 1, 4, f) < 4 ||
|
||||
!write_little_endian_uint32(f, total_size + 36) ||
|
||||
fwrite("WAVEfmt ", 1, 8, f) < 8 ||
|
||||
!write_little_endian_uint32(f, 16) ||
|
||||
!write_little_endian_uint16(f, 1) ||
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)channels) ||
|
||||
!write_little_endian_uint32(f, sample_rate) ||
|
||||
!write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
|
||||
!write_little_endian_uint16(f, (FLAC__uint16)bps) ||
|
||||
fwrite("data", 1, 4, f) < 4 ||
|
||||
!write_little_endian_uint32(f, total_size)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: write error\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
}
|
||||
|
||||
/* write decoded PCM samples */
|
||||
for(i = 0; i < frame->header.blocksize; i++) {
|
||||
if(
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) || /* left channel */
|
||||
!write_little_endian_int16(f, (FLAC__int16)buffer[1][i]) /* right channel */
|
||||
) {
|
||||
fprintf(stderr, "ERROR: write error\n");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
}
|
||||
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
|
||||
{
|
||||
/* print some stats */
|
||||
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
|
||||
/* save for later */
|
||||
total_samples = metadata->data.stream_info.total_samples;
|
||||
sample_rate = metadata->data.stream_info.sample_rate;
|
||||
channels = metadata->data.stream_info.channels;
|
||||
bps = metadata->data.stream_info.bits_per_sample;
|
||||
|
||||
fprintf(stderr, "sample rate : %u Hz\n", sample_rate);
|
||||
fprintf(stderr, "channels : %u\n", channels);
|
||||
fprintf(stderr, "bits per sample: %u\n", bps);
|
||||
fprintf(stderr, "total samples : %" PRIu64 "\n", total_samples);
|
||||
}
|
||||
}
|
||||
|
||||
void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
|
||||
{
|
||||
fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# FLAC - Free Lossless Audio Codec
|
||||
# Copyright (C) 2001-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This file is part the FLAC project. FLAC is comprised of several
|
||||
# components distributed under different licenses. The codec libraries
|
||||
# are distributed under Xiph.Org's BSD-like license (see the file
|
||||
# COPYING.Xiph in this distribution). All other programs, libraries, and
|
||||
# plugins are distributed under the GPL (see COPYING.GPL). The documentation
|
||||
# is distributed under the Gnu FDL (see COPYING.FDL). Each file in the
|
||||
# FLAC distribution contains at the top the terms under which it may be
|
||||
# distributed.
|
||||
#
|
||||
# Since this particular file is relevant to all components of FLAC,
|
||||
# it may be distributed under the Xiph.Org license, which is the least
|
||||
# restrictive of those mentioned above. See the file COPYING.Xiph in this
|
||||
# distribution.
|
||||
|
||||
SUBDIRS = file
|
||||
@@ -0,0 +1,2 @@
|
||||
add_executable(encode_file_cxx main.cpp)
|
||||
target_link_libraries(encode_file_cxx FLAC++)
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
# example_cpp_encode_file - Simple FLAC file encoder using libFLAC
|
||||
# Copyright (C) 2007-2009 Josh Coalson
|
||||
# Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
EXTRA_DIST = \
|
||||
CMakeLists.txt
|
||||
|
||||
AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include
|
||||
noinst_PROGRAMS = example_cpp_encode_file
|
||||
example_cpp_encode_file_LDADD = \
|
||||
$(top_builddir)/src/libFLAC++/libFLAC++.la \
|
||||
$(top_builddir)/src/libFLAC/libFLAC.la \
|
||||
@OGG_LIBS@ \
|
||||
-lm
|
||||
|
||||
example_cpp_encode_file_SOURCES = main.cpp
|
||||
|
||||
CLEANFILES = example_cpp_encode_file.exe
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
/* example_cpp_encode_file - Simple FLAC file encoder using libFLAC
|
||||
* Copyright (C) 2007-2009 Josh Coalson
|
||||
* Copyright (C) 2011-2023 Xiph.Org Foundation
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This example shows how to use libFLAC++ to encode a WAVE file to a FLAC
|
||||
* file. It only supports 16-bit stereo files in canonical WAVE format.
|
||||
*
|
||||
* Complete API documentation can be found at:
|
||||
* http://xiph.org/flac/api/
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "FLAC++/metadata.h"
|
||||
#include "FLAC++/encoder.h"
|
||||
#include "share/compat.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
class OurEncoder: public FLAC::Encoder::File {
|
||||
public:
|
||||
OurEncoder(): FLAC::Encoder::File() { }
|
||||
protected:
|
||||
virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate);
|
||||
};
|
||||
|
||||
#define READSIZE 1024
|
||||
|
||||
static uint32_t total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
|
||||
static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
|
||||
static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool ok = true;
|
||||
OurEncoder encoder;
|
||||
FLAC__StreamEncoderInitStatus init_status;
|
||||
FLAC__StreamMetadata *metadata[2];
|
||||
FLAC__StreamMetadata_VorbisComment_Entry entry;
|
||||
FILE *fin;
|
||||
uint32_t sample_rate = 0;
|
||||
uint32_t channels = 0;
|
||||
uint32_t bps = 0;
|
||||
|
||||
if(argc != 3) {
|
||||
fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if((fin = fopen(argv[1], "rb")) == NULL) {
|
||||
fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* read wav header and validate it */
|
||||
if(
|
||||
fread(buffer, 1, 44, fin) != 44 ||
|
||||
memcmp(buffer, "RIFF", 4) ||
|
||||
memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
|
||||
memcmp(buffer+32, "\004\000\020\000data", 8)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
|
||||
fclose(fin);
|
||||
return 1;
|
||||
}
|
||||
sample_rate = ((((((uint32_t)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
|
||||
channels = 2;
|
||||
bps = 16;
|
||||
total_samples = (((((((uint32_t)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
|
||||
|
||||
/* check the encoder */
|
||||
if(!encoder) {
|
||||
fprintf(stderr, "ERROR: allocating encoder\n");
|
||||
fclose(fin);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ok &= encoder.set_verify(true);
|
||||
ok &= encoder.set_compression_level(5);
|
||||
ok &= encoder.set_channels(channels);
|
||||
ok &= encoder.set_bits_per_sample(bps);
|
||||
ok &= encoder.set_sample_rate(sample_rate);
|
||||
ok &= encoder.set_total_samples_estimate(total_samples);
|
||||
|
||||
/* now add some metadata; we'll add some tags and a padding block */
|
||||
if(ok) {
|
||||
if(
|
||||
(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
|
||||
(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
|
||||
/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
|
||||
!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
|
||||
!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
|
||||
) {
|
||||
fprintf(stderr, "ERROR: out of memory or tag error\n");
|
||||
ok = false;
|
||||
} else {
|
||||
|
||||
metadata[1]->length = 1234; /* set the padding length */
|
||||
|
||||
ok = encoder.set_metadata(metadata, 2);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* initialize encoder */
|
||||
if(ok) {
|
||||
init_status = encoder.init(argv[2]);
|
||||
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
|
||||
fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
||||
/* read blocks of samples from WAVE file and feed to encoder */
|
||||
if(ok) {
|
||||
size_t left = (size_t)total_samples;
|
||||
while(ok && left) {
|
||||
size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
|
||||
if(fread(buffer, channels*(bps/8), need, fin) != need) {
|
||||
fprintf(stderr, "ERROR: reading from WAVE file\n");
|
||||
ok = false;
|
||||
}
|
||||
else {
|
||||
/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
|
||||
size_t i;
|
||||
for(i = 0; i < need*channels; i++) {
|
||||
/* inefficient but simple and works on big- or little-endian machines */
|
||||
pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
|
||||
}
|
||||
/* feed samples to encoder */
|
||||
ok = encoder.process_interleaved(pcm, need);
|
||||
}
|
||||
left -= need;
|
||||
}
|
||||
}
|
||||
|
||||
ok &= encoder.finish();
|
||||
|
||||
fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
|
||||
fprintf(stderr, " state: %s\n", encoder.get_state().resolved_as_cstring(encoder));
|
||||
|
||||
/* now that encoding is finished, the metadata can be freed */
|
||||
FLAC__metadata_object_delete(metadata[0]);
|
||||
FLAC__metadata_object_delete(metadata[1]);
|
||||
|
||||
fclose(fin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void OurEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate)
|
||||
{
|
||||
fprintf(stderr, "wrote %" PRIu64 " bytes, %" PRIu64 "/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
|
||||
}
|
||||
Reference in New Issue
Block a user