Initial commit

added libraries:
opus
flac
libsndfile

updated:
libvorbis
libogg
openal

- Everything works as expected for now. Bare in mind libsndfile needed the check for whether or not it could find the xiph libraries removed in order for this to work.
This commit is contained in:
marauder2k7 2024-03-21 17:33:47 +00:00
parent 05a083ca6f
commit a745fc3757
1954 changed files with 431332 additions and 21037 deletions

View file

@ -0,0 +1,131 @@
/*
** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sndfile.h>
#define BUFFER_LEN 4096
static void encode_file (const char *infilename, const char *outfilename, int filetype) ;
int
main (int argc, char **argv)
{
if (argc != 2)
{ puts ("\nEncode a single input file into a number of different output ") ;
puts ("encodings. These output encodings can then be moved to another ") ;
puts ("OS for testing.\n") ;
puts (" Usage : generate <filename>\n") ;
exit (1) ;
} ;
/* A couple of standard WAV files. Make sure Win32 plays these. */
encode_file (argv [1], "pcmu8.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
encode_file (argv [1], "pcm16.wav" , SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
encode_file (argv [1], "gsm610.wav" , SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
/* Soundforge W64. */
encode_file (argv [1], "pcmu8.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
encode_file (argv [1], "pcm16.w64" , SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
encode_file (argv [1], "gsm610.w64" , SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
return 0 ;
} /* main */
/*============================================================================================
** Helper functions and macros.
*/
#define PUT_DOTS(k) \
{ while (k--) \
putchar ('.') ; \
putchar (' ') ; \
}
/*========================================================================================
*/
static void
encode_file (const char *infilename, const char *outfilename, int filetype)
{ static float buffer [BUFFER_LEN] ;
SNDFILE *infile, *outfile ;
SF_INFO sfinfo ;
int k, readcount ;
printf (" %s -> %s ", infilename, outfilename) ;
fflush (stdout) ;
k = 16 - strlen (outfilename) ;
PUT_DOTS (k) ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
{ printf ("Error : could not open file : %s\n", infilename) ;
puts (sf_strerror (NULL)) ;
exit (1) ;
}
sfinfo.format = filetype ;
if (! sf_format_check (&sfinfo))
{ sf_close (infile) ;
printf ("Invalid encoding\n") ;
return ;
} ;
if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
{ printf ("Error : could not open file : %s\n", outfilename) ;
puts (sf_strerror (NULL)) ;
exit (1) ;
} ;
while ((readcount = (int) sf_read_float (infile, buffer, BUFFER_LEN)) > 0)
sf_write_float (outfile, buffer, readcount) ;
sf_close (infile) ;
sf_close (outfile) ;
printf ("ok\n") ;
return ;
} /* encode_file */

View file

@ -0,0 +1,250 @@
/* (c) 2004 James Robson, http://www.arbingersys.com
**
** 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.
**
** ****************************
**
** How to use:
** - libsndfile.dll must have already been compiled and be in this
** application's search path
**
** - You must edit this file to point to the file you want to convert. Set
** the following line of code (found in the Main() function further below)
** to the name of a .WAV file that exists on your system.
** 186: string sfn = "input.wav";
**
** - From a command prompt type
** csc generate.cs
**
** - Run the resulting executable 'generate.exe'
**
**
** Note: You will obviously need the csc compiler and the .NET runtime. I think
** these are freely available for download from Microsoft's website
** (part of the .NET SDK?).
*/
using System;
using System.Runtime.InteropServices;
using sf_count_t = System.Int64; //alias; see SF_INFO struct
#if PLATFORM_64
using size_t = System.UInt64;
#else
using size_t = System.UInt32;
#endif
class lsndf_example {
//sound file formats
public enum lsndf_frmts {
SF_FORMAT_WAV = 0x010000, /* Microsoft WAV format (little endian). */
SF_FORMAT_AIFF = 0x020000, /* Apple/SGI AIFF format (big endian). */
SF_FORMAT_AU = 0x030000, /* Sun/NeXT AU format (big endian). */
SF_FORMAT_RAW = 0x040000, /* RAW PCM data. */
SF_FORMAT_PAF = 0x050000, /* Ensoniq PARIS file format. */
SF_FORMAT_SVX = 0x060000, /* Amiga IFF / SVX8 / SV16 format. */
SF_FORMAT_NIST = 0x070000, /* Sphere NIST format. */
SF_FORMAT_VOC = 0x080000, /* VOC files. */
SF_FORMAT_IRCAM = 0x0A0000, /* Berkeley/IRCAM/CARL */
SF_FORMAT_W64 = 0x0B0000, /* Sonic Foundry's 64 bit RIFF/WAV */
SF_FORMAT_MAT4 = 0x0C0000, /* Matlab (tm) V4.2 / GNU Octave 2.0 */
SF_FORMAT_MAT5 = 0x0D0000, /* Matlab (tm) V5.0 / GNU Octave 2.1 */
SF_FORMAT_PVF = 0x0E0000, /* Portable Voice Format */
SF_FORMAT_XI = 0x0F0000, /* Fasttracker 2 Extended Instrument */
SF_FORMAT_HTK = 0x100000, /* HMM Tool Kit format */
SF_FORMAT_SDS = 0x110000, /* Midi Sample Dump Standard */
/* Subtypes from here on. */
SF_FORMAT_PCM_S8 = 0x0001, /* Signed 8 bit data */
SF_FORMAT_PCM_16 = 0x0002, /* Signed 16 bit data */
SF_FORMAT_PCM_24 = 0x0003, /* Signed 24 bit data */
SF_FORMAT_PCM_32 = 0x0004, /* Signed 32 bit data */
SF_FORMAT_PCM_U8 = 0x0005, /* Unsigned 8 bit data (WAV and RAW only) */
SF_FORMAT_FLOAT = 0x0006, /* 32 bit float data */
SF_FORMAT_DOUBLE = 0x0007, /* 64 bit float data */
SF_FORMAT_ULAW = 0x0010, /* U-Law encoded. */
SF_FORMAT_ALAW = 0x0011, /* A-Law encoded. */
SF_FORMAT_IMA_ADPCM = 0x0012, /* IMA ADPCM. */
SF_FORMAT_MS_ADPCM = 0x0013, /* Microsoft ADPCM. */
SF_FORMAT_GSM610 = 0x0020, /* GSM 6.10 encoding. */
SF_FORMAT_VOX_ADPCM = 0x0021, /* OKI / Dialogix ADPCM */
SF_FORMAT_G721_32 = 0x0030, /* 32kbs G721 ADPCM encoding. */
SF_FORMAT_G723_24 = 0x0031, /* 24kbs G723 ADPCM encoding. */
SF_FORMAT_G723_40 = 0x0032, /* 40kbs G723 ADPCM encoding. */
SF_FORMAT_DWVW_12 = 0x0040, /* 12 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_16 = 0x0041, /* 16 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_24 = 0x0042, /* 24 bit Delta Width Variable Word encoding. */
SF_FORMAT_DWVW_N = 0x0043, /* N bit Delta Width Variable Word encoding. */
SF_FORMAT_DPCM_8 = 0x0050, /* 8 bit differential PCM (XI only) */
SF_FORMAT_DPCM_16 = 0x0051, /* 16 bit differential PCM (XI only) */
/* Endian-ness options. */
SF_ENDIAN_FILE = 0x00000000, /* Default file endian-ness. */
SF_ENDIAN_LITTLE = 0x10000000, /* Force little endian-ness. */
SF_ENDIAN_BIG = 0x20000000, /* Force big endian-ness. */
SF_ENDIAN_CPU = 0x30000000, /* Force CPU endian-ness. */
SF_FORMAT_SUBMASK = 0x0000FFFF,
SF_FORMAT_TYPEMASK = 0x0FFF0000,
SF_FORMAT_ENDMASK = 0x30000000
}
//modes and other
public enum lsndf_tf
{ /* True and false */
SF_FALSE = 0,
SF_TRUE = 1,
/* Modes for opening files. */
SFM_READ = 0x10,
SFM_WRITE = 0x20,
SFM_RDWR = 0x30
}
//important SF_INFO structure
[StructLayout(LayoutKind.Sequential)]
public struct SF_INFO
{
public sf_count_t frames ; // Used to be called samples. Changed to avoid confusion.
public int samplerate ;
public int channels ;
public int format ;
public int sections ;
public int seekable ;
};
//function declarations
//Note: Not all functions have been prototyped here. Only the ones necessary to
// make this application work. The below code should give some clues as to
// how to add the rest since they have a lot of parameter and return type
// similarities.
[DllImport("libsndfile.dll")]
public static extern IntPtr sf_open ([MarshalAs(UnmanagedType.LPStr)] string path, int mode, ref SF_INFO sfinfo);
[DllImport("libsndfile.dll")]
static extern int sf_error (IntPtr sndfile);
[DllImport("libsndfile.dll")]
static extern IntPtr sf_strerror (IntPtr sndfile);
[DllImport("libsndfile.dll")]
static extern int sf_format_check (ref SF_INFO info);
[DllImport("libsndfile.dll")]
static extern sf_count_t sf_read_float (IntPtr sndfile, float[] ptr, sf_count_t items);
[DllImport("libsndfile.dll")]
static extern sf_count_t sf_write_float (IntPtr sndfile, float[] ptr, sf_count_t items);
[DllImport("libsndfile.dll")]
static extern int sf_close (IntPtr sndfile);
public const sf_count_t BUFFER_LEN = 4096;
//program entry
static void Main( ) {
//declarations
SF_INFO sfinfo = new SF_INFO();
float[] buffer = new float[BUFFER_LEN];
sf_count_t rcnt;
//set the input file
string sfn = "input.wav"; //set to a file on YOUR system
//string sfn = "noexist.wav"; //test with non-existent file
//set the output file
string ofn = "output.wav";
//read in sound file to convert
IntPtr infile = sf_open (sfn, (int)lsndf_tf.SFM_READ, ref sfinfo);
//exit if error was thrown
if ( (int)infile == 0 ) {
Console.WriteLine("Error opening " + sfn);
Console.WriteLine("Error #" + sf_error(infile));
return;
}
//set the file type for the output file
//uncomment one and only one of the statements below to change the output
//file encoding.
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_U8);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_PCM_16);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_MS_ADPCM);
sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_IMA_ADPCM);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_WAV | lsndf_frmts.SF_FORMAT_GSM610);
/* Soundforge W64. */
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_U8);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_PCM_16);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_MS_ADPCM);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_IMA_ADPCM);
//sfinfo.format = (int)(lsndf_frmts.SF_FORMAT_W64 | lsndf_frmts.SF_FORMAT_GSM610);
//check that SF_INFO is valid
if ( sf_format_check(ref sfinfo) == 0 ) {
Console.WriteLine("sf_format_check failed. Invalid encoding");
return;
}
//open output file
IntPtr outfile = sf_open (ofn, (int)lsndf_tf.SFM_WRITE, ref sfinfo);
//exit if error was thrown
if ( (int)outfile == 0 ) {
Console.WriteLine("Error opening " + ofn);
Console.WriteLine("Error #" + sf_error(outfile));
return;
}
//infile -> outfile
Console.Write(sfn + " -> " + ofn);
while ( (rcnt = sf_read_float (infile, buffer, BUFFER_LEN)) > 0) {
Console.Write(".");
sf_write_float (outfile, buffer, BUFFER_LEN);
}
Console.WriteLine("done.");
//close up shop
sf_close(infile);
sf_close(outfile);
} //main()
} //class lsndf_example {}

