Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/txshsoundcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelset.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/tproject.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsoundlevel.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "toutputproperties.h"
Toshihiro Shimizu 890ddd
#include "tsop.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
//  ColumnLevel
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ColumnLevel
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TXshSoundLevelP m_soundLevel;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!Offsets: in frames. Start offset is a positive number.*/
Toshihiro Shimizu 890ddd
	int m_startOffset;
Toshihiro Shimizu 890ddd
	/*!Offsets: in frames. End offset is a positive number(to subtract to..).*/
Toshihiro Shimizu 890ddd
	int m_endOffset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Starting frame in the timeline
Toshihiro Shimizu 890ddd
	int m_startFrame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! frameRate
Toshihiro Shimizu 890ddd
	double m_fps;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ColumnLevel(TXshSoundLevel *soundLevel = 0, int startFrame = -1, int startOffset = -1, int endOffset = -1, double fps = -1);
Toshihiro Shimizu 890ddd
	~ColumnLevel();
Toshihiro Shimizu 890ddd
	ColumnLevel *clone() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Overridden from TXshLevel
Toshihiro Shimizu 890ddd
	TXshSoundLevel *getSoundLevel() const { return m_soundLevel.getPointer(); }
Toshihiro Shimizu 890ddd
	void setSoundLevel(TXshSoundLevelP level) { m_soundLevel = level; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void loadData(TIStream &is);
Toshihiro Shimizu 890ddd
	void saveData(TOStream &os);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setStartOffset(int value);
Toshihiro Shimizu 890ddd
	int getStartOffset() const { return m_startOffset; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setEndOffset(int value);
Toshihiro Shimizu 890ddd
	int getEndOffset() const { return m_endOffset; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setOffsets(int startOffset, int endOffset);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Return the starting frame without offsets.
Toshihiro Shimizu 890ddd
	void setStartFrame(int frame) { m_startFrame = frame; }
Toshihiro Shimizu 890ddd
	int getStartFrame() const { return m_startFrame; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Return the ending frame without offsets.
Toshihiro Shimizu 890ddd
	int getEndFrame() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Return frame count without offset.
Toshihiro Shimizu 890ddd
	int getFrameCount() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Return frame count with offset.
Toshihiro Shimizu 890ddd
	int getVisibleFrameCount() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//! Return start frame with offset.
Toshihiro Shimizu 890ddd
	int getVisibleStartFrame() const;
Toshihiro Shimizu 890ddd
	//! Return last frame with offset.
Toshihiro Shimizu 890ddd
	int getVisibleEndFrame() const;
Toshihiro Shimizu 890ddd
	//! Updates m_startOfset and m_endOffset.
Toshihiro Shimizu 890ddd
	void updateFrameRate(double newFrameRate);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setFrameRate(double fps) { m_fps = fps; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ColumnLevel::ColumnLevel(TXshSoundLevel *soundLevel, int startFrame, int startOffset, int endOffset, double fps)
Toshihiro Shimizu 890ddd
	: m_soundLevel(soundLevel), m_startOffset(startOffset), m_endOffset(endOffset), m_startFrame(startFrame), m_fps(fps)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ColumnLevel::~ColumnLevel()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ColumnLevel *ColumnLevel::clone() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ColumnLevel *soundColumnLevel = new ColumnLevel();
Toshihiro Shimizu 890ddd
	soundColumnLevel->setSoundLevel(getSoundLevel());
Toshihiro Shimizu 890ddd
	soundColumnLevel->setStartFrame(m_startFrame);
Toshihiro Shimizu 890ddd
	soundColumnLevel->setStartOffset(m_startOffset);
Toshihiro Shimizu 890ddd
	soundColumnLevel->setEndOffset(m_endOffset);
Toshihiro Shimizu 890ddd
	soundColumnLevel->setFrameRate(m_fps);
Toshihiro Shimizu 890ddd
	return soundColumnLevel;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::loadData(TIStream &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	is.openChild(tagName);
Toshihiro Shimizu 890ddd
	if (tagName == "SoundCells") {
Toshihiro Shimizu 890ddd
		TPersist *p = 0;
Toshihiro Shimizu 890ddd
		is >> m_startOffset >> m_endOffset >> m_startFrame >> p;
Toshihiro Shimizu 890ddd
		TXshSoundLevel *xshLevel = dynamic_cast<txshsoundlevel *="">(p);</txshsoundlevel>
Toshihiro Shimizu 890ddd
		if (xshLevel)
Toshihiro Shimizu 890ddd
			m_soundLevel = xshLevel;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	is.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::saveData(TOStream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	os.child("SoundCells") << getStartOffset() << getEndOffset() << getStartFrame() << m_soundLevel.getPointer();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::setStartOffset(int value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (value < 0 || value > getFrameCount() - getEndOffset() - 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_startOffset = value;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::setEndOffset(int value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (value < 0 || getStartFrame() + getFrameCount() - value < getStartFrame() + getStartOffset() + 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_endOffset = value;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::setOffsets(int startOffset, int endOffset)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(startOffset > 0 && endOffset > 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (startOffset < 0 || startOffset > getFrameCount() - endOffset - 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_startOffset = startOffset;
Toshihiro Shimizu 890ddd
	if (endOffset < 0 || getStartFrame() + getFrameCount() - endOffset < getStartFrame() + getStartOffset() + 1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_endOffset = endOffset;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ColumnLevel::getEndFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return getStartFrame() + getFrameCount() - 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ColumnLevel::getFrameCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return m_soundLevel->getFrameCount();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ColumnLevel::getVisibleFrameCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return getFrameCount() - getStartOffset() - getEndOffset();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ColumnLevel::getVisibleStartFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return getStartFrame() + getStartOffset();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int ColumnLevel::getVisibleEndFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_soundLevel)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return getVisibleStartFrame() + getVisibleFrameCount() - 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ColumnLevel::updateFrameRate(double newFrameRate)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_fps == newFrameRate)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	double fact = (double)newFrameRate / m_fps;
Toshihiro Shimizu 890ddd
	m_startFrame = tround(m_startFrame * fact);
Toshihiro Shimizu 890ddd
	m_startOffset = tround(m_startOffset * fact);
Toshihiro Shimizu 890ddd
	m_endOffset = tround(m_endOffset * fact);
Toshihiro Shimizu 890ddd
	m_fps = newFrameRate;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool lessThan(const ColumnLevel *s1, const ColumnLevel *s2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return s1->getVisibleStartFrame() < s2->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TXshSoundColumn::TXshSoundColumn()
Toshihiro Shimizu 890ddd
	: m_player(0), m_volume(0.4), m_currentPlaySoundTrack(0), m_isOldVersion(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_timer.setInterval(500);
Toshihiro Shimizu 890ddd
	m_timer.setSingleShot(true);
Toshihiro Shimizu 890ddd
	m_timer.stop();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimerOut()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TXshSoundColumn::~TXshSoundColumn()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	clear();
Toshihiro Shimizu 890ddd
	if (m_timer.isActive()) {
Toshihiro Shimizu 890ddd
		m_timer.stop();
Toshihiro Shimizu 890ddd
		stop();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TXshColumn::ColumnType TXshSoundColumn::getColumnType() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return eSoundType;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::canSetCell(const TXshCell &cell) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return cell.isEmpty() || cell.m_level->getSoundLevel() != 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TXshColumn *TXshSoundColumn::clone() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TXshSoundColumn *column = new TXshSoundColumn();
Toshihiro Shimizu 890ddd
	column->setVolume(m_volume);
Toshihiro Shimizu 890ddd
	column->setXsheet(getXsheet());
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_levels.size(); i++)
Toshihiro Shimizu 890ddd
		column->insertColumnLevel(m_levels.at(i)->clone(), i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return column;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::assignLevels(const TXshSoundColumn *src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Svuoto la lista dei livelli
Toshihiro Shimizu 890ddd
	clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int r;
Toshihiro Shimizu 890ddd
	for (r = src->getFirstRow(); r <= src->getMaxFrame(); r++) {
Toshihiro Shimizu 890ddd
		int r0, r1;
Toshihiro Shimizu 890ddd
		if (!src->getLevelRange(r, r0, r1))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		TXshCell cell = src->getCell(r);
Toshihiro Shimizu 890ddd
		if (cell.isEmpty())
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		TXshSoundLevel *soundLevel = cell.m_level->getSoundLevel();
Toshihiro Shimizu 890ddd
		assert(!!soundLevel);
Toshihiro Shimizu 890ddd
		int startOffset = cell.getFrameId().getNumber();
Toshihiro Shimizu 890ddd
		int startFrame = r - startOffset;
Toshihiro Shimizu 890ddd
		int endOffset = startFrame + soundLevel->getFrameCount() - r1 - 1;
Toshihiro Shimizu 890ddd
		ColumnLevel *l = new ColumnLevel(soundLevel, startFrame, startOffset, endOffset);
Toshihiro Shimizu 890ddd
		insertColumnLevel(l);
Toshihiro Shimizu 890ddd
		r = r1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::loadData(TIStream &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	VersionNumber tnzVersion = is.getVersion();
Toshihiro Shimizu 890ddd
	if (tnzVersion < VersionNumber(1, 17)) {
Toshihiro Shimizu 890ddd
		TFilePath path;
Toshihiro Shimizu 890ddd
		is >> path;
Toshihiro Shimizu 890ddd
		m_isOldVersion = true;
Toshihiro Shimizu 890ddd
		int offset = 0;
Toshihiro Shimizu 890ddd
		is >> offset;
Toshihiro Shimizu 890ddd
		is >> m_volume;
Toshihiro Shimizu 890ddd
		if (!is.eos()) {
Toshihiro Shimizu 890ddd
			int status;
Toshihiro Shimizu 890ddd
			is >> status;
Toshihiro Shimizu 890ddd
			setStatusWord(status);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TXshSoundLevelP level = new TXshSoundLevel(path.getWideName());
Toshihiro Shimizu 890ddd
		level->setPath(path);
Toshihiro Shimizu 890ddd
		ColumnLevel *l = new ColumnLevel(level.getPointer(), offset, 0, 0);
Toshihiro Shimizu 890ddd
		insertColumnLevel(l);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	is >> m_volume;
Toshihiro Shimizu 890ddd
	int levelCount = 0;
Toshihiro Shimizu 890ddd
	is >> levelCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < levelCount; i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *sl = new ColumnLevel();
Toshihiro Shimizu 890ddd
		sl->loadData(is);
Toshihiro Shimizu 890ddd
		insertColumnLevel(sl, i);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!is.eos()) {
Toshihiro Shimizu 890ddd
		int status;
Toshihiro Shimizu 890ddd
		is >> status;
Toshihiro Shimizu 890ddd
		setStatusWord(status);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::saveData(TOStream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	os << m_volume;
Toshihiro Shimizu 890ddd
	int levelsCount = m_levels.size();
Toshihiro Shimizu 890ddd
	os << levelsCount;
Toshihiro Shimizu 890ddd
	if (levelsCount == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < levelsCount; i++)
Toshihiro Shimizu 890ddd
		m_levels.at(i)->saveData(os);
Toshihiro Shimizu 890ddd
	os << getStatusWord();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::getRange(int &r0, int &r1) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	r0 = getFirstRow();
Toshihiro Shimizu 890ddd
	r1 = getMaxFrame();
Toshihiro Shimizu 890ddd
	return r1 - r0 + 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::getRowCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return getMaxFrame() - getFirstRow();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::getMaxFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int levelsCount = m_levels.size();
Toshihiro Shimizu 890ddd
	if (levelsCount == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return m_levels.at(levelsCount - 1)->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::getFirstRow() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_levels.size() == 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	return m_levels.at(0)->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TXshCell &TXshSoundColumn::getCell(int row) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static TXshCell emptyCell;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	if (row < 0 || row < getFirstRow() || row > getMaxFrame()) {
Toshihiro Shimizu 890ddd
		if (l)
Toshihiro Shimizu 890ddd
			emptyCell.m_level = l->getSoundLevel();
Toshihiro Shimizu 890ddd
		return emptyCell;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!l)
Toshihiro Shimizu 890ddd
		return emptyCell;
Toshihiro Shimizu 890ddd
	TXshSoundLevel *soundLevel = l->getSoundLevel();
Toshihiro Shimizu 890ddd
	TXshCell *cell = new TXshCell(soundLevel, TFrameId(row - l->getStartFrame()));
Toshihiro Shimizu 890ddd
	// La nuova cella aggiunge un reference al TXshSoundLevel; poiche' le celle delle
Toshihiro Shimizu 890ddd
	// TXshSoundColumn non sono strutture persistenti ma sono strutture dinamiche
Toshihiro Shimizu 890ddd
	// (vengono ricreate ogni volta) devo occuparmi di fare il release altrimenti il
Toshihiro Shimizu 890ddd
	// TXshSoundLevel non viene mai buttato.
Toshihiro Shimizu 890ddd
	soundLevel->release();
Toshihiro Shimizu 890ddd
	return *cell;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::isCellEmpty(int row) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_levels.empty())
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	if (!l || !l->getSoundLevel())
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//--- debug only
Toshihiro Shimizu 890ddd
void TXshSoundColumn::checkColumn() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifndef NDEBUG
Toshihiro Shimizu 890ddd
	int size = m_levels.size();
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < size; i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *level = m_levels.at(i);
Toshihiro Shimizu 890ddd
		assert(level);
Toshihiro Shimizu 890ddd
		assert(level->getSoundLevel());
Toshihiro Shimizu 890ddd
		int start1 = level->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
		int end1 = level->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
		assert(start1 <= end1);
Toshihiro Shimizu 890ddd
		if (i < size - 1) {
Toshihiro Shimizu 890ddd
			ColumnLevel *nextLevel = m_levels.at(i + 1);
Toshihiro Shimizu 890ddd
			assert(nextLevel);
Toshihiro Shimizu 890ddd
			assert(nextLevel->getSoundLevel());
Toshihiro Shimizu 890ddd
			int start2 = nextLevel->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
			int end2 = nextLevel->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
			assert(start2 <= end2);
Toshihiro Shimizu 890ddd
			assert(end1 < start2);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::getCells(int row, int rowCount, TXshCell cells[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// le celle da settare sono [ra, rb]
Toshihiro Shimizu 890ddd
	int ra = row;
Toshihiro Shimizu 890ddd
	int rb = row + rowCount - 1;
Toshihiro Shimizu 890ddd
	assert(ra <= rb);
Toshihiro Shimizu 890ddd
	TXshCell *dstCell = cells;
Toshihiro Shimizu 890ddd
	int r;
Toshihiro Shimizu 890ddd
	for (r = ra; r <= rb; r++)
Toshihiro Shimizu 890ddd
		*cells++ = getCell(r);
Toshihiro Shimizu 890ddd
	checkColumn();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::setCellInEmptyFrame(int row, const TXshCell &cell)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (cell.isEmpty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TXshSoundLevel *soundLevel = cell.getSoundLevel();
Toshihiro Shimizu 890ddd
	if (!soundLevel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int startOffset = cell.getFrameId().getNumber();
Toshihiro Shimizu 890ddd
	int startFrame = row - startOffset;
Toshihiro Shimizu 890ddd
	int endOffset = startFrame + soundLevel->getFrameCount() - 1 - row;
Toshihiro Shimizu 890ddd
	insertColumnLevel(new ColumnLevel(soundLevel, startFrame, startOffset, endOffset));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::setCell(int row, const TXshCell &cell)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return setCell(row, cell, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::setCell(int row, const TXshCell &cell, bool updateSequence)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!canSetCell(cell))
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	assert(row >= 0);
Toshihiro Shimizu 890ddd
	ColumnLevel *lBefore = getColumnLevelByFrame(row - 1);
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	ColumnLevel *lNext = getColumnLevelByFrame(row + 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Se non devo fare un update della sequenzialita' e la cella e' uguale a quella gia' esistente ritorno.
Toshihiro Shimizu 890ddd
	if (!updateSequence &&
Toshihiro Shimizu 890ddd
		l && l->getSoundLevel() == cell.getSoundLevel() &&
Toshihiro Shimizu 890ddd
		row - l->getStartFrame() == cell.getFrameId().getNumber())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isBeforeLevelInSequence = (lBefore &&
Toshihiro Shimizu 890ddd
									lBefore->getSoundLevel() == cell.getSoundLevel() &&
Toshihiro Shimizu 890ddd
									row - lBefore->getStartFrame() == cell.getFrameId().getNumber());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isNextLevelInSequence = (lNext &&
Toshihiro Shimizu 890ddd
								  lNext->getSoundLevel() == cell.getSoundLevel() &&
Toshihiro Shimizu 890ddd
								  row - lNext->getStartFrame() == cell.getFrameId().getNumber());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Modifico solo l'endOffset del BeforeLevel
Toshihiro Shimizu 890ddd
	if (isBeforeLevelInSequence) {
Toshihiro Shimizu 890ddd
		int oldEndOffset = lBefore->getEndOffset();
Toshihiro Shimizu 890ddd
		int endOffset = (lBefore->getVisibleEndFrame() != row) ? oldEndOffset - 1 : oldEndOffset;
Toshihiro Shimizu 890ddd
		if (isNextLevelInSequence) {
Toshihiro Shimizu 890ddd
			endOffset = lNext->getEndOffset();
Toshihiro Shimizu 890ddd
			//Se precedente e successivo sono diversi elimino il successivo (caso in cui inserisci un frame in successione su una cella vuota)
Toshihiro Shimizu 890ddd
			if (lNext != lBefore)
Toshihiro Shimizu 890ddd
				removeColumnLevel(lNext);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (l && l != lBefore) {
Toshihiro Shimizu 890ddd
			if (l->getVisibleFrameCount() == 1)
Toshihiro Shimizu 890ddd
				removeColumnLevel(l);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				l->setStartOffset(l->getStartOffset() + 1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		lBefore->setEndOffset(endOffset);
Toshihiro Shimizu 890ddd
		checkColumn();
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	//Modifico solo lo startOffset del NextLevel
Toshihiro Shimizu 890ddd
	if (isNextLevelInSequence) {
Toshihiro Shimizu 890ddd
		int oldStartOffset = lNext->getStartOffset();
Toshihiro Shimizu 890ddd
		int startOffset = (lNext->getVisibleStartFrame() != row) ? oldStartOffset - 1 : oldStartOffset;
Toshihiro Shimizu 890ddd
		if (l && l != lNext) {
Toshihiro Shimizu 890ddd
			if (l->getVisibleFrameCount() == 1)
Toshihiro Shimizu 890ddd
				removeColumnLevel(l);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				l->setEndOffset(l->getEndOffset() + 1);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		lNext->setStartOffset(startOffset);
Toshihiro Shimizu 890ddd
		checkColumn();
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//I Frame non sono in successione quindi devo inserire la cella in frame un vuoto:
Toshihiro Shimizu 890ddd
	clearCells(row, 1);
Toshihiro Shimizu 890ddd
	setCellInEmptyFrame(row, cell);
Toshihiro Shimizu 890ddd
	checkColumn();
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::setCells(int row, int rowCount, const TXshCell cells[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(row >= 0);
Toshihiro Shimizu 890ddd
	// le celle da settare sono [ra, rb]
Toshihiro Shimizu 890ddd
	int ra = row;
Toshihiro Shimizu 890ddd
	int rb = row + rowCount - 1;
Toshihiro Shimizu 890ddd
	assert(ra <= rb);
Toshihiro Shimizu 890ddd
	bool ret = false;
Toshihiro Shimizu 890ddd
	int r;
Toshihiro Shimizu 890ddd
	for (r = ra; r <= rb; r++) {
Toshihiro Shimizu 890ddd
		bool isOneCellSet = setCell(r, cells[r - ra]);
Toshihiro Shimizu 890ddd
		if (isOneCellSet)
Toshihiro Shimizu 890ddd
			ret = isOneCellSet;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::insertEmptyCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_levels.isEmpty())
Toshihiro Shimizu 890ddd
		return; //se la colonna e' vuota non devo inserire celle
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int ra = row;
Toshihiro Shimizu 890ddd
	int rb = row + rowCount - 1;
Toshihiro Shimizu 890ddd
	assert(ra <= rb);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Se il livello nella riga ra inizia prima devo "spezzarlo"
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(ra);
Toshihiro Shimizu 890ddd
	if (l && l->getVisibleStartFrame() < ra) {
Toshihiro Shimizu 890ddd
		int oldOffset = l->getEndOffset();
Toshihiro Shimizu 890ddd
		l->setEndOffset(oldOffset + l->getVisibleEndFrame() - ra + 1);
Toshihiro Shimizu 890ddd
		insertColumnLevel(new ColumnLevel(l->getSoundLevel(), l->getStartFrame(), row - l->getStartFrame(), oldOffset));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Sposto il first frame di tutti i livelli successivi ad ra.
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = m_levels.count() - 1; i >= 0; i--) {
Toshihiro Shimizu 890ddd
		ColumnLevel *cl = m_levels.at(i);
Toshihiro Shimizu 890ddd
		if (cl->getVisibleStartFrame() < ra)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		cl->setStartFrame(cl->getStartFrame() + rowCount);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	checkColumn();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::removeCells(int row, int rowCount, bool shift)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// le celle da cancellare sono [ra, rb]
Toshihiro Shimizu 890ddd
	int ra = row;
Toshihiro Shimizu 890ddd
	int rb = row + rowCount - 1;
Toshihiro Shimizu 890ddd
	assert(ra <= rb);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = m_levels.size() - 1; i >= 0; i--) {
Toshihiro Shimizu 890ddd
		ColumnLevel *l = m_levels.at(i);
Toshihiro Shimizu 890ddd
		if (!l)
Toshihiro Shimizu 890ddd
			continue; // Non dovrebbe accadere!
Toshihiro Shimizu 890ddd
		int visibleStartFrame = l->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
		int visibleEndFrame = l->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
		// Fuori dal range da tagliare.
Toshihiro Shimizu 890ddd
		if (visibleStartFrame > rb || visibleEndFrame < ra)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int newEndOffset = l->getEndFrame() - ra + 1;
Toshihiro Shimizu 890ddd
		int newStartOffset = rb - l->getStartFrame() + 1;
Toshihiro Shimizu 890ddd
		// Il livello contiene il range da cancellare: devo "spezzare" il livello in due.
Toshihiro Shimizu 890ddd
		if (visibleStartFrame < ra && visibleEndFrame > rb) {
Toshihiro Shimizu 890ddd
			int oldEndOffset = l->getEndOffset();
Toshihiro Shimizu 890ddd
			l->setEndOffset(newEndOffset);
Toshihiro Shimizu 890ddd
			insertColumnLevel(new ColumnLevel(l->getSoundLevel(), l->getStartFrame(), newStartOffset, oldEndOffset));
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (visibleStartFrame < ra)
Toshihiro Shimizu 890ddd
			l->setEndOffset(newEndOffset);
Toshihiro Shimizu 890ddd
		if (visibleEndFrame > rb)
Toshihiro Shimizu 890ddd
			l->setStartOffset(newStartOffset);
Toshihiro Shimizu 890ddd
		//Il livello e' contenuto nel range da cancellare: lo rimuovo.
Toshihiro Shimizu 890ddd
		if (visibleStartFrame >= ra && visibleEndFrame <= rb)
Toshihiro Shimizu 890ddd
			removeColumnLevel(l);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (shift) {
Toshihiro Shimizu 890ddd
		for (i = m_levels.size() - 1; i >= 0; i--) {
Toshihiro Shimizu 890ddd
			ColumnLevel *l = m_levels.at(i);
Toshihiro Shimizu 890ddd
			if (!l)
Toshihiro Shimizu 890ddd
				continue; // Non dovrebbe accadere!
Toshihiro Shimizu 890ddd
			int visibleStartFrame = l->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
			int visibleEndFrame = l->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
			if (visibleStartFrame > ra)
Toshihiro Shimizu 890ddd
				l->setStartFrame(l->getStartFrame() - rowCount);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Devo controllare se il livello precedente il taglio e quello successivo sono in sequenza
Toshihiro Shimizu 890ddd
		ColumnLevel *beforeL = getColumnLevelByFrame(ra - 1);
Toshihiro Shimizu 890ddd
		ColumnLevel *nextL = getColumnLevelByFrame(ra);
Toshihiro Shimizu 890ddd
		if (beforeL && nextL &&
Toshihiro Shimizu 890ddd
			beforeL->getSoundLevel() == nextL->getSoundLevel() &&
Toshihiro Shimizu 890ddd
			beforeL->getStartFrame() == nextL->getStartFrame()) {
Toshihiro Shimizu 890ddd
			beforeL->setEndOffset(nextL->getEndOffset());
Toshihiro Shimizu 890ddd
			removeColumnLevel(nextL);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	checkColumn();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::clearCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (rowCount <= 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_levels.isEmpty())
Toshihiro Shimizu 890ddd
		return; //se la colonna e' vuota non devo "sbiancare" celle
Toshihiro Shimizu 890ddd
	removeCells(row, rowCount, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::removeCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	removeCells(row, rowCount, true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::updateCells(int row, int rowCount)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int r;
Toshihiro Shimizu 890ddd
	for (r = row; r < row + rowCount; r++) {
Toshihiro Shimizu 890ddd
		TXshCell cell = getCell(r);
Toshihiro Shimizu 890ddd
		setCell(row, cell, true);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::modifyCellRange(int row, int delta, bool modifyStartValue)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	if (!l)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int startVisibleFrame = l->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
	int endVisibleFrame = l->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
	//Non dovrebbe mai accadere, metto un controllo di sicurezza.
Toshihiro Shimizu 890ddd
	if (startVisibleFrame != row && endVisibleFrame != row)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int r0 = delta > 0 ? row : row + delta;
Toshihiro Shimizu 890ddd
	int r1 = delta > 0 ? row + delta : row;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int r;
Toshihiro Shimizu 890ddd
	for (r = r0; r <= r1; r++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *oldL = getColumnLevelByFrame(r);
Toshihiro Shimizu 890ddd
		if (!oldL || oldL == l)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int visibleStartFrame = oldL->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
		int visibleEndFrame = oldL->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
		if (visibleStartFrame >= r0 && visibleEndFrame <= r1)
Toshihiro Shimizu 890ddd
			removeColumnLevel(oldL);
Toshihiro Shimizu 890ddd
		if (visibleStartFrame <= r0)
Toshihiro Shimizu 890ddd
			oldL->setEndOffset(oldL->getEndOffset() + visibleEndFrame - r0 + 1);
Toshihiro Shimizu 890ddd
		if (visibleEndFrame >= r1)
Toshihiro Shimizu 890ddd
			oldL->setStartOffset(oldL->getStartOffset() + r1 - visibleStartFrame + 1);
Toshihiro Shimizu 890ddd
		r = visibleEndFrame;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (modifyStartValue) {
Toshihiro Shimizu 890ddd
		//Evito che vada oltre il limite infieriore
Toshihiro Shimizu 890ddd
		if (endVisibleFrame < startVisibleFrame + delta)
Toshihiro Shimizu 890ddd
			delta = endVisibleFrame - startVisibleFrame;
Toshihiro Shimizu 890ddd
		int newStartOffset = l->getStartOffset() + delta;
Toshihiro Shimizu 890ddd
		//Evito che vada oltre il limite superiore
Toshihiro Shimizu 890ddd
		if (newStartOffset < 0)
Toshihiro Shimizu 890ddd
			newStartOffset = 0;
Toshihiro Shimizu 890ddd
		l->setStartOffset(newStartOffset);
Toshihiro Shimizu 890ddd
		checkColumn();
Toshihiro Shimizu 890ddd
		getXsheet()->updateFrameCount();
Toshihiro Shimizu 890ddd
		return l->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Evito che vada oltre il limite superiore
Toshihiro Shimizu 890ddd
	if (startVisibleFrame > endVisibleFrame + delta)
Toshihiro Shimizu 890ddd
		delta = startVisibleFrame - endVisibleFrame;
Toshihiro Shimizu 890ddd
	//Evito che vada oltre il limite inferiore
Toshihiro Shimizu 890ddd
	int newEndOffset = l->getEndOffset() - delta;
Toshihiro Shimizu 890ddd
	if (newEndOffset < 0)
Toshihiro Shimizu 890ddd
		newEndOffset = 0;
Toshihiro Shimizu 890ddd
	l->setEndOffset(newEndOffset);
Toshihiro Shimizu 890ddd
	checkColumn();
Toshihiro Shimizu 890ddd
	getXsheet()->updateFrameCount();
Toshihiro Shimizu 890ddd
	return l->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::getLevelRange(int row, int &r0, int &r1) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	if (!l) {
Toshihiro Shimizu 890ddd
		r0 = r1 = row;
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	r0 = l->getVisibleStartFrame();
Toshihiro Shimizu 890ddd
	r1 = l->getVisibleEndFrame();
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::getLevelRangeWithoutOffset(int row, int &r0, int &r1) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ColumnLevel *l = getColumnLevelByFrame(row);
Toshihiro Shimizu 890ddd
	if (!l) {
Toshihiro Shimizu 890ddd
		r0 = r1 = row;
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	r0 = l->getStartFrame();
Toshihiro Shimizu 890ddd
	r1 = l->getEndFrame();
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::setXsheet(TXsheet *xsheet)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TXshColumn::setXsheet(xsheet);
Toshihiro Shimizu 890ddd
	ToonzScene *scene = xsheet->getScene();
Toshihiro Shimizu 890ddd
	if (!scene || m_levels.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_isOldVersion) {
Toshihiro Shimizu 890ddd
		assert(m_levels.size() == 1);
Toshihiro Shimizu 890ddd
		scene->getLevelSet()->insertLevel(m_levels.at(0)->getSoundLevel());
Toshihiro Shimizu 890ddd
		m_isOldVersion = false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TOutputProperties *properties = scene->getProperties()->getOutputProperties();
Toshihiro Shimizu 890ddd
	setFrameRate(properties->getFrameRate());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::setFrameRate(double fps)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_levels.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_levels.size(); i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *column = m_levels.at(i);
Toshihiro Shimizu 890ddd
		TXshSoundLevel *soundLevel = column->getSoundLevel();
Toshihiro Shimizu 890ddd
		if (soundLevel->getFrameRate() != fps)
Toshihiro Shimizu 890ddd
			soundLevel->setFrameRate(fps);
Toshihiro Shimizu 890ddd
		column->setFrameRate(fps);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::updateFrameRate(double fps)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_levels.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_levels.size(); i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *column = m_levels.at(i);
Toshihiro Shimizu 890ddd
		TXshSoundLevel *soundLevel = column->getSoundLevel();
Toshihiro Shimizu 890ddd
		double oldFps = soundLevel->getFrameRate();
Toshihiro Shimizu 890ddd
		if (oldFps != fps)
Toshihiro Shimizu 890ddd
			soundLevel->setFrameRate(fps);
Toshihiro Shimizu 890ddd
		column->updateFrameRate(fps);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::setVolume(double value)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_volume = tcrop<double>(value, 0.0, 1.0);</double>
Toshihiro Shimizu 890ddd
	if (m_player && m_player->isPlaying())
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
		m_player->setVolume(m_volume);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		stop();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double TXshSoundColumn::getVolume() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_volume;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::play(TSoundTrackP soundtrack, int s0, int s1, bool loop)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!TSoundOutputDevice::installed())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_player)
Toshihiro Shimizu 890ddd
		m_player = new TSoundOutputDevice();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_player) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
			m_player->setVolume(m_volume);
Toshihiro Shimizu 890ddd
			TSoundTrackP mixedTrack = soundtrack;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			TSoundTrackP mixedTrack = TSoundTrack::create(soundtrack->getFormat(), soundtrack->getSampleCount());
Toshihiro Shimizu 890ddd
			mixedTrack = TSop::mix(mixedTrack, soundtrack, 1.0, m_volume);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			m_player->play(mixedTrack, s0, s1, loop);
Toshihiro Shimizu 890ddd
			m_currentPlaySoundTrack = mixedTrack;
Toshihiro Shimizu 890ddd
#ifndef MACOSX
Toshihiro Shimizu 890ddd
			m_timer.start();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		} catch (TSoundDeviceException &) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::play(ColumnLevel *columnLevel, int currentFrame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(columnLevel);
Toshihiro Shimizu 890ddd
	TXshSoundLevel *soundLevel = columnLevel->getSoundLevel();
Toshihiro Shimizu 890ddd
	int offset = 0;
Toshihiro Shimizu 890ddd
	int startFrame = currentFrame - columnLevel->getStartFrame();
Toshihiro Shimizu 890ddd
	int endFrame = soundLevel->getFrameCount() - columnLevel->getEndOffset();
Toshihiro Shimizu 890ddd
	int spf = soundLevel->getSamplePerFrame();
Toshihiro Shimizu 890ddd
	int s0 = startFrame * spf;
Toshihiro Shimizu 890ddd
	int s1 = endFrame * spf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!soundLevel->getSoundTrack())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	play(soundLevel->getSoundTrack(), s0, s1, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::play(int currentFrame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TSoundTrackP soundTrack = getOverallSoundTrack(currentFrame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!soundTrack)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int spf = m_levels.at(0)->getSoundLevel()->getSamplePerFrame();
Toshihiro Shimizu 890ddd
	int startFrame = (currentFrame - getFirstRow());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int s0 = startFrame * spf;
Toshihiro Shimizu 890ddd
	int s1 = getMaxFrame() * spf;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	play(soundTrack, s0, s1, false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::stop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_player) {
Toshihiro Shimizu 890ddd
		m_player->stop();
Toshihiro Shimizu 890ddd
		m_player->close();
Toshihiro Shimizu 890ddd
		m_player = 0;
Toshihiro Shimizu 890ddd
		m_currentPlaySoundTrack = TSoundTrackP();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TXshSoundColumn::isPlaying() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_player && m_player->isPlaying();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::insertColumnLevel(ColumnLevel *columnLevel, int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (index == -1)
Toshihiro Shimizu 890ddd
		index = m_levels.size();
Toshihiro Shimizu 890ddd
	m_levels.insert(index, columnLevel);
Toshihiro Shimizu 890ddd
	qSort(m_levels.begin(), m_levels.end(), lessThan);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::removeColumnLevel(ColumnLevel *columnLevel)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!columnLevel)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int index = m_levels.indexOf(columnLevel);
Toshihiro Shimizu 890ddd
	if (index == -1)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_levels.removeAt(index);
Toshihiro Shimizu 890ddd
	delete columnLevel;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ColumnLevel *TXshSoundColumn::getColumnLevelByFrame(int frame) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int levelCount = m_levels.size();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < levelCount; i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *l = m_levels.at(i);
Toshihiro Shimizu 890ddd
		TXshSoundLevel *soundLevel = l->getSoundLevel();
Toshihiro Shimizu 890ddd
		int startFrame = l->getStartFrame() + l->getStartOffset();
Toshihiro Shimizu 890ddd
		int frameCount = soundLevel->getFrameCount() - l->getStartOffset() - l->getEndOffset();
Toshihiro Shimizu 890ddd
		if (startFrame <= frame && startFrame + frameCount > frame)
Toshihiro Shimizu 890ddd
			return l;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ColumnLevel *TXshSoundColumn::getColumnLevel(int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (index < 0 || index >= m_levels.count())
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return m_levels.at(index);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TXshSoundColumn::getColumnLevelIndex(ColumnLevel *columnLevel) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_levels.indexOf(columnLevel);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::clear()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int sequenceCount = m_levels.size();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < sequenceCount; i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *s = m_levels[i];
Toshihiro Shimizu 890ddd
		delete s;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_levels.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::onTimerOut()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifndef MACOSX
Toshihiro Shimizu 890ddd
	if (m_player && m_player->isAllQueuedItemsPlayed())
Toshihiro Shimizu 890ddd
		stop();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TXshSoundColumn::scrub(int fromFrame, int toFrame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!isCamstandVisible())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	TSoundTrackP soundTrack = getOverallSoundTrack(fromFrame, toFrame + 1);
Toshihiro Shimizu 890ddd
	if (!soundTrack)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	play(soundTrack, 0, soundTrack->getSampleCount(), false);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrackP TXshSoundColumn::getOverallSoundTrack(int fromFrame, int toFrame, double fps, TSoundTrackFormat format)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int levelsCount = m_levels.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (levelsCount == 0)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fps == -1)
Toshihiro Shimizu 890ddd
		fps = m_levels[0]->getSoundLevel()->getFrameRate();
Toshihiro Shimizu 890ddd
	if (fromFrame == -1)
Toshihiro Shimizu 890ddd
		fromFrame = getFirstRow();
Toshihiro Shimizu 890ddd
	if (toFrame == -1)
Toshihiro Shimizu 890ddd
		toFrame = getMaxFrame();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (format.m_sampleRate == 0) {
Toshihiro Shimizu 890ddd
		// Found the best format inside the soundsequences
Toshihiro Shimizu 890ddd
		int sampleRate = 0;
Toshihiro Shimizu 890ddd
		int bitsPerSample = 8;
Toshihiro Shimizu 890ddd
		int channels = 1;
Toshihiro Shimizu 890ddd
		for (int i = 0; i < levelsCount; i++) {
Toshihiro Shimizu 890ddd
			TXshSoundLevel *soundLevel = m_levels.at(i)->getSoundLevel();
Toshihiro Shimizu 890ddd
			TSoundTrackP soundTrack = soundLevel->getSoundTrack();
Toshihiro Shimizu 890ddd
			if (soundTrack.getPointer() == 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			TSoundTrackFormat f = soundTrack->getFormat();
Toshihiro Shimizu 890ddd
			if ((int)f.m_sampleRate > sampleRate) {
Toshihiro Shimizu 890ddd
				format.m_sampleRate = f.m_sampleRate;
Toshihiro Shimizu 890ddd
				sampleRate = f.m_sampleRate;
Toshihiro Shimizu 890ddd
				format.m_channelCount = f.m_channelCount;
Toshihiro Shimizu 890ddd
				channels = f.m_channelCount;
Toshihiro Shimizu 890ddd
				format.m_signedSample = f.m_signedSample;
Toshihiro Shimizu 890ddd
				format.m_bitPerSample = f.m_bitPerSample;
Toshihiro Shimizu 890ddd
				bitsPerSample = f.m_bitPerSample;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (f.m_channelCount > channels) {
Toshihiro Shimizu 890ddd
				format.m_channelCount = f.m_channelCount;
Toshihiro Shimizu 890ddd
				channels = f.m_channelCount;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (f.m_bitPerSample > bitsPerSample) {
Toshihiro Shimizu 890ddd
				format.m_bitPerSample = f.m_bitPerSample;
Toshihiro Shimizu 890ddd
				bitsPerSample = f.m_bitPerSample;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// If there is no soundSequence I use a default format
Toshihiro Shimizu 890ddd
	if (levelsCount == 0 || format.m_sampleRate == 0) {
Toshihiro Shimizu 890ddd
		format.m_sampleRate = 44100;
Toshihiro Shimizu 890ddd
		format.m_bitPerSample = 16;
Toshihiro Shimizu 890ddd
		format.m_channelCount = 1;
Toshihiro Shimizu 890ddd
		format.m_signedSample = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// We prefer to have 22050 as a maximum sampleRate (to avoid crashes or another issues)
Toshihiro Shimizu 890ddd
	if (format.m_sampleRate >= 44100)
Toshihiro Shimizu 890ddd
		format.m_sampleRate = 22050;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Create the soundTrack
Toshihiro Shimizu 890ddd
	double samplePerFrame = format.m_sampleRate / fps;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// In seconds
Toshihiro Shimizu 890ddd
	double duration = double(toFrame - fromFrame) / fps;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Seconds to Sample  (it is more accurate than using sampleCount
Toshihiro Shimizu 890ddd
	double dsamp = duration * format.m_sampleRate;
Toshihiro Shimizu 890ddd
	TINT32 lsamp = (TINT32)dsamp;
Toshihiro Shimizu 890ddd
	if ((double)lsamp < dsamp - TConsts::epsilon)
Toshihiro Shimizu 890ddd
		lsamp++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TSoundTrackP overallSoundTrack;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		overallSoundTrack = TSoundTrack::create(format, lsamp);
Toshihiro Shimizu 890ddd
	} catch (TSoundDeviceException &) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Blank the whole track
Toshihiro Shimizu 890ddd
	overallSoundTrack->blank(0, lsamp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (levelsCount == 0)
Toshihiro Shimizu 890ddd
		return overallSoundTrack;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < levelsCount; i++) {
Toshihiro Shimizu 890ddd
		ColumnLevel *l = m_levels.at(i);
Toshihiro Shimizu 890ddd
		TXshSoundLevel *soundLevel = l->getSoundLevel();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Check if the soundtrack is inside the frame Range.
Toshihiro Shimizu 890ddd
		int levelStartFrame = l->getStartFrame() + l->getStartOffset();
Toshihiro Shimizu 890ddd
		int levelEndFrame = levelStartFrame + soundLevel->getFrameCount() - l->getStartOffset() - l->getEndOffset();
Toshihiro Shimizu 890ddd
		// If the soundSequence is completely before fromFrame I will skip it
Toshihiro Shimizu 890ddd
		if ((levelStartFrame < fromFrame && levelEndFrame < fromFrame))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		// If the soundSequence is after toFrame I don't check the others (that comes later..)
Toshihiro Shimizu 890ddd
		if ((levelStartFrame > toFrame && levelEndFrame > toFrame))
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Extract the right soundTrack (using the offsets)
Toshihiro Shimizu 890ddd
		// (pay attention to convert it before)
Toshihiro Shimizu 890ddd
		TSoundTrackP s = soundLevel->getSoundTrack();
Toshihiro Shimizu 890ddd
		if (!s)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		TSoundTrackP soundTrack = TSop::convert(s, format);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int s0delta = 0;
Toshihiro Shimizu 890ddd
		if (fromFrame > levelStartFrame)
Toshihiro Shimizu 890ddd
			s0delta = fromFrame - levelStartFrame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int s0 = (l->getStartOffset() + s0delta) * samplePerFrame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int s1delta = 0;
Toshihiro Shimizu 890ddd
		if (toFrame < levelEndFrame)
Toshihiro Shimizu 890ddd
			s1delta = levelEndFrame - toFrame;
Toshihiro Shimizu 890ddd
		int s1 = (soundLevel->getFrameCount() - l->getEndOffset() - s1delta) * samplePerFrame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (s1 > 0 && s1 >= s0) {
Toshihiro Shimizu 890ddd
			soundTrack = soundTrack->extract(s0, s1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// Copy the sound track
Toshihiro Shimizu 890ddd
			overallSoundTrack->copy(soundTrack,
Toshihiro Shimizu 890ddd
									int((levelStartFrame - fromFrame) * samplePerFrame)); // The int cast is IMPORTANT, since
Toshihiro Shimizu 890ddd
		}																				  // there are 2 overloads (int & double)
Toshihiro Shimizu 890ddd
	}																					  // with DIFFERENT SCALES. We mean the
Toshihiro Shimizu 890ddd
																						  // SAMPLES-BASED one.
Toshihiro Shimizu 890ddd
	return overallSoundTrack;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TSoundTrackP TXshSoundColumn::mixingTogether(const std::vector<txshsoundcolumn *=""> &vect, int fromFrame, int toFrame, double fps)</txshsoundcolumn>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int offset = 0xffff;
Toshihiro Shimizu 890ddd
	long sampleCount = 0;
Toshihiro Shimizu 890ddd
	std::vector<tsoundtrackp> tracks;</tsoundtrackp>
Toshihiro Shimizu 890ddd
	TSoundTrackP mix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int size = vect.size(), i = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ColumnLevel *l = vect[0]->getColumnLevel(0);
Toshihiro Shimizu 890ddd
	if (!l)			// May happen if the sound level is not
Toshihiro Shimizu 890ddd
		return mix; // correctly loaded from disk
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TXshSoundLevel *soundLevel = l->getSoundLevel();
Toshihiro Shimizu 890ddd
	assert(soundLevel);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fps == -1)
Toshihiro Shimizu 890ddd
		fps = soundLevel->getFrameRate();
Toshihiro Shimizu 890ddd
	if (fromFrame == -1)
Toshihiro Shimizu 890ddd
		fromFrame = 0;
Toshihiro Shimizu 890ddd
	if (toFrame == -1)
Toshihiro Shimizu 890ddd
		toFrame = getXsheet()->getFrameCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!soundLevel->getSoundTrack())
Toshihiro Shimizu 890ddd
		return mix;
Toshihiro Shimizu 890ddd
	TSoundTrackFormat format = soundLevel->getSoundTrack()->getFormat();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TXshSoundColumn *c = 0;
Toshihiro Shimizu 890ddd
	int j;
Toshihiro Shimizu 890ddd
	for (j = 0; j < size; ++j) {
Toshihiro Shimizu 890ddd
		TXshSoundColumn *oldC = c;
Toshihiro Shimizu 890ddd
		c = vect[j];
Toshihiro Shimizu 890ddd
		if (j == 0) {
Toshihiro Shimizu 890ddd
			mix = c->getOverallSoundTrack(fromFrame, toFrame, fps, format);
Toshihiro Shimizu 890ddd
			TSoundTrackP mixedTrack = TSoundTrack::create(mix->getFormat(), mix->getSampleCount());
Toshihiro Shimizu 890ddd
			mix = TSop::mix(mixedTrack, mix, 1.0, c->getVolume());
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		assert(oldC);
Toshihiro Shimizu 890ddd
		if (c->getVolume() == 0 || c->isEmpty()) {
Toshihiro Shimizu 890ddd
			c = oldC;
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		mix = TSop::mix(mix, c->getOverallSoundTrack(fromFrame, toFrame, fps, format), 1.0, c->getVolume());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Per ora perche mov vuole solo 16 bit
Toshihiro Shimizu 890ddd
	TSoundTrackFormat fmt = mix->getFormat();
Toshihiro Shimizu 890ddd
	if (fmt.m_bitPerSample != 16)
Toshihiro Shimizu 890ddd
		fmt.m_bitPerSample = 16;
Toshihiro Shimizu 890ddd
	mix = TSop::convert(mix, fmt);
Toshihiro Shimizu 890ddd
	return mix;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
PERSIST_IDENTIFIER(TXshSoundColumn, "soundColumn")