shithub: ft²

ref: 60a3dd36bb82179a0768a4ebeb4f7c632cdd59bf
dir: /src/ft2_audio.c/

View raw version
// for finding memory leaks in debug mode with Visual Studio
#if defined _DEBUG && defined _MSC_VER
#include <crtdbg.h>
#endif

#include <stdio.h>
#include <stdint.h>
#include "ft2_header.h"
#include "ft2_config.h"
#include "ft2_scopes.h"
#include "ft2_video.h"
#include "ft2_gui.h"
#include "ft2_midi.h"
#include "ft2_wav_renderer.h"
#include "ft2_tables.h"
#include "ft2_structs.h"
// --------------------------------
#include "mixer/ft2_mix.h"
#include "mixer/ft2_center_mix.h"
#include "mixer/ft2_silence_mix.h"
// --------------------------------

#define INITIAL_DITHER_SEED 0x12345000

static int8_t pmpCountDiv, pmpChannels = 2;
static uint16_t smpBuffSize;
static int32_t masterVol, oldAudioFreq, randSeed = INITIAL_DITHER_SEED;
static int32_t prngStateL, prngStateR;
static uint32_t tickTimeLen, tickTimeLenFrac;
static double dAudioAmpMul, dPanningTab[256+1];
static voice_t voice[MAX_VOICES * 2];
static void (*sendAudSamplesFunc)(uint8_t *, uint32_t, uint8_t); // "send mixed samples" routines

static int32_t oldPeriod;
static uint32_t oldSFrqRev;
#if !defined __amd64__ && !defined _WIN64
static uint32_t oldSFrq;
#else
static uint64_t oldSFrq;
#endif

// globalized
audio_t audio;
pattSyncData_t *pattSyncEntry;
chSyncData_t *chSyncEntry;
chSync_t chSync;
pattSync_t pattSync;
volatile bool pattQueueClearing, chQueueClearing;

void resetCachedMixerVars(void)
{
	oldPeriod = -1;
	oldSFrq = 0;
	oldSFrqRev = 0xFFFFFFFF;
}

void stopVoice(int32_t i)
{
	voice_t *v;

	v = &voice[i];
	memset(v, 0, sizeof (voice_t));
	v->SPan = 128;

	// clear "fade out" voice too

	v = &voice[MAX_VOICES + i];
	memset(v, 0, sizeof (voice_t));
	v->SPan = 128;
}

bool setNewAudioSettings(void) // only call this from the main input/video thread
{
	uint32_t stringLen;

	pauseAudio();

	if (!setupAudio(CONFIG_HIDE_ERRORS))
	{
		// set back old known working settings

		config.audioFreq = audio.lastWorkingAudioFreq;
		config.specialFlags &= ~(BITDEPTH_16 + BITDEPTH_32 + BUFFSIZE_512 + BUFFSIZE_1024 + BUFFSIZE_2048);
		config.specialFlags |= audio.lastWorkingAudioBits;

		if (audio.lastWorkingAudioDeviceName != NULL)
		{
			if (audio.currOutputDevice != NULL)
			{
				free(audio.currOutputDevice);
				audio.currOutputDevice = NULL;
			}

			stringLen = (uint32_t)strlen(audio.lastWorkingAudioDeviceName);

			audio.currOutputDevice = (char *)malloc(stringLen + 2);
			if (audio.currOutputDevice != NULL)
			{
				strcpy(audio.currOutputDevice, audio.lastWorkingAudioDeviceName);
				audio.currOutputDevice[stringLen + 1] = '\0'; // UTF-8 needs double null termination
			}
		}

		// also update config audio radio buttons if we're on that screen at the moment
		if (ui.configScreenShown && editor.currConfigScreen == CONFIG_SCREEN_IO_DEVICES)
			setConfigIORadioButtonStates();

		// if it didn't work to use the old settings again, then something is seriously wrong...
		if (!setupAudio(CONFIG_HIDE_ERRORS))
			okBox(0, "System message", "Couldn't find a working audio mode... You'll get no sound / replayer timer!");

		resumeAudio();
		return false;
	}

	resumeAudio();
	return true;
}

// ampFactor = 1..32, masterVol = 0..256
void setAudioAmp(int16_t ampFactor, int16_t master, bool bitDepth32Flag)
{
	ampFactor = CLAMP(ampFactor, 1, 32);
	master = CLAMP(master, 0, 256);

	const double dAudioNorm = 1.0 / (1UL << 25); // 2^25 = internal voice mixing range (per voice)

	if (bitDepth32Flag)
	{
		// 32-bit floating point mixing mode
		dAudioAmpMul = dAudioNorm * (master / 256.0) * (ampFactor / 32.0);
	}
	else
	{
		// 16-bit integer mixing mode
		masterVol = (master * ampFactor) * 512;
	}
}

