Skip to content

Commit 0cde714

Browse files
authored
Merge pull request #3 from orchetect/main
Cleanup and Minor Refactors
2 parents 6a9aa5f + 3394b5f commit 0cde714

File tree

14 files changed

+409
-276
lines changed

14 files changed

+409
-276
lines changed

Package.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
// swift-tools-version: 6.0
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
32

43
import PackageDescription
54

65
let package = Package(
76
name: "SwiftAudioFileConverter",
8-
platforms: [.macOS(.v13)],
7+
platforms: [.macOS(.v13), .iOS(.v14)],
98
products: [
10-
// Products define the executables and libraries a package produces, making them visible to other packages.
119
.library(
1210
name: "SwiftAudioFileConverter",
13-
targets: ["SwiftAudioFileConverter"]),
11+
targets: ["SwiftAudioFileConverter"]
12+
)
1413
],
1514
dependencies: [
16-
.package(url: "https://github.com/Phisto/swift-lame.git", .upToNextMajor(from: "3.100.0")),
17-
.package(url: "https://github.com/sbooth/CFLAC.git", from: "1.3.2")
15+
.package(url: "https://github.com/Phisto/swift-lame.git", .upToNextMajor(from: "3.100.0")),
16+
.package(url: "https://github.com/sbooth/CFLAC.git", from: "1.3.2")
1817
],
1918
targets: [
20-
// Targets are the basic building blocks of a package, defining a module or a test suite.
21-
// Targets can depend on other targets in this package and products from dependencies.
2219
.target(
2320
name: "SwiftAudioFileConverter",
2421
dependencies: [
@@ -31,6 +28,6 @@ let package = Package(
3128
name: "SwiftAudioFileConverterTests",
3229
dependencies: ["SwiftAudioFileConverter"],
3330
swiftSettings: [.interoperabilityMode(.Cxx)]
34-
),
31+
)
3532
]
3633
)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# SwiftAudioFileConverter
2+
3+
Audio file conversion utilities.

Sources/SwiftAudioFileConverter/AudioFileSettings/AudioFileSettings.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,39 @@
88
import Foundation
99

