build: fix cpp formatting

This commit is contained in:
Alexander Capehart 2024-12-26 18:38:38 -05:00
parent 8d49893309
commit 5e7d575efd
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
11 changed files with 362 additions and 299 deletions

View file

@ -43,8 +43,8 @@ spotless {
} }
cpp { cpp {
target "*/src/**/cpp/*.cpp" target("*/src/**/cpp/*.cpp", "*/src/**/cpp/*.h", "*/src/**/cpp/*.hpp")
eclipseCdt() eclipseCdt().configFile("eclipse-cdt.xml")
licenseHeaderFile("NOTICE") licenseHeaderFile("NOTICE")
} }
} }

8
eclipse-cdt.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<profiles version="12">
<profile kind="CodeFormatterProfile" name="CustomProfile" version="12">
<setting id="org.eclipse.cdt.core.formatter.tabulation.char" value="space"/>
<setting id="org.eclipse.cdt.core.formatter.tabulation.size" value="4"/>
<setting id="org.eclipse.cdt.core.formatter.indentation.size" value="4"/>
</profile>
</profiles>

View file

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest
xmlns:android="http://schemas.android.com/apk/res/android">
</manifest> </manifest>

View file

@ -22,102 +22,102 @@
// TODO: Handle stream exceptions // TODO: Handle stream exceptions
JVMInputStream::JVMInputStream(JNIEnv *env, jobject inputStream) : env(env), inputStream( JVMInputStream::JVMInputStream(JNIEnv *env, jobject inputStream) : env(env), inputStream(
inputStream) { inputStream) {
if (!env->IsInstanceOf(inputStream, if (!env->IsInstanceOf(inputStream,
env->FindClass("org/oxycblt/musikr/metadata/NativeInputStream"))) { env->FindClass("org/oxycblt/musikr/metadata/NativeInputStream"))) {
throw std::runtime_error("oStream is not an instance of TagLibOStream"); throw std::runtime_error("oStream is not an instance of TagLibOStream");
} }
jclass inputStreamClass = env->FindClass( jclass inputStreamClass = env->FindClass(
"org/oxycblt/musikr/metadata/NativeInputStream"); "org/oxycblt/musikr/metadata/NativeInputStream");
inputStreamReadBlockMethod = env->GetMethodID(inputStreamClass, "readBlock", inputStreamReadBlockMethod = env->GetMethodID(inputStreamClass, "readBlock",
"(J)[B"); "(J)[B");
inputStreamIsOpenMethod = env->GetMethodID(inputStreamClass, "isOpen", inputStreamIsOpenMethod = env->GetMethodID(inputStreamClass, "isOpen",
"()Z"); "()Z");
inputStreamSeekFromBeginningMethod = env->GetMethodID(inputStreamClass, inputStreamSeekFromBeginningMethod = env->GetMethodID(inputStreamClass,
"seekFromBeginning", "(J)V"); "seekFromBeginning", "(J)V");
inputStreamSeekFromCurrentMethod = env->GetMethodID(inputStreamClass, inputStreamSeekFromCurrentMethod = env->GetMethodID(inputStreamClass,
"seekFromCurrent", "(J)V"); "seekFromCurrent", "(J)V");
inputStreamSeekFromEndMethod = env->GetMethodID(inputStreamClass, inputStreamSeekFromEndMethod = env->GetMethodID(inputStreamClass,
"seekFromEnd", "(J)V"); "seekFromEnd", "(J)V");
inputStreamTellMethod = env->GetMethodID(inputStreamClass, "tell", "()J"); inputStreamTellMethod = env->GetMethodID(inputStreamClass, "tell", "()J");
inputStreamLengthMethod = env->GetMethodID(inputStreamClass, "length", inputStreamLengthMethod = env->GetMethodID(inputStreamClass, "length",
"()J"); "()J");
env->DeleteLocalRef(inputStreamClass); env->DeleteLocalRef(inputStreamClass);
} }
JVMInputStream::~JVMInputStream() { JVMInputStream::~JVMInputStream() {
// The implicit assumption is that inputStream is managed by the owner, // The implicit assumption is that inputStream is managed by the owner,
// so we don't need to delete any references here // so we don't need to delete any references here
} }
TagLib::FileName JVMInputStream::name() const { TagLib::FileName JVMInputStream::name() const {
// Not actually used except in FileRef, can safely ignore. // Not actually used except in FileRef, can safely ignore.
return ""; return "";
} }
TagLib::ByteVector JVMInputStream::readBlock(size_t length) { TagLib::ByteVector JVMInputStream::readBlock(size_t length) {
auto data = (jbyteArray) env->CallObjectMethod(inputStream, auto data = (jbyteArray) env->CallObjectMethod(inputStream,
inputStreamReadBlockMethod, length); inputStreamReadBlockMethod, length);
jsize dataLength = env->GetArrayLength(data); jsize dataLength = env->GetArrayLength(data);
auto dataBytes = env->GetByteArrayElements(data, nullptr); auto dataBytes = env->GetByteArrayElements(data, nullptr);
TagLib::ByteVector byteVector(reinterpret_cast<const char*>(dataBytes), TagLib::ByteVector byteVector(reinterpret_cast<const char*>(dataBytes),
dataLength); dataLength);
env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT); env->ReleaseByteArrayElements(data, dataBytes, JNI_ABORT);
return byteVector; return byteVector;
} }
void JVMInputStream::writeBlock(const TagLib::ByteVector &data) { void JVMInputStream::writeBlock(const TagLib::ByteVector &data) {
throw std::runtime_error("Not implemented"); throw std::runtime_error("Not implemented");
} }
void JVMInputStream::insert(const TagLib::ByteVector &data, void JVMInputStream::insert(const TagLib::ByteVector &data,
TagLib::offset_t start, size_t replace) { TagLib::offset_t start, size_t replace) {
throw std::runtime_error("Not implemented"); throw std::runtime_error("Not implemented");
} }
void JVMInputStream::removeBlock(TagLib::offset_t start, size_t length) { void JVMInputStream::removeBlock(TagLib::offset_t start, size_t length) {
throw std::runtime_error("Not implemented"); throw std::runtime_error("Not implemented");
} }
bool JVMInputStream::readOnly() const { bool JVMInputStream::readOnly() const {
return true; return true;
} }
bool JVMInputStream::isOpen() const { bool JVMInputStream::isOpen() const {
return env->CallBooleanMethod(inputStream, inputStreamIsOpenMethod); return env->CallBooleanMethod(inputStream, inputStreamIsOpenMethod);
} }
void JVMInputStream::seek(TagLib::offset_t offset, Position p) { void JVMInputStream::seek(TagLib::offset_t offset, Position p) {
auto joffset = static_cast<jlong>(std::llround(offset)); auto joffset = static_cast<jlong>(std::llround(offset));
switch (p) { switch (p) {
case Beginning: case Beginning:
env->CallVoidMethod(inputStream, inputStreamSeekFromBeginningMethod, env->CallVoidMethod(inputStream, inputStreamSeekFromBeginningMethod,
joffset); joffset);
break; break;
case Current: case Current:
env->CallVoidMethod(inputStream, inputStreamSeekFromCurrentMethod, env->CallVoidMethod(inputStream, inputStreamSeekFromCurrentMethod,
joffset); joffset);
break; break;
case End: case End:
env->CallVoidMethod(inputStream, inputStreamSeekFromEndMethod, joffset); env->CallVoidMethod(inputStream, inputStreamSeekFromEndMethod, joffset);
break; break;
} }
} }
void JVMInputStream::clear() { void JVMInputStream::clear() {
// Nothing to do // Nothing to do
} }
TagLib::offset_t JVMInputStream::tell() const { TagLib::offset_t JVMInputStream::tell() const {
jlong jposition = env->CallLongMethod(inputStream, inputStreamTellMethod); jlong jposition = env->CallLongMethod(inputStream, inputStreamTellMethod);
return static_cast<TagLib::offset_t>(jposition); return static_cast<TagLib::offset_t>(jposition);
} }
TagLib::offset_t JVMInputStream::length() { TagLib::offset_t JVMInputStream::length() {
jlong jlength = env->CallLongMethod(inputStream, inputStreamLengthMethod); jlong jlength = env->CallLongMethod(inputStream, inputStreamLengthMethod);
return static_cast<TagLib::offset_t>(jlength); return static_cast<TagLib::offset_t>(jlength);
} }
void JVMInputStream::truncate(TagLib::offset_t length) { void JVMInputStream::truncate(TagLib::offset_t length) {
throw std::runtime_error("Not implemented"); throw std::runtime_error("Not implemented");
} }