void setNewAudioFreq(uint32_t freq) // for song to WAV rendering
{
	if (freq == 0)
		return;

	oldAudioFreq = audio.freq;
	audio.freq = freq;

	calcReplayRate(audio.freq);
}

void setBackOldAudioFreq(void) // for song to WAV rendering
{
	audio.freq = oldAudioFreq;
	calcReplayRate(audio.freq);
}

void setSpeed(uint16_t bpm)
{
	if (bpm == 0)
		return;

	// non-FT2 check for security
	if (bpm > MAX_BPM)
		return;

	audio.dSamplesPerTick = audio.dSpeedValTab[bpm];
	audio.samplesPerTick = (int32_t)(audio.dSamplesPerTick + 0.5);

	// get tick time length for audio/video sync timestamp
	const uint64_t tickTimeLen64 = audio.tickTimeLengthTab[bpm];
	tickTimeLen = tickTimeLen64 >> 32;
	tickTimeLenFrac = tickTimeLen64 & 0xFFFFFFFF;

	// used for calculating volume ramp length for "ticks" ramps
	audio.rampSpeedValMul = audio.rampSpeedValMulTab[bpm];
}

void audioSetVolRamp(bool volRamp)
{
	lockMixerCallback();
	audio.volumeRampingFlag = volRamp;
	unlockMixerCallback();
}

void audioSetInterpolation(bool interpolation)
{
	lockMixerCallback();
	audio.interpolationFlag = interpolation;
	unlockMixerCallback();
}

void calcPanningTable(void)
{
	// same formula as FT2's panning table (with 0.0..1.0 range)
	for (int32_t i = 0; i <= 256; i++)
		dPanningTab[i] = sqrt(i * (1.0 / 256.0));
}

static void voiceUpdateVolumes(int32_t i, uint8_t status)
{
	int32_t destVolL, destVolR;

	voice_t *v = &voice[i];

	const int32_t volR = (int32_t)((v->SVol * dPanningTab[    v->SPan]) + 0.5);
	const int32_t volL = (int32_t)((v->SVol * dPanningTab[256-v->SPan]) + 0.5);

	if (!audio.volumeRampingFlag) // is volume ramping disabled?
	{
		v->SVolIPLen = 0; // reset volume ramp length
		v->SLVol2 = volL;
		v->SRVol2 = volR;
	}
	else
	{
		v->SLVol1 = volL; // ramp destination volumes
		v->SRVol1 = volR;

		if (status & IS_NyTon)
		{
			// sample is about to start, ramp out/in at the same time

			// setup "fade out" voice (only if current voice volume>0)
			if (v->SLVol2 > 0 || v->SRVol2 > 0)
			{
				voice_t *f = &voice[MAX_VOICES + i];

				*f = *v; // copy voice

				f->SVolIPLen = audio.quickVolSizeVal;

				destVolL = -f->SLVol2;
				destVolR = -f->SRVol2;

				f->SLVolIP = ((int64_t)destVolL * audio.rampQuickVolMul) >> 32; // x = a / b
				f->SRVolIP = ((int64_t)destVolR * audio.rampQuickVolMul) >> 32;

				f->isFadeOutVoice = true;
			}

			// make current voice fade in from zero when it starts
			v->SLVol2 = 0;
			v->SRVol2 = 0;
		}

		// ramp volume changes

		/* FT2 has two internal volume ramping lengths:
		** IS_QuickVol: 5ms (audioFreq / 200)
		** Normal: The duration of a tick (speedVal)
		*/

		// if destination volume and current volume is the same (and we have no sample trigger), don't do ramp
		if (volL == v->SLVol2 && volR == v->SRVol2 && !(status & IS_NyTon))
		{
			v->SVolIPLen = 0; // there is no volume change
		}
		else
		{
			destVolL = volL - v->SLVol2;
			destVolR = volR - v->SRVol2;

			if (status & IS_QuickVol)
			{
				v->SVolIPLen = audio.quickVolSizeVal;
				v->SLVolIP = ((int64_t)destVolL * audio.rampQuickVolMul) >> 32; // x = a / b
				v->SRVolIP = ((int64_t)destVolR * audio.rampQuickVolMul) >> 32;
			}
			else
			{
				v->SVolIPLen = audio.samplesPerTick;
				v->SLVolIP = ((int64_t)destVolL * audio.rampSpeedValMul) >> 32; // x = a / b
				v->SRVolIP = ((int64_t)destVolR * audio.rampSpeedValMul) >> 32;
			}
		}
	}
}

