upgrade to libresample 0.1.3

This commit is contained in:
Glenn Maynard
2004-01-08 03:12:11 +00:00
parent 7bf6149cb8
commit 5e05b951d5
17 changed files with 304 additions and 91 deletions
Binary file not shown.
@@ -3,7 +3,7 @@
#include "RageUtil.h"
#include "RageSoundReader_Resample_Good.h"
#include "libresample/include/resample.h"
#include "libresample/include/libresample.h"
#ifdef _WINDOWS
#pragma comment(lib, "libresample/resample.lib")
#endif
+13 -8
View File
@@ -10,6 +10,7 @@ LIBS = @LIBS@ -lm
AR = @AR@
RANLIB = @RANLIB@
srcdir=@srcdir@
OBJS = \
src/resample.c.o \
@@ -17,6 +18,7 @@ OBJS = \
src/filterkit.c.o
TARGETS = @TARGETS@
DIRS=tests
all: $(TARGETS)
@@ -24,21 +26,24 @@ libresample.a: $(OBJS) Makefile
$(AR) ruv libresample.a $(OBJS)
ranlib libresample.a
tests/testresample: libresample.a tests/testresample.c
tests/testresample: libresample.a $(srcdir)/tests/testresample.c $(DIRS)
$(CC) -o tests/testresample \
$(CFLAGS) tests/testresample.c \
$(CFLAGS) $(srcdir)/tests/testresample.c \
libresample.a $(LIBS)
tests/compareresample: libresample.a tests/compareresample.c
tests/compareresample: libresample.a $(srcdir)/tests/compareresample.c $(DIRS)
$(CC) -o tests/compareresample \
$(CFLAGS) tests/compareresample.c \
$(CFLAGS) $(srcdir)/tests/compareresample.c \
libresample.a -lsamplerate $(LIBS)
tests/resample-sndfile: libresample.a tests/resample-sndfile.c
tests/resample-sndfile: libresample.a $(srcdir)/tests/resample-sndfile.c $(DIRS)
$(CC) -o tests/resample-sndfile \
$(CFLAGS) tests/resample-sndfile.c \
$(CFLAGS) $(srcdir)/tests/resample-sndfile.c \
libresample.a -lsndfile $(LIBS)
$(DIRS):
mkdir $(DIRS)
clean:
rm -f $(TARGETS) $(OBJS)
@@ -47,6 +52,6 @@ dist: clean
rm -f config.status config.cache config.log src/config.h
rm -f *~ src/*~ tests/*~ include/*~
%.c.o: %.c Makefile include/resample.h \
src/resample_defs.h src/filterkit.h src/config.h
$(OBJS): %.c.o: $(srcdir)/%.c Makefile $(srcdir)/include/libresample.h \
$(srcdir)/src/resample_defs.h $(srcdir)/src/filterkit.h $(srcdir)/src/config.h
$(CC) -c $(CFLAGS) $< -o $@
+16
View File
@@ -31,6 +31,22 @@ I removed all of that code and replaced it with an API that
lets you pass samples in small chunks. It should be easy
to link to resample-1.7 as a library.
Changes in version 0.1.3:
* Fixed two bugs that were causing subtle problems
on Intel x86 processors due to differences in roundoff errors.
* Prefixed most function names with lrs and changed header file
from resample.h to libresample.h, to avoid namespace
collisions with existing programs and libraries.
* Added resample_dup (thanks to Glenn Maynard)
* Argument to resample_get_filter_width takes a const void *
(thanks to Glenn Maynard)
* resample-sndfile clips output to -1...1 (thanks to Glenn Maynard)
Usage notes:
- If the output buffer you pass is too small, resample_process
+6
View File
@@ -0,0 +1,6 @@
EXPORTS
resample_open
resample_dup
resample_get_filter_width
resample_process
resample_close
Binary file not shown.
+23 -18
View File
@@ -81,9 +81,9 @@ static double Izero(double x)
return(sum);
}
void LpFilter(double c[], int N, double frq, double Beta, int Num)
void lrsLpFilter(double c[], int N, double frq, double Beta, int Num)
{
double IBeta, temp, inm1;
double IBeta, temp, temp1, inm1;
int i;
/* Calculate ideal lowpass filter impulse response coefficients: */
@@ -103,17 +103,22 @@ void LpFilter(double c[], int N, double frq, double Beta, int Num)
inm1 = 1.0/((double)(N-1));
for (i=1; i<N; i++) {
temp = (double)i * inm1;
c[i] *= Izero(Beta*sqrt(1.0-temp*temp)) * IBeta;
temp1 = 1.0 - temp*temp;
temp1 = (temp1<0? 0: temp1); /* make sure it's not negative since
we're taking the square root - this
happens on Pentium 4's due to tiny
roundoff errors */
c[i] *= Izero(Beta*sqrt(temp1)) * IBeta;
}
}
float FilterUp(float Imp[], /* impulse response */
float ImpD[], /* impulse response deltas */
UWORD Nwing, /* len of one wing of filter */
BOOL Interp, /* Interpolate coefs using deltas? */
float *Xp, /* Current sample */
double Ph, /* Phase */
int Inc) /* increment (1 for right wing or -1 for left) */
float lrsFilterUp(float Imp[], /* impulse response */
float ImpD[], /* impulse response deltas */
UWORD Nwing, /* len of one wing of filter */
BOOL Interp, /* Interpolate coefs using deltas? */
float *Xp, /* Current sample */
double Ph, /* Phase */
int Inc) /* increment (1 for right wing or -1 for left) */
{
float *Hp, *Hdp = NULL, *End;
double a = 0;
@@ -161,14 +166,14 @@ float FilterUp(float Imp[], /* impulse response */
return v;
}
float FilterUD(float Imp[], /* impulse response */
float ImpD[], /* impulse response deltas */
UWORD Nwing, /* len of one wing of filter */
BOOL Interp, /* Interpolate coefs using deltas? */
float *Xp, /* Current sample */
double Ph, /* Phase */
int Inc, /* increment (1 for right wing or -1 for left) */
double dhb) /* filter sampling period */
float lrsFilterUD(float Imp[], /* impulse response */
float ImpD[], /* impulse response deltas */
UWORD Nwing, /* len of one wing of filter */
BOOL Interp, /* Interpolate coefs using deltas? */
float *Xp, /* Current sample */
double Ph, /* Phase */
int Inc, /* increment (1 for right wing or -1 for left) */
double dhb) /* filter sampling period */
{
float a;
float *Hp, *Hdp, *End;
+5 -5
View File
@@ -19,10 +19,10 @@
* FilterUD() - Applies a filter to a given sample when up- or down-
*/
float FilterUp(float Imp[], float ImpD[], UWORD Nwing, BOOL Interp,
float *Xp, double Ph, int Inc);
float lrsFilterUp(float Imp[], float ImpD[], UWORD Nwing, BOOL Interp,
float *Xp, double Ph, int Inc);
float FilterUD(float Imp[], float ImpD[], UWORD Nwing, BOOL Interp,
float *Xp, double Ph, int Inc, double dhb);
float lrsFilterUD(float Imp[], float ImpD[], UWORD Nwing, BOOL Interp,
float *Xp, double Ph, int Inc, double dhb);
void LpFilter(double c[], int N, double frq, double Beta, int Num);
void lrsLpFilter(double c[], int N, double frq, double Beta, int Num);
+6 -6
View File
@@ -15,7 +15,7 @@
**********************************************************************/
/* External interface */
#include "../include/resample.h"
#include "../include/libresample.h"
/* Definitions */
#include "resample_defs.h"
@@ -114,7 +114,7 @@ void *resample_open(int highQuality, double minFactor, double maxFactor)
Imp64 = (double *)malloc(hp->Nwing * sizeof(double));
LpFilter(Imp64, hp->Nwing, 0.5*Rolloff, Beta, Npc);
lrsLpFilter(Imp64, hp->Nwing, 0.5*Rolloff, Beta, Npc);
hp->Imp = (float *)malloc(hp->Nwing * sizeof(float));
hp->ImpD = (float *)malloc(hp->Nwing * sizeof(float));
@@ -269,12 +269,12 @@ int resample_process(void *handle,
/* Resample stuff in input buffer */
if (factor >= 1) { /* SrcUp() is faster if we can use it */
Nout = SrcUp(hp->X, hp->Y, factor, &hp->Time, Nx,
Nwing, LpScl, Imp, ImpD, interpFilt);
Nout = lrsSrcUp(hp->X, hp->Y, factor, &hp->Time, Nx,
Nwing, LpScl, Imp, ImpD, interpFilt);
}
else {
Nout = SrcUD(hp->X, hp->Y, factor, &hp->Time, Nx,
Nwing, LpScl, Imp, ImpD, interpFilt);
Nout = lrsSrcUD(hp->X, hp->Y, factor, &hp->Time, Nx,
Nwing, LpScl, Imp, ImpD, interpFilt);
}
#ifdef DEBUG
+8 -10
View File
@@ -14,11 +14,9 @@
#ifndef __RESAMPLE_DEFS__
#define __RESAMPLE_DEFS__
//#ifdef WIN32
//#include "configwin.h"
//#else
#if !defined(WIN32) && !defined(__CYGWIN__)
#include "config.h"
//#endif
#endif
#ifndef TRUE
#define TRUE 1
@@ -77,12 +75,12 @@
/* Function prototypes */
int SrcUp(float X[], float Y[], double factor, double *Time,
UWORD Nx, UWORD Nwing, float LpScl,
float Imp[], float ImpD[], BOOL Interp);
int lrsSrcUp(float X[], float Y[], double factor, double *Time,
UWORD Nx, UWORD Nwing, float LpScl,
float Imp[], float ImpD[], BOOL Interp);
int SrcUD(float X[], float Y[], double factor, double *Time,
UWORD Nx, UWORD Nwing, float LpScl,
float Imp[], float ImpD[], BOOL Interp);
int lrsSrcUD(float X[], float Y[], double factor, double *Time,
UWORD Nx, UWORD Nwing, float LpScl,
float Imp[], float ImpD[], BOOL Interp);
#endif
+51 -38
View File
@@ -27,61 +27,68 @@
/* Sampling rate up-conversion only subroutine;
* Slightly faster than down-conversion;
*/
int SrcUp(float X[],
float Y[],
double factor,
double *Time,
UWORD Nx,
UWORD Nwing,
float LpScl,
float Imp[],
float ImpD[],
BOOL Interp)
int lrsSrcUp(float X[],
float Y[],
double factor,
double *TimePtr,
UWORD Nx,
UWORD Nwing,
float LpScl,
float Imp[],
float ImpD[],
BOOL Interp)
{
float *Xp, *Ystart;
float v;
double CurrentTime = *TimePtr;
double dt; /* Step through input signal */
double endTime; /* When Time reaches EndTime, return to user */
dt = 1.0/factor; /* Output sampling period */
Ystart = Y;
endTime = *Time + Nx;
while (*Time < endTime)
endTime = CurrentTime + Nx;
while (CurrentTime < endTime)
{
Xp = &X[(int)(*Time)]; /* Ptr to current input sample */
double LeftPhase = CurrentTime-floor(CurrentTime);
double RightPhase = 1.0 - LeftPhase;
Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */
/* Perform left-wing inner product */
v = FilterUp(Imp, ImpD, Nwing, Interp, Xp,
(*Time)-floor(*Time), -1);
v = lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp,
LeftPhase, -1);
/* Perform right-wing inner product */
v += FilterUp(Imp, ImpD, Nwing, Interp, Xp+1,
((-(*Time))-floor(-(*Time))), 1);
v += lrsFilterUp(Imp, ImpD, Nwing, Interp, Xp+1,
RightPhase, 1);
v *= LpScl; /* Normalize for unity filter gain */
*Y++ = v; /* Deposit output */
*Time += dt; /* Move to next sample by time increment */
CurrentTime += dt; /* Move to next sample by time increment */
}
*TimePtr = CurrentTime;
return (Y - Ystart); /* Return the number of output samples */
}
/* Sampling rate conversion subroutine */
int SrcUD(float X[],
float Y[],
double factor,
double *Time,
UWORD Nx,
UWORD Nwing,
float LpScl,
float Imp[],
float ImpD[],
BOOL Interp)
int lrsSrcUD(float X[],
float Y[],
double factor,
double *TimePtr,
UWORD Nx,
UWORD Nwing,
float LpScl,
float Imp[],
float ImpD[],
BOOL Interp)
{
float *Xp, *Ystart;
float v;
double CurrentTime = (*TimePtr);
double dh; /* Step through filter impulse response */
double dt; /* Step through input signal */
double endTime; /* When Time reaches EndTime, return to user */
@@ -91,20 +98,26 @@ int SrcUD(float X[],
dh = MIN(Npc, factor*Npc); /* Filter sampling period */
Ystart = Y;
endTime = *Time + Nx;
while (*Time < endTime)
endTime = CurrentTime + Nx;
while (CurrentTime < endTime)
{
Xp = &X[(int)(*Time)]; /* Ptr to current input sample */
v = FilterUD(Imp, ImpD, Nwing, Interp, Xp,
(*Time)-floor(*Time), -1, dh);
/* Perform left-wing inner product */
v += FilterUD(Imp, ImpD, Nwing, Interp, Xp+1,
((-(*Time))-floor(-(*Time))), 1, dh);
double LeftPhase = CurrentTime-floor(CurrentTime);
double RightPhase = 1.0 - LeftPhase;
Xp = &X[(int)CurrentTime]; /* Ptr to current input sample */
/* Perform left-wing inner product */
v = lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp,
LeftPhase, -1, dh);
/* Perform right-wing inner product */
v += lrsFilterUD(Imp, ImpD, Nwing, Interp, Xp+1,
RightPhase, 1, dh);
v *= LpScl; /* Normalize for unity filter gain */
*Y++ = v; /* Deposit output */
*Time += dt; /* Move to next sample by time increment */
CurrentTime += dt; /* Move to next sample by time increment */
}
*TimePtr = CurrentTime;
return (Y - Ystart); /* Return the number of output samples */
}
@@ -11,7 +11,7 @@
**********************************************************************/
#include "../include/resample.h"
#include "../include/libresample.h"
#include <samplerate.h>
@@ -11,7 +11,7 @@
**********************************************************************/
#include "../include/resample.h"
#include "../include/libresample.h"
#include <sndfile.h>
@@ -174,7 +174,14 @@ int main(int argc, char **argv)
}
for(i=0; i<out; i++)
dsti[i*channels+c] = dst[i];
{
if(dst[i] <= -1)
dsti[i*channels+c] = -1;
else if(dst[i] >= 1)
dsti[i*channels+c] = 1;
else
dsti[i*channels+c] = dst[i];
}
}
sf_writef_float(dstfile, dsti, out);
@@ -11,7 +11,7 @@
**********************************************************************/
#include "../include/resample.h"
#include "../include/libresample.h"
#include <stdio.h>
#include <stdlib.h>
@@ -91,7 +91,7 @@ void runtest(int srclen, double freq, double factor,
for(i=0; i<statlen; i++) {
double diff = sin((i/freq)/factor) - dst[i];
if (diff > 0.05) {
if (fabs(diff) > 0.05) {
if (errcount == 0)
printf(" First error at i=%d: expected %.3f, got %.3f\n",
i, sin((i/freq)/factor), dst[i]);
@@ -0,0 +1,134 @@
# Microsoft Developer Studio Project File - Name="libresample" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=libresample - 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 "libresample.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 "libresample.mak" CFG="libresample - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "libresample - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "libresample - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "libresample - 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 "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBRESAMPLE_EXPORTS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBRESAMPLE_EXPORTS" /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# 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 /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib comdlg32.lib advapi32.lib shell32.lib /nologo /dll /pdb:none /machine:I386 /out:"../resample.dll" /implib:"../resample.lib"
!ELSEIF "$(CFG)" == "libresample - 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 "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBRESAMPLE_EXPORTS" /YX /FD /GZ /c
# ADD CPP /nologo /MD /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "LIBRESAMPLE_EXPORTS" /YX /FD /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# 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 /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD 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 /nologo /dll /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "libresample - Win32 Release"
# Name "libresample - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\src\filterkit.c
# End Source File
# Begin Source File
SOURCE=..\src\resample.c
# End Source File
# Begin Source File
SOURCE=..\src\resamplesubs.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\src\config.h
# End Source File
# Begin Source File
SOURCE=..\src\filterkit.h
# End Source File
# Begin Source File
SOURCE=..\include\libresample.h
# End Source File
# Begin Source File
SOURCE=..\src\resample_defs.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# Begin Source File
SOURCE=..\resample.def
# End Source File
# End Target
# End Project
@@ -0,0 +1,29 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "libresample"=.\libresample.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################