View file

@ -0,0 +1,76 @@
/*
** Copyright (C) 2001-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sndfile.h>
int
main (void)
{ SF_FORMAT_INFO info ;
SF_INFO sfinfo ;
int format, major_count, subtype_count, m, s ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
printf ("Version : %s\n\n", sf_version_string ()) ;
sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &major_count, sizeof (int)) ;
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &subtype_count, sizeof (int)) ;
sfinfo.channels = 1 ;
for (m = 0 ; m < major_count ; m++)
{ info.format = m ;
sf_command (NULL, SFC_GET_FORMAT_MAJOR, &info, sizeof (info)) ;
printf ("%s (extension \"%s\")\n", info.name, info.extension) ;
format = info.format ;
for (s = 0 ; s < subtype_count ; s++)
{ info.format = s ;
sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &info, sizeof (info)) ;
format = (format & SF_FORMAT_TYPEMASK) | info.format ;
sfinfo.format = format ;
if (sf_format_check (&sfinfo))
printf (" %s\n", info.name) ;
} ;
puts ("") ;
} ;
puts ("") ;
return 0 ;
} /* main */

View file

@ -0,0 +1,100 @@
/*
** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sndfile.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338
#endif
#define SAMPLE_RATE 44100
#define SAMPLE_COUNT (SAMPLE_RATE * 4) /* 4 seconds */
#define AMPLITUDE (1.0 * 0x7F000000)
#define LEFT_FREQ (344.0 / SAMPLE_RATE)
#define RIGHT_FREQ (466.0 / SAMPLE_RATE)
int
main (void)
{ SNDFILE *file ;
SF_INFO sfinfo ;
int k ;
int *buffer ;
if (! (buffer = malloc (2 * SAMPLE_COUNT * sizeof (int))))
{ printf ("Error : Malloc failed.\n") ;
return 1 ;
} ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
sfinfo.samplerate = SAMPLE_RATE ;
sfinfo.frames = SAMPLE_COUNT ;
sfinfo.channels = 2 ;
sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ;
if (! (file = sf_open ("sine.wav", SFM_WRITE, &sfinfo)))
{ printf ("Error : Not able to open output file.\n") ;
free (buffer) ;
return 1 ;
} ;
if (sfinfo.channels == 1)
{ for (k = 0 ; k < SAMPLE_COUNT ; k++)
buffer [k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ;
}
else if (sfinfo.channels == 2)
{ for (k = 0 ; k < SAMPLE_COUNT ; k++)
{ buffer [2 * k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ;
buffer [2 * k + 1] = (int) (AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI)) ;
} ;
}
else
{ printf ("Error : make_sine can only generate mono or stereo files.\n") ;
sf_close (file) ;
free (buffer) ;
return 1 ;
} ;
if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) !=
sfinfo.channels * SAMPLE_COUNT)
puts (sf_strerror (file)) ;
sf_close (file) ;
free (buffer) ;
return 0 ;
} /* main */

