feat:mp3 (#3)
--------- Co-authored-by: intelligence <intelligence@debian.local> Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
1 parent
32e7f32e0a
commit
64b9d704fb
25 files changed
+488
-98
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import XCTest
|
||||
|
||||
@testable import Transui
|
||||
|
||||
final class MP3EncoderTests: XCTestCase {
|
||||
func testExtractsSupportedPCMDataFromWAV() throws {
|
||||
let samples = Data([0x01, 0x02, 0x03, 0x04])
|
||||
let wav = makeWAV(samples: samples)
|
||||
|
||||
XCTAssertEqual(try MP3Encoder.pcmData(fromWAV: wav), samples)
|
||||
}
|
||||
|
||||
func testRejectsWrongSampleRate() {
|
||||
XCTAssertThrowsError(try MP3Encoder.pcmData(fromWAV: makeWAV(sampleRate: 44_100)))
|
||||
}
|
||||
|
||||
private func makeWAV(sampleRate: UInt32 = 16_000, samples: Data = Data([0, 0])) -> Data {
|
||||
var data = Data("RIFF".utf8)
|
||||
append(UInt32(36 + samples.count), to: &data)
|
||||
data.append(Data("WAVEfmt ".utf8))
|
||||
append(UInt32(16), to: &data)
|
||||
append(UInt16(1), to: &data)
|
||||
append(UInt16(1), to: &data)
|
||||
append(sampleRate, to: &data)
|
||||
append(sampleRate * 2, to: &data)
|
||||
append(UInt16(2), to: &data)
|
||||
append(UInt16(16), to: &data)
|
||||
data.append(Data("data".utf8))
|
||||
append(UInt32(samples.count), to: &data)
|
||||
data.append(samples)
|
||||
return data
|
||||
}
|
||||
|
||||
private func append(_ value: UInt16, to data: inout Data) {
|
||||
data.append(UInt8(value & 0xff))
|
||||
data.append(UInt8(value >> 8))
|
||||
}
|
||||
|
||||
private func append(_ value: UInt32, to data: inout Data) {
|
||||
data.append(UInt8(value & 0xff))
|
||||
data.append(UInt8((value >> 8) & 0xff))
|
||||
data.append(UInt8((value >> 16) & 0xff))
|
||||
data.append(UInt8((value >> 24) & 0xff))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user