1010
public struct AudioFileSettings {
11-
let sampleRate: SampleRate
12-
let bitDepth: BitDepth
13-
let fileFormat: FileFormat
14-
let channelFormat: ChannelFormat
15-
16-
var isPCM: Bool {
17-
return fileFormat == .aiff || fileFormat == .wav
18-
}
11+
public let sampleRate: SampleRate
12+
public let bitDepth: BitDepth
13+
public let fileFormat: FileFormat
14+
public let channelFormat: ChannelFormat
1915

2016
public init(sampleRate: SampleRate, bitDepth: BitDepth, fileFormat: FileFormat, channelFormat: ChannelFormat) throws {
2117
self.sampleRate = sampleRate
2218
self.bitDepth = bitDepth
2319
self.fileFormat = fileFormat
2420
self.channelFormat = channelFormat
2521

26-
if !isPCM && bitDepth != .float32 {
22+
if !isPCM, bitDepth != .float32 {
2723
throw SwiftAudioFileConverterError.invalidAudioFileSettings(self)
2824
}
2925
}
3026
}
3127

32-
extension AudioFileSettings: Sendable {}
28+
extension AudioFileSettings: Equatable { }
29+
30+
extension AudioFileSettings: Hashable { }
31+
32+
extension AudioFileSettings: Sendable { }
33+
34+
extension AudioFileSettings: CustomStringConvertible {
35+
public var description: String {
36+
"\(fileFormat) \(channelFormat) @ \(sampleRate)/\(bitDepth)"
37+
}
38+
}
39+
40+
// MARK: - Properties
41+
42+
extension AudioFileSettings {
43+
public var isPCM: Bool {
44+
fileFormat == .aiff || fileFormat == .wav
45+
}
46+
}

Sources/SwiftAudioFileConverter/AudioFileSettings/BitDepth.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@
44
//
55
// Created by Devin Roth on 2025-04-03.
66
//
7-
import Foundation
87

8+
import Foundation
99

1010
public enum BitDepth {
1111
case int16
1212
case int24
1313
case float32
1414
}
15-
extension BitDepth: Sendable {}
16-
extension BitDepth: CaseIterable {}
15+
16+
extension BitDepth: Equatable { }
17+
18+
extension BitDepth: Hashable { }
19+
20+
extension BitDepth: Sendable { }
21+
22+
extension BitDepth: CaseIterable { }
23+
24+
extension BitDepth: CustomStringConvertible {
25+
public var description: String {
26+
switch self {
27+
case .int16:
28+
"16-bit Int"
29+
case .int24:
30+
"24-bit Int"
31+
case .float32:
32+
"32-bit Float"
33+
}
34+
}
35+
}

Sources/SwiftAudioFileConverter/AudioFileSettings/ChannelFormat.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55
// Created by Devin Roth on 2025-04-03.
66
//
77

8+
import Foundation
89

910
public enum ChannelFormat {
1011
case mono
1112
case stereo
1213
}
13-
extension ChannelFormat: Sendable {}
14-
extension ChannelFormat: CaseIterable {}
14+
15+
extension ChannelFormat: Equatable { }
16+
17+
extension ChannelFormat: Hashable { }
18+
19+
extension ChannelFormat: CaseIterable { }
20+
21+
extension ChannelFormat: Sendable { }
22+
23+
extension ChannelFormat: CustomStringConvertible {
24+
public var description: String {
25+
switch self {
26+
case .mono:
27+
"Mono"
28+
case .stereo:
29+
"Stereo"
30+
}
31+
}
32+
}

Sources/SwiftAudioFileConverter/AudioFileSettings/FileFormat.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55
// Created by Devin Roth on 2025-04-03.
66
//
7-
import Foundation
87

8+
import Foundation
99

1010
public enum FileFormat: String {
1111
case aac
@@ -15,5 +15,11 @@ public enum FileFormat: String {
1515
case flac
1616
case alac = "m4a"
1717
}
18-
extension FileFormat: Sendable {}
19-
extension FileFormat: CaseIterable {}
18+
19+
extension FileFormat: Equatable { }
20+
21+
extension FileFormat: Hashable { }
22+
23+
extension FileFormat: Sendable { }
24+
25+
extension FileFormat: CaseIterable { }

Sources/SwiftAudioFileConverter/AudioFileSettings/SampleRate.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55
// Created by Devin Roth on 2025-04-03.
66
//
7-
import Foundation
87

8+
import Foundation
99

1010
public enum SampleRate: Double {
1111
case kHz8 = 8000
@@ -14,5 +14,17 @@ public enum SampleRate: Double {
1414
case kHz44 = 44100
1515
case kHz48 = 48000
1616
}
17-
extension SampleRate: Sendable {}
18-
extension SampleRate: CaseIterable {}
17+
18+
extension SampleRate: Equatable { }
19+
20+
extension SampleRate: Hashable { }
21+
22+
extension SampleRate: Sendable { }
23+
24+
extension SampleRate: CaseIterable { }
25+
26+
extension SampleRate: CustomStringConvertible {
27+
public var description: String {
28+
"\(rawValue)Hz"
29+
}
30+
}

Sources/SwiftAudioFileConverter/Errors/CoreAudioError.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
//
55
// Created by Devin Roth on 2025-04-03.
66
//
7+
78
import Foundation
89
import AudioToolbox
910

10-
public enum CoreAudioError {
11+
public enum CoreAudioError: LocalizedError {
1112
case formatNotSupported
1213
case unspecified
1314
case unsupportedProperty
@@ -16,8 +17,7 @@ public enum CoreAudioError {
1617
case unknownFormat
1718
case unknownError(OSStatus)
1819

19-
init(status: OSStatus) {
20-
20+
public init(status: OSStatus) {
2121
switch status {
2222
case kAudioFormatUnsupportedDataFormatError:
2323
self = .formatNotSupported
@@ -34,9 +34,32 @@ public enum CoreAudioError {
3434
default:
3535
self = .unknownError(status)
3636
}
37-
38-
3937
}
4038
}
4139

42-
extension CoreAudioError: Sendable {}
40+
extension CoreAudioError: Equatable { }
41+
42+
extension CoreAudioError: Hashable { }
43+
44+
extension CoreAudioError: Sendable { }
45+
46+
extension CoreAudioError {
47+
public var errorDescription: String? {
48+
switch self {
49+
case .formatNotSupported:
50+
"Format not supported."
51+
case .unspecified:
52+
"Unspecified error."
53+
case .unsupportedProperty:
54+
"Unsupported property."
55+
case .badPropertySize:
56+
"Bad property size."
57+
case .badSpecifierSize:
58+
"Bad specifier size."
59+
case .unknownFormat:
60+
"Unknown format."
61+
case let .unknownError(osStatus):
62+
"Unknown error (OSStatus \(osStatus))"
63+
}
64+
}
65+
}

Sources/SwiftAudioFileConverter/Errors/SwiftAudioFileConverterError.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
//
55
// Created by Devin Roth on 2025-04-03.
66
//
7+
78
import Foundation
89

9-
public enum SwiftAudioFileConverterError: Error {
10+
public enum SwiftAudioFileConverterError: LocalizedError {
1011
case invalidAudioFileSettings(AudioFileSettings)
1112
case unsupportedAudioFileExtension(URL)
1213
case audioFileExtensionSettingsMismatch(URL, AudioFileSettings)
@@ -18,3 +19,36 @@ public enum SwiftAudioFileConverterError: Error {
1819
case coreAudioError(CoreAudioError)
1920
case flacConversionUnknownError
2021
}
22+
23+
extension SwiftAudioFileConverterError: Equatable { }
24+
25+
extension SwiftAudioFileConverterError: Hashable { }
26+
27+
extension SwiftAudioFileConverterError: Sendable { }
28+
29+
extension SwiftAudioFileConverterError {
30+
public var errorDescription: String? {
31+
switch self {
32+
case let .invalidAudioFileSettings(audioFileSettings):
33+
"Invalid audio file settings. \(audioFileSettings)"
34+
case let .unsupportedAudioFileExtension(url):
35+
"Unsupported audio file extension (\(url.path))"
36+
case let .audioFileExtensionSettingsMismatch(url, audioFileSettings):
37+
"Audio file extension settings mismatch. (\(url.path)) \(audioFileSettings)"
38+
case let .fileDoesNotExist(url):
39+
"File does not exist. (\(url.path))"
40+
case let .fileIsNotReadable(url):
41+
"File is not readable. (\(url.path))"
42+
case let .unableToOpenFile(url):
43+
"Unable to open file. (\(url.path))"
44+
case let .fileIsNotWritable(url):
45+
"File is not writable. (\(url.path))"
46+
case let .unsupportedConversion(fileFormat):
47+
"Unsupported conversion: \(fileFormat)"
48+
case let .coreAudioError(coreAudioError):
49+
"Core Audio error: \(coreAudioError)"
50+
case .flacConversionUnknownError:
51+
"Unknown FLAC conversion error."
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)