Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ add_subdirectory(
include_directories(
taglib/taglib
taglib/taglib/toolkit
taglib/taglib/mpeg
taglib/taglib/mpeg/id3v1
taglib/taglib/mpeg/id3v2
taglib/taglib/mpeg/id3v2/frames
taglib/taglib/ogg
taglib/taglib/ogg/flac
taglib/taglib/flac
taglib/taglib/mp4
)

add_executable(taglib taglib.cpp)
Expand Down
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ghcr.io/webassembly/wasi-sdk:sha-5e4756e

RUN apt-get update
RUN apt-get install -y --no-install-recommends binaryen
RUN rm -rf /var/lib/apt/lists/*

COPY . /taglib

WORKDIR /taglib
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ func main() {

## Manually Building and Using the WASM Binary

The binary is already included in the package. However if you want to manually build and override it, you can with WASI SDK and Go build flags
A docker file is provided to build the WASM binary.
[WASI SDK](https://github.com/WebAssembly/wasi-sdk) (includes Autotools, CMake, and Ninja) and
[Binaryen](https://github.com/WebAssembly/binaryen) are installed during the docker container build.

1. Install [WASI SDK](https://github.com/WebAssembly/wasi-sdk) globally. The default installation path is `/opt/wasi-sdk/`
2. Install [Binaryen](https://github.com/WebAssembly/binaryen) globally.
3. Clone this repository and Git submodules
1. Install Docker
2. Clone this repository and Git submodules

```console
$ git clone "https://github.com/sentriz/go-taglib.git" --recursive
Expand All @@ -96,14 +97,14 @@ The binary is already included in the package. However if you want to manually b
> [!NOTE]
> Make sure to use the `--recursive` flag, without it there will be no TagLib submodule to build with

4. Generate the WASM binary:
3. Generate the WASM binary:

```console
$ go generate ./...
$ docker compose run taglib ./build-wasm.sh
$ # taglib.wasm created
```

5. Use the new binary in your project
4. Use the new binary in your project

```console
$ CGO_ENABLED=0 go build -ldflags="-X 'go.senan.xyz/taglib.binaryPath=/path/to/taglib.wasm'" ./your/project/...
Expand Down
5 changes: 5 additions & 0 deletions build-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cmake -DWASI_SDK_PREFIX=/opt/wasi-sdk -DCMAKE_TOOLCHAIN_FILE=/opt/wasi-sdk/share/cmake/wasi-sdk.cmake -B build .
cmake --build build --target taglib
mv build/taglib.wasm .
wasm-opt --strip -c -O3 taglib.wasm -o taglib.wasm
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
taglib:
build:
context: .
dockerfile: Dockerfile
container_name: taglib
volumes:
- .:/taglib
working_dir: /taglib
261 changes: 261 additions & 0 deletions taglib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

#include "fileref.h"
#include "tpropertymap.h"
#include "mpeg/mpegfile.h"
#include "mpeg/id3v1/id3v1tag.h"
#include "mpeg/id3v2/id3v2tag.h"
#include "mpeg/id3v2/frames/textidentificationframe.h"
#include "mpeg/id3v2/frames/commentsframe.h"
#include "mpeg/id3v2/frames/popularimeterframe.h"
#include "mpeg/id3v2/frames/unsynchronizedlyricsframe.h"

char *to_char_array(const TagLib::String &s) {
const std::string str = s.to8Bit(true);
Expand Down Expand Up @@ -101,3 +108,257 @@ taglib_file_audioproperties(const char *filename) {

return arr;
}

__attribute__((export_name("taglib_file_id3v2_frames"))) char **
taglib_file_id3v2_frames(const char *filename) {
// First check if this is an MP3 file with ID3v2 tags
TagLib::FileRef fileRef(filename);
if (fileRef.isNull())
return nullptr;

// Try to cast to MPEG::File
TagLib::MPEG::File *mpegFile = dynamic_cast<TagLib::MPEG::File *>(fileRef.file());
if (!mpegFile || !mpegFile->hasID3v2Tag()) {
// Return empty array instead of nullptr when there are no ID3v2 tags
char **emptyFrames = static_cast<char **>(malloc(sizeof(char *)));
if (!emptyFrames)
return nullptr;
emptyFrames[0] = nullptr;
return emptyFrames;
}

TagLib::ID3v2::Tag *id3v2Tag = mpegFile->ID3v2Tag();
const TagLib::ID3v2::FrameListMap &frameListMap = id3v2Tag->frameListMap();

// Count total number of frames
size_t frameCount = 0;
for (TagLib::ID3v2::FrameListMap::ConstIterator it = frameListMap.begin(); it != frameListMap.end(); ++it) {
frameCount += it->second.size();
}

if (frameCount == 0) {
// Return empty array if there are no frames
char **emptyFrames = static_cast<char **>(malloc(sizeof(char *)));
if (!emptyFrames)
return nullptr;
emptyFrames[0] = nullptr;
return emptyFrames;
}

// Allocate result array
char **frames = static_cast<char **>(malloc(sizeof(char *) * (frameCount + 1)));
if (!frames)
return nullptr;

size_t i = 0;

// Process each frame
for (TagLib::ID3v2::FrameListMap::ConstIterator it = frameListMap.begin(); it != frameListMap.end(); ++it) {
TagLib::String frameID = TagLib::String(it->first);

for (TagLib::ID3v2::FrameList::ConstIterator frameIt = it->second.begin(); frameIt != it->second.end(); ++frameIt) {
TagLib::String key = frameID;
TagLib::String value;

// Handle special frame types
if (frameID == "TXXX") {
// User text identification frame
auto userFrame = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame *>(*frameIt);
if (userFrame) {
key = frameID + ":" + userFrame->description();
if (!userFrame->fieldList().isEmpty()) {
value = userFrame->fieldList().back();
}
}
}
else if (frameID == "COMM") {
// Comments frame
auto commFrame = dynamic_cast<TagLib::ID3v2::CommentsFrame *>(*frameIt);
if (commFrame) {
key = frameID + ":" + commFrame->description();
value = commFrame->text();
}
}
else if (frameID == "POPM") {
// Popularimeter frame (used for WMP ratings)
auto popmFrame = dynamic_cast<TagLib::ID3v2::PopularimeterFrame *>(*frameIt);
if (popmFrame) {
key = frameID + ":" + popmFrame->email();
value = TagLib::String::number(popmFrame->rating());
}
}
else {
// Standard frame
value = (*frameIt)->toString();
}

// Create the output string
TagLib::String row = key + "\t" + value;
frames[i++] = to_char_array(row);
}
}

frames[i] = nullptr;
return frames;
}

__attribute__((export_name("taglib_file_id3v1_tags"))) char **
taglib_file_id3v1_tags(const char *filename) {
// First check if this is an MP3 file with ID3v1 tags
TagLib::FileRef fileRef(filename);
if (fileRef.isNull())
return nullptr;

// Try to cast to MPEG::File
TagLib::MPEG::File *mpegFile = dynamic_cast<TagLib::MPEG::File *>(fileRef.file());
if (!mpegFile || !mpegFile->hasID3v1Tag()) {
// Return empty array instead of nullptr when there are no ID3v1 tags
char **emptyTags = static_cast<char **>(malloc(sizeof(char *)));
if (!emptyTags)
return nullptr;
emptyTags[0] = nullptr;
return emptyTags;
}

TagLib::ID3v1::Tag *id3v1Tag = mpegFile->ID3v1Tag();

// ID3v1 has a fixed set of fields
const int fieldCount = 7; // title, artist, album, year, comment, track, genre
char **tags = static_cast<char **>(malloc(sizeof(char *) * (fieldCount + 1)));
if (!tags)
return nullptr;

int i = 0;

// Add each standard ID3v1 field
if (!id3v1Tag->title().isEmpty())
tags[i++] = to_char_array(TagLib::String("TITLE\t") + id3v1Tag->title());

if (!id3v1Tag->artist().isEmpty())
tags[i++] = to_char_array(TagLib::String("ARTIST\t") + id3v1Tag->artist());

if (!id3v1Tag->album().isEmpty())
tags[i++] = to_char_array(TagLib::String("ALBUM\t") + id3v1Tag->album());

// Year is an unsigned int in ID3v1, convert to string
if (id3v1Tag->year() > 0)
tags[i++] = to_char_array(TagLib::String("YEAR\t") + TagLib::String::number(id3v1Tag->year()));

if (!id3v1Tag->comment().isEmpty())
tags[i++] = to_char_array(TagLib::String("COMMENT\t") + id3v1Tag->comment());

if (id3v1Tag->track() > 0)
tags[i++] = to_char_array(TagLib::String("TRACK\t") + TagLib::String::number(id3v1Tag->track()));

// Genre is an int in ID3v1, need to get the string representation
if (id3v1Tag->genreNumber() != 255) { // 255 is used for "unknown genre"
if (!id3v1Tag->genre().isEmpty())
tags[i++] = to_char_array(TagLib::String("GENRE\t") + id3v1Tag->genre());
}

tags[i] = nullptr;
return tags;
}

__attribute__((export_name("taglib_file_write_id3v2_frames"))) bool
taglib_file_write_id3v2_frames(const char *filename, const char **frames, uint8_t opts) {
if (!filename || !frames)
return false;

// First check if this is an MP3 file with ID3v2 tags
TagLib::MPEG::File file(filename);
if (!file.isValid())
return false;

// Create a new ID3v2 tag if one doesn't exist
if (!file.hasID3v2Tag()) {
file.ID3v2Tag(true);
}

TagLib::ID3v2::Tag *id3v2Tag = file.ID3v2Tag();

// If clear option is set, collect all frame IDs we want to keep
bool clearFrames = (opts & CLEAR);

// First collect all the frame IDs we're going to set
std::vector<TagLib::ByteVector> frameIDsToKeep;
if (clearFrames) {
for (int i = 0; frames[i] != nullptr; i++) {
TagLib::String row(frames[i], TagLib::String::UTF8);
int ti = row.find("\t");
if (ti != -1) {
TagLib::String key = row.substr(0, ti);
// Store the base frame ID (without description for TXXX, COMM, etc.)
if (key.find(":") != -1) {
key = key.substr(0, key.find(":"));
}
frameIDsToKeep.push_back(key.data(TagLib::String::Latin1));
}
}

// Now remove all frames except those we're going to set
const TagLib::ID3v2::FrameListMap &frameListMap = id3v2Tag->frameListMap();
for (TagLib::ID3v2::FrameListMap::ConstIterator it = frameListMap.begin();
it != frameListMap.end(); ++it) {
bool keepFrame = false;
for (size_t i = 0; i < frameIDsToKeep.size(); ++i) {
if (it->first == frameIDsToKeep[i]) {
keepFrame = true;
break;
}
}
if (!keepFrame) {
id3v2Tag->removeFrames(it->first);
}
}
}

// Now add the new frames
for (int i = 0; frames[i] != nullptr; i++) {
TagLib::String row(frames[i], TagLib::String::UTF8);
int ti = row.find("\t");
if (ti != -1) {
TagLib::String key = row.substr(0, ti);
TagLib::String value = row.substr(ti + 1);

// Remove existing frames with this ID
id3v2Tag->removeFrames(key.toCString(true));

// Add new frame if value is not empty
if (!value.isEmpty()) {
if (key.startsWith("T")) {
// Text identification frame
auto newFrame = new TagLib::ID3v2::TextIdentificationFrame(key.toCString(true), TagLib::String::UTF8);
TagLib::StringList values;

// Split value by vertical tab
int pos = 0;
while (pos != -1) {
int nextPos = value.find("\v", pos);
if (nextPos == -1) {
values.append(value.substr(pos));
break;
} else {
values.append(value.substr(pos, nextPos - pos));
pos = nextPos + 1;
}
}

newFrame->setText(values);
id3v2Tag->addFrame(newFrame);
}
else if (key == "COMM") {
// Comments frame
auto newFrame = new TagLib::ID3v2::CommentsFrame(TagLib::String::UTF8);
newFrame->setText(value);
id3v2Tag->addFrame(newFrame);
}
// Add other frame types as needed
}
}
}

// Save the file
return file.save();
}

Loading