static void voiceTrigger(int32_t i, sampleTyp *s, int32_t position)
{
	bool sampleIs16Bit;
	uint8_t loopType;
	int32_t oldSLen, length, loopBegin, loopLength;
	voice_t *v;

	v = &voice[i];

	length = s->len;
	loopBegin = s->repS;
	loopLength = s->repL;
	loopType = s->typ & 3;
	sampleIs16Bit = (s->typ >> 4) & 1;

	if (sampleIs16Bit)
	{
		assert(!(length & 1));
		assert(!(loopBegin & 1));
		assert(!(loopLength & 1));

		length >>= 1;
		loopBegin >>= 1;
		loopLength >>= 1;
	}

	if (s->pek == NULL || length < 1)
	{
		v->active = false; // shut down voice (illegal parameters)
		return;
	}

	if (loopLength < 1) // disable loop if loopLength is below 1
		loopType = 0;

	if (sampleIs16Bit)
	{
		v->SBase16 = (const int16_t *)s->pek;
		v->SRevBase16 = &v->SBase16[loopBegin + (loopBegin + loopLength)]; // for pingpong loops
	}
	else
	{
		v->SBase8 = s->pek;
		v->SRevBase8 = &v->SBase8[loopBegin + (loopBegin + loopLength)]; // for pingpong loops
	}

	v->backwards = false;
	v->SLoopType = loopType;
	v->SLen = (loopType > 0) ? (loopBegin + loopLength) : length;
	v->SRepS = loopBegin;
	v->SRepL = loopLength;
	v->SPos = position;
	v->SPosDec = 0; // position fraction

	// if 9xx position overflows, shut down voice
	oldSLen = (loopType > 0) ? (loopBegin + loopLength) : length;
	if (v->SPos >= oldSLen)
	{
		v->active = false;
		return;
	}

	v->mixFuncOffset = (sampleIs16Bit * 6) + (audio.interpolationFlag * 3) + loopType;
	v->active = true;
}

void mix_SaveIPVolumes(void) // for volume ramping
{
	voice_t *v = voice;
	for (int32_t i = 0; i < song.antChn; i++, v++)
	{
		v->SLVol2 = v->SLVol1;
		v->SRVol2 = v->SRVol1;
		v->SVolIPLen = 0;
	}
}

void mix_UpdateChannelVolPanFrq(void)
{
	uint8_t status;
	stmTyp *ch;
	voice_t *v;

	ch = stm;
	v = voice;

	for (int32_t i = 0; i < song.antChn; i++, ch++, v++)
	{
		status = ch->tmpStatus = ch->status; // (tmpStatus is used for audio/video sync queue)
		if (status == 0) continue; // nothing to do
		ch->status = 0;

		if (status & IS_Vol)
			v->SVol = ch->finalVol;

		if (status & IS_Pan)
			v->SPan = ch->finalPan;

		if (status & (IS_Vol + IS_Pan))
			voiceUpdateVolumes(i, status);

		if (status & IS_Period)
		{
			// use cached values if possible

			const uint16_t period = ch->finalPeriod;
			if (period != oldPeriod)
			{
				oldPeriod = period;
				oldSFrq = getMixerDelta(period);

				oldSFrqRev = 0xFFFFFFFF;
				if (oldSFrq != 0)
				{
#if defined __amd64__ || defined _WIN64
					oldSFrqRev = (uint32_t)((0xFFFFFFFFFFFFFFFF / oldSFrq) >> 16);
#else
					oldSFrqRev /= oldSFrq;
#endif
				}
			}

			v->SFrq = oldSFrq;
			v->SFrqRev = oldSFrqRev;
		}

		if (status & IS_NyTon)
			voiceTrigger(i, ch->smpPtr, ch->smpStartPos);
	}
}

void resetAudioDither(void)
{
	randSeed = INITIAL_DITHER_SEED;
	prngStateL = 0;
	prngStateR = 0;
}

static inline uint32_t random32(void)
{
	// LCG 32-bit random
	randSeed *= 134775813;
	randSeed++;

	return randSeed;
}

static void sendSamples16BitStereo(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	int32_t out32;

	int16_t *streamPointer16 = (int16_t *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel
		out32 = ((int64_t)audio.mixBufferL[i] * masterVol) >> 32;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// right channel
		out32 = ((int64_t)audio.mixBufferR[i] * masterVol) >> 32;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;
	}

	(void)numAudioChannels;
}

static void sendSamples16BitMultiChan(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	int32_t out32;

	int16_t *streamPointer16 = (int16_t *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel
		out32 = ((int64_t)audio.mixBufferL[i] * masterVol) >> 32;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// right channel
		out32 = ((int64_t)audio.mixBufferR[i] * masterVol) >> 32;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// send zeroes to the rest of the channels
		for (uint32_t j = 2; j < numAudioChannels; j++)
			*streamPointer16++ = 0;
	}
}