View file

@ -1,6 +1,20 @@
// /*
// Created by oxycblt on 12/12/24. * Copyright (c) 2024 Auxio Project
// * JVMInputStream.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef AUXIO_JVMINPUTSTREAM_H #ifndef AUXIO_JVMINPUTSTREAM_H
#define AUXIO_JVMINPUTSTREAM_H #define AUXIO_JVMINPUTSTREAM_H
@ -9,14 +23,14 @@
#include "taglib/tiostream.h" #include "taglib/tiostream.h"
class JVMInputStream : public TagLib::IOStream { class JVMInputStream: public TagLib::IOStream {
public: public:
JVMInputStream(JNIEnv *env, jobject inputStream); JVMInputStream(JNIEnv *env, jobject inputStream);
~JVMInputStream(); ~JVMInputStream();
JVMInputStream(const JVMInputStream &) = delete; JVMInputStream(const JVMInputStream&) = delete;
JVMInputStream &operator=(const JVMInputStream &) = delete; JVMInputStream& operator=(const JVMInputStream&) = delete;
/*! /*!
* Returns the stream name in the local file system encoding. * Returns the stream name in the local file system encoding.
@ -46,8 +60,8 @@ public:
* \note This method is slow since it requires rewriting all of the file * \note This method is slow since it requires rewriting all of the file
* after the insertion point. * after the insertion point.
*/ */
void insert(const TagLib::ByteVector &data, void insert(const TagLib::ByteVector &data, TagLib::offset_t start = 0,
TagLib::offset_t start = 0, size_t replace = 0) override; size_t replace = 0) override;
/*! /*!
* Removes a block of the file starting a \a start and continuing for * Removes a block of the file starting a \a start and continuing for
@ -110,5 +124,4 @@ private:
}; };
#endif //AUXIO_JVMINPUTSTREAM_H #endif //AUXIO_JVMINPUTSTREAM_H

View file

@ -26,142 +26,142 @@
#include <taglib/tpropertymap.h> #include <taglib/tpropertymap.h>
JVMMetadataBuilder::JVMMetadataBuilder(JNIEnv *env) : env(env), id3v2(env), xiph( JVMMetadataBuilder::JVMMetadataBuilder(JNIEnv *env) : env(env), id3v2(env), xiph(
env), mp4(env), cover(), properties(nullptr) { env), mp4(env), cover(), properties(nullptr) {
} }
void JVMMetadataBuilder::setMimeType(const std::string_view type) { void JVMMetadataBuilder::setMimeType(const std::string_view type) {
this->mimeType = type; this->mimeType = type;
} }
void JVMMetadataBuilder::setId3v2(const TagLib::ID3v2::Tag &tag) { void JVMMetadataBuilder::setId3v2(const TagLib::ID3v2::Tag &tag) {
for (auto frame : tag.frameList()) { for (auto frame : tag.frameList()) {
if (auto txxxFrame = if (auto txxxFrame =
dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(frame)) { dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(frame)) {
TagLib::StringList frameText = txxxFrame->fieldList(); TagLib::StringList frameText = txxxFrame->fieldList();
// Frame text starts with the description then the remaining values // Frame text starts with the description then the remaining values
auto begin = frameText.begin(); auto begin = frameText.begin();
TagLib::String key = TagLib::String(frame->frameID()) + ":" TagLib::String key = TagLib::String(frame->frameID()) + ":"
+ begin->upper(); + begin->upper();
frameText.erase(begin); frameText.erase(begin);
id3v2.add(key, frameText); id3v2.add(key, frameText);
} else if (auto textFrame = } else if (auto textFrame =
dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(frame)) { dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(frame)) {
TagLib::String key = frame->frameID(); TagLib::String key = frame->frameID();
TagLib::StringList frameText = textFrame->fieldList(); TagLib::StringList frameText = textFrame->fieldList();
id3v2.add(key, frameText); id3v2.add(key, frameText);
} else { } else {
continue; continue;
} }
} }
} }
void JVMMetadataBuilder::setXiph(const TagLib::Ogg::XiphComment &tag) { void JVMMetadataBuilder::setXiph(const TagLib::Ogg::XiphComment &tag) {
for (auto field : tag.fieldListMap()) { for (auto field : tag.fieldListMap()) {
auto key = field.first.upper(); auto key = field.first.upper();
auto values = field.second; auto values = field.second;
xiph.add(key, values); xiph.add(key, values);
} }
} }
void JVMMetadataBuilder::setMp4(const TagLib::MP4::Tag &tag) { void JVMMetadataBuilder::setMp4(const TagLib::MP4::Tag &tag) {
auto map = tag.itemMap(); auto map = tag.itemMap();
for (auto item : map) { for (auto item : map) {
auto itemName = item.first; auto itemName = item.first;
if (itemName.startsWith("----")) { if (itemName.startsWith("----")) {
// Capitalize description atoms only // Capitalize description atoms only
// Other standard atoms are cased so we want to avoid collissions there. // Other standard atoms are cased so we want to avoid collissions there.
itemName = itemName.upper(); itemName = itemName.upper();
} }
auto itemValue = item.second; auto itemValue = item.second;
auto type = itemValue.type(); auto type = itemValue.type();
// Only read out the atoms for the reasonable tags we are expecting. // Only read out the atoms for the reasonable tags we are expecting.
// None of the crazy binary atoms. // None of the crazy binary atoms.
if (type == TagLib::MP4::Item::Type::StringList) { if (type == TagLib::MP4::Item::Type::StringList) {
auto value = itemValue.toStringList(); auto value = itemValue.toStringList();
mp4.add(itemName, value); mp4.add(itemName, value);
continue; continue;
} }
// Assume that taggers will be unhinged and store track numbers // Assume that taggers will be unhinged and store track numbers
// as ints, uints, or longs. // as ints, uints, or longs.
if (type == TagLib::MP4::Item::Type::Int) { if (type == TagLib::MP4::Item::Type::Int) {
auto value = std::to_string(itemValue.toInt()); auto value = std::to_string(itemValue.toInt());
id3v2.add(itemName, value); id3v2.add(itemName, value);
continue; continue;
} }
if (type == TagLib::MP4::Item::Type::UInt) { if (type == TagLib::MP4::Item::Type::UInt) {
auto value = std::to_string(itemValue.toUInt()); auto value = std::to_string(itemValue.toUInt());
id3v2.add(itemName, value); id3v2.add(itemName, value);
continue; continue;
} }
if (type == TagLib::MP4::Item::Type::LongLong) { if (type == TagLib::MP4::Item::Type::LongLong) {
auto value = std::to_string(itemValue.toLongLong()); auto value = std::to_string(itemValue.toLongLong());
id3v2.add(itemName, value); id3v2.add(itemName, value);
continue; continue;
} }
if (type == TagLib::MP4::Item::Type::IntPair) { if (type == TagLib::MP4::Item::Type::IntPair) {
// It's inefficient going from the integer representation back into // It's inefficient going from the integer representation back into
// a string, but I fully expect taggers to just write "NN/TT" strings // a string, but I fully expect taggers to just write "NN/TT" strings
// anyway, and musikr doesn't have to do as much fiddly variant handling. // anyway, and musikr doesn't have to do as much fiddly variant handling.
auto value = std::to_string(itemValue.toIntPair().first) + "/" auto value = std::to_string(itemValue.toIntPair().first) + "/"
+ std::to_string(itemValue.toIntPair().second); + std::to_string(itemValue.toIntPair().second);
id3v2.add(itemName, value); id3v2.add(itemName, value);
continue; continue;
} }
} }
} }
void JVMMetadataBuilder::setCover( void JVMMetadataBuilder::setCover(
const TagLib::List<TagLib::VariantMap> covers) { const TagLib::List<TagLib::VariantMap> covers) {
if (covers.isEmpty()) { if (covers.isEmpty()) {
return; return;
} }
// Find the cover with a "front cover" type // Find the cover with a "front cover" type
for (auto cover : covers) { for (auto cover : covers) {
auto type = cover["pictureType"].toString(); auto type = cover["pictureType"].toString();
if (type == "Front Cover") { if (type == "Front Cover") {
this->cover = cover["data"].toByteVector(); this->cover = cover["data"].toByteVector();
return; return;
} }
} }
// No front cover, just pick first. // No front cover, just pick first.
// TODO: Consider having cascading fallbacks to increasingly less // TODO: Consider having cascading fallbacks to increasingly less
// relevant covers perhaps // relevant covers perhaps
this->cover = covers.front()["data"].toByteVector(); this->cover = covers.front()["data"].toByteVector();
} }
void JVMMetadataBuilder::setProperties(TagLib::AudioProperties *properties) { void JVMMetadataBuilder::setProperties(TagLib::AudioProperties *properties) {
this->properties = properties; this->properties = properties;
} }
jobject JVMMetadataBuilder::build() { jobject JVMMetadataBuilder::build() {
jclass propertiesClass = env->FindClass( jclass propertiesClass = env->FindClass(
"org/oxycblt/musikr/metadata/Properties"); "org/oxycblt/musikr/metadata/Properties");
jmethodID propertiesInit = env->GetMethodID(propertiesClass, "<init>", jmethodID propertiesInit = env->GetMethodID(propertiesClass, "<init>",
"(Ljava/lang/String;JII)V"); "(Ljava/lang/String;JII)V");
jobject propertiesObj = env->NewObject(propertiesClass, propertiesInit, jobject propertiesObj = env->NewObject(propertiesClass, propertiesInit,
env->NewStringUTF(mimeType.data()), env->NewStringUTF(mimeType.data()),
(jlong) properties->lengthInMilliseconds(), properties->bitrate(), (jlong) properties->lengthInMilliseconds(), properties->bitrate(),
properties->sampleRate()); properties->sampleRate());
env->DeleteLocalRef(propertiesClass); env->DeleteLocalRef(propertiesClass);
jclass metadataClass = env->FindClass( jclass metadataClass = env->FindClass(
"org/oxycblt/musikr/metadata/Metadata"); "org/oxycblt/musikr/metadata/Metadata");
jmethodID metadataInit = env->GetMethodID(metadataClass, "<init>", jmethodID metadataInit = env->GetMethodID(metadataClass, "<init>",
"(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;[BLorg/" "(Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;[BLorg/"
"oxycblt/musikr/metadata/Properties;)V"); "oxycblt/musikr/metadata/Properties;)V");
jobject id3v2Map = id3v2.getObject(); jobject id3v2Map = id3v2.getObject();
jobject xiphMap = xiph.getObject(); jobject xiphMap = xiph.getObject();
jobject mp4Map = mp4.getObject(); jobject mp4Map = mp4.getObject();
jbyteArray coverArray = nullptr; jbyteArray coverArray = nullptr;
if (cover.has_value()) { if (cover.has_value()) {
auto coverSize = static_cast<jsize>(cover->size()); auto coverSize = static_cast<jsize>(cover->size());
coverArray = env->NewByteArray(coverSize); coverArray = env->NewByteArray(coverSize);
env->SetByteArrayRegion(coverArray, 0, coverSize, env->SetByteArrayRegion(coverArray, 0, coverSize,
reinterpret_cast<const jbyte*>(cover->data())); reinterpret_cast<const jbyte*>(cover->data()));
} }
jobject metadataObj = env->NewObject(metadataClass, metadataInit, id3v2Map, jobject metadataObj = env->NewObject(metadataClass, metadataInit, id3v2Map,
xiphMap, mp4Map, coverArray, propertiesObj); xiphMap, mp4Map, coverArray, propertiesObj);
env->DeleteLocalRef(metadataClass); env->DeleteLocalRef(metadataClass);
return metadataObj; return metadataObj;
} }

View file

@ -1,6 +1,20 @@
// /*
// Created by oxycblt on 12/12/24. * Copyright (c) 2024 Auxio Project
// * JVMMetadataBuilder.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef AUXIO_JVMMETADATABUILDER_H #ifndef AUXIO_JVMMETADATABUILDER_H
#define AUXIO_JVMMETADATABUILDER_H #define AUXIO_JVMMETADATABUILDER_H

View file

@ -19,75 +19,75 @@
#include "JVMTagMap.h" #include "JVMTagMap.h"
JVMTagMap::JVMTagMap(JNIEnv *env) : env(env) { JVMTagMap::JVMTagMap(JNIEnv *env) : env(env) {
jclass hashMapClass = env->FindClass("java/util/HashMap"); jclass hashMapClass = env->FindClass("java/util/HashMap");
jmethodID init = env->GetMethodID(hashMapClass, "<init>", "()V"); jmethodID init = env->GetMethodID(hashMapClass, "<init>", "()V");
hashMap = env->NewObject(hashMapClass, init); hashMap = env->NewObject(hashMapClass, init);
hashMapGetMethod = env->GetMethodID(hashMapClass, "get", hashMapGetMethod = env->GetMethodID(hashMapClass, "get",
"(Ljava/lang/Object;)Ljava/lang/Object;"); "(Ljava/lang/Object;)Ljava/lang/Object;");
hashMapPutMethod = env->GetMethodID(hashMapClass, "put", hashMapPutMethod = env->GetMethodID(hashMapClass, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
env->DeleteLocalRef(hashMapClass); env->DeleteLocalRef(hashMapClass);
jclass arrayListClass = env->FindClass("java/util/ArrayList"); jclass arrayListClass = env->FindClass("java/util/ArrayList");
arrayListInitMethod = env->GetMethodID(arrayListClass, "<init>", "()V"); arrayListInitMethod = env->GetMethodID(arrayListClass, "<init>", "()V");
arrayListAddMethod = env->GetMethodID(arrayListClass, "add", arrayListAddMethod = env->GetMethodID(arrayListClass, "add",
"(Ljava/lang/Object;)Z"); "(Ljava/lang/Object;)Z");
env->DeleteLocalRef(arrayListClass); env->DeleteLocalRef(arrayListClass);
} }
JVMTagMap::~JVMTagMap() { JVMTagMap::~JVMTagMap() {
env->DeleteLocalRef(hashMap); env->DeleteLocalRef(hashMap);
} }
void JVMTagMap::add(TagLib::String &key, std::string_view value) { void JVMTagMap::add(TagLib::String &key, std::string_view value) {
jstring jKey = env->NewStringUTF(key.toCString(true)); jstring jKey = env->NewStringUTF(key.toCString(true));
jstring jValue = env->NewStringUTF(value.data()); jstring jValue = env->NewStringUTF(value.data());
// check if theres already a value arraylist in the map // check if theres already a value arraylist in the map
jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod, jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod,
jKey); jKey);
// if there is, add to the value to the existing arraylist // if there is, add to the value to the existing arraylist
if (existingValue != nullptr) { if (existingValue != nullptr) {
env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue); env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue);
} else { } else {
// if there isn't, create a new arraylist and add the value to it // if there isn't, create a new arraylist and add the value to it
jclass arrayListClass = env->FindClass("java/util/ArrayList"); jclass arrayListClass = env->FindClass("java/util/ArrayList");
jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod); jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod);
env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue); env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue);
env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList); env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList);
env->DeleteLocalRef(arrayListClass); env->DeleteLocalRef(arrayListClass);
} }
} }
void JVMTagMap::add(TagLib::String &key, TagLib::StringList &value) { void JVMTagMap::add(TagLib::String &key, TagLib::StringList &value) {
if (value.isEmpty()) { if (value.isEmpty()) {
// Nothing to add // Nothing to add
return; return;
} }
jstring jKey = env->NewStringUTF(key.toCString(true)); jstring jKey = env->NewStringUTF(key.toCString(true));
// check if theres already a value arraylist in the map // check if theres already a value arraylist in the map
jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod, jobject existingValue = env->CallObjectMethod(hashMap, hashMapGetMethod,
jKey); jKey);
// if there is, add to the value to the existing arraylist // if there is, add to the value to the existing arraylist
if (existingValue != nullptr) { if (existingValue != nullptr) {
for (auto &val : value) { for (auto &val : value) {
jstring jValue = env->NewStringUTF(val.toCString(true)); jstring jValue = env->NewStringUTF(val.toCString(true));
env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue); env->CallBooleanMethod(existingValue, arrayListAddMethod, jValue);
} }
} else { } else {
// if there isn't, create a new arraylist and add the value to it // if there isn't, create a new arraylist and add the value to it
jclass arrayListClass = env->FindClass("java/util/ArrayList"); jclass arrayListClass = env->FindClass("java/util/ArrayList");
jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod); jobject arrayList = env->NewObject(arrayListClass, arrayListInitMethod);
for (auto &val : value) { for (auto &val : value) {
jstring jValue = env->NewStringUTF(val.toCString(true)); jstring jValue = env->NewStringUTF(val.toCString(true));
env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue); env->CallBooleanMethod(arrayList, arrayListAddMethod, jValue);
} }
env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList); env->CallObjectMethod(hashMap, hashMapPutMethod, jKey, arrayList);
env->DeleteLocalRef(arrayListClass); env->DeleteLocalRef(arrayListClass);
} }
} }
jobject JVMTagMap::getObject() { jobject JVMTagMap::getObject() {
return hashMap; return hashMap;
} }

View file

@ -1,6 +1,20 @@
// /*
// Created by oxycblt on 12/12/24. * Copyright (c) 2024 Auxio Project
// * JVMTagMap.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef AUXIO_JVMTAGMAP_H #ifndef AUXIO_JVMTAGMAP_H
#define AUXIO_JVMTAGMAP_H #define AUXIO_JVMTAGMAP_H
@ -15,8 +29,8 @@ public:
JVMTagMap(JNIEnv *env); JVMTagMap(JNIEnv *env);
~JVMTagMap(); ~JVMTagMap();
JVMTagMap(const JVMTagMap &) = delete; JVMTagMap(const JVMTagMap&) = delete;
JVMTagMap &operator=(const JVMTagMap &) = delete; JVMTagMap& operator=(const JVMTagMap&) = delete;
void add(TagLib::String &key, std::string_view value); void add(TagLib::String &key, std::string_view value);
void add(TagLib::String &key, TagLib::StringList &value); void add(TagLib::String &key, TagLib::StringList &value);
@ -32,5 +46,4 @@ private:
jmethodID arrayListAddMethod; jmethodID arrayListAddMethod;
}; };
#endif //AUXIO_JVMTAGMAP_H #endif //AUXIO_JVMTAGMAP_H

View file

@ -1,6 +1,20 @@
// /*
// Created by oxycblt on 12/23/24. * Copyright (c) 2024 Auxio Project
// * log.h is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef AUXIO_LOG_H #ifndef AUXIO_LOG_H
#define AUXIO_LOG_H #define AUXIO_LOG_H

View file

@ -32,48 +32,48 @@
extern "C" JNIEXPORT jobject JNICALL extern "C" JNIEXPORT jobject JNICALL
Java_org_oxycblt_musikr_metadata_TagLibJNI_openNative(JNIEnv *env, Java_org_oxycblt_musikr_metadata_TagLibJNI_openNative(JNIEnv *env,
jobject /* this */, jobject /* this */,
jobject inputStream) { jobject inputStream) {
try { try {
JVMInputStream stream {env, inputStream}; JVMInputStream stream {env, inputStream};
TagLib::FileRef fileRef {&stream}; TagLib::FileRef fileRef {&stream};
if (fileRef.isNull()) { if (fileRef.isNull()) {
return nullptr; return nullptr;
} }
TagLib::File *file = fileRef.file(); TagLib::File *file = fileRef.file();
JVMMetadataBuilder builder {env}; JVMMetadataBuilder builder {env};
if (auto *mpegFile = dynamic_cast<TagLib::MPEG::File *>(file)) { if (auto *mpegFile = dynamic_cast<TagLib::MPEG::File *>(file)) {
builder.setMimeType("audio/mpeg"); builder.setMimeType("audio/mpeg");
builder.setId3v2(*mpegFile->ID3v2Tag()); builder.setId3v2(*mpegFile->ID3v2Tag());
} else if (auto *mp4File = dynamic_cast<TagLib::MP4::File *>(file)) { } else if (auto *mp4File = dynamic_cast<TagLib::MP4::File *>(file)) {
builder.setMimeType("audio/mp4"); builder.setMimeType("audio/mp4");
builder.setMp4(*mp4File->tag()); builder.setMp4(*mp4File->tag());
} else if (auto *flacFile = dynamic_cast<TagLib::FLAC::File *>(file)) { } else if (auto *flacFile = dynamic_cast<TagLib::FLAC::File *>(file)) {
builder.setMimeType("audio/flac"); builder.setMimeType("audio/flac");
builder.setId3v2(*flacFile->ID3v2Tag()); builder.setId3v2(*flacFile->ID3v2Tag());
builder.setXiph(*flacFile->xiphComment()); builder.setXiph(*flacFile->xiphComment());
} else if (auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File *>(file)) { } else if (auto *opusFile = dynamic_cast<TagLib::Ogg::Opus::File *>(file)) {
builder.setMimeType("audio/opus"); builder.setMimeType("audio/opus");
builder.setXiph(*opusFile->tag()); builder.setXiph(*opusFile->tag());
} else if (auto *vorbisFile = } else if (auto *vorbisFile =
dynamic_cast<TagLib::Ogg::Vorbis::File *>(file)) { dynamic_cast<TagLib::Ogg::Vorbis::File *>(file)) {
builder.setMimeType("audio/vorbis"); builder.setMimeType("audio/vorbis");
builder.setXiph(*vorbisFile->tag()); builder.setXiph(*vorbisFile->tag());
} else if (auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File *>(file)) { } else if (auto *wavFile = dynamic_cast<TagLib::RIFF::WAV::File *>(file)) {
builder.setMimeType("audio/wav"); builder.setMimeType("audio/wav");
builder.setId3v2(*wavFile->ID3v2Tag()); builder.setId3v2(*wavFile->ID3v2Tag());
} else { } else {
// While taglib supports other formats, ExoPlayer does not. Ignore them. // While taglib supports other formats, ExoPlayer does not. Ignore them.
return nullptr; return nullptr;
} }
builder.setProperties(file->audioProperties()); builder.setProperties(file->audioProperties());
builder.setCover(file->tag()->complexProperties("PICTURE")); builder.setCover(file->tag()->complexProperties("PICTURE"));
return builder.build(); return builder.build();
} catch (std::runtime_error e) { } catch (std::runtime_error e) {
LOGE("Error opening file: %s", e.what()); LOGE("Error opening file: %s", e.what());
return nullptr; return nullptr;
} }
} }