<commit_before>// Copyright 2012-2013 Samplecount S.L.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <methcla/plugins/patch-cable.h>
#include <methcla/plugin.hpp>

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace Methcla::Plugin;

typedef enum {
    kPort_in,
    kPort_out,
    kNumPorts
} PortIndex;

typedef struct {
    float* ports[kNumPorts];
} PatchCable;

static const Methcla_PortDescriptor kPortDescriptors[] = {
    { kMethcla_Input, kMethcla_AudioPort, kMethcla_PortFlags },
    { kMethcla_Output, kMethcla_AudioPort, kMethcla_PortFlags }
};

static bool
port_descriptor( const Methcla_SynthOptions*
               , Methcla_PortCount index
               , Methcla_PortDescriptor* port )
{
    if (index < kNumPorts) {
        *port = kPortDescriptors[index];
        return true;
    }
    return false;
}

static void
construct( const Methcla_World*
         , const Methcla_SynthDef*
         , const Methcla_SynthOptions*
         , Methcla_Synth* synth )
{
    PatchCable* self = static_cast<PatchCable*>(synth);
    std::fill(self->ports, self->ports + kNumPorts, nullptr);
}

static void
connect( Methcla_Synth* synth
       , Methcla_PortCount port
       , void* data)
{
    PatchCable* self = static_cast<PatchCable*>(synth);
    self->ports[port] = static_cast<float*>(data);
}

static void
process(const Methcla_World*, Methcla_Synth* synth, size_t numFrames)
{
    PatchCable* self = static_cast<PatchCable*>(synth);

    const float* src = self->ports[kPort_in];
    float* dst = self->ports[kPort_out];

    // float rms = 0;
    // for (size_t i=0; i < numFrames; i++) {
    //     rms += src[i] * src[i];
    // }
    // rms = sqrt(rms);
    // std::cout << "patch_cable " << rms << "\n";

    std::copy(src, src + numFrames, dst);
}

static const Methcla_SynthDef descriptor =
{
    METHCLA_PLUGINS_PATCH_CABLE_URI,
    sizeof(PatchCable),
    0, NULL,
    port_descriptor,
    construct,
    connect,
    NULL,
    process,
    NULL
};

// Amplifier

namespace {

typedef NoOptions AmplifierOptions;

class AmplifierPorts
{
public:
    enum Port
    {
        kInput
      , kOutput
      , kGain
    };

    static constexpr size_t numPorts() { return 3; }

    static Methcla_PortDescriptor descriptor(Port port)
    {
        switch (port)
        {
            case kInput:  return Methcla::Plugin::PortDescriptor::audioInput();
            case kOutput: return Methcla::Plugin::PortDescriptor::audioOutput();
            case kGain:   return Methcla::Plugin::PortDescriptor::controlInput();
            default: throw std::runtime_error(METHCLA_PLUGINS_AMPLIFIER_URI ": Invalid port index");
        }
    }
};

class Amplifier
{
    float* m_ports[AmplifierPorts::numPorts()];

public:
    Amplifier(const World<Amplifier>& world, const Methcla_SynthDef*, const AmplifierOptions& options)
    {
    }

    void connect(AmplifierPorts::Port port, void* data)
    {
        m_ports[port] = static_cast<float*>(data);
    }

    void process(const World<Amplifier>& world, size_t numFrames)
    {
        const float* const input = m_ports[AmplifierPorts::kInput];
        float* output = m_ports[AmplifierPorts::kOutput];
        const float gain = *m_ports[AmplifierPorts::kGain];

        if (gain == 0.0f) {
            for (size_t i=0; i < numFrames; i++) {
                output[i] = 0.0f;
            }
        } else {
            for (size_t i=0; i < numFrames; i++) {
                output[i] = gain * input[i];
            }
        }
    }
};

StaticSynthDef<Amplifier,AmplifierOptions,AmplifierPorts> kAmplifierDef;

};

// Library initialization

static const Methcla_Library library = { NULL, NULL };

