Jeremy Bullock 3f5067
#include <memory></memory>
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
#include "tmachine.h"
Jeremy Bullock 3f5067
#include "tsio_mp3.h"
Jeremy Bullock 3f5067
#include "tsystem.h"
Jeremy Bullock 3f5067
#include "tfilepath_io.h"
Jeremy Bullock 3f5067
#include "tsound_t.h"
Jeremy Bullock 3f5067
#include "toonz/preferences.h"
Jeremy Bullock 3f5067
#include "toonz/toonzfolders.h"
justburner 64e039
#include "thirdparty.h"
justburner 64e039
Jeremy Bullock 3f5067
#include <qdir></qdir>
Jeremy Bullock 3f5067
#include <qprocess></qprocess>
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
//==============================================================================
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
TSoundTrackReaderMp3::TSoundTrackReaderMp3(const TFilePath &fp)
Jeremy Bullock 3f5067
    : TSoundTrackReader(fp) {}
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
//------------------------------------------------------------------------------
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
TSoundTrackP TSoundTrackReaderMp3::load() {
Jeremy Bullock 3f5067
  FfmpegAudio *ffmepegAudio = new FfmpegAudio();
Jeremy Bullock 3f5067
  TFilePath tempFile        = ffmepegAudio->getRawAudio(m_path);
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
  Tifstream is(tempFile);
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
  if (!is)
Jeremy Bullock 3f5067
    throw TException(L"Unable to load the RAW file " + m_path.getWideString() +
Jeremy Bullock 3f5067
                     L" : doesn't exist");
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
  is.seekg(0, std::ios_base::end);
Jeremy Bullock 3f5067
  long sampleCount = is.tellg() / 4;
Jeremy Bullock 3f5067
  is.seekg(0, std::ios_base::beg);
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
  TSoundTrack *track = new TSoundTrackStereo16(44100, 2, sampleCount);
Jeremy Bullock 3f5067
  is.read((char *)track->getRawData(), sampleCount * 4);
Jeremy Bullock 3f5067
  return track;
Jeremy Bullock 3f5067
}
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
TFilePath FfmpegAudio::getFfmpegCache() {
Jeremy Bullock 3f5067
  QString cacheRoot = ToonzFolder::getCacheRootFolder().getQString();
Jeremy Bullock 3f5067
  if (!TSystem::doesExistFileOrLevel(TFilePath(cacheRoot + "/ffmpeg"))) {
Jeremy Bullock 3f5067
    TSystem::mkDir(TFilePath(cacheRoot + "/ffmpeg"));
Jeremy Bullock 3f5067
  }
Jeremy Bullock 3f5067
  std::string ffmpegPath =
Jeremy Bullock 3f5067
      TFilePath(cacheRoot + "/ffmpeg").getQString().toStdString();
Jeremy Bullock 3f5067
  return TFilePath(cacheRoot + "/ffmpeg");
Jeremy Bullock 3f5067
}
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
void FfmpegAudio::runFfmpeg(QStringList args) {
Jeremy Bullock 3f5067
  // write the file
Jeremy Bullock 3f5067
  QProcess ffmpeg;
justburner 64e039
  ThirdParty::runFFmpeg(ffmpeg, args);
Jeremy Bullock 3f5067
  ffmpeg.waitForFinished(30000);
Jeremy Bullock 3f5067
  QString results = ffmpeg.readAllStandardError();
Jeremy Bullock 3f5067
  results += ffmpeg.readAllStandardOutput();
Jeremy Bullock 3f5067
  int exitCode = ffmpeg.exitCode();
Jeremy Bullock 3f5067
  ffmpeg.close();
Jeremy Bullock 3f5067
  std::string strResults = results.toStdString();
Jeremy Bullock 3f5067
}
Jeremy Bullock 3f5067
Jeremy Bullock 3f5067
TFilePath FfmpegAudio::getRawAudio(TFilePath path) {
Jeremy Bullock 3f5067
  std::string name       = path.getName();
Jeremy Bullock 3f5067
  TFilePath outPath      = getFfmpegCache() + TFilePath(name + ".raw");
Jeremy Bullock 3f5067
  std::string strPath    = path.getQString().toStdString();
Jeremy Bullock 3f5067
  std::string strOutPath = outPath.getQString().toStdString();
Jeremy Bullock 3f5067
  QStringList args;
Jeremy Bullock 3f5067
  args << "-i";
Jeremy Bullock 3f5067
  args << path.getQString();
Jeremy Bullock 3f5067
  args << "-f";
Jeremy Bullock 3f5067
  args << "s16le";
Jeremy Bullock 3f5067
  args << "-ac";
Jeremy Bullock 3f5067
  args << "2";
Jeremy Bullock 3f5067
  args << "-ar";
Jeremy Bullock 3f5067
  args << "44100";
Jeremy Bullock 3f5067
  args << "-y";
Jeremy Bullock 3f5067
  args << outPath.getQString();
Jeremy Bullock 3f5067
  runFfmpeg(args);
Jeremy Bullock 3f5067
  return outPath;
justburner 64e039
}