static void sendSamples16BitDitherStereo(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	int32_t prng, out32;

	int16_t *streamPointer16 = (int16_t *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel - 1-bit triangular dithering
		prng = random32();
		out32 = ((((int64_t)audio.mixBufferL[i] * masterVol) + prng) - prngStateL) >> 32;
		prngStateL = prng;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// right channel - 1-bit triangular dithering
		prng = random32();
		out32 = ((((int64_t)audio.mixBufferR[i] * masterVol) + prng) - prngStateR) >> 32;
		prngStateR = prng;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;
	}

	(void)numAudioChannels;
}

static void sendSamples16BitDitherMultiChan(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	int32_t prng, out32;

	int16_t *streamPointer16 = (int16_t *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel - 1-bit triangular dithering
		prng = random32();
		out32 = ((((int64_t)audio.mixBufferL[i] * masterVol) + prng) - prngStateL) >> 32;
		prngStateL = prng;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// right channel - 1-bit triangular dithering
		prng = random32();
		out32 = ((((int64_t)audio.mixBufferR[i] * masterVol) + prng) - prngStateR) >> 32;
		prngStateR = prng;
		CLAMP16(out32);
		*streamPointer16++ = (int16_t)out32;

		// send zeroes to the rest of the channels
		for (uint32_t j = 2; j < numAudioChannels; j++)
			*streamPointer16++ = 0;
	}
}

static void sendSamples32BitStereo(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	double dOut;

	float *fStreamPointer32 = (float *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel
		dOut = audio.mixBufferL[i] * dAudioAmpMul;
		dOut = CLAMP(dOut, -1.0, 1.0);
		*fStreamPointer32++ = (float)dOut;

		// right channel
		dOut = audio.mixBufferR[i] * dAudioAmpMul;
		dOut = CLAMP(dOut, -1.0, 1.0);
		*fStreamPointer32++ = (float)dOut;
	}

	(void)numAudioChannels;
}

static void sendSamples32BitMultiChan(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	double dOut;

	float *fStreamPointer32 = (float *)stream;
	for (uint32_t i = 0; i < sampleBlockLength; i++)
	{
		// left channel
		dOut = audio.mixBufferL[i] * dAudioAmpMul;
		dOut = CLAMP(dOut, -1.0, 1.0);
		*fStreamPointer32++ = (float)dOut;

		// right channel
		dOut = audio.mixBufferR[i] * dAudioAmpMul;
		dOut = CLAMP(dOut, -1.0, 1.0);
		*fStreamPointer32++ = (float)dOut;

		// send zeroes to the rest of the channels
		for (uint32_t j = 2; j < numAudioChannels; j++)
			*fStreamPointer32++ = 0.0f;
	}
}

static void doChannelMixing(int32_t samplesToMix)
{
	voice_t *v = voice; // normal voices
	voice_t *r = &voice[MAX_VOICES]; // volume ramp fadeout-voices

	for (int32_t i = 0; i < song.antChn; i++, v++, r++)
	{
		if (v->active)
		{
			bool centerMixFlag;

			const bool volRampFlag = v->SVolIPLen > 0;
			if (volRampFlag)
			{
				centerMixFlag = (v->SLVol1 == v->SRVol1) && (v->SLVolIP == v->SRVolIP);
			}
			else
			{
				if (v->SLVol2 == 0 && v->SRVol2 == 0)
				{ 
					silenceMixRoutine(v, samplesToMix);
					continue;
				}

				centerMixFlag = v->SLVol2 == v->SRVol2;
			}

			mixFuncTab[(centerMixFlag * 24) + (volRampFlag * 12) + v->mixFuncOffset](v, samplesToMix);
		}

		if (r->active) // volume ramp fadeout-voice
		{
			const bool centerMixFlag = (r->SLVol1 == r->SRVol1) && (r->SLVolIP == r->SRVolIP);
			mixFuncTab[(centerMixFlag * 24) + 12 + r->mixFuncOffset](r, samplesToMix);
		}
	}
}

static void mixAudio(uint8_t *stream, uint32_t sampleBlockLength, uint8_t numAudioChannels)
{
	assert(sampleBlockLength <= MAX_WAV_RENDER_SAMPLES_PER_TICK);
	memset(audio.mixBufferL, 0, sampleBlockLength * sizeof (int32_t));
	memset(audio.mixBufferR, 0, sampleBlockLength * sizeof (int32_t));

	doChannelMixing(sampleBlockLength);

	// normalize mix buffer and send to audio stream
	sendAudSamplesFunc(stream, sampleBlockLength, numAudioChannels);
}