View file

@ -0,0 +1,144 @@
/*
** Copyright (C) 2001-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
/* Include this header file to use functions from libsndfile. */
#include <sndfile.h>
/* This will be the length of the buffer used to hold.frames while
** we process them.
*/
#define BUFFER_LEN 1024
/* libsndfile can handle more than 6 channels but we'll restrict it to 6. */
#define MAX_CHANNELS 6
/* Function prototype. */
static void process_data (double *data, int count, int channels) ;
int
main (void)
{ /* This is a buffer of double precision floating point values
** which will hold our data while we process it.
*/
static double data [BUFFER_LEN] ;
/* A SNDFILE is very much like a FILE in the Standard C library. The
** sf_open function return an SNDFILE* pointer when they sucessfully
** open the specified file.
*/
SNDFILE *infile, *outfile ;
/* A pointer to an SF_INFO struct is passed to sf_open.
** On read, the library fills this struct with information about the file.
** On write, the struct must be filled in before calling sf_open.
*/
SF_INFO sfinfo ;
int readcount ;
const char *infilename = "input.wav" ;
const char *outfilename = "output.wav" ;
/* The SF_INFO struct must be initialized before using it.
*/
memset (&sfinfo, 0, sizeof (sfinfo)) ;
/* Here's where we open the input file. We pass sf_open the file name and
** a pointer to an SF_INFO struct.
** On successful open, sf_open returns a SNDFILE* pointer which is used
** for all subsequent operations on that file.
** If an error occurs during sf_open, the function returns a NULL pointer.
**
** If you are trying to open a raw headerless file you will need to set the
** format and channels fields of sfinfo before calling sf_open(). For
** instance to open a raw 16 bit stereo PCM file you would need the following
** two lines:
**
** sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
** sfinfo.channels = 2 ;
*/
if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
{ /* Open failed so print an error message. */
printf ("Not able to open input file %s.\n", infilename) ;
/* Print the error message from libsndfile. */
puts (sf_strerror (NULL)) ;
return 1 ;
} ;
if (sfinfo.channels > MAX_CHANNELS)
{ printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ;
sf_close (infile) ;
return 1 ;
} ;
/* Open the output file. */
if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
{ printf ("Not able to open output file %s.\n", outfilename) ;
puts (sf_strerror (NULL)) ;
sf_close (infile) ;
return 1 ;
} ;
/* While there are.frames in the input file, read them, process
** them and write them to the output file.
*/
while ((readcount = (int) sf_read_double (infile, data, BUFFER_LEN)))
{ process_data (data, readcount, sfinfo.channels) ;
sf_write_double (outfile, data, readcount) ;
} ;
/* Close input and output files. */
sf_close (infile) ;
sf_close (outfile) ;
return 0 ;
} /* main */
static void
process_data (double *data, int count, int channels)
{ double channel_gain [MAX_CHANNELS] = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 } ;
int k, chan ;
/* Process the data here.
** If the soundfile contains more then 1 channel you need to take care of
** the data interleaving youself.
** Current we just apply a channel dependant gain.
*/
for (chan = 0 ; chan < channels ; chan ++)
for (k = chan ; k < count ; k+= channels)
data [k] *= channel_gain [chan] ;
return ;
} /* process_data */

