Commit message: Fix use-after-free in PlayerGeneric destructor
CVE: CVE-2020-15569
CWE: CWE-416
Vulnerability description: PlayerGeneric.cpp in MilkyTracker through 1.02.00 has a use-after-free in the PlayerGeneric destructor.

Project: MilkyTracker
File: PlayerGeneric.cpp

	PeakAutoAdjustFilter() : 
		mixerShift(0),
		masterVolume(256),
		lastPeakValue(0)
	{
	}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::setBufferSize(mp_uint32 bufferSize)
{
	mp_sint32 res = 0;
	
	this->bufferSize = bufferSize;
	
	if (mixer)
	{
		// If we're told to compensate the samples until we 
		// we reached 2^n buffer sizes
		if (compensateBufferFlag)
		{
			for (mp_uint32 i = 0; i < 16; i++)
			{
				if ((unsigned)(1 << i) >= (unsigned)bufferSize)
				{
					bufferSize = 1 << i;
					break;
				}
			}
		}		
	
		res = mixer->setBufferSize(bufferSize);
	}
	
	return res;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::resumePlaying()
{
	if (player && !player->isPlaying())
		player->resumePlaying();

	if (mixer && mixer->isPaused())
		return mixer->resume();
	else if (mixer && !mixer->isPlaying())
		return mixer->start();

	return MP_OK;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setResamplerType(ResamplerTypes type)
{
	resamplerType = type;
	if (player)
		player->setResamplerType(type);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setDisableMixing(bool b)
{
	disableMixing = b;

	if (player)
		player->setDisableMixing(disableMixing);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getNumMaxVirChannels() const
{
#ifndef MILKYTRACKER
	if (player)
	{
		if (player->getType() == PlayerBase::PlayerType_IT)
		{
			return static_cast<PlayerIT*>(player)->getNumMaxVirChannels();
		}
	}
#endif
	return numMaxVirChannels;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getSongMainVolume() const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		return player->getSongMainVolume(index);
	}
	
	return 255;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::resetOnStop(bool b) 
{
	resetOnStopFlag = b;
	if (player)
		player->resetOnStop(b); 
}

Project: MilkyTracker
File: PlayerGeneric.cpp

PlayerGeneric::PlayModes PlayerGeneric::getPlayMode() const
{
	if (player)
		return player->getPlayMode();
		
	return playMode;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::resetAllSpeed()
{
	if (player)
		player->resetAllSpeed();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::lastPattern()
{
	if (player)
		player->lastPattern();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::adjustSettings()
{
	mp_uint32 bufferSize = mixer->getBufferSize();
	mp_uint32 sampleRate = mixer->getSampleRate();
	
	this->bufferSize = bufferSize;
	this->frequency = sampleRate;
	
	if (player)
	{
		player->setBufferSize(bufferSize);
		player->adjustFrequency(sampleRate);
	}
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::setPowerOfTwoCompensationFlag(bool b)
{
	if (mixer && compensateBufferFlag != b)
	{
		compensateBufferFlag = b;
		setBufferSize(bufferSize);
	}

	return MP_OK;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::enable(PlayModeOptions option, bool b)
{
	ASSERT(option>=PlayModeOptionFirst && option<PlayModeOptionLast);
	options[option] = b;
	
	if (player)
		player->enable(option, b);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getSpeed() const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		return player->getSpeed(index);
	}
		
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPatternToPlay(mp_sint32 patternIndex)
{
	if (player)
		player->setPatternToPlay(patternIndex);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::restart(mp_uint32 startPosition/* = 0*/, mp_uint32 startRow/* = 0*/, bool resetMixer/* = true*/, const mp_ubyte* customPanningTable/* = NULL*/, bool playOneRowOnly/* = false*/)
{
	if (player)
		player->restart(startPosition, startRow, resetMixer, customPanningTable, playOneRowOnly);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_uint32 PlayerGeneric::getSyncSampleCounter() const
{
	if (player)
		return player->getSyncSampleCounter();
		
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::getPosition(mp_sint32& order, mp_sint32& row) const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());	
		player->getPosition(order, row, index);
		return;
	}
	
	order = row = 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setResamplerType(bool interpolation, bool ramping)
{
	if (interpolation)
	{
		if (ramping)
			resamplerType = MIXER_LERPING_RAMPING;
		else
			resamplerType = MIXER_LERPING;
	}
	else
	{
		if (ramping)
			resamplerType = MIXER_NORMAL_RAMPING;
		else
			resamplerType = MIXER_NORMAL;
	}

	if (player)
		player->setResamplerType(resamplerType);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::pausePlaying()
{
	paused = true;
	if (mixer)
		return mixer->pause();
		
	return MP_OK;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getCurrentSample(mp_sint32 position, mp_sint32 channel)
{
	if (mixer)
		return mixer->getCurrentSample(position, channel);
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::resetChannels()
{
	if (player)
		player->resetChannelsFull();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setIdle(bool idle)
{
	this->idle = idle;
	if (player)
		player->setIdle(idle);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

	virtual void mix(mp_sint32* buffer, mp_uint32 bufferSize)
	{
		const mp_sint32* buffer32 = buffer;
		
		for (mp_uint32 i = 0; i < bufferSize*MP_NUMCHANNELS; i++)
		{
			mp_sint32 b = *buffer32++;
			
			if (abs(b) > lastPeakValue)
				lastPeakValue = abs(b);					
		}
	}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::exportToWAV(const SYSCHAR* fileName, XModule* module, 
									 mp_sint32 startOrder/* = 0*/, mp_sint32 endOrder/* = -1*/, 
									 const mp_ubyte* mutingArray/* = NULL*/, mp_uint32 mutingNumChannels/* = 0*/,
									 const mp_ubyte* customPanningTable/* = NULL*/,
									 AudioDriverBase* preferredDriver/* = NULL*/,
									 mp_sint32* timingLUT/* = NULL*/)
{
	PlayerBase* player = NULL;
	
	AudioDriverBase* wavWriter = preferredDriver;
	bool isWAVWriterDriver = false;
	
	if (wavWriter == NULL)
	{
		wavWriter = new WAVWriter(fileName);
		isWAVWriterDriver = true;
	
		if (!static_cast<WAVWriter*>(wavWriter)->isOpen())
		{
			delete wavWriter;
			return MP_DEVICE_ERROR;
		}
	}
	
	MasterMixer mixer(frequency, bufferSize, 1, wavWriter);
	mixer.setSampleShift(sampleShift);
	mixer.setDisableMixing(disableMixing);
	
	player = getPreferredPlayer(module);
	
	PeakAutoAdjustFilter filter;
	if (autoAdjustPeak)
		mixer.setFilterHook(&filter);
		
	if (player)
	{
		player->adjustFrequency(frequency);
		player->resetOnStop(resetOnStopFlag);
		player->setBufferSize(bufferSize);
		player->setResamplerType(resamplerType);
		player->setMasterVolume(masterVolume);
		player->setPlayMode(playMode);
		player->setDisableMixing(disableMixing);
		player->setAllowFilters(allowFilters);		
#ifndef MILKYTRACKER
		if (player->getType() == PlayerBase::PlayerType_IT)
		{
			static_cast<PlayerIT*>(player)->setNumMaxVirChannels(numMaxVirChannels);
		}
#endif
		mixer.addDevice(player);
	}

	if (player)
	{
		if (mutingArray && mutingNumChannels > 0 && mutingNumChannels <= module->header.channum)
		{
			for (mp_uint32 i = 0; i < mutingNumChannels; i++)
				player->muteChannel(i, mutingArray[i] == 1);
		}
		player->startPlaying(module, false, startOrder, 0, -1, customPanningTable, false, -1);
		
		mixer.start();
	}

	if (endOrder == -1 || endOrder < startOrder || endOrder > module->header.ordnum - 1)
		endOrder = module->header.ordnum - 1;		

	mp_sint32 curOrderPos = startOrder;
	if (timingLUT)
	{
		for (mp_sint32 i = 0; i < module->header.ordnum; i++)
			timingLUT[i] = -1;
		
		timingLUT[curOrderPos] = 0;
	}

	while (!player->hasSongHalted() && player->getOrder(0) <= endOrder)
	{
		wavWriter->advance();

		if (player->getOrder(0) != curOrderPos)
		{
#ifdef __VERBOSE__
			printf("%f\n", (float)wavWriter->getNumPlayedSamples() / (float)getMixFrequency());
#endif
			curOrderPos = player->getOrder(0);
			if (timingLUT && curOrderPos < module->header.ordnum && timingLUT[curOrderPos] == -1)
				timingLUT[curOrderPos] = wavWriter->getNumPlayedSamples();			
		}
	}

	player->stopPlaying();
	
	mixer.stop();
	// important step, otherwise destruction of the audio driver will cause
	// trouble if the mixer instance is removed from this function's stack 
	// and trys to access the driver which is no longer existant
	mixer.closeAudioDevice();

	// Sync value
	sampleShift = mixer.getSampleShift();
	filter.mixerShift = sampleShift;
	filter.calculateMasterVolume();
	masterVolume = filter.masterVolume;

	delete player;

	mp_sint32 numWrittenSamples = wavWriter->getNumPlayedSamples();
	
	if (isWAVWriterDriver)
		delete wavWriter;

	return numWrittenSamples;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getTempo() const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		return player->getTempo(index);
	}
		
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

	MixerNotificationListener(PlayerGeneric& player) :
		player(player)
	{
	}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isRepeating() const
{
	if (player)
		return player->isRepeating();
		
	return repeat;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::beatPacketsToBufferSize(mp_uint32 numBeats)
{
	return ChannelMixer::beatPacketsToBufferSize(getMixFrequency(), numBeats);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getNumActiveChannels() const
{
	if (player)
		return player->getNumActiveChannels();

	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isEnabled(PlayModeOptions option) const
{
	ASSERT(option>=PlayModeOptionFirst && option<PlayModeOptionLast);
	
	if (!player)
		return options[option];
	else 
		return player->isEnabled(option);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getNumAllocatedChannels() const
{
	if (player)
		return player->getNumAllocatedChannels();

	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getOrder() const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		return player->getOrder(index);
	}
		
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

PlayerBase::PlayerTypes PlayerGeneric::getPreferredPlayerType(XModule* module)
{
	if (module == NULL)
		return PlayerBase::PlayerType_INVALID;
		
	switch (module->getType())
	{
		case XModule::ModuleType_669:
		case XModule::ModuleType_FAR:
#ifndef MILKYTRACKER
			return PlayerBase::PlayerType_FAR;
			break;
#endif
		case XModule::ModuleType_IT:
#ifndef MILKYTRACKER
			return PlayerBase::PlayerType_IT;
			break;
#endif
		case XModule::ModuleType_UNKNOWN: // just assume our standard player can handle this
		//case XModule::ModuleType_669:
		case XModule::ModuleType_AMF:
		case XModule::ModuleType_AMS:
		case XModule::ModuleType_CBA:
		case XModule::ModuleType_DBM:
		case XModule::ModuleType_DIGI:
		case XModule::ModuleType_DSM:
		case XModule::ModuleType_DSm:
		case XModule::ModuleType_DTM_1:
		case XModule::ModuleType_DTM_2:
		case XModule::ModuleType_GDM:
		case XModule::ModuleType_GMC:
		case XModule::ModuleType_IMF:
		case XModule::ModuleType_MDL:
		case XModule::ModuleType_MOD:
		case XModule::ModuleType_MTM:
		case XModule::ModuleType_MXM:
		case XModule::ModuleType_OKT:
		case XModule::ModuleType_PLM:
		case XModule::ModuleType_PSM:
		case XModule::ModuleType_PTM:
		case XModule::ModuleType_S3M:
		case XModule::ModuleType_STM:
		case XModule::ModuleType_SFX:
		case XModule::ModuleType_UNI:
		case XModule::ModuleType_ULT:
		case XModule::ModuleType_XM:
		case XModule::ModuleType_NONE:
			return PlayerBase::PlayerType_Generic;
			break;
			
		default:
			return PlayerBase::PlayerType_INVALID;
	}
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::getAllowFilters() const
{
	if (player)
		return player->getAllowFilters();
		
	return allowFilters;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setNumMaxVirChannels(mp_sint32 max)
{
	numMaxVirChannels = max;
#ifndef MILKYTRACKER
	if (player)
	{
		if (player->getType() == PlayerBase::PlayerType_IT)
		{
			static_cast<PlayerIT*>(player)->setNumMaxVirChannels(max);
		}
	}
#endif
}

Project: MilkyTracker
File: PlayerGeneric.cpp

PlayerGeneric::PlayerGeneric(mp_sint32 frequency, AudioDriverInterface* audioDriver/* = NULL*/) :
	mixer(NULL),
	player(NULL),
	frequency(frequency),
	audioDriver(audioDriver),
	audioDriverName(NULL)
{
	listener = new MixerNotificationListener(*this);

	bufferSize = 0;
	sampleShift = 0;
	
	resamplerType = MIXER_NORMAL;

	idle = false;
	playOneRowOnly = false;
	paused = false;
	repeat = false;
	resetOnStopFlag = false;
	autoAdjustPeak = false;
	disableMixing = false;
	allowFilters = false;
#ifdef __FORCEPOWEROFTWOBUFFERSIZE__
	compensateBufferFlag = true;
#else
	compensateBufferFlag = false;
#endif
	masterVolume = panningSeparation = numMaxVirChannels = 256;
	resetMainVolumeOnStartPlayFlag = true;
	playMode = PlayMode_Auto;

	// Special playmode settings
	options[PlayModeOptionPanning8xx] = true;
	options[PlayModeOptionPanningE8x] = false;
	options[PlayModeOptionForcePTPitchLimit] = true;

	AudioDriverManager audioDriverManager;
	const char* defaultName = audioDriverManager.getPreferredAudioDriver()->getDriverID();
	if (defaultName)
	{
		audioDriverName = new char[strlen(defaultName)+1];
		strcpy(audioDriverName, defaultName);
	}
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isIdle() const
{
	if (player)
		return player->isIdle();
		
	return idle;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

PlayerGeneric::~PlayerGeneric()
{

	if (player)
	{
		if (mixer && mixer->isActive() && !mixer->isDeviceRemoved(player))
			mixer->removeDevice(player);
		delete player;
	}
	
	if (mixer)
		delete mixer;

	delete[] audioDriverName;
	
	delete listener;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPeakAutoAdjust(bool b)
{
	this->autoAdjustPeak = b;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getLastUnvisitedPosition() const
{
	if (player)
		return player->getLastUnvisitedPosition();
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setAllowFilters(bool b)
{
	allowFilters = b;

	if (player)
		player->setAllowFilters(allowFilters);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::grabChannelInfo(mp_sint32 chn, TPlayerChannelInfo& channelInfo) const
{
	if (player)
		return player->grabChannelInfo(chn, channelInfo);
		
	return false;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::adjustFrequency(mp_uint32 frequency)
{
	this->frequency = frequency;
	
	mp_sint32 res = MP_OK;
	
	if (mixer)
		res = mixer->setSampleRate(frequency);
	
	return res;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getPanningSeparation() const
{
	if (player)
		return player->getPanningSeparation();
		
	return panningSeparation;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPanningSeparation(mp_sint32 separation)
{
	panningSeparation = separation;
	if (player)
		player->setPanningSeparation(separation);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::resetMainVolumeOnStartPlay(bool b)
{
	resetMainVolumeOnStartPlayFlag = b;
	if (player)
		player->resetMainVolumeOnStartPlay(b); 
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::hasSongHalted() const
{
	if (player)
		return player->hasSongHalted();
		
	return true;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setMasterVolume(mp_sint32 vol)
{
	masterVolume = vol;
	if (player)
		player->setMasterVolume(vol);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

ChannelMixer::ResamplerTypes PlayerGeneric::getResamplerType() const
{
	if (player)
		return player->getResamplerType();
		
	return resamplerType;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getSampleShift() const
{
	if (mixer)
		return mixer->getSampleShift();
	
	return sampleShift;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getMasterVolume() const
{
	if (player)
		return player->getMasterVolume();
		
	return masterVolume;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_int64 PlayerGeneric::getSampleCounter() const
{
	if (player)
		return player->getSampleCounter();
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::adjustBufferSize(mp_uint32 numBeats)
{
	return setBufferSize(beatPacketsToBufferSize(numBeats));
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setSampleShift(mp_sint32 shift)
{
	sampleShift = shift;
	if (mixer)
		mixer->setSampleShift(shift);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getCurrentSamplePosition() const
{
	if (mixer && mixer->getAudioDriver())
		return mixer->getAudioDriver()->getBufferPos();
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isPlaying() const
{
	if (mixer)
		return mixer->isPlaying();
		
	return false;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

PlayerBase*	PlayerGeneric::getPreferredPlayer(XModule* module) const
{
	switch (getPreferredPlayerType(module))
	{
#ifndef MILKYTRACKER
		case PlayerBase::PlayerType_FAR:
			return new PlayerFAR(frequency);
		case PlayerBase::PlayerType_IT:
			return new PlayerIT(frequency);
#endif
		case PlayerBase::PlayerType_Generic:
			return new PlayerSTD(frequency);
			
		default:
			return NULL;
	}
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getMixFrequency() const
{
	if (player)
		return player->getMixFrequency();
		
	return frequency;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::setCurrentAudioDriverByName(const char* name)
{
	if (name == NULL)
		return false;

	if (mixer)
	{
		bool res = mixer->setCurrentAudioDriverByName(name);

		if (audioDriverName)
			delete[] audioDriverName;

		const char* curDrvName = getCurrentAudioDriverName();
		ASSERT(curDrvName);
		audioDriverName = new char[strlen(curDrvName)+1];
		strcpy(audioDriverName, curDrvName);
		return res;
	}

	AudioDriverManager audioDriverManager;
	if (audioDriverManager.getAudioDriverByName(name))
	{
		if (audioDriverName)
			delete[] audioDriverName;

		audioDriverName = new char[strlen(name)+1];
		strcpy(audioDriverName, name);		
		return true;
	}

	return false;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

const char*	PlayerGeneric::getCurrentAudioDriverName() const
{
	if (mixer)
		return mixer->getCurrentAudioDriverName();

	return audioDriverName;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getCurrentSamplePeak(mp_sint32 position, mp_sint32 channel)
{
	if (mixer)
		return mixer->getCurrentSamplePeak(position, channel);

	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPlayMode(PlayModes mode)
{
	playMode = mode;
	if (player)
		player->setPlayMode(mode);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::stopPlaying()
{
	if (player)
		player->stopPlaying();

	if (mixer)
		return mixer->stop();
		
	return MP_OK;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_int64 PlayerGeneric::getSyncCount() const
{
	if (player)
		return player->getSyncCount();
		
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getRow() const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		return player->getRow(index);
	}
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

	virtual void masterMixerNotification(MasterMixer::MasterMixerNotifications notification)
	{
		player.adjustSettings();
	}

Project: MilkyTracker
File: PlayerGeneric.cpp

	void calculateMasterVolume()
	{
		if (lastPeakValue)
		{
			float v = 32768.0f*(1<<mixerShift) / (float)lastPeakValue;
			masterVolume = (mp_sint32)((float)masterVolume*v);
			if (masterVolume > 256)
				masterVolume = 256;
		}
	}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setRepeat(bool repeat)
{
	this->repeat = repeat;
	if (player)
		player->setRepeat(repeat);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::reset()
{
	if (player)
		player->reset();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isInitialized() const
{
	if (mixer)
		return mixer->isInitialized();
		
	return false;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPanning(mp_ubyte chn, mp_ubyte pan)
{
	if (player)
		player->setPanning(chn, pan);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::startPlaying(XModule* module, 
									  bool repeat/* = false*/, 
									  mp_uint32 startPosition/* = 0*/, 
									  mp_uint32 startRow/* = 0*/,
									  mp_sint32 numChannels/* = -1*/, 
									  const mp_ubyte* customPanningTable/* = NULL*/,
									  bool idle/* = false*/,
									  mp_sint32 patternIndex/* = -1*/,
									  bool playOneRowOnly/* = false*/)
{
	this->idle = idle;
	this->repeat = repeat;
	this->playOneRowOnly = playOneRowOnly;

	if (mixer == NULL)
	{
		mixer = new MasterMixer(frequency, bufferSize, 1, audioDriver);
		mixer->setMasterMixerNotificationListener(listener);
		mixer->setSampleShift(sampleShift);
		if (audioDriver == NULL)
			mixer->setCurrentAudioDriverByName(audioDriverName);
	}

	if (!player || player->getType() != getPreferredPlayerType(module))
	{
		if (player)
		{
			if (!mixer->isDeviceRemoved(player))
				mixer->removeDevice(player);
			delete player;
		}
		
		player = getPreferredPlayer(module);
		
		if (player)
		{
			// apply our own "state" to the state of the newly allocated player
			player->resetMainVolumeOnStartPlay(resetMainVolumeOnStartPlayFlag);
			player->resetOnStop(resetOnStopFlag);
			player->setBufferSize(bufferSize);
			player->setResamplerType(resamplerType);
			player->setMasterVolume(masterVolume);
			player->setPanningSeparation(panningSeparation);
			player->setPlayMode(playMode);

			for (mp_sint32 i = PlayModeOptionFirst; i < PlayModeOptionLast; i++)
				player->enable((PlayModeOptions)i, options[i]);			
			
			player->setDisableMixing(disableMixing);
			player->setAllowFilters(allowFilters);
			//if (paused)
			//	player->pausePlaying();

			// adjust number of virtual channels if necessary
			setNumMaxVirChannels(numMaxVirChannels);
		}		
	}
	
	if (player && mixer)
	{
		if (!mixer->isDeviceRemoved(player))
			mixer->removeDevice(player);
			
		player->startPlaying(module, repeat, startPosition, startRow, numChannels, customPanningTable, idle, patternIndex, playOneRowOnly);
		
		mixer->addDevice(player);
		
		if (!mixer->isPlaying())
			return mixer->start();
	}


	return MP_OK;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::isPaused() const
{
	if (mixer)
		return mixer->isPaused();
		
	return paused;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::nextPattern()
{
	if (player)
		player->nextPattern();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::resetSampleCounter()
{
	if (player)
		player->resetSampleCounter();
}

Project: MilkyTracker
File: PlayerGeneric.cpp

bool PlayerGeneric::getPowerOfTwoCompensationFlag() const
{
	return compensateBufferFlag;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

mp_sint32 PlayerGeneric::getCurrentBeatIndex()
{
	if (player)
		return player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
	
	return 0;
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::setPatternPos(mp_uint32 pos, mp_uint32 row/* = 0*/, bool resetChannels/* = true*/, bool resetFXMemory/* = true*/)
{
	if (player)
		player->setPatternPos(pos, row, resetChannels, resetFXMemory);
}

Project: MilkyTracker
File: PlayerGeneric.cpp

void PlayerGeneric::getPosition(mp_sint32& order, mp_sint32& row, mp_sint32& ticker) const
{
	if (player)
	{
		mp_uint32 index = player->getBeatIndexFromSamplePos(getCurrentSamplePosition());
		player->getPosition(order, row, ticker, index);
		return;
	}
	
	order = row = ticker = 0;
}