Skip to content

Commit

Permalink
Support downmixing to mono
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Jan 31, 2025
1 parent 39d7b3c commit d7b950a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions include/AudioPortAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ class AudioPortAudio : public AudioDevice
const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void* arg);

PaStream * m_paStream;
std::unique_ptr<SampleFrame[]> m_outBuf;
std::vector<SampleFrame> m_outBuf;
std::size_t m_outBufPos;
fpp_t m_outBufSize;
PortAudioInitializationGuard m_initGuard;
};

Expand Down
26 changes: 15 additions & 11 deletions src/core/audio/AudioPortAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ namespace lmms {
AudioPortAudio::AudioPortAudio(bool& successful, AudioEngine* engine)
: AudioDevice(DEFAULT_CHANNELS, engine)
, m_paStream(nullptr)
, m_outBuf(std::make_unique<SampleFrame[]>(engine->framesPerPeriod()))
, m_outBuf(engine->framesPerPeriod())
, m_outBufPos(0)
, m_outBufSize(engine->framesPerPeriod())
{
const auto backend = ConfigManager::inst()->value("audioportaudio", "backend");
const auto device = ConfigManager::inst()->value("audioportaudio", "device");
Expand Down Expand Up @@ -111,28 +110,33 @@ void AudioPortAudio::stopProcessing()

int AudioPortAudio::processCallback(const float* inputBuffer, float* outputBuffer, f_cnt_t framesPerBuffer)
{
std::fill_n(outputBuffer, framesPerBuffer * channels(), 0.0f);

while (framesPerBuffer)
{
if (m_outBufPos == 0)
{
m_outBufSize = getNextBuffer(m_outBuf.get());
if (m_outBufSize == 0)
{
std::fill_n(outputBuffer, framesPerBuffer * channels(), 0.0f);
return paComplete;
}
const auto err = getNextBuffer(m_outBuf.data());
if (err == 0) { return paComplete; }
}

const auto minLen = std::min(framesPerBuffer, m_outBufSize - m_outBufPos);
const auto minLen = std::min(framesPerBuffer, m_outBuf.size() - m_outBufPos);
for (auto sample = std::size_t{0}; sample < framesPerBuffer * channels(); ++sample)
{
outputBuffer[sample] = m_outBuf[sample / channels()][sample % channels()];
if (channels() == 1)
{
outputBuffer[sample] = m_outBuf[sample].average();
}
else
{
outputBuffer[sample] = m_outBuf[sample / channels()][sample % channels()];
}
}

outputBuffer += minLen * channels();
framesPerBuffer -= minLen;
m_outBufPos += minLen;
m_outBufPos %= m_outBufSize;
m_outBufPos %= m_outBuf.size();
}

return paContinue;
Expand Down

0 comments on commit d7b950a

Please sign in to comment.