<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/Audio/Engine.hpp"
#include "Methcla/Audio/Group.hpp"
#include "Methcla/Audio/Synth.hpp"
#include "Methcla/Exception.hpp"
#include "Methcla/Utility/MessageQueue.hpp"

#include <boost/current_function.hpp>
#include <cstdlib>
#include <iostream>
#include <oscpp/print.hpp>

using namespace Methcla;
using namespace Methcla::Audio;
using namespace Methcla::Memory;

struct Command
{
    void perform()
    {
        if (m_perform != nullptr)
            m_perform(m_env, m_data);
    }

    Environment* m_env;
    PerformFunc  m_perform;
    void*        m_data;
};

class Methcla::Audio::EnvironmentImpl
{
public:
    struct ErrorData
    {
        int32_t requestId;
        char*   message;
    };

    static const size_t kQueueSize = 8192;

    EnvironmentImpl(size_t realtimeMemorySize)
        : m_rtMem(realtimeMemorySize)
        , m_worker(kQueueSize, 2)
    { }

    typedef Utility::MessageQueue<Environment::Request,kQueueSize> MessageQueue;
    typedef Utility::WorkerThread<Command> Worker;

    Memory::RTMemoryManager m_rtMem;
    MessageQueue            m_requests;
    Worker                  m_worker;
};

extern "C" {

static void methcla_api_host_register_synthdef(const Methcla_Host* host, const Methcla_SynthDef* synthDef)
{
    return static_cast<Environment*>(host->handle)->registerSynthDef(synthDef);
}

static const Methcla_SoundFileAPI* methcla_api_host_get_soundfile_api(const Methcla_Host* host, const char* mimeType)
{
    return static_cast<Environment*>(host->handle)->soundFileAPI(mimeType);
}

static double methcla_api_world_samplerate(const Methcla_World* world)
{
    return static_cast<Environment*>(world->handle)->sampleRate();
}

static void* methcla_api_world_alloc(const Methcla_World* world, size_t size)
{
    return static_cast<Environment*>(world->handle)->rtMem().alloc(size);
}

static void* methcla_api_world_alloc_aligned(const Methcla_World* world, size_t alignment, size_t size)
{
    return static_cast<Environment*>(world->handle)->rtMem().allocAligned(alignment, size);
}

static void methcla_api_world_free(const Methcla_World* world, void* ptr)
{
    return static_cast<Environment*>(world->handle)->rtMem().free(ptr);
}

static void methcla_api_world_resource_retain(const Methcla_World*, Methcla_Resource* resource)
{
    static_cast<Reference*>(resource)->retain();
}

static void methcla_api_world_resource_release(const Methcla_World*, Methcla_Resource* resource)
{
    static_cast<Reference*>(resource)->release();
}

static void methcla_api_host_perform_command(const Methcla_Host*, Methcla_WorldPerformFunction, void*);
static void methcla_api_world_perform_command(const Methcla_World*, Methcla_HostPerformFunction, void*);

} // extern "C"

Environment::Environment(PacketHandler handler, const Options& options)<commit_msg>Add assertions and exception handlers to API interface callbacks<commit_after>// 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/Audio/Engine.hpp"
#include "Methcla/Audio/Group.hpp"
#include "Methcla/Audio/Synth.hpp"
#include "Methcla/Exception.hpp"
#include "Methcla/Utility/MessageQueue.hpp"

#include <boost/current_function.hpp>
#include <cstdlib>
#include <iostream>
#include <oscpp/print.hpp>

using namespace Methcla;
using namespace Methcla::Audio;
using namespace Methcla::Memory;

struct Command
{
    void perform()
    {
        if (m_perform != nullptr)
            m_perform(m_env, m_data);
    }

    Environment* m_env;
    PerformFunc  m_perform;
    void*        m_data;
};

class Methcla::Audio::EnvironmentImpl
{
public:
    struct ErrorData
    {
        int32_t requestId;
        char*   message;
    };

    static const size_t kQueueSize = 8192;

    EnvironmentImpl(size_t realtimeMemorySize)
        : m_rtMem(realtimeMemorySize)
        , m_worker(kQueueSize, 2)
    { }

    typedef Utility::MessageQueue<Environment::Request,kQueueSize> MessageQueue;
    typedef Utility::WorkerThread<Command> Worker;

    Memory::RTMemoryManager m_rtMem;
    MessageQueue            m_requests;
    Worker                  m_worker;
};

extern "C" {

static void methcla_api_host_register_synthdef(const Methcla_Host* host, const Methcla_SynthDef* synthDef)
{
    assert(host && host->handle);
    assert(synthDef);
    return static_cast<Environment*>(host->handle)->registerSynthDef(synthDef);
}

static const Methcla_SoundFileAPI* methcla_api_host_get_soundfile_api(const Methcla_Host* host, const char* mimeType)
{
    assert(host && host->handle);
    assert(mimeType);
    return static_cast<Environment*>(host->handle)->soundFileAPI(mimeType);
}

static double methcla_api_world_samplerate(const Methcla_World* world)
{
    assert(world && world->handle);
    return static_cast<Environment*>(world->handle)->sampleRate();
}

static void* methcla_api_world_alloc(const Methcla_World* world, size_t size)
{
    assert(world && world->handle);
    try {
        return static_cast<Environment*>(world->handle)->rtMem().alloc(size);
    } catch (std::invalid_argument) {
    } catch (std::bad_alloc) {
    }
    return nullptr;
}

static void* methcla_api_world_alloc_aligned(const Methcla_World* world, size_t alignment, size_t size)
{
    assert(world && world->handle);
    try {
        return static_cast<Environment*>(world->handle)->rtMem().allocAligned(alignment, size);
    } catch (std::invalid_argument) {
    } catch (std::bad_alloc) {
    }
    return nullptr;
}

static void methcla_api_world_free(const Methcla_World* world, void* ptr)
{
    assert(world && world->handle);
    return static_cast<Environment*>(world->handle)->rtMem().free(ptr);
}

static void methcla_api_world_resource_retain(const Methcla_World*, Methcla_Resource* resource)
{
    assert(world && world->handle);
    assert(resource);
    static_cast<Reference*>(resource)->retain();
}

static void methcla_api_world_resource_release(const Methcla_World*, Methcla_Resource* resource)
{
    assert(world && world->handle);
    assert(resource);
    static_cast<Reference*>(resource)->release();
}

static void methcla_api_host_perform_command(const Methcla_Host*, Methcla_WorldPerformFunction, void*);
static void methcla_api_world_perform_command(const Methcla_World*, Methcla_HostPerformFunction, void*);

} // extern "C"

Environment::Environment(PacketHandler handler, const Options& options)
{'fraction_non_alphanumeric': 0.10895980771798638, 'fraction_numerical': 0.005074108692749366, 'mean_word_length': 4.58538404175988, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 67, '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': '3292979', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2315, 'n_words': 539}