View file

@ -0,0 +1,175 @@
/*
** Copyright (C) 1999-2015 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
** A quick/rough hack to add SF_INSTRUMENT data to a file. It compiles, but
** no guarantees beyond that. Happy to receive patches to fix/improve it.
**
** Code for this was stolen from programs/sndfile-convert.c and related code.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sndfile.h>
#define BUFFER_LEN (1 << 14)
const char * program_name (const char * argv0) ;
static void sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) ;
static void add_instrument_data (SNDFILE *outfile, const SF_INFO * in_info) ;
static void
usage_exit (const char *progname)
{
printf ("\nUsage : %s <input file> <output file>\n", progname) ;
puts ("") ;
exit (1) ;
} /* usage_exit */
int
main (int argc, char * argv [])
{ const char *progname, *infilename, *outfilename ;
SNDFILE *infile = NULL, *outfile = NULL ;
SF_INFO in_sfinfo, out_sfinfo ;
progname = program_name (argv [0]) ;
if (argc < 3 || argc > 5)
usage_exit (progname) ;
infilename = argv [argc-2] ;
outfilename = argv [argc-1] ;
if (strcmp (infilename, outfilename) == 0)
{ printf ("Error : Input and output filenames are the same.\n\n") ;
usage_exit (progname) ;
} ;
if (strlen (infilename) > 1 && infilename [0] == '-')
{ printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
usage_exit (progname) ;
} ;
if (outfilename [0] == '-')
{ printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
usage_exit (progname) ;
} ;
memset (&in_sfinfo, 0, sizeof (in_sfinfo)) ;
if ((infile = sf_open (infilename, SFM_READ, &in_sfinfo)) == NULL)
{ printf ("Not able to open input file %s.\n", infilename) ;
puts (sf_strerror (NULL)) ;
return 1 ;
} ;
memcpy (&out_sfinfo, &in_sfinfo, sizeof (out_sfinfo)) ;
/* Open the output file. */
if ((outfile = sf_open (outfilename, SFM_WRITE, &out_sfinfo)) == NULL)
{ printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
sf_close (infile) ;
return 1 ;
} ;
/* Add the loop data */
add_instrument_data (outfile, &in_sfinfo) ;
/* Copy the audio data */
sfe_copy_data_int (outfile, infile, in_sfinfo.channels) ;
sf_close (infile) ;
sf_close (outfile) ;
return 0 ;
} /* main */
const char *
program_name (const char * argv0)
{ const char * tmp ;
tmp = strrchr (argv0, '/') ;
argv0 = tmp ? tmp + 1 : argv0 ;
/* Remove leading libtool name mangling. */
if (strstr (argv0, "lt-") == argv0)
return argv0 + 3 ;
return argv0 ;
} /* program_name */
static void
sfe_copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels)
{ static int data [BUFFER_LEN] ;
int frames, readcount ;
frames = BUFFER_LEN / channels ;
readcount = frames ;
while (readcount > 0)
{ readcount = (int) sf_readf_int (infile, data, frames) ;
sf_writef_int (outfile, data, readcount) ;
} ;
return ;
} /* sfe_copy_data_int */
static void
add_instrument_data (SNDFILE *file, const SF_INFO *info)
{ SF_INSTRUMENT instr ;
memset (&instr, 0, sizeof (instr)) ;
instr.gain = 1 ;
instr.basenote = 0 ;
instr.detune = 0 ;
instr.velocity_lo = 0 ;
instr.velocity_hi = 0 ;
instr.key_lo = 0 ;
instr.key_hi = 0 ;
instr.loop_count = 1 ;
instr.loops [0].mode = SF_LOOP_FORWARD ;
instr.loops [0].start = 0 ;
instr.loops [0].end = info->frames ;
instr.loops [0].count = 0 ;
if (sf_command (file, SFC_SET_INSTRUMENT, &instr, sizeof (instr)) == SF_FALSE)
{ printf ("\n\nLine %d : sf_command (SFC_SET_INSTRUMENT) failed.\n\n", __LINE__) ;
exit (1) ;
} ;
return ;
} /* add_instrument_data */

