Example #1
0
File: ISmb.cpp Project: FEI17N/Lgi
	bool Write()
	{
		Conn->Write((char*) Protocol, sizeof(Protocol), 0);
		WriteUShort(Command);
		WriteULong(Status.Status);
		WriteUChar(Flags);
		WriteUShort(Flags2);
		Conn->Write((char*) Pad, sizeof(Pad), 0);
		WriteUShort(Tid);
		WriteUShort(Pid);
		WriteUShort(Uid);
		WriteUShort(Mid);
		
		WriteUChar(WordCount);
		if (ParameterWords)
		{
			Conn->Write((char*) ParameterWords, sizeof(ushort)*WordCount, 0);
		}

		WriteUChar(ByteCount);
		if (Buffer)
		{
			Conn->Write((char*) Buffer, sizeof(uchar)*ByteCount, 0);
		}

		return true;
	}
status_t
AudioFormatConverter::Read(void* buffer, int64 pos, int64 frames)
{
	TRACE("AudioFormatConverter::Read(%p, %Ld, %Ld)\n", buffer, pos, frames);
	status_t error = InitCheck();
	if (error != B_OK) {
		TRACE("AudioFormatConverter::Read() done 1\n");
		return error;
	}
	pos += fOutOffset;

	if (fFormat.u.raw_audio.format == fSource->Format().u.raw_audio.format
		&& fFormat.u.raw_audio.byte_order
		   == fSource->Format().u.raw_audio.byte_order) {
		TRACE("AudioFormatConverter::Read() done 2\n");
		return fSource->Read(buffer, pos, frames);
	}

	int32 inSampleSize = fSource->Format().u.raw_audio.format
		& media_raw_audio_format::B_AUDIO_SIZE_MASK;
	int32 outSampleSize = fFormat.u.raw_audio.format
		& media_raw_audio_format::B_AUDIO_SIZE_MASK;
	int32 channelCount = fFormat.u.raw_audio.channel_count;
	int32 inFrameSize = inSampleSize * channelCount;
	int32 outFrameSize = outSampleSize * channelCount;
	char* reformatBuffer = NULL;
	char* inBuffer = (char*)buffer;

	#ifdef TRACE_AUDIO_CONVERTER
		char formatString[256];
		string_for_format(fSource->Format(), formatString, 256);
		TRACE("  source format: %s\n", formatString);
		TRACE("  in format : format: %lx, sample size: %ld, channels: %ld, "
			"byte order: %lu\n", fSource->Format().u.raw_audio.format,
			inSampleSize, channelCount,
			fSource->Format().u.raw_audio.byte_order);
		TRACE("  out format: format: %lx, sample size: %ld, channels: %ld, "
			"byte order: %lu\n", fFormat.u.raw_audio.format, outSampleSize,
			channelCount, fFormat.u.raw_audio.byte_order);
	#endif // TRACE_AUDIO_CONVERTER

	if (inSampleSize != outSampleSize) {
		reformatBuffer = new char[frames * inFrameSize];
		inBuffer = reformatBuffer;
	}
	error = fSource->Read(inBuffer, pos, frames);
	// convert samples to host endianess
	uint32 hostByteOrder
		= (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN;
	if (fSource->Format().u.raw_audio.byte_order != hostByteOrder) {
		swap_sample_byte_order(inBuffer, fSource->Format().u.raw_audio.format,
							   frames * inFrameSize);
	}
	// convert the sample type
	switch (fSource->Format().u.raw_audio.format) {
		// float
		case media_raw_audio_format::B_AUDIO_FLOAT:
			switch (fFormat.u.raw_audio.format) {
				case media_raw_audio_format::B_AUDIO_FLOAT:
					break;
				case media_raw_audio_format::B_AUDIO_INT:
					convert(ReadFloat(), WriteInt(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_SHORT:
					convert(ReadFloat(), WriteShort(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_UCHAR:
					convert(ReadFloat(), WriteUChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_CHAR:
					convert(ReadFloat(), WriteChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
			}
			break;
		// int
		case media_raw_audio_format::B_AUDIO_INT:
			switch (fFormat.u.raw_audio.format) {
				case media_raw_audio_format::B_AUDIO_FLOAT:
					convert(ReadInt(), WriteFloat(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_INT:
					break;
				case media_raw_audio_format::B_AUDIO_SHORT:
					convert(ReadInt(), WriteShort(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_UCHAR:
					convert(ReadInt(), WriteUChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_CHAR:
					convert(ReadInt(), WriteChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
			}
			break;
		// short
		case media_raw_audio_format::B_AUDIO_SHORT:
			switch (fFormat.u.raw_audio.format) {
				case media_raw_audio_format::B_AUDIO_FLOAT:
					convert(ReadShort(), WriteFloat(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_INT:
					convert(ReadShort(), WriteInt(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_SHORT:
					break;
				case media_raw_audio_format::B_AUDIO_UCHAR:
					convert(ReadShort(), WriteUChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_CHAR:
					convert(ReadShort(), WriteChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
			}
			break;
		// uchar
		case media_raw_audio_format::B_AUDIO_UCHAR:
			switch (fFormat.u.raw_audio.format) {
				case media_raw_audio_format::B_AUDIO_FLOAT:
					convert(ReadUChar(), WriteFloat(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_INT:
					convert(ReadUChar(), WriteInt(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_SHORT:
					convert(ReadUChar(), WriteShort(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_UCHAR:
					break;
				case media_raw_audio_format::B_AUDIO_CHAR:
					convert(ReadUChar(), WriteChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
			}
			break;
		// char
		case media_raw_audio_format::B_AUDIO_CHAR:
			switch (fFormat.u.raw_audio.format) {
				case media_raw_audio_format::B_AUDIO_FLOAT:
					convert(ReadChar(), WriteFloat(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_INT:
					convert(ReadChar(), WriteInt(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_SHORT:
					convert(ReadChar(), WriteShort(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_UCHAR:
					convert(ReadChar(), WriteUChar(), inBuffer, (char*)buffer,
							frames, inSampleSize, outSampleSize, channelCount);
					break;
				case media_raw_audio_format::B_AUDIO_CHAR:
					break;
			}
			break;
	}
	// convert samples to output endianess
	if (fFormat.u.raw_audio.byte_order != hostByteOrder) {
		swap_sample_byte_order(buffer, fFormat.u.raw_audio.format,
							   frames * outFrameSize);
	}

	delete[] reformatBuffer;
	TRACE("AudioFormatConverter::Read() done\n");
	return B_OK;
}