<commit_before>/* Copyright 2011-2015 <NAME> and <NAME>
 *
 * Modipulate-OSC is part of the Modipulate distribution.
 * https://github.com/MrEricSir/Modipulate/
 *
 * Modipulate is released under the BSD license.  See LICENSE for details.
 *
 * modipulate-osc
 * Command-line Modipulate player which can be controlled
 * via Open Sound Control (OSC).
 * ----
 * Jobs:
 * - Run the Modipulate engine
 * - Open a UDP socket and receive OSC messages
 * - Process OSC messages which map to Modipulate functions
 *   - Load/unload song, play/pause, jump position, mixing, etc.
 * - Send OSC messages corresponding to Modipulate events
 *   - Row played, pattern changed, etc.
 * Notes:
 * - Uses the zlib-licensed OSC library "oscpkt"
 * TODO:
 * - Lots.
 */


#include <iostream> // cout
#include <string>   // std::string
#include <cstdint>  // int32_t, etc
#include <map>      // map song IDs to song pointers

// Modipulate
#include <modipulate.h>

// oscpkt
#define OSCPKT_OSTREAM_OUTPUT
#include "oscpkt/oscpkt.hh"
#include "oscpkt/udp.hh"

// Hardcoded constants
#define APPNAME "modipulate-osc"
#define OUTPUT_BUFFER_SIZE 1024 // holds outbound OSC data
#define SLEEP_MS 50             // sleep between cycles (milliseconds)
// Prefixes for messages printed while the engine is running
#define PFX_INFO   "<info>    "
#define PFX_ERR    "<error>   "
#define PFX_CMD    "<execute> "
#define PFX_OSCIN  "<osc-in>  "
#define PFX_OSCOUT "<osc-out> "
// Helper macros
#define USAGE_MSG \
"Usage:\n" \
"  " APPNAME " --help\n" \
"  " APPNAME " [options]\n" \
"Options:\n" \
"  --help             View this help message and exit\n" \
"  --bind-port=port   Port for incoming messages (default: 7070)\n" \
"  --send-port=port   Port for outgoing messages (default: 7071)\n" \
"  --send-addr=addr   Address for outgoing messages (default: localhost)\n" \
"  --skip-bytes=N     Skip N bytes of incoming packets (default: 0)\n" \
"Examples:\n" \
"  " APPNAME "\n" \
"  " APPNAME " --send-addr=192.168.1.15 -send-port=9442\n" \
"  " APPNAME " --bind-port=7071 --skip-bytes=16\n"


// Modipulate: globals
ModipulateErr err = MODIPULATE_ERROR_NONE;
std::map<std::int32_t, ModipulateSong> song_map;

// oscpkt: globals
oscpkt::UdpSocket socketSend;
oscpkt::UdpSocket socketReceive;
int skip_bytes = 0;
int rcv_port = 7070;
int snd_port = 7071;
std::string snd_address = "127.0.0.1";
char snd_buffer[OUTPUT_BUFFER_SIZE] = "";

static ModipulateSong get_song_from_id(std::int32_t song_id)
{
    std::map<std::int32_t, ModipulateSong>::iterator iter = song_map.find(song_id);
    return iter != song_map.end() ? iter->second : NULL;
}

/*****
       Modipulate Callbacks
                            *****/


// Modipulate: pattern change callback
void on_pattern_change(ModipulateSong song, int pattern_number, void* user_data)
{
    oscpkt::Message msg("/modipulate/cb/patternchange");
    msg.pushInt32(pattern_number);

    oscpkt::PacketWriter pw;
    pw.startBundle().startBundle().addMessage(msg).endBundle().endBundle();
    socketSend.sendPacket(pw.packetData(), pw.packetSize());

    std::cout << PFX_OSCOUT << "Modipulate: Pattern change: " << pattern_number << "\n";
}

