46 lines
1.4 KiB
Swift
46 lines
1.4 KiB
Swift
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))
|
|
}
|
|
}
|