Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsound.h"
Toshihiro Shimizu 890ddd
#include "tsound_t.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TRK_M8 9
Toshihiro Shimizu 890ddd
#define TRK_S8 10
Toshihiro Shimizu 890ddd
#define TRK_M16 17
Toshihiro Shimizu 890ddd
#define TRK_S16 18
Toshihiro Shimizu 890ddd
#define TRK_M24 25
Toshihiro Shimizu 890ddd
#define TRK_S24 26
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(TSoundTrack, 12)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrack::TSoundTrack()
Shinya Kitaoka 120a6e
    : TSmartObject(m_classCode)
Shinya Kitaoka 120a6e
    , m_parent(0)
Shinya Kitaoka 120a6e
    , m_buffer(0)
Shinya Kitaoka 120a6e
    , m_bufferOwner(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrack::TSoundTrack(TUINT32 sampleRate, int bitPerSample, int channelCount,
Shinya Kitaoka 120a6e
                         int sampleSize, TINT32 sampleCount,
Shinya Kitaoka 120a6e
                         bool isSampleSigned)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    : TSmartObject(m_classCode)
Shinya Kitaoka 120a6e
    , m_sampleRate(sampleRate)
Shinya Kitaoka 120a6e
    , m_sampleSize(sampleSize)
Shinya Kitaoka 120a6e
    , m_bitPerSample(bitPerSample)
Shinya Kitaoka 120a6e
    , m_sampleCount(sampleCount)
Shinya Kitaoka 120a6e
    , m_channelCount(channelCount)
Shinya Kitaoka 120a6e
    , m_parent(0)
Shinya Kitaoka 120a6e
    , m_bufferOwner(true) {
Shinya Kitaoka 120a6e
  m_buffer = (UCHAR *)malloc(sampleCount * m_sampleSize);
Shinya Kitaoka 120a6e
  if (!m_buffer) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // m_buffer = new UCHAR[sampleCount*m_sampleSize];
Shinya Kitaoka 120a6e
  if (isSampleSigned)
Shinya Kitaoka 120a6e
    memset(m_buffer, 0, sampleCount * sampleSize);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    memset(m_buffer, 127, sampleCount * sampleSize);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrack::TSoundTrack(TUINT32 sampleRate, int bitPerSample, int channelCount,
Shinya Kitaoka 120a6e
                         int sampleSize, TINT32 sampleCount, UCHAR *buffer,
Shinya Kitaoka 120a6e
                         TSoundTrack *parent)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    : TSmartObject(m_classCode)
Shinya Kitaoka 120a6e
    , m_sampleRate(sampleRate)
Shinya Kitaoka 120a6e
    , m_sampleSize(sampleSize)
Shinya Kitaoka 120a6e
    , m_bitPerSample(bitPerSample)
Shinya Kitaoka 120a6e
    , m_sampleCount(sampleCount)
Shinya Kitaoka 120a6e
    , m_channelCount(channelCount)
Shinya Kitaoka 120a6e
    , m_parent(parent)
Shinya Kitaoka 120a6e
    , m_buffer(buffer)
Shinya Kitaoka 120a6e
    , m_bufferOwner(false) {
Shinya Kitaoka 120a6e
  if (m_parent) m_parent->addRef();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrack::~TSoundTrack() {
Shinya Kitaoka 120a6e
  if (m_parent) m_parent->release();
Shinya Kitaoka 120a6e
  // if (m_buffer && m_bufferOwner) delete [] m_buffer;
Shinya Kitaoka 120a6e
  if (m_buffer && m_bufferOwner) free(m_buffer);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackP TSoundTrack::create(TUINT32 sampleRate, int bitPerSample,
Shinya Kitaoka 120a6e
                                 int channelCount, TINT32 sampleCount,
Shinya Kitaoka 120a6e
                                 bool signedSample) {
Shinya Kitaoka 120a6e
  TSoundTrackP st;
Shinya Kitaoka 120a6e
  int type = bitPerSample + channelCount;
Shinya Kitaoka 120a6e
  switch (type) {
Shinya Kitaoka 120a6e
  case TRK_M8:
Shinya Kitaoka 120a6e
    if (signedSample)
Shinya Kitaoka 120a6e
      st = new TSoundTrackMono8Signed(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      st = new TSoundTrackMono8Unsigned(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case TRK_S8:
Shinya Kitaoka 120a6e
    if (signedSample)
Shinya Kitaoka 120a6e
      st = new TSoundTrackStereo8Signed(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      st =
Shinya Kitaoka 120a6e
          new TSoundTrackStereo8Unsigned(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_M16:
Shinya Kitaoka 120a6e
    st = new TSoundTrackMono16(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_S16:
Shinya Kitaoka 120a6e
    st = new TSoundTrackStereo16(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_M24:
Shinya Kitaoka 120a6e
    st = new TSoundTrackMono24(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_S24:
Shinya Kitaoka 120a6e
    st = new TSoundTrackStereo24(sampleRate, channelCount, sampleCount);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    std::string s;
Shinya Kitaoka 120a6e
    s = "Type " + std::to_string(sampleRate) + " Hz " +
Shinya Kitaoka 120a6e
        std::to_string(bitPerSample) + " bits ";
Shinya Kitaoka 120a6e
    if (channelCount == 1)
Shinya Kitaoka 120a6e
      s += "mono: ";
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      s += "stereo: ";
Shinya Kitaoka 120a6e
    s += "Unsupported\n";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    throw TException(s);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!st->getRawData()) {
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return st;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackP TSoundTrack::create(TUINT32 sampleRate, int bitPerSample,
Shinya Kitaoka 120a6e
                                 int channelCount, TINT32 sampleCount,
Shinya Kitaoka 120a6e
                                 void *buffer, bool signedSample) {
Shinya Kitaoka 120a6e
  TSoundTrackP st;
Shinya Kitaoka 120a6e
  int type = bitPerSample + channelCount;
Shinya Kitaoka 120a6e
  switch (type) {
Shinya Kitaoka 120a6e
  case TRK_M8:
Shinya Kitaoka 120a6e
    if (signedSample)
Shinya Kitaoka 120a6e
      st = new TSoundTrackMono8Signed(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                      (TMono8SignedSample *)buffer, 0);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      st = new TSoundTrackMono8Unsigned(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                        (TMono8UnsignedSample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case TRK_S8:
Shinya Kitaoka 120a6e
    if (signedSample)
Shinya Kitaoka 120a6e
      st = new TSoundTrackStereo8Signed(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                        (TStereo8SignedSample *)buffer, 0);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      st = new TSoundTrackStereo8Unsigned(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                          (TStereo8UnsignedSample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_M16:
Shinya Kitaoka 120a6e
    st = new TSoundTrackMono16(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                               (TMono16Sample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_S16:
Shinya Kitaoka 120a6e
    st = new TSoundTrackStereo16(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                 (TStereo16Sample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_M24:
Shinya Kitaoka 120a6e
    st = new TSoundTrackMono24(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                               (TMono24Sample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  case TRK_S24:
Shinya Kitaoka 120a6e
    st = new TSoundTrackStereo24(sampleRate, channelCount, sampleCount,
Shinya Kitaoka 120a6e
                                 (TStereo24Sample *)buffer, 0);
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    std::string s;
Shinya Kitaoka 120a6e
    s = "Type " + std::to_string(sampleRate) + " Hz " +
Shinya Kitaoka 120a6e
        std::to_string(bitPerSample) + " bits ";
Shinya Kitaoka 120a6e
    if (channelCount == 1)
Shinya Kitaoka 120a6e
      s += "mono: ";
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      s += "stereo: ";
Shinya Kitaoka 120a6e
    s += "Unsupported\n";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    throw TException(s);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return st;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackP TSoundTrack::create(const TSoundTrackFormat &format,
Shinya Kitaoka 120a6e
                                 TINT32 sampleCount, void *buffer) {
Shinya Kitaoka 120a6e
  return TSoundTrack::create((int)format.m_sampleRate, format.m_bitPerSample,
Shinya Kitaoka 120a6e
                             format.m_channelCount, sampleCount, buffer,
Shinya Kitaoka 120a6e
                             format.m_signedSample);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackP TSoundTrack::create(const TSoundTrackFormat &format,
Shinya Kitaoka 120a6e
                                 TINT32 sampleCount) {
Shinya Kitaoka 120a6e
  return TSoundTrack::create((int)format.m_sampleRate, format.m_bitPerSample,
Shinya Kitaoka 120a6e
                             format.m_channelCount, sampleCount,
Shinya Kitaoka 120a6e
                             format.m_signedSample);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TINT32 TSoundTrack::secondsToSamples(double s) const {
Shinya Kitaoka 120a6e
  double dsamp = s * m_sampleRate;
Shinya Kitaoka 120a6e
  TINT32 lsamp = (TINT32)dsamp;
Shinya Kitaoka 120a6e
  if ((double)lsamp < dsamp - TConsts::epsilon) lsamp++;
Shinya Kitaoka 120a6e
  return lsamp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::samplesToSeconds(TINT32 f) const {
Shinya Kitaoka 120a6e
  return f / (double)m_sampleRate;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TSoundTrackFormat::operator==(const TSoundTrackFormat &rhs) {
Shinya Kitaoka 120a6e
  return (m_sampleRate == rhs.m_sampleRate &&
Shinya Kitaoka 120a6e
          m_bitPerSample == rhs.m_bitPerSample &&
Shinya Kitaoka 120a6e
          m_channelCount == rhs.m_channelCount &&
Shinya Kitaoka 120a6e
          m_signedSample == rhs.m_signedSample);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TSoundTrackFormat::operator!=(const TSoundTrackFormat &rhs) {
Shinya Kitaoka 120a6e
  return !operator==(rhs);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getDuration() const {
Shinya Kitaoka 120a6e
  return samplesToSeconds(m_sampleCount);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackFormat TSoundTrack::getFormat() const {
Shinya Kitaoka 120a6e
  return TSoundTrackFormat(getSampleRate(), getBitPerSample(),
Shinya Kitaoka 120a6e
                           getChannelCount(), isSampleSigned());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TSoundTrackP TSoundTrack::extract(double t0, double t1) {
Shinya Kitaoka 120a6e
  return extract(secondsToSamples(t0), secondsToSamples(t1));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSoundTrack::copy(const TSoundTrackP &src, double dst_t0) {
Shinya Kitaoka 120a6e
  copy(src, secondsToSamples(dst_t0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSoundTrack::blank(double t0, double t1) {
Shinya Kitaoka 120a6e
  blank(secondsToSamples(t0), secondsToSamples(t1));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getPressure(double second, TSound::Channel chan) const {
Shinya Kitaoka 120a6e
  return getPressure(secondsToSamples(second), chan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSoundTrack::getMinMaxPressure(double t0, double t1, TSound::Channel chan,
Shinya Kitaoka 120a6e
                                    double &min, double &max) const {
Shinya Kitaoka 120a6e
  getMinMaxPressure(secondsToSamples(t0), secondsToSamples(t1), chan, min, max);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TSoundTrack::getMinMaxPressure(TSound::Channel chan, double &min,
Shinya Kitaoka 120a6e
                                    double &max) const {
Shinya Kitaoka 120a6e
  getMinMaxPressure(0, (TINT32)(getSampleCount() - 1), chan, min, max);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getMaxPressure(double t0, double t1,
Shinya Kitaoka 120a6e
                                   TSound::Channel chan) const {
Shinya Kitaoka 120a6e
  return getMaxPressure(secondsToSamples(t0), secondsToSamples(t1), chan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getMaxPressure(TSound::Channel chan) const {
Shinya Kitaoka 120a6e
  return getMaxPressure(0, (TINT32)(getSampleCount() - 1), chan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getMinPressure(double t0, double t1,
Shinya Kitaoka 120a6e
                                   TSound::Channel chan) const {
Shinya Kitaoka 120a6e
  return getMinPressure(secondsToSamples(t0), secondsToSamples(t1), chan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TSoundTrack::getMinPressure(TSound::Channel chan) const {
Shinya Kitaoka 120a6e
  return getMinPressure(0, (TINT32)(getSampleCount() - 1), chan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------