// used for song-to-WAV renderer
void mixReplayerTickToBuffer(uint32_t samplesToMix, uint8_t *stream, uint8_t bitDepth)
{
	assert(samplesToMix <= MAX_WAV_RENDER_SAMPLES_PER_TICK);
	memset(audio.mixBufferL, 0, samplesToMix * sizeof (int32_t));
	memset(audio.mixBufferR, 0, samplesToMix * sizeof (int32_t));

	doChannelMixing(samplesToMix);

	// normalize mix buffer and send to audio stream
	if (bitDepth == 16)
	{
		if (config.specialFlags2 & DITHERED_AUDIO)
			sendSamples16BitDitherStereo(stream, samplesToMix, 2);
		else
			sendSamples16BitStereo(stream, samplesToMix, 2);
	}
	else
	{
		sendSamples32BitStereo(stream, samplesToMix, 2);
	}
}

int32_t pattQueueReadSize(void)
{
	while (pattQueueClearing);

	if (pattSync.writePos > pattSync.readPos)
		return pattSync.writePos - pattSync.readPos;
	else if (pattSync.writePos < pattSync.readPos)
		return pattSync.writePos - pattSync.readPos + SYNC_QUEUE_LEN + 1;
	else
		return 0;
}

int32_t pattQueueWriteSize(void)
{
	int32_t size;

	if (pattSync.writePos > pattSync.readPos)
	{
		size = pattSync.readPos - pattSync.writePos + SYNC_QUEUE_LEN;
	}
	else if (pattSync.writePos < pattSync.readPos)
	{
		pattQueueClearing = true;

		/* Buffer is full, reset the read/write pos. This is actually really nasty since
		** read/write are two different threads, but because of timestamp validation it
		** shouldn't be that dangerous.
		** It will also create a small visual stutter while the buffer is getting filled,
		** though that is barely noticable on normal buffer sizes, and it takes a minute
		** or two at max BPM between each time (when queue size is default, 4095)
		*/
		pattSync.data[0].timestamp = 0;
		pattSync.readPos = 0;
		pattSync.writePos = 0;

		size = SYNC_QUEUE_LEN;

		pattQueueClearing = false;
	}
	else
	{
		size = SYNC_QUEUE_LEN;
	}

	return size;
}

bool pattQueuePush(pattSyncData_t t)
{
	if (!pattQueueWriteSize())
		return false;

	assert(pattSync.writePos <= SYNC_QUEUE_LEN);
	pattSync.data[pattSync.writePos] = t;
	pattSync.writePos = (pattSync.writePos + 1) & SYNC_QUEUE_LEN;

	return true;
}

bool pattQueuePop(void)
{
	if (!pattQueueReadSize())
		return false;

	pattSync.readPos = (pattSync.readPos + 1) & SYNC_QUEUE_LEN;
	assert(pattSync.readPos <= SYNC_QUEUE_LEN);

	return true;
}

pattSyncData_t *pattQueuePeek(void)
{
	if (!pattQueueReadSize())
		return NULL;

	assert(pattSync.readPos <= SYNC_QUEUE_LEN);
	return &pattSync.data[pattSync.readPos];
}

uint64_t getPattQueueTimestamp(void)
{
	if (!pattQueueReadSize())
		return 0;

	assert(pattSync.readPos <= SYNC_QUEUE_LEN);
	return pattSync.data[pattSync.readPos].timestamp;
}

int32_t chQueueReadSize(void)
{
	while (chQueueClearing);

	if (chSync.writePos > chSync.readPos)
		return chSync.writePos - chSync.readPos;
	else if (chSync.writePos < chSync.readPos)
		return chSync.writePos - chSync.readPos + SYNC_QUEUE_LEN + 1;
	else
		return 0;
}

int32_t chQueueWriteSize(void)
{
	int32_t size;

	if (chSync.writePos > chSync.readPos)
	{
		size = chSync.readPos - chSync.writePos + SYNC_QUEUE_LEN;
	}
	else if (chSync.writePos < chSync.readPos)
	{
		chQueueClearing = true;

		/* Buffer is full, reset the read/write pos. This is actually really nasty since
		** read/write are two different threads, but because of timestamp validation it
		** shouldn't be that dangerous.
		** It will also create a small visual stutter while the buffer is getting filled,
		** though that is barely noticable on normal buffer sizes, and it takes several
		** minutes between each time (when queue size is default, 16384)
		*/
		chSync.data[0].timestamp = 0;
		chSync.readPos = 0;
		chSync.writePos = 0;

		size = SYNC_QUEUE_LEN;

		chQueueClearing = false;
	}
	else
	{
		size = SYNC_QUEUE_LEN;
	}

	return size;
}

bool chQueuePush(chSyncData_t t)
{
	if (!chQueueWriteSize())
		return false;

	assert(chSync.writePos <= SYNC_QUEUE_LEN);
	chSync.data[chSync.writePos] = t;
	chSync.writePos = (chSync.writePos + 1) & SYNC_QUEUE_LEN;

	return true;
}

bool chQueuePop(void)
{
	if (!chQueueReadSize())
		return false;

	chSync.readPos = (chSync.readPos + 1) & SYNC_QUEUE_LEN;
	assert(chSync.readPos <= SYNC_QUEUE_LEN);

	return true;
}

