Skip to content

Commit 89f94bd

Browse files
committed
Added Metadata writing, metadat conversion to c++, and format detection performance tests;
Changed AudioFile.Format.Detector to an enum; Swapped nullptr to kCFAllocatorDefault in CFDataCreate calls; Added a convenience static method to create AudioMetadata::Picture in swift from CFData which fixes previous performance inefficiency; Recompiled xcframeworks Fixes #2
1 parent 945daf1 commit 89f94bd

File tree

27 files changed

+6007
-4262
lines changed

27 files changed

+6007
-4262
lines changed

XCFrameworkPackage/Sources/CxxTagLibBridge/AudioMetadata/Picture/AudioMetadata.Picture+ImageIO.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33
#import <CoreFoundation/CoreFoundation.h>
44
#import <ImageIO/ImageIO.h>
55

6+
/// Creates `AudioMetadata::Picture` from `CFDataRef data`, `std::string description` and `Kind kind`.
7+
AudioMetadata::Picture AudioMetadata::Picture::create_from_CFData(
8+
CFDataRef data,
9+
std::string description,
10+
Kind kind
11+
) {
12+
const UInt8 *bytes = CFDataGetBytePtr(data);
13+
CFIndex length = CFDataGetLength(data);
14+
auto buffer = Picture::Bytes(bytes, bytes + length);
15+
return {
16+
.bytes = std::move(buffer),
17+
.size = static_cast<unsigned int>(length),
18+
.description = description,
19+
.kind = kind
20+
};
21+
}
22+
623
/// Attempts to extract `MIME type` from raw image `bytes`, if fails returns empty string.
724
AudioMetadata::Picture::MIMEType AudioMetadata::Picture::mime_type() {
8-
auto data = CFDataCreate(nullptr, reinterpret_cast<const UInt8*>(bytes.data()), size);
25+
auto data = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(bytes.data()), size);
926
auto imageSource = CGImageSourceCreateWithData(data, nullptr);
1027
std::string mimeType;
1128
if (imageSource) {
@@ -30,7 +47,7 @@ AudioMetadata::Picture::MIMEType AudioMetadata::Picture::mime_type() {
3047
/// Attempts to extract `MIME type, pixel width & height, color depth` from raw image `bytes`,
3148
/// if fails returns empty string and 0's.
3249
AudioMetadata::Picture::Properties AudioMetadata::Picture::properties() {
33-
auto data = CFDataCreate(nullptr, reinterpret_cast<const UInt8*>(bytes.data()), size);
50+
auto data = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(bytes.data()), size);
3451
auto imageSource = CGImageSourceCreateWithData(data, nullptr);
3552
std::string mimeType;
3653
int width, height, depth;

XCFrameworkPackage/Sources/CxxTagLibBridge/include/CxxTagLibBridge/AudioMetadata.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#import <swift/bridging>
3+
#import <CoreFoundation/CoreFoundation.h>
34
#import <tuple>
45
#import <string>
56
#import <optional>
@@ -118,6 +119,9 @@ struct AudioMetadata final {
118119
using Properties = std::tuple<std::string, int, int, int>;
119120
Properties properties();
120121
public:
122+
// MARK: - Convenience Constructor for Swift
123+
static Picture create_from_CFData(CFDataRef data, std::string description, Kind kind);
124+
121125
// MARK: - Attached Picture: Format specific builders
122126
using FLACPicture = TagLib::FLAC::Picture *;
123127
FLACPicture convert_to_FLACPicture();

XCFrameworkPackage/Sources/SwiftTagLib/AttachedPicture.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ public extension AudioFile.Metadata {
2525
}
2626

2727
var cxxRepresentation: AudioMetadata.Picture {
28-
var picture = AudioMetadata.Picture();
29-
#warning("this is rather slow & inefficient way to go from Data to [UInt8] to std::vector<char>")
30-
/// take a look at [pointers to C](https://github.com/swiftlang/swift/blob/main/docs/HowSwiftImportsCAPIs.md#pointers-to-data).
31-
var bytes = AudioMetadata.Picture.Bytes()
32-
data.forEach { bytes.push_back(Int8(bitPattern: $0)) }
33-
picture.bytes = bytes;
34-
picture.size = UInt32(bytes.count);
35-
picture.description = std.string(description)
36-
picture.kind = kind.cxxRepresentation
37-
return picture;
28+
.create_from_CFData(data as CFData, std.string(description), kind.cxxRepresentation)
3829
}
3930
}
4031
}

XCFrameworkPackage/Sources/SwiftTagLib/Utility/Format.Detector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import class Foundation.FileHandle
44

55
extension AudioFile.Format {
66
/// Attempts to determine `AudioFile.Format` from given `URL`.
7-
struct Detector {
7+
enum Detector {
88
static func format(at url: URL) throws(AudioFile.InitializationError) -> AudioFile.Format? {
99
let fileExtension = url.pathExtension.lowercased()
1010
guard let fileHandle = try? FileHandle(forReadingFrom: url) else {

XCFrameworkPackage/SwiftTagLib.xcframework/Info.plist

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@
88
<key>BinaryPath</key>
99
<string>SwiftTagLib.framework/SwiftTagLib</string>
1010
<key>LibraryIdentifier</key>
11-
<string>ios-arm64</string>
11+
<string>ios-arm64_x86_64-simulator</string>
1212
<key>LibraryPath</key>
1313
<string>SwiftTagLib.framework</string>
1414
<key>SupportedArchitectures</key>
1515
<array>
1616
<string>arm64</string>
17+
<string>x86_64</string>
1718
</array>
1819
<key>SupportedPlatform</key>
1920
<string>ios</string>
21+
<key>SupportedPlatformVariant</key>
22+
<string>simulator</string>
2023
</dict>
2124
<dict>
2225
<key>BinaryPath</key>
2326
<string>SwiftTagLib.framework/SwiftTagLib</string>
2427
<key>LibraryIdentifier</key>
25-
<string>ios-arm64_x86_64-simulator</string>
28+
<string>ios-arm64</string>
2629
<key>LibraryPath</key>
2730
<string>SwiftTagLib.framework</string>
2831
<key>SupportedArchitectures</key>
2932
<array>
3033
<string>arm64</string>
31-
<string>x86_64</string>
3234
</array>
3335
<key>SupportedPlatform</key>
3436
<string>ios</string>
35-
<key>SupportedPlatformVariant</key>
36-
<string>simulator</string>
3737
</dict>
3838
</array>
3939
<key>CFBundlePackageType</key>

0 commit comments

Comments
 (0)