// Modipulate: row change callback
void on_row_change(ModipulateSong song, int row, void* user_data)
{
    ; // TODO
    // Note: see below comments in on_note()<commit_msg>Rid osc-midpulate of pattern change prints.<commit_after>/* Copyright 2011-2015 <NAME> and <NAME>
 *
 * Modipulate-OSC is part of the Modipulate distribution.
 * https://github.com/MrEricSir/Modipulate/
 *
 * Modipulate is released under the BSD license.  See LICENSE for details.
 *
 * modipulate-osc
 * Command-line Modipulate player which can be controlled
 * via Open Sound Control (OSC).
 * ----
 * Jobs:
 * - Run the Modipulate engine
 * - Open a UDP socket and receive OSC messages
 * - Process OSC messages which map to Modipulate functions
 *   - Load/unload song, play/pause, jump position, mixing, etc.
 * - Send OSC messages corresponding to Modipulate events
 *   - Row played, pattern changed, etc.
 * Notes:
 * - Uses the zlib-licensed OSC library "oscpkt"
 * TODO:
 * - Lots.
 */


#include <iostream> // cout
#include <string>   // std::string
#include <cstdint>  // int32_t, etc
#include <map>      // map song IDs to song pointers

// Modipulate
#include <modipulate.h>

// oscpkt
#define OSCPKT_OSTREAM_OUTPUT
#include "oscpkt/oscpkt.hh"
#include "oscpkt/udp.hh"

// Hardcoded constants
#define APPNAME "modipulate-osc"
#define OUTPUT_BUFFER_SIZE 1024 // holds outbound OSC data
#define SLEEP_MS 50             // sleep between cycles (milliseconds)
// Prefixes for messages printed while the engine is running
#define PFX_INFO   "<info>    "
#define PFX_ERR    "<error>   "
#define PFX_CMD    "<execute> "
#define PFX_OSCIN  "<osc-in>  "
#define PFX_OSCOUT "<osc-out> "
// Helper macros
#define USAGE_MSG \
"Usage:\n" \
"  " APPNAME " --help\n" \
"  " APPNAME " [options]\n" \
"Options:\n" \
"  --help             View this help message and exit\n" \
"  --bind-port=port   Port for incoming messages (default: 7070)\n" \
"  --send-port=port   Port for outgoing messages (default: 7071)\n" \
"  --send-addr=addr   Address for outgoing messages (default: localhost)\n" \
"  --skip-bytes=N     Skip N bytes of incoming packets (default: 0)\n" \
"Examples:\n" \
"  " APPNAME "\n" \
"  " APPNAME " --send-addr=192.168.1.15 -send-port=9442\n" \
"  " APPNAME " --bind-port=7071 --skip-bytes=16\n"


// Modipulate: globals
ModipulateErr err = MODIPULATE_ERROR_NONE;
std::map<std::int32_t, ModipulateSong> song_map;

// oscpkt: globals
oscpkt::UdpSocket socketSend;
oscpkt::UdpSocket socketReceive;
int skip_bytes = 0;
int rcv_port = 7070;
int snd_port = 7071;
std::string snd_address = "127.0.0.1";
char snd_buffer[OUTPUT_BUFFER_SIZE] = "";

static ModipulateSong get_song_from_id(std::int32_t song_id)
{
    std::map<std::int32_t, ModipulateSong>::iterator iter = song_map.find(song_id);
    return iter != song_map.end() ? iter->second : NULL;
}

/*****
       Modipulate Callbacks
                            *****/


// Modipulate: pattern change callback
void on_pattern_change(ModipulateSong song, int pattern_number, void* user_data)
{
    oscpkt::Message msg("/modipulate/cb/patternchange");
    msg.pushInt32(pattern_number);

    oscpkt::PacketWriter pw;
    pw.startBundle().startBundle().addMessage(msg).endBundle().endBundle();
    socketSend.sendPacket(pw.packetData(), pw.packetSize());

    // std::cout << PFX_OSCOUT << "Modipulate: Pattern change: " << pattern_number << "\n";
}

// Modipulate: row change callback
void on_row_change(ModipulateSong song, int row, void* user_data)
{
    ; // TODO
    // Note: see below comments in on_note()
{'fraction_non_alphanumeric': 0.13679034421099687, 'fraction_numerical': 0.019967218000298017, 'mean_word_length': 3.4657351962741183, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5973601', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2302, 'n_words': 606}