<commit_before>//
//  osc_playback_manager.cpp
//  emptyExample
//
//  Created by <NAME> on 16-05-21.
//
//

#include "osc_playback_manager.hpp"
#include "osc_receiver.hpp"
#include "osc_sender.hpp"

using namespace of2030;

SINGLETON_INLINE_IMPLEMENTATION_CODE(OscPlaybackManager)


bool OscPlaybackManager::start(const string &name){
    // load file
    OscAsciiFile* file = new OscAsciiFile();
    if(!file){
        return false;
    }

    string path = nameToPath(name);
    if(!file->load(path)){
        ofLog() << "could not load osc file: " << path;
        delete file;
        return false;
    }

    // start playback
    OscPlayback* playback = new OscPlayback(*file);

    if(!playback){
        delete file;
        return false;
    }

    playback->start();
    // save it
    add(*playback);

    // ofLogVerbose() << "Started playback of: " << file->getReadPath();
    return true;
}

bool OscPlaybackManager::stop(const string &name){
    if(name == ""){ // stop all
        for(int i=playbacks.size()-1; i>=0; i--){
            remove(playbacks[i]);
        }

        playbacks.clear();
        return true;
    }

    OscPlayback *playback = getPlayback(name);
    if(!playback) return false;
    remove(playback);
    return true;
}

OscPlayback* OscPlaybackManager::getPlayback(const string &name){
    string p = nameToPath(name);

    for(int i=playbacks.size()-1; i>=0; i--){
        if(playbacks[i]->getFile()->getReadPath() == p){
            return playbacks[i];
        }
    }
    return NULL;
}

string OscPlaybackManager::nameToPath(const string &name){
    string p = ofToDataPath("osc/"+name+".csv");
    if(ofFile::doesFileExist(p))
        return p;

    p = ofToDataPath("osc/"+name);
    if(ofFile::doesFileExist(p))
        return p;

    if(ofFile::doesFileExist(name))
        return name;
}

void OscPlaybackManager::update(float dt){
    OscPlayback *playback;

    for(int i=playbacks.size()-1; i>=0; i--){
        playback = playbacks[i];
        if(!playback->update(dt)){
            remove(playback);
        }
    }
}

void OscPlaybackManager::add(OscPlayback &playback){
    // save for continued use
    playbacks.push_back(&playback);
    // register callback
    ofAddListener(playback.messageEvent, this, &OscPlaybackManager::onMessage);
}

bool OscPlaybackManager::remove(OscPlayback *playback){
    // find specified playback<commit_msg>FIX don't crash on missing osc file<commit_after>//
//  osc_playback_manager.cpp
//  emptyExample
//
//  Created by <NAME> on 16-05-21.
//
//

#include "osc_playback_manager.hpp"
#include "osc_receiver.hpp"
#include "osc_sender.hpp"

using namespace of2030;

SINGLETON_INLINE_IMPLEMENTATION_CODE(OscPlaybackManager)


bool OscPlaybackManager::start(const string &name){
    // load file
    OscAsciiFile* file = new OscAsciiFile();
    if(!file){
        return false;
    }

    string path = nameToPath(name);
    if(!file->load(path)){
        ofLog() << "could not load osc file: " << path;
        delete file;
        return false;
    }

    // start playback
    OscPlayback* playback = new OscPlayback(*file);

    if(!playback){
        delete file;
        return false;
    }

    playback->start();
    // save it
    add(*playback);

    // ofLogVerbose() << "Started playback of: " << file->getReadPath();
    return true;
}

bool OscPlaybackManager::stop(const string &name){
    if(name == ""){ // stop all
        for(int i=playbacks.size()-1; i>=0; i--){
            remove(playbacks[i]);
        }

        playbacks.clear();
        return true;
    }

    OscPlayback *playback = getPlayback(name);
    if(!playback) return false;
    remove(playback);
    return true;
}

OscPlayback* OscPlaybackManager::getPlayback(const string &name){
    string p = nameToPath(name);

    for(int i=playbacks.size()-1; i>=0; i--){
        if(playbacks[i]->getFile()->getReadPath() == p){
            return playbacks[i];
        }
    }
    return NULL;
}

string OscPlaybackManager::nameToPath(const string &name){
    string p = ofToDataPath("osc/"+name+".csv");
    if(ofFile::doesFileExist(p))
        return p;

    p = ofToDataPath("osc/"+name);
    if(ofFile::doesFileExist(p))
        return p;

    // if(ofFile::doesFileExist(name))
    return name;
}

void OscPlaybackManager::update(float dt){
    OscPlayback *playback;

    for(int i=playbacks.size()-1; i>=0; i--){
        playback = playbacks[i];
        if(!playback->update(dt)){
            remove(playback);
        }
    }
}

void OscPlaybackManager::add(OscPlayback &playback){
    // save for continued use
    playbacks.push_back(&playback);
    // register callback
    ofAddListener(playback.messageEvent, this, &OscPlaybackManager::onMessage);
}

bool OscPlaybackManager::remove(OscPlayback *playback){
    // find specified playback
{'fraction_non_alphanumeric': 0.14505084042332433, 'fraction_numerical': 0.006640381821954763, 'mean_word_length': 2.83147853736089, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '5198388', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1613, 'n_words': 323}