A plain Swift MPEG-1 Layer 3 (MP3) encoder and decoder implementation. All code is written with the assistance of coding agents based on the MP3 specification.
In your Package.swift or in Xcode, add the MP3CoderSwift Swift package and
add the MP3Coder or MP3CoderAVAudio library to your target.
-
MP3Coderprovides plain MP3 encoding and decoding logic that can be used for raw sample buffers. -
MP3CoderAVAudioprovides extensions toAVAudioPCMBufferand also offers simpleencode(url:)anddecode(data:url:)methods for PCM (WAV) files.
let package = Package(
// ...
dependencies: [
// Add Swift Package
.package(url: "https://github.com/niw/MP3CoderSwift.git", from: "0.1.0")
],
// ...
.target(
// Add dependency
dependencies: [
.product(name: "MP3CoderAVAudio", package: "MP3CoderSwift")
]
)
// ...
)Then encode to or decode from MP3 with the following code.
import MP3Coder
import MP3CoderAVAudio
// Encode to MP3
let inputURL = URL(fileURLWithPath: "/path/to/wav")
let mp3Data = try MP3Encoder.encode(url: inputURL)
// Decode from MP3
let outputURL = URL(fileURLWithPath: "/path/to/wav")
try MP3Decoder.decode(data: data, to: outputURL)There is a simple CLI command for testing.
Prepare a WAV file and use the following command.
Use the release configuration for testing because debug is really slow.
# Encode a WAV file
swift run -c release mp3coder-cli encode --output encoded.mp3 input.wav
# Decode an MP3 file
swift run -c release mp3coder-cli decode --output decoded.wav encoded.mp3