<commit_before>#include "WinRTMidi.h"
#include "WinRTMidiportWatcher.h"

using namespace WinRT;
using namespace Windows::Devices::Midi;
using namespace Windows::Storage::Streams;
using namespace std::placeholders;

/*****************************************************
    MidiInPortImpl
*****************************************************/

MidiInPortImpl::MidiInPortImpl()
    : mCallback(nullptr)
{
    mPort = ref new WinRTMidiInPort;
    mPort->SetMidiInCallback(std::bind(&MidiInPortImpl::MidiInCallback, this, _1, _2));
}

MidiInPortImpl::~MidiInPortImpl()
{

}

void MidiInPortImpl::OpenPort(unsigned int index)
{
    mPort->OpenPort(index);
}

void MidiInPortImpl::ClosePort()
{
    mPort->ClosePort();
}

void MidiInPortImpl::MidiInCallback(double timestamp, std::vector<unsigned char>* message)
{
    if (mCallback)
    {
        mCallback(this, timestamp, message);
    }
}

void MidiInPortImpl::RemoveCallback()
{
    mCallback = nullptr;
}

void MidiInPortImpl::SetCallback(const IMidiInCallbackType& callback)
{
    mCallback = callback;
}

void MidiInPortImpl::Destroy()
{
    delete this;
}


/*****************************************************
    WinRTMidiInPort
*****************************************************/


WinRTMidiInPort::WinRTMidiInPort()
    : mMessageReceivedCallback(nullptr)
{
}

WinRTMidiInPort::~WinRTMidiInPort()
{

}

//Blocks until port is open
void WinRTMidiInPort::OpenPort(int index)
{
    auto id = WinRTMidiInPortWatcher::getInstance()->GetPortId(index);
    OpenPort(id);
}


//Blocks until port is open
void WinRTMidiInPort::OpenPort(Platform::String^ id)
{
    try
    {
        mLastMessageTime = 0;
        mFirstMessage = true;
        auto task = MidiInPort::FromIdAsync(id);
        mMidiInPort = task->GetResults();
        mMidiInPort->MessageReceived += ref new Windows::Foundation::TypedEventHandler<MidiInPort ^, MidiMessageReceivedEventArgs ^>(this, &WinRTMidiInPort::OnMidiInMessageReceived);<commit_msg>use async task to create midi in port<commit_after>#include "WinRTMidi.h"
#include "WinRTMidiportWatcher.h"
#include <ppltasks.h>

using namespace WinRT;
using namespace Windows::Devices::Midi;
using namespace Windows::Storage::Streams;
using namespace std::placeholders;
using namespace concurrency;

/*****************************************************
    MidiInPortImpl
*****************************************************/

MidiInPortImpl::MidiInPortImpl()
    : mCallback(nullptr)
{
    mPort = ref new WinRTMidiInPort;
    mPort->SetMidiInCallback(std::bind(&MidiInPortImpl::MidiInCallback, this, _1, _2));
}

MidiInPortImpl::~MidiInPortImpl()
{

}

void MidiInPortImpl::OpenPort(unsigned int index)
{
    mPort->OpenPort(index);
}

void MidiInPortImpl::ClosePort()
{
    mPort->ClosePort();
}

void MidiInPortImpl::MidiInCallback(double timestamp, std::vector<unsigned char>* message)
{
    if (mCallback)
    {
        mCallback(this, timestamp, message);
    }
}

void MidiInPortImpl::RemoveCallback()
{
    mCallback = nullptr;
}

void MidiInPortImpl::SetCallback(const IMidiInCallbackType& callback)
{
    mCallback = callback;
}

void MidiInPortImpl::Destroy()
{
    delete this;
}


/*****************************************************
    WinRTMidiInPort
*****************************************************/


WinRTMidiInPort::WinRTMidiInPort()
    : mMessageReceivedCallback(nullptr)
{
}

WinRTMidiInPort::~WinRTMidiInPort()
{

}

//Blocks until port is open
void WinRTMidiInPort::OpenPort(int index)
{
    auto id = WinRTMidiInPortWatcher::getInstance()->GetPortId(index);
    OpenPort(id);
}


//Blocks until port is open
void WinRTMidiInPort::OpenPort(Platform::String^ id)
{
    mLastMessageTime = 0;
    mFirstMessage = true;
    auto task = create_task(MidiInPort::FromIdAsync(id));

    // wait for port to be created
    task.wait();

    // get results
    try
    {
        mMidiInPort = task.get();
        mMidiInPort->MessageReceived += ref new Windows::Foundation::TypedEventHandler<MidiInPort ^, MidiMessageReceivedEventArgs ^>(this, &WinRTMidiInPort::OnMidiInMessageReceived);
{'fraction_non_alphanumeric': 0.21129860601614087, 'fraction_numerical': 0.001467351430667645, 'mean_word_length': 5.492063492063492, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 19, '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': '5250303', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1293, 'n_words': 224}