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