chSyncData_t *chQueuePeek(void)
{
	if (!chQueueReadSize())
		return NULL;

	assert(chSync.readPos <= SYNC_QUEUE_LEN);
	return &chSync.data[chSync.readPos];
}

uint64_t getChQueueTimestamp(void)
{
	if (!chQueueReadSize())
		return 0;

	assert(chSync.readPos <= SYNC_QUEUE_LEN);
	return chSync.data[chSync.readPos].timestamp;
}

void lockAudio(void)
{
	if (audio.dev != 0)
		SDL_LockAudioDevice(audio.dev);

	audio.locked = true;
}

void unlockAudio(void)
{
	if (audio.dev != 0)
		SDL_UnlockAudioDevice(audio.dev);

	audio.locked = false;
}

static void resetSyncQueues(void)
{
	pattSync.data[0].timestamp = 0;
	pattSync.readPos = 0;
	pattSync.writePos = 0;
	
	chSync.data[0].timestamp = 0;
	chSync.writePos = 0;
	chSync.readPos = 0;
}

void lockMixerCallback(void) // lock audio + clear voices/scopes (for short operations)
{
	if (!audio.locked)
		lockAudio();

	audio.resetSyncTickTimeFlag = true;

	stopVoices(); // VERY important! prevents potential crashes by purging pointers

	// scopes, mixer and replayer are guaranteed to not be active at this point

	resetSyncQueues();
}

void unlockMixerCallback(void)
{
	stopVoices(); // VERY important! prevents potential crashes by purging pointers
	
	if (audio.locked)
		unlockAudio();
}

void pauseAudio(void) // lock audio + clear voices/scopes + render silence (for long operations)
{
	if (audioPaused)
	{
		stopVoices(); // VERY important! prevents potential crashes by purging pointers
		return;
	}

	if (audio.dev > 0)
		SDL_PauseAudioDevice(audio.dev, true);

	audio.resetSyncTickTimeFlag = true;

	stopVoices(); // VERY important! prevents potential crashes by purging pointers

	// scopes, mixer and replayer are guaranteed to not be active at this point

	resetSyncQueues();
	audioPaused = true;
}

void resumeAudio(void) // unlock audio
{
	if (!audioPaused)
		return;

	if (audio.dev > 0)
		SDL_PauseAudioDevice(audio.dev, false);

	audioPaused = false;
}

static void fillVisualsSyncBuffer(void)
{
	pattSyncData_t pattSyncData;
	chSyncData_t chSyncData;
	syncedChannel_t *c;
	stmTyp *s;

	if (audio.resetSyncTickTimeFlag)
	{
		audio.resetSyncTickTimeFlag = false;

		audio.tickTime64 = SDL_GetPerformanceCounter() + audio.audLatencyPerfValInt;
		audio.tickTime64Frac = audio.audLatencyPerfValFrac;
	}

	if (songPlaying)
	{
		// push pattern variables to sync queue
		pattSyncData.timer = song.curReplayerTimer;
		pattSyncData.patternPos = song.curReplayerPattPos;
		pattSyncData.pattern = song.curReplayerPattNr;
		pattSyncData.songPos = song.curReplayerSongPos;
		pattSyncData.speed = song.speed;
		pattSyncData.tempo = (uint8_t)song.tempo;
		pattSyncData.globalVol = (uint8_t)song.globVol;
		pattSyncData.timestamp = audio.tickTime64;
		pattQueuePush(pattSyncData);
	}

	// push channel variables to sync queue

	c = chSyncData.channels;
	s = stm;

	for (int32_t i = 0; i < song.antChn; i++, c++, s++)
	{
		c->finalPeriod = s->finalPeriod;
		c->fineTune = s->fineTune;
		c->relTonNr = s->relTonNr;
		c->instrNr = s->instrNr;
		c->sampleNr = s->sampleNr;
		c->envSustainActive = s->envSustainActive;
		c->status = s->tmpStatus;
		c->finalVol = s->finalVol;
		c->smpStartPos = s->smpStartPos;
	}

	chSyncData.timestamp = audio.tickTime64;
	chQueuePush(chSyncData);

	audio.tickTime64 += tickTimeLen;
	audio.tickTime64Frac += tickTimeLenFrac;
	if (audio.tickTime64Frac > 0xFFFFFFFF)
	{
		audio.tickTime64Frac &= 0xFFFFFFFF;
		audio.tickTime64++;
	}
}

