<commit_before>#include "pch.h"
#include "DspTempo.h"

namespace SaneAudioRenderer
{
    void DspTempo::Initialize(double tempo, uint32_t rate, uint32_t channels)
    {
        m_stouch.clear();

        m_active = false;

        m_rate = rate;
        m_channels = channels;

        m_tempo = tempo;
        m_ftempo1 = (float)tempo;
        m_ftempo2 = std::nexttoward(m_ftempo1, tempo);
        m_ftempo = m_ftempo1;
        m_outSamples1 = 0;
        m_outSamples2 = 0;

        if (tempo != 1.0)
        {
            m_stouch.setSampleRate(rate);
            m_stouch.setChannels(channels);

            m_stouch.setTempo(m_ftempo);

            //m_stouch.setSetting(SETTING_SEQUENCE_MS, 40);
            //m_stouch.setSetting(SETTING_SEEKWINDOW_MS, 15);
            //m_stouch.setSetting(SETTING_OVERLAP_MS, 8);

            m_active = true;
        }
    }

    bool DspTempo::Active()
    {
        return m_active;
    }

    void DspTempo::Process(DspChunk& chunk)
    {
        if (!m_active || chunk.IsEmpty())
            return;

        assert(chunk.GetRate() == m_rate);
        assert(chunk.GetChannelCount() == m_channels);

        // DirectShow speed is in double precision, SoundTouch operates in single.
        // We have to adjust it dynamically.
        AdjustTempo();

        DspChunk::ToFloat(chunk);

        m_stouch.putSamples((const float*)chunk.GetConstData(), (uint32_t)chunk.GetFrameCount());

        DspChunk output(DspFormat::Float, m_channels, m_stouch.numSamples(), m_rate);

        uint32_t done = m_stouch.receiveSamples((float*)output.GetData(), (uint32_t)output.GetFrameCount());
        assert(done == output.GetFrameCount());
        output.Shrink(done);

        auto& outSamples = (m_ftempo == m_ftempo1) ? m_outSamples1 : m_outSamples2;
        outSamples += done;

        chunk = std::move(output);
    }

    void DspTempo::Finish(DspChunk& chunk)
    {
        if (!m_active)
            return;

        Process(chunk);

        m_stouch.flush();
        uint32_t undone = m_stouch.numSamples();

        if (undone > 0)
        {
            DspChunk output(DspFormat::Float, m_channels, chunk.GetFrameCount() + undone, m_rate);

            if (!chunk.IsEmpty())
                memcpy(output.GetData(), chunk.GetConstData(), chunk.GetSize());

            m_stouch.flush();

            uint32_t done = m_stouch.receiveSamples((float*)output.GetData() + chunk.GetSampleCount(), undone);
            assert(done == undone);
            output.Shrink(chunk.GetFrameCount() + done);

            chunk = std::move(output);
        }
    }

    void DspTempo::AdjustTempo()
    {
        if (m_tempo != m_ftempo)
        {
            assert(m_tempo != m_ftempo1);
            assert(m_tempo != m_ftempo2);

            double ratio = std::abs((m_tempo - m_ftempo1) / (m_tempo - m_ftempo2));

            if (m_ftempo != m_ftempo2 &&
                m_outSamples1 * ratio - m_outSamples2 > 60 * m_rate)
            {
                DebugOut("DspTempo adjusting for float/double imprecision (2), ratio", ratio);
                m_ftempo = m_ftempo2;
                m_stouch.setTempo(m_ftempo);
            }
            else if (m_ftempo != m_ftempo1 &&
                     m_outSamples2 - m_outSamples1 * ratio > 60 * m_rate)
            {
                DebugOut("DspTempo adjusting for float/double imprecision (1), ratio", ratio);
                m_ftempo = m_ftempo1;
                m_stouch.setTempo(m_ftempo);
            }
        }
    }
}<commit_msg>More cosmetics<commit_after>#include "pch.h"
#include "DspTempo.h"

