summaryrefslogtreecommitdiff
path: root/examples/cpp/encode
diff options
context:
space:
mode:
authorJosh Coalson <jcoalson@users.sourceforce.net>2007-09-13 00:33:25 +0000
committerJosh Coalson <jcoalson@users.sourceforce.net>2007-09-13 00:33:25 +0000
commit044303c98dc22972891be239e7341dc271baf605 (patch)
tree0d904abb0c8cb8e4e67e11016b966e91c2bce60a /examples/cpp/encode
parentb7cb737d1563da66a101eb66738710821e5d746f (diff)
downloadflac-044303c98dc22972891be239e7341dc271baf605.tar.gz
*** empty log message ***
Diffstat (limited to 'examples/cpp/encode')
-rw-r--r--examples/cpp/encode/Makefile.am18
-rw-r--r--examples/cpp/encode/file/Makefile.am28
-rw-r--r--examples/cpp/encode/file/Makefile.lite41
-rw-r--r--examples/cpp/encode/file/example_cpp_encode_file.dsp100
-rw-r--r--examples/cpp/encode/file/example_cpp_encode_file.vcproj204
-rw-r--r--examples/cpp/encode/file/main.cpp175
6 files changed, 566 insertions, 0 deletions
diff --git a/examples/cpp/encode/Makefile.am b/examples/cpp/encode/Makefile.am
new file mode 100644
index 00000000..35bb987b
--- /dev/null
+++ b/examples/cpp/encode/Makefile.am
@@ -0,0 +1,18 @@
+# FLAC - Free Lossless Audio Codec
+# Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Josh Coalson
+#
+# This file is part the FLAC project. FLAC is comprised of several
+# components distributed under difference 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
diff --git a/examples/cpp/encode/file/Makefile.am b/examples/cpp/encode/file/Makefile.am
new file mode 100644
index 00000000..4539dd99
--- /dev/null
+++ b/examples/cpp/encode/file/Makefile.am
@@ -0,0 +1,28 @@
+# example_cpp_encode_file - Simple FLAC file encoder using libFLAC
+# Copyright (C) 2007 Josh Coalson
+#
+# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+EXTRA_DIST = \
+ Makefile.lite \
+ example_cpp_encode_file.dsp \
+ example_cpp_encode_file.vcproj \
+
+noinst_PROGRAMS = example_cpp_encode_file
+example_cpp_encode_file_LDADD = \
+ @OGG_LIBS@ \
+ @MINGW_WINSOCK_LIBS@ \
+ -lm
+example_cpp_encode_file_SOURCES = main.c
diff --git a/examples/cpp/encode/file/Makefile.lite b/examples/cpp/encode/file/Makefile.lite
new file mode 100644
index 00000000..9ae1b129
--- /dev/null
+++ b/examples/cpp/encode/file/Makefile.lite
@@ -0,0 +1,41 @@
+# example_cpp_encode_file - Simple FLAC file encoder using libFLAC
+# Copyright (C) 2007 Josh Coalson
+#
+# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+#
+# GNU makefile
+#
+
+topdir = ../../../..
+libdir = $(topdir)/obj/$(BUILD)/lib
+
+PROGRAM_NAME = example_cpp_encode_file
+
+INCLUDES = -I$(topdir)/include
+
+ifeq ($(DARWIN_BUILD),yes)
+EXPLICIT_LIBS = $(libdir)/libFLAC++.a $(libdir)/libFLAC.a $(OGG_LIB_DIR)/libogg.a -lm
+else
+LIBS = -lFLAC++ -lFLAC -L$(OGG_LIB_DIR) -logg -lm
+endif
+
+SRCS_CPP = main.cpp
+
+include $(topdir)/build/exe.mk
+
+LINK = $(CCC) $(LINKAGE)
+
+# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/examples/cpp/encode/file/example_cpp_encode_file.dsp b/examples/cpp/encode/file/example_cpp_encode_file.dsp
new file mode 100644
index 00000000..cf150305
--- /dev/null
+++ b/examples/cpp/encode/file/example_cpp_encode_file.dsp
@@ -0,0 +1,100 @@
+# Microsoft Developer Studio Project File - Name="example_cpp_encode_file" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=example_cpp_encode_file - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE
+!MESSAGE NMAKE /f "example_cpp_encode_file.mak".
+!MESSAGE
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE
+!MESSAGE NMAKE /f "example_cpp_encode_file.mak" CFG="example_cpp_encode_file - Win32 Debug"
+!MESSAGE
+!MESSAGE Possible choices for configuration are:
+!MESSAGE
+!MESSAGE "example_cpp_encode_file - Win32 Release" (based on "Win32 (x86) Console Application")
+!MESSAGE "example_cpp_encode_file - Win32 Debug" (based on "Win32 (x86) Console Application")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "example_cpp_encode_file - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "..\..\..\..\obj\release\bin"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\..\..\..\include" /D "NDEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /c
+# SUBTRACT CPP /YX /Yc /Yu
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 ..\..\..\..\obj\release\lib\libFLAC++_static.lib ..\..\..\..\obj\release\lib\libFLAC_static.lib ..\..\..\..\obj\release\lib\ogg_static.lib /nologo /subsystem:console /machine:I386
+
+!ELSEIF "$(CFG)" == "example_cpp_encode_file - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "..\..\..\..\obj\debug\bin"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c
+# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\..\..\..\include" /D "_DEBUG" /D "DEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c
+# SUBTRACT CPP /YX /Yc /Yu
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 ..\..\..\..\obj\debug\lib\libFLAC++_static.lib ..\..\..\..\obj\debug\lib\libFLAC_static.lib ..\..\..\..\obj\release\lib\ogg_static.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
+
+!ENDIF
+
+# Begin Target
+
+# Name "example_cpp_encode_file - Win32 Release"
+# Name "example_cpp_encode_file - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=.\main.cpp
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# End Group
+# End Target
+# End Project
diff --git a/examples/cpp/encode/file/example_cpp_encode_file.vcproj b/examples/cpp/encode/file/example_cpp_encode_file.vcproj
new file mode 100644
index 00000000..72117f15
--- /dev/null
+++ b/examples/cpp/encode/file/example_cpp_encode_file.vcproj
@@ -0,0 +1,204 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+ ProjectType="Visual C++"
+ Version="8.00"
+ Name="example_cpp_encode_file"
+ ProjectGUID="{4cefbe01-c215-11db-8314-0800200c9a66}"
+ RootNamespace="example_cpp_encode_file"
+ Keyword="Win32Proj"
+ >
+ <Platforms>
+ <Platform
+ Name="Win32"
+ />
+ </Platforms>
+ <ToolFiles>
+ </ToolFiles>
+ <Configurations>
+ <Configuration
+ Name="Debug|Win32"
+ OutputDirectory="..\..\..\..\obj\debug\bin"
+ IntermediateDirectory="Debug"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\..\..\..\include"
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FLAC__NO_DLL;DEBUG"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="4"
+ CompileAs="0"
+ DisableSpecificWarnings="4267;4996"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"
+ LinkIncremental="2"
+ IgnoreDefaultLibraryNames="uuid.lib"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Release|Win32"
+ OutputDirectory="..\..\..\..\obj\release\bin"
+ IntermediateDirectory="Release"
+ ConfigurationType="1"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ EnableIntrinsicFunctions="true"
+ FavorSizeOrSpeed="1"
+ OmitFramePointers="true"
+ WholeProgramOptimization="true"
+ AdditionalIncludeDirectories="..\..\..\..\include"
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;FLAC__NO_DLL"
+ RuntimeLibrary="0"
+ BufferSecurityCheck="false"
+ UsePrecompiledHeader="0"
+ WarningLevel="3"
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ CompileAs="0"
+ DisableSpecificWarnings="4267;4996"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"
+ LinkIncremental="1"
+ IgnoreDefaultLibraryNames="uuid.lib"
+ GenerateDebugInformation="true"
+ SubSystem="1"
+ OptimizeReferences="2"
+ EnableCOMDATFolding="2"
+ LinkTimeCodeGeneration="1"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ </Configurations>
+ <References>
+ </References>
+ <Files>
+ <Filter
+ Name="Header Files"
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"
+ UniqueIdentifier="{93292580-829B-b441-E8B8-65A95828BEB0}"
+ >
+ </Filter>
+ <Filter
+ Name="Source Files"
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+ UniqueIdentifier="{9C7247F1-CA27-1761-A016-0F27452AD2F0}"
+ >
+ <File
+ RelativePath=".\main.cpp"
+ >
+ </File>
+ </Filter>
+ </Files>
+ <Globals>
+ </Globals>
+</VisualStudioProject>
diff --git a/examples/cpp/encode/file/main.cpp b/examples/cpp/encode/file/main.cpp
new file mode 100644
index 00000000..8471bedd
--- /dev/null
+++ b/examples/cpp/encode/file/main.cpp
@@ -0,0 +1,175 @@
+/* example_cpp_encode_file - Simple FLAC file encoder using libFLAC
+ * Copyright (C) 2007 Josh Coalson
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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://flac.sourceforge.net/api/
+ */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "FLAC++/metadata.h"
+#include "FLAC++/encoder.h"
+
+class OurEncoder: public FLAC::Encoder::File {
+public:
+ OurEncoder(): FLAC::Encoder::File() { }
+protected:
+ virtual void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
+};
+
+#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*/];
+static FLAC__int32 *pcm_[2] = { pcm, pcm+READSIZE };
+
+int main(int argc, char *argv[])
+{
+ FLAC__bool ok = true;
+ OurEncoder encoder;
+ 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;
+
+ /* 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;
+ }
+
+ 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, unsigned frames_written, unsigned total_frames_estimate)
+{
+#ifdef _MSC_VER
+ fprintf(stderr, "wrote %I64u bytes, %I64u/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
+#else
+ fprintf(stderr, "wrote %llu bytes, %llu/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
+#endif
+}