View file

@ -0,0 +1,168 @@
/*
** Copyright (C) 2008-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the author nor the names of any contributors may be used
** to endorse or promote products derived from this software without
** specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <sndfile.h>
#define BLOCK_SIZE 4096
#ifdef DBL_DECIMAL_DIG
#define OP_DBL_Digs (DBL_DECIMAL_DIG)
#else
#ifdef DECIMAL_DIG
#define OP_DBL_Digs (DECIMAL_DIG)
#else
#define OP_DBL_Digs (DBL_DIG + 3)
#endif
#endif
static void
print_usage (char *progname)
{ printf ("\nUsage : %s [--full-precision] <input file> <output file>\n", progname) ;
puts ("\n"
" Where the output file will contain a line for each frame\n"
" and a column for each channel.\n"
) ;
} /* print_usage */
static int
convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision)
{ float *buf ;
sf_count_t frames ;
int k, m, readcount ;
buf = malloc (BLOCK_SIZE * sizeof (float)) ;
if (buf == NULL)
{ printf ("Error : Out of memory.\n\n") ;
return 1 ;
} ;
frames = BLOCK_SIZE / channels ;
while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0)
{ for (k = 0 ; k < readcount ; k++)
{ for (m = 0 ; m < channels ; m++)
if (full_precision)
fprintf (outfile, " %.*e", OP_DBL_Digs - 1, buf [k * channels + m]) ;
else
fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
fprintf (outfile, "\n") ;
} ;
} ;
free (buf) ;
return 0 ;
} /* convert_to_text */
int
main (int argc, char * argv [])
{ char *progname, *infilename, *outfilename ;
SNDFILE *infile = NULL ;
FILE *outfile = NULL ;
SF_INFO sfinfo ;
int full_precision = 0 ;
int ret = 1 ;
progname = strrchr (argv [0], '/') ;
progname = progname ? progname + 1 : argv [0] ;
switch (argc)
{ case 4 :
if (!strcmp ("--full-precision", argv [3]))
{ print_usage (progname) ;
goto cleanup ;
} ;
full_precision = 1 ;
argv++ ;
case 3 :
break ;
default:
print_usage (progname) ;
goto cleanup ;
} ;
infilename = argv [1] ;
outfilename = argv [2] ;
if (strcmp (infilename, outfilename) == 0)
{ printf ("Error : Input and output filenames are the same.\n\n") ;
print_usage (progname) ;
goto cleanup ;
} ;
if (infilename [0] == '-')
{ printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
print_usage (progname) ;
goto cleanup ;
} ;
if (outfilename [0] == '-')
{ printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
print_usage (progname) ;
goto cleanup ;
} ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
{ printf ("Not able to open input file %s.\n", infilename) ;
puts (sf_strerror (NULL)) ;
goto cleanup ;
} ;
/* Open the output file. */
if ((outfile = fopen (outfilename, "w")) == NULL)
{ printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
goto cleanup ;
} ;
fprintf (outfile, "# Converted from file %s.\n", infilename) ;
fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
ret = convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
cleanup :
sf_close (infile) ;
if (outfile != NULL)
fclose (outfile) ;
return ret ;
} /* main */

