Add test for sample format conversion.
This commit is contained in:
parent
c152ef3966
commit
0ebc551e6e
5 changed files with 275 additions and 31 deletions
|
|
@ -38,6 +38,7 @@
|
|||
|
||||
#include "paqa_tools.h"
|
||||
|
||||
|
||||
/*******************************************************************/
|
||||
void PaQa_ListAudioDevices(void)
|
||||
{
|
||||
|
|
@ -54,3 +55,95 @@ void PaQa_ListAudioDevices(void)
|
|||
printf( ", on %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name );
|
||||
}
|
||||
}
|
||||
|
||||
/*******************************************************************/
|
||||
void PaQa_ConvertToFloat( const void *input, int numSamples, PaSampleFormat inFormat, float *output )
|
||||
{
|
||||
int i;
|
||||
switch( inFormat )
|
||||
{
|
||||
case paUInt8:
|
||||
{
|
||||
unsigned char *data = (unsigned char *)input;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
int value = *data++;
|
||||
value -= 128;
|
||||
*output++ = value / 128.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case paInt16:
|
||||
{
|
||||
short *data = (short *)input;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
*output++ = *data++ / 32768.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case paInt32:
|
||||
{
|
||||
int *data = (int *)input;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
int value = *data++;
|
||||
value = value >> 8;
|
||||
float fval = (float) (value / ((double) 0x00800000));
|
||||
*output++ = fval;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*******************************************************************/
|
||||
void PaQa_ConvertFromFloat( const float *input, int numSamples, PaSampleFormat outFormat, void *output )
|
||||
{
|
||||
int i;
|
||||
switch( outFormat )
|
||||
{
|
||||
case paUInt8:
|
||||
{
|
||||
unsigned char *data = (unsigned char *)output;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
float value = *input++;
|
||||
int byte = ((int) (value * 127)) + 128;
|
||||
*data++ = (unsigned char) byte;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case paInt16:
|
||||
{
|
||||
short *data = (short *)output;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
float value = *input++;
|
||||
// Use assymmetric conversion to avoid clipping.
|
||||
short sval = value * 32767.0;
|
||||
*data++ = sval;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case paInt32:
|
||||
{
|
||||
int *data = (int *)output;
|
||||
for( i=0; i<numSamples; i++ )
|
||||
{
|
||||
float value = *input++;
|
||||
// Use assymmetric conversion to avoid clipping.
|
||||
int ival = value * ((double) 0x007FFFF0);
|
||||
ival = ival << 8;
|
||||
*data++ = ival;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue