/* Copyright (c) 2020 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "nativeaudio/AudioOut_private.h" #include #include AudioResampleState g_AudioOut_resampleState = {{0.0f, 0.0f}, 0.0f}; AudioOut_sampleFormat g_AudioOut_hostFormat = {2, 44100, 2}; AudioOut_sampleFormat g_AudioOut_transportFormat = {2, 44100, 2}; int g_AudioOut_outputFileHandle = 0; AudioOutCallback g_AudioOut_outputCallback; void * g_AudioOut_outputContext; bool g_AudioOut_outputActive; void AudioOut_transferToOutput(void * outputBuffer, AudioOutCallback outputCallback, void * outputContext, uint64_t frameCount, void * transportBuffer, uint64_t transportBufferSize, AudioOut_sampleFormat hostFormat, AudioOut_sampleFormat transportFormat, AudioResampleState * resampleState) { if (transportFormat.channelCount == hostFormat.channelCount && transportFormat.sampleRate == hostFormat.sampleRate && transportFormat.bytesPerSample == hostFormat.bytesPerSample) { outputCallback(outputBuffer, frameCount, outputContext); if (g_AudioOut_outputFileHandle != 0) { write(g_AudioOut_outputFileHandle, outputBuffer, frameCount * hostFormat.channelCount * hostFormat.bytesPerSample); } } else { uint64_t inFrameCountMax = transportBufferSize / transportFormat.bytesPerSample / transportFormat.channelCount; uint64_t remainingOutFrameCount = frameCount; uint64_t framesWritten, framesWrittenTotal = 0; while (remainingOutFrameCount > 0) { uint64_t inFrameCount = ceil((double) remainingOutFrameCount * transportFormat.sampleRate / hostFormat.sampleRate - resampleState->lastSampleBlend); if (inFrameCount == 0) { inFrameCount = 1; } if (inFrameCount > inFrameCountMax) { inFrameCount = inFrameCountMax; } outputCallback(transportBuffer, inFrameCount, outputContext); convertAudioSamples(transportBuffer, inFrameCount, transportFormat.channelCount, transportFormat.sampleRate, transportFormat.bytesPerSample, outputBuffer + framesWrittenTotal * hostFormat.bytesPerSample * hostFormat.channelCount, remainingOutFrameCount, hostFormat.channelCount, hostFormat.sampleRate, hostFormat.bytesPerSample, NULL, &framesWritten, resampleState); if (g_AudioOut_outputFileHandle != 0) { write(g_AudioOut_outputFileHandle, outputBuffer + framesWrittenTotal * hostFormat.bytesPerSample * hostFormat.channelCount, framesWritten * hostFormat.channelCount * hostFormat.bytesPerSample); } remainingOutFrameCount -= framesWritten; framesWrittenTotal += framesWritten; } } }