ITASampleTypeConversion.cpp 2.92 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1 2 3
#include <ITASampleTypeConversion.h>
#include <ITAEndianness.h>

js908001's avatar
js908001 committed
4 5
#include <stddef.h>

Jonas Stienen's avatar
Jonas Stienen committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
void stc_sint16_to_float(float* dest, const short* src, size_t count, int input_stride, int output_stride)
{
	for (size_t i=0; i<count; i++)
		dest[i*output_stride] = (float) src[i*input_stride] / 32767.0F;
}

void stc_sint24_to_float(float* dest, const void* src, size_t count, int input_stride, int output_stride)
{	
	const unsigned char* p = (const unsigned char*) src;

	union
	{
		int ivalue;
		unsigned char byte[4];
	};

	// TODO: Hier muss noch die Endianness geprft werden.

	if (GetEndianness() == ITA_BIG_ENDIAN)
	{
		for (size_t i=0; i<count; i++)
		{
			
			// Big endian
			// TODO: Testen!
			byte[1] = p[0];
			byte[2] = p[1];
			byte[3] = p[2];

			// Eingangswert negativ (hchstes Bit = 1)?
			if (p[1] & 0x80)
				byte[0] = 0xFF;
			else
				byte[0] = 0x00;

			dest[i*output_stride] = (float) ivalue / 8388607.0F;

			p = p + input_stride*3;
		}
	} 
	else
	{
		for (size_t i=0; i<count; i++) 
		{
			
			// Little endian
			byte[0] = p[0];
			byte[1] = p[1];
			byte[2] = p[2];

			// Input value negative (higher Bit = 1)?
			if (p[2] & 0x80)
				byte[3] = 0xFF;
			else
				byte[3] = 0x00;

			dest[i*output_stride] = (float) ivalue / 8388607.0F;

			p = p + input_stride*3;
		}
	}
}

void stc_sint32_to_float(float* dest, const int* src, size_t count, int input_stride, int output_stride)
{
	for (size_t i=0; i<count; i++)
		dest[i*output_stride] = (float) src[i*input_stride] / 2147483647.0F;
}

void stc_double_to_float(float* dest, const double* src, size_t count, int input_stride, int output_stride)
{
	for (size_t i=0; i<count; i++)
		dest[i*output_stride] = (float) src[i*input_stride];
}

void stc_float_to_sint16(short* dest, const float* src, size_t count, int input_stride, int output_stride)
{
	/* berlauf-Schutz: bersteuerte Werte grer +/-1.0 werden auf +/-32767 abgebildet.
	                    Dies mag zwar langsam sein, ist aber sicherer! */

	for (size_t i=0; i<count; i++) {
		if (src[i*input_stride] >= +1.0) 
			dest[i*output_stride] = 32767;
		else 
			if (src[i*input_stride] <= -1.0)
				dest[i*output_stride] = -32767;
			else
				dest[i*output_stride] = (short) (src[i*input_stride] * 32767.0F);
	}
}

void stc_float_to_sint32(int* dest, const float* src, size_t count, int input_stride, int output_stride)
{
	/* berlauf-Schutz: bersteuerte Werte grer +/-1.0 werden auf +/-2147483647 abgebildet.
	                    Dies mag zwar langsam sein, ist aber sicherer! */

	for (size_t i=0; i<count; i++) {
		if (src[i*input_stride] >= +1.0) 
			dest[i*output_stride] = 2147483647;
		else 
			if (src[i*input_stride] <= -1.0)
				dest[i*output_stride] = -2147483647;
			else
				dest[i*output_stride] = (int) (src[i*input_stride] * 2147483647.0F);
	}
}

void stc_float_to_double(double* dest, const float* src, size_t count, int input_stride, int output_stride)
{
	for (size_t i=0; i<count; i++)
		dest[i*output_stride] = (double) src[i*input_stride];
}