<commit_before>//------------------------------------------------------------------------------
//  Audio.cc
//------------------------------------------------------------------------------
#include "Audio.h"
#include "Synth/Synth.h"

using namespace Oryol;
using namespace yakc;

//------------------------------------------------------------------------------
void
Audio::Setup(kc85& kc_) {
    this->kc = &kc_;
    this->kc->ctc.connect_constant_changed0(ctc0_const_changed, this);
    this->kc->ctc.connect_constant_changed1(ctc1_const_changed, this);
}

//------------------------------------------------------------------------------
void
Audio::Discard() {
    this->kc = nullptr;
}

//------------------------------------------------------------------------------
void
Audio::Update() {
    // switch off any channels?
    for (int channel = 0; channel < 2; channel++) {
        const auto& chn = this->kc->ctc.channels[channel];
        if (!(this->ctc_state[channel].mode & z80ctc::RESET) && (chn.mode & z80ctc::RESET)) {
            SynthOp op;
            op.Op = SynthOp::Nop;
            Synth::AddOp(channel, 0, op, 0);
            this->ctc_state[channel].mode = chn.mode;
        }
    }
    Synth::Update();
}

//------------------------------------------------------------------------------
void
Audio::updateVoice(int channel) {
    YAKC_ASSERT(channel < 2);
    const auto& chn = this->kc->ctc.channels[channel];
    if ((chn.constant != this->ctc_state[channel].constant) ||
        ((chn.mode ^ this->ctc_state[channel].mode) && z80ctc::RESET)) {

        if (!(chn.mode & z80ctc::RESET)) {
            const float synthRate = 88.0f; // FIXME: should be 44.1?
            const float clockRate = float(this->kc->clck.baseFreqKHz);
            const int tickDiv = int(clockRate / synthRate);
            unsigned int synthTickOffset = this->kc->cycles_frame / tickDiv;
            int div = chn.constant * ((chn.mode & z80ctc::PRESCALER_256) ? 256 : 16);
            float freq = (float(1750000) / float(div)) / 2.0f;
            SynthOp op;
            op.Op = SynthOp::Replace;
            op.Wave = SynthOp::Square;
            op.Freq = freq;<commit_msg>Audio tweaks<commit_after>//------------------------------------------------------------------------------
//  Audio.cc
//------------------------------------------------------------------------------
#include "Audio.h"
#include "Synth/Synth.h"

using namespace Oryol;
using namespace yakc;

//------------------------------------------------------------------------------
void
Audio::Setup(kc85& kc_) {
    this->kc = &kc_;
    this->kc->ctc.connect_constant_changed0(ctc0_const_changed, this);
    this->kc->ctc.connect_constant_changed1(ctc1_const_changed, this);
}

//------------------------------------------------------------------------------
void
Audio::Discard() {
    this->kc = nullptr;
}

//------------------------------------------------------------------------------
void
Audio::Update() {
    // switch off any channels?
    for (int channel = 0; channel < 2; channel++) {
        const auto& chn = this->kc->ctc.channels[channel];
        if (!(this->ctc_state[channel].mode & z80ctc::RESET) && (chn.mode & z80ctc::RESET)) {
            SynthOp op;
            op.Op = SynthOp::Nop;
            Synth::AddOp(channel, 0, op, 0);
            this->ctc_state[channel].mode = chn.mode;
        }
    }
    Synth::Update();
}

//------------------------------------------------------------------------------
void
Audio::updateVoice(int channel) {
    YAKC_ASSERT(channel < 2);
    const auto& chn = this->kc->ctc.channels[channel];
    if ((chn.constant != this->ctc_state[channel].constant) ||
        ((chn.mode ^ this->ctc_state[channel].mode) && z80ctc::RESET)) {

        if (!(chn.mode & z80ctc::RESET)) {
            const float synthRate = 88.2f; // FIXME: should be 44.1?
            const float clockRate = float(this->kc->clck.baseFreqKHz);
            const int tickDiv = int(clockRate / synthRate);
            unsigned int synthTickOffset = this->kc->cycles_frame / tickDiv;
            int div = chn.constant * ((chn.mode & z80ctc::PRESCALER_256) ? 256 : 16);
            float freq = (float(1750000) / float(div)) / 2.0f;
            SynthOp op;
            op.Op = SynthOp::Replace;
            op.Wave = SynthOp::Square;
            op.Freq = freq;
{'fraction_non_alphanumeric': 0.34090387703601743, 'fraction_numerical': 0.02018811654049094, 'mean_word_length': 1.2543950361944156, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '7614275', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1205, 'n_words': 236}