--- /dev/null
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!-- Copyright (c) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson -->
+<!-- Permission is granted to copy, distribute and/or modify this document -->
+<!-- under the terms of the GNU Free Documentation License, Version 1.1 -->
+<!-- or any later version published by the Free Software Foundation; -->
+<!-- with no invariant sections. -->
+<!-- A copy of the license can be found at http://www.gnu.org/copyleft/fdl.html -->
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
+ <meta name="author" content="Josh Coalson" />
+ <meta name="description" content="A free, open source codec for lossless audio compression and decompression" />
+ <meta name="keywords" content="free,lossless,audio,codec,encoder,decoder,compression,compressor,archival,archive,archiving,backup,music" />
+ <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
+ <link rel="stylesheet" type="text/css" href="flac.css" />
+ <title>FLAC - developers</title>
+</head>
+
+<body>
+
+<div class="logo">
+ <a href="http://flac.sourceforge.net/"><img src="images/logo130.gif" alt="FLAC Logo" align="middle" border="0" hspace="0" /></a>
+</div>
+
+<div class="above_nav"></div>
+
+<div class="navbar">
+ <a href="index.html">home</a> |
+ <a href="faq.html">faq</a> |
+ <a href="news.html">news</a> |
+ <a href="download.html">download</a> |
+ <a href="documentation.html">documentation</a> |
+ <a href="comparison.html">comparison</a> |
+ <a href="changelog.html">changelog</a> |
+ <a href="links.html">links</a> |
+ <a href="developers.html">developers</a>
+</div>
+
+<div class="langbar">
+ english |
+ <a href="ru/documentation.html">russian</a>
+</div>
+
+<div class="below_nav"></div>
+
+<div class="box">
+ <div class="box_title">
+ example code
+ </div>
+ <div class="box_header"></div>
+ <div class="box_body">
+ The FLAC source code has several small example programs that demonstrate how to use the libraries. The source is available on the <a href="download.html">download page</a>, or can be <a href="http://sourceforge.net/cvs/?group_id=13478">checked out from CVS</a> or <a href="http://flac.cvs.sourceforge.net/flac/flac/examples/">browsed online</a>. The examples complement the <a href="api/index.html">API documentation</a>.<br />
+ <br />
+ Currently the examples show how to encode WAV files to FLAC and vice-versa using both libFLAC and libFLAC++. Over time we'll be adding more examples.
+ </div>
+ <div class="box_footer"></div>
+</div>
+
+
+<div class="copyright">
+ <!-- @@@ oh so hacky -->
+ <table>
+ <tr>
+ <td align="left">
+ Copyright (c) 2007 Josh Coalson
+ </td>
+ <td width="1%" align="right">
+ <a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=13478&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" /></a>
+ </td>
+ <td width="1%" align="right">
+ <a href="http://www.eff.org/cafe/"><img src="images/cafebug.gif" alt="CAFE Logo" border="0" /></a><br />
+ </td>
+ <td width="1%" align="right">
+ <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" border="0" hspace="0" /></a>
+ </td>
+ </tr>
+ </table>
+</div>
+
+</body>
+</html>
--- /dev/null
+# 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
--- /dev/null
+# example_c_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_c_encode_file.dsp \
+ example_c_encode_file.vcproj \
+
+noinst_PROGRAMS = example_c_encode_file
+example_c_encode_file_LDADD = \
+ @OGG_LIBS@ \
+ @MINGW_WINSOCK_LIBS@ \
+ -lm
+example_c_encode_file_SOURCES = main.c
--- /dev/null
+# example_c_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_c_encode_file
+
+INCLUDES = -I$(topdir)/include
+
+ifeq ($(DARWIN_BUILD),yes)
+EXPLICIT_LIBS = $(libdir)/libFLAC.a $(OGG_LIB_DIR)/libogg.a -lm
+else
+LIBS = -lFLAC -L$(OGG_LIB_DIR) -logg -lm
+endif
+
+SRCS_C = main.c
+
+include $(topdir)/build/exe.mk
+
+# DO NOT DELETE THIS LINE -- make depend depends on it.
--- /dev/null
+# Microsoft Developer Studio Project File - Name="example_c_encode_file" - Package Owner=<4>\r
+# Microsoft Developer Studio Generated Build File, Format Version 6.00\r
+# ** DO NOT EDIT **\r
+\r
+# TARGTYPE "Win32 (x86) Console Application" 0x0103\r
+\r
+CFG=example_c_encode_file - Win32 Debug\r
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r
+!MESSAGE use the Export Makefile command and run\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "example_c_encode_file.mak".\r
+!MESSAGE \r
+!MESSAGE You can specify a configuration when running NMAKE\r
+!MESSAGE by defining the macro CFG on the command line. For example:\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "example_c_encode_file.mak" CFG="example_c_encode_file - Win32 Debug"\r
+!MESSAGE \r
+!MESSAGE Possible choices for configuration are:\r
+!MESSAGE \r
+!MESSAGE "example_c_encode_file - Win32 Release" (based on "Win32 (x86) Console Application")\r
+!MESSAGE "example_c_encode_file - Win32 Debug" (based on "Win32 (x86) Console Application")\r
+!MESSAGE \r
+\r
+# Begin Project\r
+# PROP AllowPerConfigDependencies 0\r
+# PROP Scc_ProjName ""\r
+# PROP Scc_LocalPath ""\r
+CPP=cl.exe\r
+RSC=rc.exe\r
+\r
+!IF "$(CFG)" == "example_c_encode_file - Win32 Release"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 0\r
+# PROP BASE Output_Dir "Release"\r
+# PROP BASE Intermediate_Dir "Release"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 0\r
+# PROP Output_Dir "..\..\..\..\obj\release\bin"\r
+# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c\r
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\..\..\..\include" /D "NDEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /c\r
+# SUBTRACT CPP /YX /Yc /Yu\r
+# ADD BASE RSC /l 0x409 /d "NDEBUG"\r
+# ADD RSC /l 0x409 /d "NDEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# 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\r
+# ADD LINK32 ..\..\..\..\obj\release\lib\libFLAC_static.lib ..\..\..\..\obj\release\lib\ogg_static.lib /nologo /subsystem:console /machine:I386\r
+\r
+!ELSEIF "$(CFG)" == "example_c_encode_file - Win32 Debug"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 1\r
+# PROP BASE Output_Dir "Debug"\r
+# PROP BASE Intermediate_Dir "Debug"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 1\r
+# PROP Output_Dir "..\..\..\..\obj\debug\bin"\r
+# PROP Intermediate_Dir "Debug"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c\r
+# 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\r
+# SUBTRACT CPP /YX /Yc /Yu\r
+# ADD BASE RSC /l 0x409 /d "_DEBUG"\r
+# ADD RSC /l 0x409 /d "_DEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# 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\r
+# ADD LINK32 ..\..\..\..\obj\debug\lib\libFLAC_static.lib ..\..\..\..\obj\release\lib\ogg_static.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept\r
+\r
+!ENDIF \r
+\r
+# Begin Target\r
+\r
+# Name "example_c_encode_file - Win32 Release"\r
+# Name "example_c_encode_file - Win32 Debug"\r
+# Begin Group "Source Files"\r
+\r
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
+# Begin Source File\r
+\r
+SOURCE=.\main.c\r
+# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+# End Group\r
+# End Target\r
+# End Project\r
--- /dev/null
+<?xml version="1.0" encoding="Windows-1252"?>\r
+<VisualStudioProject\r
+ ProjectType="Visual C++"\r
+ Version="8.00"\r
+ Name="example_c_encode_file"\r
+ ProjectGUID="{4cefbd01-c215-11db-8314-0800200c9a66}"\r
+ RootNamespace="example_c_encode_file"\r
+ Keyword="Win32Proj"\r
+ >\r
+ <Platforms>\r
+ <Platform\r
+ Name="Win32"\r
+ />\r
+ </Platforms>\r
+ <ToolFiles>\r
+ </ToolFiles>\r
+ <Configurations>\r
+ <Configuration\r
+ Name="Debug|Win32"\r
+ OutputDirectory="..\..\..\..\obj\debug\bin"\r
+ IntermediateDirectory="Debug"\r
+ ConfigurationType="1"\r
+ >\r
+ <Tool\r
+ Name="VCPreBuildEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCCustomBuildTool"\r
+ />\r
+ <Tool\r
+ Name="VCXMLDataGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebServiceProxyGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCMIDLTool"\r
+ />\r
+ <Tool\r
+ Name="VCCLCompilerTool"\r
+ Optimization="0"\r
+ AdditionalIncludeDirectories="..\..\..\..\include"\r
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FLAC__NO_DLL;DEBUG"\r
+ MinimalRebuild="true"\r
+ BasicRuntimeChecks="3"\r
+ RuntimeLibrary="1"\r
+ UsePrecompiledHeader="0"\r
+ WarningLevel="3"\r
+ Detect64BitPortabilityProblems="true"\r
+ DebugInformationFormat="4"\r
+ CompileAs="0"\r
+ DisableSpecificWarnings="4267;4996"\r
+ />\r
+ <Tool\r
+ Name="VCManagedResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCPreLinkEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCLinkerTool"\r
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"\r
+ LinkIncremental="2"\r
+ IgnoreDefaultLibraryNames="uuid.lib"\r
+ GenerateDebugInformation="true"\r
+ SubSystem="1"\r
+ TargetMachine="1"\r
+ />\r
+ <Tool\r
+ Name="VCALinkTool"\r
+ />\r
+ <Tool\r
+ Name="VCManifestTool"\r
+ />\r
+ <Tool\r
+ Name="VCXDCMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCBscMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCFxCopTool"\r
+ />\r
+ <Tool\r
+ Name="VCAppVerifierTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebDeploymentTool"\r
+ />\r
+ <Tool\r
+ Name="VCPostBuildEventTool"\r
+ />\r
+ </Configuration>\r
+ <Configuration\r
+ Name="Release|Win32"\r
+ OutputDirectory="..\..\..\..\obj\release\bin"\r
+ IntermediateDirectory="Release"\r
+ ConfigurationType="1"\r
+ >\r
+ <Tool\r
+ Name="VCPreBuildEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCCustomBuildTool"\r
+ />\r
+ <Tool\r
+ Name="VCXMLDataGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebServiceProxyGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCMIDLTool"\r
+ />\r
+ <Tool\r
+ Name="VCCLCompilerTool"\r
+ EnableIntrinsicFunctions="true"\r
+ FavorSizeOrSpeed="1"\r
+ OmitFramePointers="true"\r
+ WholeProgramOptimization="true"\r
+ AdditionalIncludeDirectories="..\..\..\..\include"\r
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;FLAC__NO_DLL"\r
+ RuntimeLibrary="0"\r
+ BufferSecurityCheck="false"\r
+ UsePrecompiledHeader="0"\r
+ WarningLevel="3"\r
+ Detect64BitPortabilityProblems="true"\r
+ DebugInformationFormat="3"\r
+ CompileAs="0"\r
+ DisableSpecificWarnings="4267;4996"\r
+ />\r
+ <Tool\r
+ Name="VCManagedResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCPreLinkEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCLinkerTool"\r
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"\r
+ LinkIncremental="1"\r
+ IgnoreDefaultLibraryNames="uuid.lib"\r
+ GenerateDebugInformation="true"\r
+ SubSystem="1"\r
+ OptimizeReferences="2"\r
+ EnableCOMDATFolding="2"\r
+ LinkTimeCodeGeneration="1"\r
+ TargetMachine="1"\r
+ />\r
+ <Tool\r
+ Name="VCALinkTool"\r
+ />\r
+ <Tool\r
+ Name="VCManifestTool"\r
+ />\r
+ <Tool\r
+ Name="VCXDCMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCBscMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCFxCopTool"\r
+ />\r
+ <Tool\r
+ Name="VCAppVerifierTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebDeploymentTool"\r
+ />\r
+ <Tool\r
+ Name="VCPostBuildEventTool"\r
+ />\r
+ </Configuration>\r
+ </Configurations>\r
+ <References>\r
+ </References>\r
+ <Files>\r
+ <Filter\r
+ Name="Header Files"\r
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"\r
+ UniqueIdentifier="{39992580-89DB-4b41-8E8B-625F9E28BEBF}"\r
+ >\r
+ </Filter>\r
+ <Filter\r
+ Name="Source Files"\r
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"\r
+ UniqueIdentifier="{4FC727F1-C7A5-1376-A061-2AF2D742A2F0}"\r
+ >\r
+ <File\r
+ RelativePath=".\main.c"\r
+ >\r
+ </File>\r
+ </Filter>\r
+ </Files>\r
+ <Globals>\r
+ </Globals>\r
+</VisualStudioProject>\r
--- /dev/null
+/* example_c_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 <string.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;
+ }
+
+ 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;
+
+#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
+}
--- /dev/null
+# 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
--- /dev/null
+# 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
--- /dev/null
+# 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.
--- /dev/null
+# Microsoft Developer Studio Project File - Name="example_cpp_encode_file" - Package Owner=<4>\r
+# Microsoft Developer Studio Generated Build File, Format Version 6.00\r
+# ** DO NOT EDIT **\r
+\r
+# TARGTYPE "Win32 (x86) Console Application" 0x0103\r
+\r
+CFG=example_cpp_encode_file - Win32 Debug\r
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r
+!MESSAGE use the Export Makefile command and run\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "example_cpp_encode_file.mak".\r
+!MESSAGE \r
+!MESSAGE You can specify a configuration when running NMAKE\r
+!MESSAGE by defining the macro CFG on the command line. For example:\r
+!MESSAGE \r
+!MESSAGE NMAKE /f "example_cpp_encode_file.mak" CFG="example_cpp_encode_file - Win32 Debug"\r
+!MESSAGE \r
+!MESSAGE Possible choices for configuration are:\r
+!MESSAGE \r
+!MESSAGE "example_cpp_encode_file - Win32 Release" (based on "Win32 (x86) Console Application")\r
+!MESSAGE "example_cpp_encode_file - Win32 Debug" (based on "Win32 (x86) Console Application")\r
+!MESSAGE \r
+\r
+# Begin Project\r
+# PROP AllowPerConfigDependencies 0\r
+# PROP Scc_ProjName ""\r
+# PROP Scc_LocalPath ""\r
+CPP=cl.exe\r
+RSC=rc.exe\r
+\r
+!IF "$(CFG)" == "example_cpp_encode_file - Win32 Release"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 0\r
+# PROP BASE Output_Dir "Release"\r
+# PROP BASE Intermediate_Dir "Release"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 0\r
+# PROP Output_Dir "..\..\..\..\obj\release\bin"\r
+# PROP Intermediate_Dir "Release"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c\r
+# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\..\..\..\include" /D "NDEBUG" /D "FLAC__NO_DLL" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /c\r
+# SUBTRACT CPP /YX /Yc /Yu\r
+# ADD BASE RSC /l 0x409 /d "NDEBUG"\r
+# ADD RSC /l 0x409 /d "NDEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# 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\r
+# 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\r
+\r
+!ELSEIF "$(CFG)" == "example_cpp_encode_file - Win32 Debug"\r
+\r
+# PROP BASE Use_MFC 0\r
+# PROP BASE Use_Debug_Libraries 1\r
+# PROP BASE Output_Dir "Debug"\r
+# PROP BASE Intermediate_Dir "Debug"\r
+# PROP BASE Target_Dir ""\r
+# PROP Use_MFC 0\r
+# PROP Use_Debug_Libraries 1\r
+# PROP Output_Dir "..\..\..\..\obj\debug\bin"\r
+# PROP Intermediate_Dir "Debug"\r
+# PROP Ignore_Export_Lib 0\r
+# PROP Target_Dir ""\r
+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c\r
+# 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\r
+# SUBTRACT CPP /YX /Yc /Yu\r
+# ADD BASE RSC /l 0x409 /d "_DEBUG"\r
+# ADD RSC /l 0x409 /d "_DEBUG"\r
+BSC32=bscmake.exe\r
+# ADD BASE BSC32 /nologo\r
+# ADD BSC32 /nologo\r
+LINK32=link.exe\r
+# 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\r
+# 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\r
+\r
+!ENDIF \r
+\r
+# Begin Target\r
+\r
+# Name "example_cpp_encode_file - Win32 Release"\r
+# Name "example_cpp_encode_file - Win32 Debug"\r
+# Begin Group "Source Files"\r
+\r
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
+# Begin Source File\r
+\r
+SOURCE=.\main.cpp\r
+# End Source File\r
+# End Group\r
+# Begin Group "Header Files"\r
+\r
+# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
+# End Group\r
+# End Target\r
+# End Project\r
--- /dev/null
+<?xml version="1.0" encoding="Windows-1252"?>\r
+<VisualStudioProject\r
+ ProjectType="Visual C++"\r
+ Version="8.00"\r
+ Name="example_cpp_encode_file"\r
+ ProjectGUID="{4cefbe01-c215-11db-8314-0800200c9a66}"\r
+ RootNamespace="example_cpp_encode_file"\r
+ Keyword="Win32Proj"\r
+ >\r
+ <Platforms>\r
+ <Platform\r
+ Name="Win32"\r
+ />\r
+ </Platforms>\r
+ <ToolFiles>\r
+ </ToolFiles>\r
+ <Configurations>\r
+ <Configuration\r
+ Name="Debug|Win32"\r
+ OutputDirectory="..\..\..\..\obj\debug\bin"\r
+ IntermediateDirectory="Debug"\r
+ ConfigurationType="1"\r
+ >\r
+ <Tool\r
+ Name="VCPreBuildEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCCustomBuildTool"\r
+ />\r
+ <Tool\r
+ Name="VCXMLDataGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebServiceProxyGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCMIDLTool"\r
+ />\r
+ <Tool\r
+ Name="VCCLCompilerTool"\r
+ Optimization="0"\r
+ AdditionalIncludeDirectories="..\..\..\..\include"\r
+ PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;FLAC__NO_DLL;DEBUG"\r
+ MinimalRebuild="true"\r
+ BasicRuntimeChecks="3"\r
+ RuntimeLibrary="1"\r
+ UsePrecompiledHeader="0"\r
+ WarningLevel="3"\r
+ Detect64BitPortabilityProblems="true"\r
+ DebugInformationFormat="4"\r
+ CompileAs="0"\r
+ DisableSpecificWarnings="4267;4996"\r
+ />\r
+ <Tool\r
+ Name="VCManagedResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCPreLinkEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCLinkerTool"\r
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"\r
+ LinkIncremental="2"\r
+ IgnoreDefaultLibraryNames="uuid.lib"\r
+ GenerateDebugInformation="true"\r
+ SubSystem="1"\r
+ TargetMachine="1"\r
+ />\r
+ <Tool\r
+ Name="VCALinkTool"\r
+ />\r
+ <Tool\r
+ Name="VCManifestTool"\r
+ />\r
+ <Tool\r
+ Name="VCXDCMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCBscMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCFxCopTool"\r
+ />\r
+ <Tool\r
+ Name="VCAppVerifierTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebDeploymentTool"\r
+ />\r
+ <Tool\r
+ Name="VCPostBuildEventTool"\r
+ />\r
+ </Configuration>\r
+ <Configuration\r
+ Name="Release|Win32"\r
+ OutputDirectory="..\..\..\..\obj\release\bin"\r
+ IntermediateDirectory="Release"\r
+ ConfigurationType="1"\r
+ >\r
+ <Tool\r
+ Name="VCPreBuildEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCCustomBuildTool"\r
+ />\r
+ <Tool\r
+ Name="VCXMLDataGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebServiceProxyGeneratorTool"\r
+ />\r
+ <Tool\r
+ Name="VCMIDLTool"\r
+ />\r
+ <Tool\r
+ Name="VCCLCompilerTool"\r
+ EnableIntrinsicFunctions="true"\r
+ FavorSizeOrSpeed="1"\r
+ OmitFramePointers="true"\r
+ WholeProgramOptimization="true"\r
+ AdditionalIncludeDirectories="..\..\..\..\include"\r
+ PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;FLAC__NO_DLL"\r
+ RuntimeLibrary="0"\r
+ BufferSecurityCheck="false"\r
+ UsePrecompiledHeader="0"\r
+ WarningLevel="3"\r
+ Detect64BitPortabilityProblems="true"\r
+ DebugInformationFormat="3"\r
+ CompileAs="0"\r
+ DisableSpecificWarnings="4267;4996"\r
+ />\r
+ <Tool\r
+ Name="VCManagedResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCResourceCompilerTool"\r
+ />\r
+ <Tool\r
+ Name="VCPreLinkEventTool"\r
+ />\r
+ <Tool\r
+ Name="VCLinkerTool"\r
+ AdditionalDependencies="..\..\..\..\obj\release\lib\ogg_static.lib"\r
+ LinkIncremental="1"\r
+ IgnoreDefaultLibraryNames="uuid.lib"\r
+ GenerateDebugInformation="true"\r
+ SubSystem="1"\r
+ OptimizeReferences="2"\r
+ EnableCOMDATFolding="2"\r
+ LinkTimeCodeGeneration="1"\r
+ TargetMachine="1"\r
+ />\r
+ <Tool\r
+ Name="VCALinkTool"\r
+ />\r
+ <Tool\r
+ Name="VCManifestTool"\r
+ />\r
+ <Tool\r
+ Name="VCXDCMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCBscMakeTool"\r
+ />\r
+ <Tool\r
+ Name="VCFxCopTool"\r
+ />\r
+ <Tool\r
+ Name="VCAppVerifierTool"\r
+ />\r
+ <Tool\r
+ Name="VCWebDeploymentTool"\r
+ />\r
+ <Tool\r
+ Name="VCPostBuildEventTool"\r
+ />\r
+ </Configuration>\r
+ </Configurations>\r
+ <References>\r
+ </References>\r
+ <Files>\r
+ <Filter\r
+ Name="Header Files"\r
+ Filter="h;hpp;hxx;hm;inl;inc;xsd"\r
+ UniqueIdentifier="{93292580-829B-b441-E8B8-65A95828BEB0}"\r
+ >\r
+ </Filter>\r
+ <Filter\r
+ Name="Source Files"\r
+ Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"\r
+ UniqueIdentifier="{9C7247F1-CA27-1761-A016-0F27452AD2F0}"\r
+ >\r
+ <File\r
+ RelativePath=".\main.cpp"\r
+ >\r
+ </File>\r
+ </Filter>\r
+ </Files>\r
+ <Globals>\r
+ </Globals>\r
+</VisualStudioProject>\r
--- /dev/null
+/* 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
+}