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

namespace SaneAudioRenderer
{
    void SampleCorrection::NewFormat(SharedWaveFormat format)
    {
        assert(format);
        assert(format->nSamplesPerSec > 0);

        if (m_format)
        {
            m_segmentTimeInPreviousFormats += FramesToTime(m_segmentFramesInCurrentFormat);
            m_segmentFramesInCurrentFormat = 0;
        }

        m_format = format;
        m_bitstream = (DspFormatFromWaveFormat(*m_format) == DspFormat::Unknown);
    }

    void SampleCorrection::NewSegment(double rate)
    {
        assert(rate > 0.0);

        m_rate = rate;

        m_segmentTimeInPreviousFormats = 0;
        m_segmentFramesInCurrentFormat = 0;

        m_lastFrameEnd = 0;

        m_timeDivergence = 0;
    }

    void SampleCorrection::NewDeviceBuffer()
    {
        m_freshBuffer = true;
    }

    DspChunk SampleCorrection::ProcessSample(IMediaSample* pSample, AM_SAMPLE2_PROPERTIES& sampleProps, bool realtimeDevice)
    {
        assert(m_format);

        DspChunk chunk(pSample, sampleProps, *m_format);

        if (m_bitstream)
        {
            if (m_freshBuffer && !(sampleProps.dwSampleFlags & AM_SAMPLE_SPLICEPOINT))
            {
                // Drop the sample.
                DebugOut(ClassName(this), "drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
        }
        else if (!realtimeDevice && (m_lastFrameEnd == 0 || (sampleProps.dwSampleFlags & AM_SAMPLE_TIMEDISCONTINUITY)))
        {
            if ((sampleProps.dwSampleFlags & AM_SAMPLE_STOPVALID) && sampleProps.tStop <= m_lastFrameEnd)
            {
                // Drop the sample.
                DebugOut(ClassName(this), "drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart < m_lastFrameEnd)
            {
                // Crop the sample.
                const size_t cropFrames = (size_t)TimeToFrames(m_lastFrameEnd - sampleProps.tStart);

                if (cropFrames > 0)
                {
                    DebugOut(ClassName(this), "crop", cropFrames, "frames from [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    chunk.ShrinkHead(chunk.GetFrameCount() > cropFrames ? chunk.GetFrameCount() - cropFrames : 0);
                }
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart > m_lastFrameEnd)
            {
                // Zero-pad the sample.
                const size_t padFrames = (size_t)TimeToFrames(sampleProps.tStart - m_lastFrameEnd);

                if (padFrames > 0 &&
                    FramesToTime(padFrames) < 10 * OneSecond)
                {
                    DebugOut(ClassName(this), "pad", padFrames, "frames before [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    chunk.PadHead(padFrames);
                }<commit_msg>Return two missing lines to SampleCorrection<commit_after>#include "pch.h"
#include "SampleCorrection.h"

namespace SaneAudioRenderer
{
    void SampleCorrection::NewFormat(SharedWaveFormat format)
    {
        assert(format);
        assert(format->nSamplesPerSec > 0);

        if (m_format)
        {
            m_segmentTimeInPreviousFormats += FramesToTime(m_segmentFramesInCurrentFormat);
            m_segmentFramesInCurrentFormat = 0;
        }

        m_format = format;
        m_bitstream = (DspFormatFromWaveFormat(*m_format) == DspFormat::Unknown);
    }

    void SampleCorrection::NewSegment(double rate)
    {
        assert(rate > 0.0);

        m_rate = rate;

        m_segmentTimeInPreviousFormats = 0;
        m_segmentFramesInCurrentFormat = 0;

        m_lastFrameEnd = 0;

        m_timeDivergence = 0;
    }

    void SampleCorrection::NewDeviceBuffer()
    {
        m_freshBuffer = true;
    }

    DspChunk SampleCorrection::ProcessSample(IMediaSample* pSample, AM_SAMPLE2_PROPERTIES& sampleProps, bool realtimeDevice)
    {
        assert(m_format);

        DspChunk chunk(pSample, sampleProps, *m_format);

        if (m_bitstream)
        {
            if (m_freshBuffer && !(sampleProps.dwSampleFlags & AM_SAMPLE_SPLICEPOINT))
            {
                // Drop the sample.
                DebugOut(ClassName(this), "drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
        }
        else if (!realtimeDevice && (m_lastFrameEnd == 0 || (sampleProps.dwSampleFlags & AM_SAMPLE_TIMEDISCONTINUITY)))
        {
            if ((sampleProps.dwSampleFlags & AM_SAMPLE_STOPVALID) && sampleProps.tStop <= m_lastFrameEnd)
            {
                // Drop the sample.
                DebugOut(ClassName(this), "drop [", sampleProps.tStart, sampleProps.tStop, "]");
                chunk = DspChunk();
                assert(chunk.IsEmpty());
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart < m_lastFrameEnd)
            {
                // Crop the sample.
                const size_t cropFrames = (size_t)TimeToFrames(m_lastFrameEnd - sampleProps.tStart);

                if (cropFrames > 0)
                {
                    DebugOut(ClassName(this), "crop", cropFrames, "frames from [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    chunk.ShrinkHead(chunk.GetFrameCount() > cropFrames ? chunk.GetFrameCount() - cropFrames : 0);

                    sampleProps.tStart += FramesToTime(cropFrames);
                }
            }
            else if ((sampleProps.dwSampleFlags & AM_SAMPLE_TIMEVALID) && sampleProps.tStart > m_lastFrameEnd)
            {
                // Zero-pad the sample.
                const size_t padFrames = (size_t)TimeToFrames(sampleProps.tStart - m_lastFrameEnd);

                if (padFrames > 0 &&
                    FramesToTime(padFrames) < 10 * OneSecond)
                {
                    DebugOut(ClassName(this), "pad", padFrames, "frames before [",
                             sampleProps.tStart, sampleProps.tStop, "]");

                    chunk.PadHead(padFrames);

                    sampleProps.tStart -= FramesToTime(padFrames);
                }
{'fraction_non_alphanumeric': 0.10110294117647059, 'fraction_numerical': 0.004595588235294118, 'mean_word_length': 1.8902169101372288, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '4705731', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1813, 'n_words': 307}