METHCLA_EXPORT const Methcla_Library* methcla_plugins_patch_cable(const Methcla_Host* host, const char* /* bundlePath */)
{
    methcla_host_register_synthdef(host, &descriptor);
    kAmplifierDef(host, METHCLA_PLUGINS_AMPLIFIER_URI);
    return &library;<commit_msg>Optimize patch cable if gain is 1<commit_after>// Copyright 2012-2013 <NAME>.L.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <methcla/plugins/patch-cable.h>
#include <methcla/plugin.hpp>

#include <algorithm>
#include <cmath>
#include <iostream>

using namespace Methcla::Plugin;

typedef enum {
    kPort_in,
    kPort_out,
    kNumPorts
} PortIndex;

typedef struct {
    float* ports[kNumPorts];
} PatchCable;

static const Methcla_PortDescriptor kPortDescriptors[] = {
    { kMethcla_Input, kMethcla_AudioPort, kMethcla_PortFlags },
    { kMethcla_Output, kMethcla_AudioPort, kMethcla_PortFlags }
};

static bool
port_descriptor( const Methcla_SynthOptions*
               , Methcla_PortCount index
               , Methcla_PortDescriptor* port )
{
    if (index < kNumPorts) {
        *port = kPortDescriptors[index];
        return true;
    }
    return false;
}

static void
construct( const Methcla_World*
         , const Methcla_SynthDef*
         , const Methcla_SynthOptions*
         , Methcla_Synth* synth )
{
    PatchCable* self = static_cast<PatchCable*>(synth);
    std::fill(self->ports, self->ports + kNumPorts, nullptr);
}

static void
connect( Methcla_Synth* synth
       , Methcla_PortCount port
       , void* data)
{
    PatchCable* self = static_cast<PatchCable*>(synth);
    self->ports[port] = static_cast<float*>(data);
}

static void
process(const Methcla_World*, Methcla_Synth* synth, size_t numFrames)
{
    PatchCable* self = static_cast<PatchCable*>(synth);

    const float* src = self->ports[kPort_in];
    float* dst = self->ports[kPort_out];

    // float rms = 0;
    // for (size_t i=0; i < numFrames; i++) {
    //     rms += src[i] * src[i];
    // }
    // rms = sqrt(rms);
    // std::cout << "patch_cable " << rms << "\n";

    std::copy(src, src + numFrames, dst);
}

static const Methcla_SynthDef descriptor =
{
    METHCLA_PLUGINS_PATCH_CABLE_URI,
    sizeof(PatchCable),
    0, NULL,
    port_descriptor,
    construct,
    connect,
    NULL,
    process,
    NULL
};

// Amplifier

namespace {

typedef NoOptions AmplifierOptions;

class AmplifierPorts
{
public:
    enum Port
    {
        kInput
      , kOutput
      , kGain
    };

    static constexpr size_t numPorts() { return 3; }

    static Methcla_PortDescriptor descriptor(Port port)
    {
        switch (port)
        {
            case kInput:  return Methcla::Plugin::PortDescriptor::audioInput();
            case kOutput: return Methcla::Plugin::PortDescriptor::audioOutput();
            case kGain:   return Methcla::Plugin::PortDescriptor::controlInput();
            default: throw std::runtime_error(METHCLA_PLUGINS_AMPLIFIER_URI ": Invalid port index");
        }
    }
};

class Amplifier
{
    float* m_ports[AmplifierPorts::numPorts()];

public:
    Amplifier(const World<Amplifier>& world, const Methcla_SynthDef*, const AmplifierOptions& options)
    {
    }

    void connect(AmplifierPorts::Port port, void* data)
    {
        m_ports[port] = static_cast<float*>(data);
    }

    void process(const World<Amplifier>& world, size_t numFrames)
    {
        const float* const input = m_ports[AmplifierPorts::kInput];
        float* output = m_ports[AmplifierPorts::kOutput];
        const float gain = *m_ports[AmplifierPorts::kGain];

        if (gain == 0.0f) {
            for (size_t i=0; i < numFrames; i++) {
                output[i] = 0.0f;
            }
        } else if (gain == 1.f) {
            for (size_t i=0; i < numFrames; i++) {
                output[i] = input[i];
            }
        } else {
            for (size_t i=0; i < numFrames; i++) {
                output[i] = gain * input[i];
            }
        }
    }
};

StaticSynthDef<Amplifier,AmplifierOptions,AmplifierPorts> kAmplifierDef;

};

// Library initialization

static const Methcla_Library library = { NULL, NULL };

METHCLA_EXPORT const Methcla_Library* methcla_plugins_patch_cable(const Methcla_Host* host, const char* /* bundlePath */)
{
    methcla_host_register_synthdef(host, &descriptor);
    kAmplifierDef(host, METHCLA_PLUGINS_AMPLIFIER_URI);
    return &library;
{'fraction_non_alphanumeric': 0.11256602112676056, 'fraction_numerical': 0.005171654929577465, 'mean_word_length': 2.9794220665499123, 'pattern_counts': {'":': 2, '<': 51, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5653740', 'n_tokens_mistral': 3162, 'n_tokens_neox': 2987, 'n_words': 712}