Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsio_raw.h"
Toshihiro Shimizu 890ddd
#include "tsound_t.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrackReaderRaw::TSoundTrackReaderRaw(const TFilePath &fp)
Toshihiro Shimizu 890ddd
	: TSoundTrackReader(fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrackP TSoundTrackReaderRaw::load()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Tifstream is(m_path);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!is)
Toshihiro Shimizu 890ddd
		throw TException(L"Unable to load the RAW file " +
Toshihiro Shimizu 890ddd
						 m_path.getWideString() + L" : doesn't exist");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is.seekg(0, ios_base::end);
Toshihiro Shimizu 890ddd
	long sampleCount = is.tellg() / 2;
Toshihiro Shimizu 890ddd
	is.seekg(0, ios_base::beg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TSoundTrack *track = new TSoundTrackMono16(22050, 1, sampleCount);
Toshihiro Shimizu 890ddd
	is.read((char *)track->getRawData(), sampleCount * 2);
Toshihiro Shimizu 890ddd
	return track;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrackWriterRaw::TSoundTrackWriterRaw(const TFilePath &fp)
Toshihiro Shimizu 890ddd
	: TSoundTrackWriter(fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TSoundTrackWriterRaw::save(const TSoundTrackP &track)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFileStatus fs(m_path);
Toshihiro Shimizu 890ddd
	if (fs.doesExist() && !fs.isWritable())
Toshihiro Shimizu 890ddd
		throw TException(L"Unable to save the soundtrack: " +
Toshihiro Shimizu 890ddd
						 m_path.getWideString() + L" is read-only");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Tofstream os(m_path);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//inserisco i dati nell'output stream
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// os << (ULONG) (track->getSampleCount()) << " ";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int sampleCount = track->getSampleCount();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (track->getChannelCount()) {
Toshihiro Shimizu 890ddd
	case 1: // mono
Toshihiro Shimizu 890ddd
		switch (track->getSampleSize()) {
Toshihiro Shimizu 890ddd
		case 1: // un byte per campione
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			const char *srcCharSamples = (char *)track->getRawData();
Toshihiro Shimizu 890ddd
			for (i = 0; i < sampleCount; i++) {
Toshihiro Shimizu 890ddd
				short sample = *(srcCharSamples + i);
Toshihiro Shimizu 890ddd
				os.write((char *)&sample, sizeof(short));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		case 2: // due byte per campione
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			const short *srcShortSamples = (short *)track->getRawData();
Toshihiro Shimizu 890ddd
			for (i = 0; i < sampleCount; i++)
Toshihiro Shimizu 890ddd
				os.write((char *)(srcShortSamples + i), sizeof(short));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 2: // stereo
Toshihiro Shimizu 890ddd
		switch (track->getSampleSize()) {
Toshihiro Shimizu 890ddd
		case 2: // due byte per campione, un byte per canale
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			// considera solo il canale sinistro
Toshihiro Shimizu 890ddd
			const char *srcCharSamples = (char *)track->getRawData();
Toshihiro Shimizu 890ddd
			for (i = 0; i < sampleCount; i += 2) {
Toshihiro Shimizu 890ddd
				short sample = *(srcCharSamples + i);
Toshihiro Shimizu 890ddd
				os.write((char *)&sample, sizeof(short));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		case 4: // quattro byte per campione, due byte per canale
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			// considera solo il canale sinistro
Toshihiro Shimizu 890ddd
			const short *srcShortSamples = (short *)track->getRawData();
Toshihiro Shimizu 890ddd
			for (i = 0; i < sampleCount; i += 2)
Toshihiro Shimizu 890ddd
				os.write((char *)(srcShortSamples + i), sizeof(short));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}