Skip to content

Commit

Permalink
Unified MIDI extension.
Browse files Browse the repository at this point in the history
context: #142
  • Loading branch information
atsushieno committed Jan 28, 2023
1 parent bf0d41a commit 4ae6bfd
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <sys/mman.h>
#include "aap/unstable/logging.h"
#include "aap/ext/aap-midi2.h"
#include "aap/ext/midi.h"
#include "AAPMidiProcessor.h"

namespace aapmidideviceservice {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,73 @@
#include <functional>
#include "aap/android-audio-plugin.h"
#include "aap/unstable/aapxs.h"
#include "aap/ext/aap-midi2.h"
#include "aap/ext/midi.h"
#include "aap/unstable/logging.h"
#include "aap/core/aapxs/extension-service.h"
#include "extension-service-impl.h"

namespace aap {

// implementation details
const int32_t OPCODE_GET_MAPPING_POLICY = 0;

class Midi2PluginClientExtension : public PluginClientExtensionImplBase {

class MidiPluginClientExtension : public PluginClientExtensionImplBase {
class Instance {
friend class Midi2PluginClientExtension;
friend class MidiPluginClientExtension;

aap_midi2_extension_t proxy{};

//Midi2PluginClientExtension *owner;
MidiPluginClientExtension *owner;
AAPXSClientInstance* aapxsInstance;

static enum aap_midi_mapping_policy internalGetMappingPolicy(AndroidAudioPluginExtensionTarget target, const char* pluginId) {
return ((Instance *) target.aapxs_context)->getMappingPolicy(pluginId);
}

public:
Instance(Midi2PluginClientExtension *owner, AAPXSClientInstance *clientInstance)
//: owner(owner)
Instance(MidiPluginClientExtension *owner, AAPXSClientInstance *clientInstance)
: owner(owner)
{
aapxsInstance = clientInstance;
}

/*
enum aap_midi_mapping_policy getMappingPolicy(const char* pluginId) {
auto len = strlen(pluginId);
assert(len < MAX_PLUGIN_ID_SIZE);
*((int32_t*) aapxsInstance->data) = len;
strcpy((char*) ((int32_t*) aapxsInstance->data + 1), pluginId);
clientInvokePluginExtension(OPCODE_GET_MAPPING_POLICY);
return *((enum aap_midi_mapping_policy *) aapxsInstance->data);
}

void clientInvokePluginExtension(int32_t opcode) {
owner->clientInvokePluginExtension(aapxsInstance, opcode);
}
*/

AAPXSProxyContext asProxy() {
proxy.context = this;
return AAPXSProxyContext{aapxsInstance, nullptr, &proxy};
proxy.get_mapping_policy = internalGetMappingPolicy;
return AAPXSProxyContext{aapxsInstance, this, &proxy};
}
};

// FIXME: This tells there is maximum # of instances - we need some better method to retain pointers
// to each Instance that at least lives as long as AAPXSClientInstance lifetime.
// (Should we add `addDisposableListener` at AAPXSClient to make it possible to free
// this Instance at plugin instance disposal? Maybe when if 1024 for instances sounds insufficient...)
#define MIDI2_AAPXS_MAX_INSTANCE_COUNT 1024
#define MIDI_AAPXS_MAX_INSTANCE_COUNT 1024

std::unique_ptr<Instance> instances[MIDI2_AAPXS_MAX_INSTANCE_COUNT]{};
std::unique_ptr<Instance> instances[MIDI_AAPXS_MAX_INSTANCE_COUNT]{};
std::map<int32_t,int32_t> instance_map{}; // map from instanceId to the index of the Instance in `instances`.

public:
Midi2PluginClientExtension()
MidiPluginClientExtension()
: PluginClientExtensionImplBase() {
}

AAPXSProxyContext asProxy(AAPXSClientInstance *clientInstance) override {
size_t last = 0;
for (; last < MIDI2_AAPXS_MAX_INSTANCE_COUNT; last++) {
for (; last < MIDI_AAPXS_MAX_INSTANCE_COUNT; last++) {
if (instances[last] == nullptr)
break;
if (instances[last]->aapxsInstance == clientInstance)
Expand All @@ -71,30 +84,39 @@ class Midi2PluginClientExtension : public PluginClientExtensionImplBase {
}
};

class Midi2PluginServiceExtension : public PluginServiceExtensionImplBase {
class MidiPluginServiceExtension : public PluginServiceExtensionImplBase {

public:
Midi2PluginServiceExtension()
: PluginServiceExtensionImplBase(AAP_MIDI2_EXTENSION_URI) {
MidiPluginServiceExtension()
: PluginServiceExtensionImplBase(AAP_MIDI_EXTENSION_URI) {
}

// invoked by AudioPluginService
void onInvoked(AndroidAudioPlugin* plugin, AAPXSServiceInstance *extensionInstance,
int32_t opcode) override {
switch (opcode) {
case OPCODE_GET_MAPPING_POLICY:
auto len = *(int32_t*) extensionInstance->data;
assert(len < MAX_PLUGIN_ID_SIZE);
char* pluginId = (char*) calloc(len, 1);
strncpy(pluginId, (const char*) ((int32_t*) extensionInstance->data + 1), len);
*((int32_t*) extensionInstance->data) = getMidiSettingsFromSharedPreference(pluginId);
return;
}
assert(false); // should not happen
}
};


class Midi2ExtensionFeature : public PluginExtensionFeatureImpl {
class MidiExtensionFeature : public PluginExtensionFeatureImpl {
std::unique_ptr<PluginClientExtensionImplBase> client;
std::unique_ptr<PluginServiceExtensionImplBase> service;

public:
Midi2ExtensionFeature()
: PluginExtensionFeatureImpl(AAP_MIDI2_EXTENSION_URI, false, sizeof(aap_midi2_extension_t)),
client(std::make_unique<Midi2PluginClientExtension>()),
service(std::make_unique<Midi2PluginServiceExtension>()) {
MidiExtensionFeature()
: PluginExtensionFeatureImpl(AAP_MIDI_EXTENSION_URI, false, sizeof(aap_midi2_extension_t)),
client(std::make_unique<MidiPluginClientExtension>()),
service(std::make_unique<MidiPluginServiceExtension>()) {
}

PluginClientExtensionImplBase* getClient() { return client.get(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
namespace aap {

// implementation details
const int32_t OPCODE_GET_MAPPING_POLICY = 0;

const int32_t MAX_PLUGIN_ID_SIZE = 1024; // FIXME: there should be some official definition.

class ParametersPluginClientExtension : public PluginClientExtensionImplBase {
Expand All @@ -27,32 +25,18 @@ class ParametersPluginClientExtension : public PluginClientExtensionImplBase {
ParametersPluginClientExtension *owner;
AAPXSClientInstance* aapxsInstance;

static enum aap_parameters_mapping_policy internalGetMappingPolicy(AndroidAudioPluginExtensionTarget target, const char* pluginId) {
return ((Instance *) target.aapxs_context)->getMappingPolicy(pluginId);
}

public:
Instance(ParametersPluginClientExtension *owner, AAPXSClientInstance *clientInstance)
: owner(owner)
{
aapxsInstance = clientInstance;
}

enum aap_parameters_mapping_policy getMappingPolicy(const char* pluginId) {
auto len = strlen(pluginId);
assert(len < MAX_PLUGIN_ID_SIZE);
*((int32_t*) aapxsInstance->data) = len;
strcpy((char*) ((int32_t*) aapxsInstance->data + 1), pluginId);
clientInvokePluginExtension(OPCODE_GET_MAPPING_POLICY);
return *((enum aap_parameters_mapping_policy *) aapxsInstance->data);
}

void clientInvokePluginExtension(int32_t opcode) {
owner->clientInvokePluginExtension(aapxsInstance, opcode);
}

AAPXSProxyContext asProxy() {
proxy.get_mapping_policy = internalGetMappingPolicy;
return AAPXSProxyContext{aapxsInstance, this, &proxy};
}
};
Expand Down Expand Up @@ -95,15 +79,6 @@ class ParametersPluginServiceExtension : public PluginServiceExtensionImplBase {
// invoked by AudioPluginService
void onInvoked(AndroidAudioPlugin* plugin, AAPXSServiceInstance *extensionInstance,
int32_t opcode) override {
switch (opcode) {
case OPCODE_GET_MAPPING_POLICY:
auto len = *(int32_t*) extensionInstance->data;
assert(len < MAX_PLUGIN_ID_SIZE);
char* pluginId = (char*) calloc(len, 1);
strncpy(pluginId, (const char*) ((int32_t*) extensionInstance->data + 1), len);
*((int32_t*) extensionInstance->data) = getMidiSettingsFromSharedPreference(pluginId);
return;
}
assert(false); // should not happen
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "aap/core/host/plugin-client-system.h"
#include "../extensions/parameters-service.h"
#include "../extensions/presets-service.h"
#include "../extensions/midi2-service.h"
#include "../extensions/midi-service.h"
#include "../extensions/port-config-service.h"

#define AAP_HOST_TAG "AAP_HOST"
Expand Down Expand Up @@ -153,7 +153,7 @@ int32_t PluginSharedMemoryStore::allocateServiceBuffer(std::vector<int32_t>& cli
std::unique_ptr<StateExtensionFeature> aapxs_state{nullptr};
std::unique_ptr<PresetsExtensionFeature> aapxs_presets{nullptr};
std::unique_ptr<PortConfigExtensionFeature> aapxs_port_config{nullptr};
std::unique_ptr<Midi2ExtensionFeature> aapxs_midi2{nullptr};
std::unique_ptr<MidiExtensionFeature> aapxs_midi2{nullptr};
std::unique_ptr<ParametersExtensionFeature> aapxs_parameters{nullptr};

PluginHost::PluginHost(PluginListSnapshot* contextPluginList)
Expand All @@ -177,7 +177,7 @@ PluginHost::PluginHost(PluginListSnapshot* contextPluginList)
aapxs_registry->add(aapxs_state->asPublicApi());
// midi2
if (aapxs_midi2 == nullptr)
aapxs_midi2 = std::make_unique<Midi2ExtensionFeature>();
aapxs_midi2 = std::make_unique<MidiExtensionFeature>();
aapxs_registry->add(aapxs_midi2->asPublicApi());
// parameters
if (aapxs_parameters == nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import android.os.Build
import org.androidaudioplugin.hosting.AudioPluginMidiSettings

/**
* The Audio plugin service class. Every AAP (plugin) derives from this class, either directly or indirectly.
* The Audio plugin service class. It should not be derived. We check the service class name strictly.
*
* Every AAP should implement AudioPluginInterface.aidl, in native code.
*
* Typical user developers don't have to write any code to create their own plugins in Kotlin code.
* They are implemented in native code to get closest to realtime audio processing.
Expand Down
3 changes: 0 additions & 3 deletions include/aap/core/aapxs/extension-service.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
#include <map>
#include <cassert>
#include "aap/unstable/aapxs.h"
#include "aap/ext/presets.h"
#include "aap/unstable/logging.h"
#include "../plugin-information.h"
#include "aap/android-audio-plugin.h"
#include "aap/ext/presets.h"
#include "aap/ext/state.h"

namespace aap {

Expand Down
33 changes: 32 additions & 1 deletion include/aap/core/aapxs/standard-extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define AAP_CORE_STANDARD_EXTENSIONS_H

#include "extension-service.h"
#include "aap/ext/midi.h"
#include "aap/ext/state.h"
#include "aap/ext/presets.h"

// FIXME: should this be moved to somewhere?
extern "C" int32_t getMidiSettingsFromSharedPreference(std::string pluginId);
Expand Down Expand Up @@ -46,6 +49,12 @@ virtual TYPE withParametersExtension(TYPE dummyValue, std::function<TYPE(aap_par
DEFINE_WITH_PARAMETERS_EXTENSION(int32_t)
virtual void withParametersExtension(std::function<void(aap_parameters_extension_t*, AndroidAudioPluginExtensionTarget)> func) = 0;

#define DEFINE_WITH_MIDI_EXTENSION(TYPE) \
virtual TYPE withMidiExtension(TYPE dummyValue, std::function<TYPE(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget target)> func) = 0;

DEFINE_WITH_MIDI_EXTENSION(int32_t)
virtual void withMidiExtension(std::function<void(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget)> func) = 0;

public:
size_t getStateSize() {
return withStateExtension/*<size_t>*/(0, [&](aap_state_extension_t* ext, AndroidAudioPluginExtensionTarget target) {
Expand Down Expand Up @@ -101,7 +110,7 @@ virtual TYPE withParametersExtension(TYPE dummyValue, std::function<TYPE(aap_par
}

int32_t getMidiMappingPolicy(std::string pluginId) {
return withParametersExtension/*<int32_t >*/(0, [&](aap_parameters_extension_t* ext, AndroidAudioPluginExtensionTarget target) {
return withMidiExtension(0, [&](aap_midi_extension_t* ext, AndroidAudioPluginExtensionTarget target) {
if (ext && ext->get_mapping_policy)
return (int32_t) ext->get_mapping_policy(target, pluginId.c_str());
else
Expand Down Expand Up @@ -155,6 +164,17 @@ TYPE withParametersExtension(TYPE dummyValue, std::function<TYPE(aap_parameters_
});
}

#define DEFINE_WITH_MIDI_EXTENSION_LOCAL(TYPE) \
TYPE withMidiExtension(TYPE dummyValue, std::function<TYPE(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget target)> func) override { return withExtension<TYPE, aap_midi_extension_t>(false, 0, AAP_PARAMETERS_EXTENSION_URI, func); }

DEFINE_WITH_MIDI_EXTENSION_LOCAL(int32_t)
virtual void withMidiExtension(std::function<void(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget)> func) override {
withExtension<int, aap_midi_extension_t>(false, 0, AAP_MIDI_EXTENSION_URI, [&](aap_midi_extension_t* ext, AndroidAudioPluginExtensionTarget target) {
func(ext, target);
return 0;
});
}

public:
virtual AndroidAudioPlugin* getPlugin() = 0;
};
Expand Down Expand Up @@ -206,6 +226,17 @@ TYPE withParametersExtension(TYPE dummyValue, std::function<TYPE(aap_parameters_
});
}

#define DEFINE_WITH_MIDI_EXTENSION_REMOTE(TYPE) \
TYPE withMidiExtension(TYPE dummyValue, std::function<TYPE(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget target)> func) override { return withExtension<TYPE, aap_midi_extension_t>(false, 0, AAP_MIDI_EXTENSION_URI, func); }

DEFINE_WITH_MIDI_EXTENSION_REMOTE(int32_t)
virtual void withMidiExtension(std::function<void(aap_midi_extension_t*, AndroidAudioPluginExtensionTarget)> func) override {
withExtension<int, aap_midi_extension_t>(false, 0, AAP_MIDI_EXTENSION_URI, [&](aap_midi_extension_t* ext, AndroidAudioPluginExtensionTarget target) {
func(ext, target);
return 0;
});
}

public:
virtual AndroidAudioPlugin* getPlugin() = 0;
virtual AAPXSClientInstanceManager* getAAPXSManager() = 0;
Expand Down
32 changes: 0 additions & 32 deletions include/aap/ext/aap-midi2.h

This file was deleted.

Loading

0 comments on commit 4ae6bfd

Please sign in to comment.