<commit_before>/*
 * Copyright (c) 2015 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "roc_core/log.h"
#include "roc_audio/delayed_writer.h"

namespace roc {
namespace audio {

DelayedWriter::DelayedWriter(ISampleBufferWriter& output,
                             packet::channel_mask_t channels,
                             size_t latency)
    : output_(output)
    , n_ch_(packet::num_channels(channels))
    , latency_(latency * n_ch_)
    , pending_(0)
    , flushed_(false) {
}

void DelayedWriter::write(const ISampleBufferConstSlice& buffer) {
    if (flushed_) {
        output_.write(buffer);
    } else {
        queue_.append(buffer);
        pending_ += buffer.size();

        if (pending_ >= latency_ || !buffer) {
            roc_log(LOG_TRACE, "delayed writer: starting output: latency=%lu pending=%lu",
                    (unsigned long)latency_ / n_ch_, (unsigned long)pending_ / n_ch_);

            for (size_t n = 0; n < queue_.size(); n++) {
                output_.write(queue_[n]);
            }

            flushed_ = true;
        }
    }<commit_msg>Minor optimization in DelayedWriter<commit_after>/*
 * Copyright (c) 2015 <NAME>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "roc_core/log.h"
#include "roc_audio/delayed_writer.h"

namespace roc {
namespace audio {

DelayedWriter::DelayedWriter(ISampleBufferWriter& output,
                             packet::channel_mask_t channels,
                             size_t latency)
    : output_(output)
    , n_ch_(packet::num_channels(channels))
    , latency_(latency * n_ch_)
    , pending_(0)
    , flushed_(latency_ == 0) {
}

void DelayedWriter::write(const ISampleBufferConstSlice& buffer) {
    if (flushed_) {
        output_.write(buffer);
    } else {
        queue_.append(buffer);
        pending_ += buffer.size();

        if (pending_ >= latency_ || !buffer) {
            roc_log(LOG_TRACE, "delayed writer: starting output: latency=%lu pending=%lu",
                    (unsigned long)latency_ / n_ch_, (unsigned long)pending_ / n_ch_);

            for (size_t n = 0; n < queue_.size(); n++) {
                output_.write(queue_[n]);
            }

            flushed_ = true;
        }
    }
{'fraction_non_alphanumeric': 0.11773940345368916, 'fraction_numerical': 0.008241758241758242, 'mean_word_length': 2.327676240208877, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6947410', 'n_tokens_mistral': 805, 'n_tokens_neox': 759, 'n_words': 198}