static void SDLCALL audioCallback(void *userdata, Uint8 *stream, int len)
{
	if (editor.wavIsRendering)
		return;

	int32_t samplesLeft = len / pmpCountDiv;
	if (samplesLeft <= 0)
		return;

	while (samplesLeft > 0)
	{
		if (audio.dTickSampleCounter <= 0.0)
		{
			// new replayer tick

			replayerBusy = true;

			if (audio.volumeRampingFlag)
				mix_SaveIPVolumes();

			mainPlayer();
			mix_UpdateChannelVolPanFrq();
			fillVisualsSyncBuffer();

			audio.dTickSampleCounter += audio.dSamplesPerTick;

			replayerBusy = false;
		}

		const int32_t remainingTick = (int32_t)ceil(audio.dTickSampleCounter);

		int32_t samplesToMix = samplesLeft;
		if (samplesToMix > remainingTick)
			samplesToMix = remainingTick;

		mixAudio(stream, samplesToMix, pmpChannels);
		stream += samplesToMix * pmpCountDiv;

		samplesLeft -= samplesToMix;
		audio.dTickSampleCounter -= samplesToMix;
	}

	(void)userdata; // make compiler not complain
}

static bool setupAudioBuffers(void)
{
	const uint32_t sampleSize = sizeof (int32_t);

	audio.mixBufferLUnaligned = (int32_t *)MALLOC_PAD(MAX_WAV_RENDER_SAMPLES_PER_TICK * sampleSize, 256);
	audio.mixBufferRUnaligned = (int32_t *)MALLOC_PAD(MAX_WAV_RENDER_SAMPLES_PER_TICK * sampleSize, 256);

	if (audio.mixBufferLUnaligned == NULL || audio.mixBufferRUnaligned == NULL)
		return false;

	// make aligned main pointers
	audio.mixBufferL = (int32_t *)ALIGN_PTR(audio.mixBufferLUnaligned, 256);
	audio.mixBufferR = (int32_t *)ALIGN_PTR(audio.mixBufferRUnaligned, 256);

	return true;
}

static void freeAudioBuffers(void)
{
	if (audio.mixBufferLUnaligned != NULL)
	{
		free(audio.mixBufferLUnaligned);
		audio.mixBufferLUnaligned = NULL;
	}

	if (audio.mixBufferRUnaligned != NULL)
	{
		free(audio.mixBufferRUnaligned);
		audio.mixBufferRUnaligned = NULL;
	}

	audio.mixBufferL = NULL;
	audio.mixBufferR = NULL;
}

void updateSendAudSamplesRoutine(bool lockMixer)
{
	if (lockMixer)
		lockMixerCallback();

	if (config.specialFlags & BITDEPTH_16)
	{
		if (config.specialFlags2 & DITHERED_AUDIO)
		{
			if (pmpChannels > 2)
				sendAudSamplesFunc = sendSamples16BitDitherMultiChan;
			else
				sendAudSamplesFunc = sendSamples16BitDitherStereo;
		}
		else
		{
			if (pmpChannels > 2)
				sendAudSamplesFunc = sendSamples16BitMultiChan;
			else
				sendAudSamplesFunc = sendSamples16BitStereo;
		}
	}
	else
	{
		if (pmpChannels > 2)
			sendAudSamplesFunc = sendSamples32BitMultiChan;
		else
			sendAudSamplesFunc = sendSamples32BitStereo;
	}

	if (lockMixer)
		unlockMixerCallback();
}

static void calcAudioLatencyVars(int32_t audioBufferSize, int32_t audioFreq)
{
	double dInt, dFrac;

	if (audioFreq == 0)
		return;

	const double dAudioLatencySecs = audioBufferSize / (double)audioFreq;

	dFrac = modf(dAudioLatencySecs * editor.dPerfFreq, &dInt);

	// integer part
	audio.audLatencyPerfValInt = (int32_t)dInt;

	// fractional part (scaled to 0..2^32-1)
	dFrac *= UINT32_MAX+1.0;
	audio.audLatencyPerfValFrac = (uint32_t)dFrac;

	audio.dAudioLatencyMs = dAudioLatencySecs * 1000.0;
}

static void setLastWorkingAudioDevName(void)
{
	uint32_t stringLen;

	if (audio.lastWorkingAudioDeviceName != NULL)
	{
		free(audio.lastWorkingAudioDeviceName);
		audio.lastWorkingAudioDeviceName = NULL;
	}

	if (audio.currOutputDevice != NULL)
	{
		stringLen = (uint32_t)strlen(audio.currOutputDevice);

		audio.lastWorkingAudioDeviceName = (char *)malloc(stringLen + 2);
		if (audio.lastWorkingAudioDeviceName != NULL)
		{
			if (stringLen > 0)
				strcpy(audio.lastWorkingAudioDeviceName, audio.currOutputDevice);
			audio.lastWorkingAudioDeviceName[stringLen + 1] = '\0'; // UTF-8 needs double null termination
		}
	}
}

