/* ==================================== JUCER_BINARY_RESOURCE ====================================

   This is an auto-generated file: Any edits you make may be overwritten!

*/

#include <cstring>

namespace scripts
{

//================== mtc_generator.lua ==================
static const unsigned char temp_binary_data_8[] =
"--- MTC Generator.\n"
"--\n"
"-- Generates MIDI Timecode (MTC) based on the transport state.\n"
"--\n"
"-- @script      mtc_generator\n"
"-- @type        DSP\n"
"-- @license     GPL v3\n"
"-- @author      Alexandr Zyurkalov\n"
"\n"
"local MidiBuffer = require ('el.MidiBuffer')\n"
"local midi       = require ('el.midi')\n"
"local bytes      = require ('el.bytes')\n"
"\n"
"-- Buffer to render filtered output\n"
"local output = MidiBuffer.new ()\n"
"\n"
"-- Transport state and buffer properties\n"
"local wasPlaying = false\n"
"local lastSamplePosition = -1\n"
"local sampleRate = 44100.0\n"
"local bufSize    = 512\n"
"local buffer     = bytes.new (10)\n"
"local framesPerSecond = 24\n"
"\n"
"local function layout ()\n"
"    return {\n"
"        audio = { 0, 0 },\n"
"        midi = { 0, 1 }\n"
"    }\n"
"end\n"
"\n"
"-- prepare for rendering\n"
"local function prepare (rate, block)\n"
"    -- reserve memory and clear the output buffer\n"
"    output:reserve (256)\n"
"    output:clear ()\n"
"    sampleRate = rate\n"
"    bufSize    = block\n"
"end\n"
"\n"
"local function process (_, m, _, _, t)\n"
"    -- Get MIDI input buffer from the MidiPipe\n"
"    local input = m:get (1)\n"
"\n"
"    output:clear ()\n"
"\n"
"    local playing        = t:playing ()\n"
"    local samplePosition = t:frame ()\n"
"\n"
"    local wasJumped = samplePosition ~= lastSamplePosition and lastSamplePosition >= 0\n"
"    if not playing and (wasPlaying or wasJumped) then\n"
"        local size = midi.mtcfullframetransport (buffer, samplePosition + bufSize - 1, sampleRate, framesPerSecond)\n"
"        output:insertBytes (buffer, size, bufSize)\n"
"    end\n"
"\n"
"    if playing then\n"
"        local msg, frame, sentMessages = 0\n"
"\n"
"        repeat\n"
"            sentMessages, msg, frame = midi.quarterframetransport (samplePosition, bufSize, sampleRate, framesPerSecond, sentMessages)\n"
"            output:insertPacked (msg, frame)\n"
"        until msg == nil\n"
"    end\n"
"\n"
"    wasPlaying = playing\n"
"    lastSamplePosition = samplePosition\n"
"\n"
"    -- DSP scripts use replace processing, so swap in the rendered output\n"
"    input:swap (output)\n"
"end\n"
"\n"
"return {\n"
"    type = 'DSP',\n"
"    layout = layout,\n"
"    prepare = prepare,\n"
"    process = process\n"
"}\n"
"\n"
"-- SPDX-FileCopyrightText: Copyright (C) Kushview, LLC.\n"
"-- SPDX-License-Identifier: GPL-3.0-or-later";

const char* mtc_generator_lua = (const char*) temp_binary_data_8;
}
