Add Kotlin API for audio tagging (#770)
This commit is contained in:
@@ -4,6 +4,11 @@
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging-impl.h"
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging-zipformer-impl.h"
|
||||
#include "sherpa-onnx/csrc/macros.h"
|
||||
|
||||
@@ -20,4 +25,17 @@ std::unique_ptr<AudioTaggingImpl> AudioTaggingImpl::Create(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
std::unique_ptr<AudioTaggingImpl> AudioTaggingImpl::Create(
|
||||
AAssetManager *mgr, const AudioTaggingConfig &config) {
|
||||
if (!config.model.zipformer.model.empty()) {
|
||||
return std::make_unique<AudioTaggingZipformerImpl>(mgr, config);
|
||||
}
|
||||
|
||||
SHERPA_ONNX_LOG(
|
||||
"Please specify an audio tagging model! Return a null pointer");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace sherpa_onnx
|
||||
|
||||
@@ -7,6 +7,11 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging.h"
|
||||
|
||||
namespace sherpa_onnx {
|
||||
@@ -18,6 +23,11 @@ class AudioTaggingImpl {
|
||||
static std::unique_ptr<AudioTaggingImpl> Create(
|
||||
const AudioTaggingConfig &config);
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
static std::unique_ptr<AudioTaggingImpl> Create(
|
||||
AAssetManager *mgr, const AudioTaggingConfig &config);
|
||||
#endif
|
||||
|
||||
virtual std::unique_ptr<OfflineStream> CreateStream() const = 0;
|
||||
|
||||
virtual std::vector<AudioEvent> Compute(OfflineStream *s,
|
||||
|
||||
@@ -8,7 +8,15 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include <strstream>
|
||||
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/macros.h"
|
||||
#include "sherpa-onnx/csrc/onnx-utils.h"
|
||||
#include "sherpa-onnx/csrc/text-utils.h"
|
||||
|
||||
namespace sherpa_onnx {
|
||||
@@ -18,6 +26,15 @@ AudioTaggingLabels::AudioTaggingLabels(const std::string &filename) {
|
||||
Init(is);
|
||||
}
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
AudioTaggingLabels::AudioTaggingLabels(AAssetManager *mgr,
|
||||
const std::string &filename) {
|
||||
auto buf = ReadFile(mgr, filename);
|
||||
std::istrstream is(buf.data(), buf.size());
|
||||
Init(is);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Format of a label file
|
||||
/*
|
||||
index,mid,display_name
|
||||
|
||||
@@ -8,11 +8,19 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
namespace sherpa_onnx {
|
||||
|
||||
class AudioTaggingLabels {
|
||||
public:
|
||||
explicit AudioTaggingLabels(const std::string &filename);
|
||||
#if __ANDROID_API__ >= 9
|
||||
AudioTaggingLabels(AAssetManager *mgr, const std::string &filename);
|
||||
#endif
|
||||
|
||||
// Return the event name for the given index.
|
||||
// The returned reference is valid as long as this object is alive
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging-impl.h"
|
||||
#include "sherpa-onnx/csrc/audio-tagging-label-file.h"
|
||||
#include "sherpa-onnx/csrc/audio-tagging.h"
|
||||
@@ -28,6 +33,20 @@ class AudioTaggingZipformerImpl : public AudioTaggingImpl {
|
||||
}
|
||||
}
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
explicit AudioTaggingZipformerImpl(AAssetManager *mgr,
|
||||
const AudioTaggingConfig &config)
|
||||
: config_(config),
|
||||
model_(mgr, config.model),
|
||||
labels_(mgr, config.labels) {
|
||||
if (model_.NumEventClasses() != labels_.NumEventClasses()) {
|
||||
SHERPA_ONNX_LOGE("number of classes: %d (model) != %d (label file)",
|
||||
model_.NumEventClasses(), labels_.NumEventClasses());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
std::unique_ptr<OfflineStream> CreateStream() const override {
|
||||
return std::make_unique<OfflineStream>();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging.h"
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging-impl.h"
|
||||
#include "sherpa-onnx/csrc/file-utils.h"
|
||||
#include "sherpa-onnx/csrc/macros.h"
|
||||
@@ -61,6 +66,11 @@ std::string AudioTaggingConfig::ToString() const {
|
||||
AudioTagging::AudioTagging(const AudioTaggingConfig &config)
|
||||
: impl_(AudioTaggingImpl::Create(config)) {}
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
AudioTagging::AudioTagging(AAssetManager *mgr, const AudioTaggingConfig &config)
|
||||
: impl_(AudioTaggingImpl::Create(mgr, config)) {}
|
||||
#endif
|
||||
|
||||
AudioTagging::~AudioTagging() = default;
|
||||
|
||||
std::unique_ptr<OfflineStream> AudioTagging::CreateStream() const {
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
#include "android/asset_manager.h"
|
||||
#include "android/asset_manager_jni.h"
|
||||
#endif
|
||||
|
||||
#include "sherpa-onnx/csrc/audio-tagging-model-config.h"
|
||||
#include "sherpa-onnx/csrc/offline-stream.h"
|
||||
#include "sherpa-onnx/csrc/parse-options.h"
|
||||
@@ -46,6 +51,10 @@ class AudioTagging {
|
||||
public:
|
||||
explicit AudioTagging(const AudioTaggingConfig &config);
|
||||
|
||||
#if __ANDROID_API__ >= 9
|
||||
AudioTagging(AAssetManager *mgr, const AudioTaggingConfig &config);
|
||||
#endif
|
||||
|
||||
~AudioTagging();
|
||||
|
||||
std::unique_ptr<OfflineStream> CreateStream() const;
|
||||
|
||||
Reference in New Issue
Block a user