View file

@ -0,0 +1,84 @@
/*
** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** 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.
*/
#include <cstdio>
#include <cstring>
#include <sndfile.hh>
#define BUFFER_LEN 1024
static void
create_file (const char * fname, int format)
{ static short buffer [BUFFER_LEN] ;
SndfileHandle file ;
int channels = 2 ;
int srate = 48000 ;
printf ("Creating file named '%s'\n", fname) ;
file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ;
memset (buffer, 0, sizeof (buffer)) ;
file.write (buffer, BUFFER_LEN) ;
puts ("") ;
/*
** The SndfileHandle object will automatically close the file and
** release all allocated memory when the object goes out of scope.
** This is the Resource Acquisition Is Initailization idom.
** See : http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
*/
} /* create_file */
static void
read_file (const char * fname)
{ static short buffer [BUFFER_LEN] ;
SndfileHandle file ;
file = SndfileHandle (fname) ;
printf ("Opened file '%s'\n", fname) ;
printf (" Sample rate : %d\n", file.samplerate ()) ;
printf (" Channels : %d\n", file.channels ()) ;
file.read (buffer, BUFFER_LEN) ;
puts ("") ;
/* RAII takes care of destroying SndfileHandle object. */
} /* read_file */
int
main (void)
{ const char * fname = "test.wav" ;
puts ("\nSimple example showing usage of the C++ SndfileHandle object.\n") ;
create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
read_file (fname) ;
puts ("Done.\n") ;
return 0 ;
} /* main */