Skip to content

Commit

Permalink
Use new chunking function in soundio device
Browse files Browse the repository at this point in the history
  • Loading branch information
sakertooth committed Dec 17, 2024
1 parent 25802da commit 663ca7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 44 deletions.
5 changes: 0 additions & 5 deletions include/AudioSoundIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ class AudioSoundIo : public AudioDevice
SoundIo *m_soundio;
SoundIoOutStream *m_outstream;

SampleFrame* m_outBuf;
int m_outBufSize;
fpp_t m_outBufFramesTotal;
fpp_t m_outBufFrameIndex;

bool m_stopped;
bool m_outstreamStarted;

Expand Down
52 changes: 13 additions & 39 deletions src/core/audio/AudioSoundIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ AudioSoundIo::AudioSoundIo( bool & outSuccessful, AudioEngine * _audioEngine ) :
outSuccessful = false;
m_soundio = nullptr;
m_outstream = nullptr;
m_outBuf = nullptr;
m_disconnectErr = 0;
m_outBufFrameIndex = 0;
m_outBufFramesTotal = 0;
m_stopped = true;
m_outstreamStarted = false;

Expand Down Expand Up @@ -165,7 +162,7 @@ AudioSoundIo::AudioSoundIo( bool & outSuccessful, AudioEngine * _audioEngine ) :
}

m_outstream->name = "LMMS";
m_outstream->software_latency = (double)audioEngine()->framesPerPeriod() / (double)currentSampleRate;
m_outstream->software_latency = static_cast<double>(audioEngine()->userFramesPerPeriod()) / currentSampleRate;
m_outstream->userdata = this;
m_outstream->write_callback = staticWriteCallback;
m_outstream->error_callback = staticErrorCallback;
Expand Down Expand Up @@ -209,12 +206,6 @@ AudioSoundIo::~AudioSoundIo()

void AudioSoundIo::startProcessing()
{
m_outBufFrameIndex = 0;
m_outBufFramesTotal = 0;
m_outBufSize = audioEngine()->framesPerPeriod();

m_outBuf = new SampleFrame[m_outBufSize];

if (! m_outstreamStarted)
{
if (int err = soundio_outstream_start(m_outstream))
Expand Down Expand Up @@ -250,12 +241,6 @@ void AudioSoundIo::stopProcessing()
soundio_strerror(err));
}
}

if (m_outBuf)
{
delete[] m_outBuf;
m_outBuf = nullptr;
}
}

void AudioSoundIo::errorCallback(int err)
Expand All @@ -270,11 +255,10 @@ void AudioSoundIo::underflowCallback()

void AudioSoundIo::writeCallback(int frameCountMin, int frameCountMax)
{
if (m_stopped) {return;}
const struct SoundIoChannelLayout *layout = &m_outstream->layout;
SoundIoChannelArea* areas;
int bytesPerSample = m_outstream->bytes_per_sample;
int framesLeft = frameCountMax;
const auto layout = &m_outstream->layout;

auto areas = static_cast<SoundIoChannelArea*>(nullptr);
auto framesLeft = frameCountMax;

while (framesLeft > 0)
{
Expand All @@ -288,37 +272,27 @@ void AudioSoundIo::writeCallback(int frameCountMin, int frameCountMax)
if (!frameCount)
break;


if (m_stopped)
{
for (int channel = 0; channel < layout->channel_count; ++channel)
{
memset(areas[channel].ptr, 0, bytesPerSample * frameCount);
memset(areas[channel].ptr, 0, m_outstream->bytes_per_sample * frameCount);
areas[channel].ptr += areas[channel].step * frameCount;
}
continue;
}

for (int frame = 0; frame < frameCount; frame += 1)
{
if (m_outBufFrameIndex >= m_outBufFramesTotal)
{
m_outBufFramesTotal = getNextBuffer(m_outBuf);
if (m_outBufFramesTotal == 0)
{
m_stopped = true;
break;
}
m_outBufFrameIndex = 0;
}
auto buf = std::vector<SampleFrame>(frameCount);
audioEngine()->renderNextBufferChunked(buf.data(), buf.size());

for (int channel = 0; channel < layout->channel_count; channel += 1)
for (const auto& frame : buf)
{
for (auto channel = 0; channel < layout->channel_count; ++channel)
{
float sample = m_outBuf[m_outBufFrameIndex][channel];
memcpy(areas[channel].ptr, &sample, bytesPerSample);
const auto sample = frame[channel];
memcpy(areas[channel].ptr, &sample, m_outstream->bytes_per_sample);
areas[channel].ptr += areas[channel].step;
}
m_outBufFrameIndex += 1;
}

if (int err = soundio_outstream_end_write(m_outstream))
Expand Down

0 comments on commit 663ca7e

Please sign in to comment.