namespace SaneAudioRenderer
{
    void DspTempo::Initialize(double tempo, uint32_t rate, uint32_t channels)
    {
        m_stouch.clear();

        m_active = false;

        m_rate = rate;
        m_channels = channels;

        m_tempo = tempo;
        m_ftempo1 = (float)tempo;
        m_ftempo2 = std::nexttoward(m_ftempo1, tempo);
        m_ftempo = m_ftempo1;
        m_outSamples1 = 0;
        m_outSamples2 = 0;

        if (tempo != 1.0)
        {
            m_stouch.setSampleRate(rate);
            m_stouch.setChannels(channels);

            m_stouch.setTempo(m_ftempo);

            //m_stouch.setSetting(SETTING_SEQUENCE_MS, 40);
            //m_stouch.setSetting(SETTING_SEEKWINDOW_MS, 15);
            //m_stouch.setSetting(SETTING_OVERLAP_MS, 8);

            m_active = true;
        }
    }

    bool DspTempo::Active()
    {
        return m_active;
    }

    void DspTempo::Process(DspChunk& chunk)
    {
        if (!m_active || chunk.IsEmpty())
            return;

        assert(chunk.GetRate() == m_rate);
        assert(chunk.GetChannelCount() == m_channels);

        // DirectShow speed is in double precision, SoundTouch operates in single.
        // We have to adjust it dynamically.
        AdjustTempo();

        DspChunk::ToFloat(chunk);

        m_stouch.putSamples((const float*)chunk.GetConstData(), (uint32_t)chunk.GetFrameCount());

        DspChunk output(DspFormat::Float, m_channels, m_stouch.numSamples(), m_rate);

        uint32_t done = m_stouch.receiveSamples((float*)output.GetData(), (uint32_t)output.GetFrameCount());
        assert(done == output.GetFrameCount());
        output.Shrink(done);

        auto& outSamples = (m_ftempo == m_ftempo1) ? m_outSamples1 : m_outSamples2;
        outSamples += done;

        chunk = std::move(output);
    }

    void DspTempo::Finish(DspChunk& chunk)
    {
        if (!m_active)
            return;

        Process(chunk);

        m_stouch.flush();
        uint32_t undone = m_stouch.numSamples();

        if (undone > 0)
        {
            DspChunk output(DspFormat::Float, m_channels, chunk.GetFrameCount() + undone, m_rate);

            if (!chunk.IsEmpty())
                memcpy(output.GetData(), chunk.GetConstData(), chunk.GetSize());

            m_stouch.flush();

            uint32_t done = m_stouch.receiveSamples((float*)output.GetData() + chunk.GetSampleCount(), undone);
            assert(done == undone);
            output.Shrink(chunk.GetFrameCount() + done);

            chunk = std::move(output);
        }
    }

    void DspTempo::AdjustTempo()
    {
        if (m_tempo != m_ftempo)
        {
            assert(m_tempo != m_ftempo1);
            assert(m_tempo != m_ftempo2);

            double ratio21 = std::abs((m_tempo - m_ftempo1) / (m_tempo - m_ftempo2));

            if (m_ftempo != m_ftempo2 &&
                m_outSamples1 * ratio21 - m_outSamples2 > 60 * m_rate)
            {
                DebugOut("DspTempo adjusting for float/double imprecision (2), ratio", ratio21);
                m_ftempo = m_ftempo2;
                m_stouch.setTempo(m_ftempo);
            }
            else if (m_ftempo != m_ftempo1 &&
                     m_outSamples2 - m_outSamples1 * ratio21 > 60 * m_rate)
            {
                DebugOut("DspTempo adjusting for float/double imprecision (1), ratio", ratio21);
                m_ftempo = m_ftempo1;
                m_stouch.setTempo(m_ftempo);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11622962437987243, 'fraction_numerical': 0.015875265768958185, 'mean_word_length': 1.9974511469838572, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1407890', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2308, 'n_words': 393}