bool setupAudio(bool showErrorMsg)
{
	int8_t newBitDepth;
	uint16_t configAudioBufSize;
	SDL_AudioSpec want, have;

	closeAudio();

	if (config.audioFreq < MIN_AUDIO_FREQ || config.audioFreq > MAX_AUDIO_FREQ)
		config.audioFreq = 48000; // set default rate

	// get audio buffer size from config special flags

	configAudioBufSize = 1024;
	if (config.specialFlags & BUFFSIZE_512)
		configAudioBufSize = 512;
	else if (config.specialFlags & BUFFSIZE_2048)
		configAudioBufSize = 2048;

	audio.wantFreq = config.audioFreq;
	audio.wantSamples = configAudioBufSize;
	audio.wantChannels = 2;

	// set up audio device
	memset(&want, 0, sizeof (want));

	// these three may change after opening a device, but our mixer is dealing with it
	want.freq = config.audioFreq;
	want.format = (config.specialFlags & BITDEPTH_32) ? AUDIO_F32 : AUDIO_S16;
	want.channels = 2;
	// -------------------------------------------------------------------------------
	want.callback = audioCallback;
	want.samples  = configAudioBufSize;

	audio.dev = SDL_OpenAudioDevice(audio.currOutputDevice, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE); // prevent SDL2 from resampling
	if (audio.dev == 0)
	{
		if (showErrorMsg)
			showErrorMsgBox("Couldn't open audio device:\n\"%s\"\n\nDo you have any audio device enabled and plugged in?", SDL_GetError());

		return false;
	}

	// test if the received audio format is compatible
	if (have.format != AUDIO_S16 && have.format != AUDIO_F32)
	{
		if (showErrorMsg)
			showErrorMsgBox("Couldn't open audio device:\nThe program doesn't support an SDL_AudioFormat of '%d' (not 16-bit or 32-bit float).",
				(uint32_t)have.format);

		closeAudio();
		return false;
	}

	// test if the received audio rate is compatible

#if defined _WIN64 || defined __amd64__
	if (have.freq != 44100 && have.freq != 48000 && have.freq != 96000 && have.freq != 192000)
#else
	if (have.freq != 44100 && have.freq != 48000)
#endif
	{
		if (showErrorMsg)
			showErrorMsgBox("Couldn't open audio device:\nThe program doesn't support an audio output rate of %dHz. Sorry!", have.freq);

		closeAudio();
		return false;
	}

	if (!setupAudioBuffers())
	{
		if (showErrorMsg)
			showErrorMsgBox("Not enough memory!");

		closeAudio();
		return false;
	}

	// set new bit depth flag

	newBitDepth = 16;
	config.specialFlags &= ~BITDEPTH_32;
	config.specialFlags |=  BITDEPTH_16;

	if (have.format == AUDIO_F32)
	{
		newBitDepth = 24;
		config.specialFlags &= ~BITDEPTH_16;
		config.specialFlags |=  BITDEPTH_32;
	}

	audio.haveFreq = have.freq;
	audio.haveSamples = have.samples;
	audio.haveChannels = have.channels;

	// set a few variables

	config.audioFreq = have.freq;
	audio.freq = have.freq;
	smpBuffSize = have.samples;

	calcAudioLatencyVars(have.samples, have.freq);

	pmpChannels = have.channels;
	pmpCountDiv = pmpChannels * ((newBitDepth == 16) ? sizeof (int16_t) : sizeof (float));

	// make a copy of the new known working audio settings

	audio.lastWorkingAudioFreq = config.audioFreq;
	audio.lastWorkingAudioBits = config.specialFlags & (BITDEPTH_16 + BITDEPTH_32 + BUFFSIZE_512 + BUFFSIZE_1024 + BUFFSIZE_2048);
	setLastWorkingAudioDevName();

	// update config audio radio buttons if we're on that screen at the moment
	if (ui.configScreenShown && editor.currConfigScreen == CONFIG_SCREEN_IO_DEVICES)
		showConfigScreen();

	updateWavRendererSettings();
	setAudioAmp(config.boostLevel, config.masterVol, (config.specialFlags & BITDEPTH_32) ? true : false);

	// don't call stopVoices() in this routine
	for (int32_t i = 0; i < MAX_VOICES; i++)
		stopVoice(i);

	stopAllScopes();

	audio.dTickSampleCounter = 0.0; // zero tick sample counter so that it will instantly initiate a tick

	calcReplayRate(audio.freq);

	if (song.speed == 0)
		song.speed = 125;

	setSpeed(song.speed); // this is important

	updateSendAudSamplesRoutine(false);
	audio.resetSyncTickTimeFlag = true;

	return true;
}

void closeAudio(void)
{
	if (audio.dev > 0)
	{
		SDL_PauseAudioDevice(audio.dev, true);
		SDL_CloseAudioDevice(audio.dev);
		audio.dev = 0;
	}

	freeAudioBuffers();
}