Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed break audio with stereo format. #1255

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Codec/AudioCodecRingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ final class AudioCodecRingBuffer {
let channelCount = Int(format.channelCount)
switch format.commonFormat {
case .pcmFormatInt16:
memcpy(current.int16ChannelData?[0].advanced(by: index), workingBuffer.int16ChannelData?[0].advanced(by: offset), numSamples * 2 * channelCount)
memcpy(current.int16ChannelData?[0].advanced(by: index * channelCount), workingBuffer.int16ChannelData?[0].advanced(by: offset * channelCount), numSamples * 2 * channelCount)
case .pcmFormatInt32:
memcpy(current.int32ChannelData?[0].advanced(by: index), workingBuffer.int32ChannelData?[0].advanced(by: offset), numSamples * 4 * channelCount)
memcpy(current.int32ChannelData?[0].advanced(by: index * channelCount), workingBuffer.int32ChannelData?[0].advanced(by: offset * channelCount), numSamples * 4 * channelCount)
case .pcmFormatFloat32:
memcpy(current.floatChannelData?[0].advanced(by: index), workingBuffer.floatChannelData?[0].advanced(by: offset), numSamples * 4 * channelCount)
memcpy(current.floatChannelData?[0].advanced(by: index * channelCount), workingBuffer.floatChannelData?[0].advanced(by: offset * channelCount), numSamples * 4 * channelCount)
default:
break
}
Expand Down
96 changes: 93 additions & 3 deletions Tests/Codec/AudioCodecBufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AVFoundation
@testable import HaishinKit

final class AudioCodecBufferTests: XCTestCase {
func testMain() {
func testMonoSamples256_16bit() {
guard
let sampleBuffer = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 256),
var asbd = sampleBuffer.formatDescription?.audioStreamBasicDescription else {
Expand All @@ -29,7 +29,31 @@ final class AudioCodecBufferTests: XCTestCase {
}
}

func testMain2() {
func testStereoSamples256_16bit() {
guard
let sampleBuffer = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 256, channels: 2),
var asbd = sampleBuffer.formatDescription?.audioStreamBasicDescription else {
XCTFail()
return
}
let buffer = AudioCodecRingBuffer(&asbd, numSamples: 1024)
for _ in 0..<1024/256 {
_ = buffer?.appendSampleBuffer(sampleBuffer, offset: 0)
}
XCTAssertTrue(buffer?.isReady == true)
let sampleBufferData = (try? sampleBuffer.dataBuffer?.dataBytes()) ?? Data()
var expectedData = Data()
expectedData.append(sampleBufferData)
expectedData.append(sampleBufferData)
expectedData.append(sampleBufferData)
expectedData.append(sampleBufferData)
if let pointer = buffer?.current.int16ChannelData?[0] {
let data = Data(bytes: pointer, count: 1024 * 2 * 2)
XCTAssertEqual(expectedData, data)
}
}

func testMonoSamples920_921_16bit() {
guard
let sampleBuffer_1 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 920),
let sampleBuffer_2 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 921),
Expand Down Expand Up @@ -70,7 +94,48 @@ final class AudioCodecBufferTests: XCTestCase {
}
}

func testMain3() {
func testStereoSamples920_921_16bit() {
guard
let sampleBuffer_1 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 920, channels: 2),
let sampleBuffer_2 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 921, channels: 2),
var asbd = sampleBuffer_1.formatDescription?.audioStreamBasicDescription,
let buffer = AudioCodecRingBuffer(&asbd, numSamples: 1024) else {
XCTFail()
return
}

let sampleBuffer_1Data = (try? sampleBuffer_1.dataBuffer?.dataBytes()) ?? Data()
let sampleBuffer_2Data = (try? sampleBuffer_2.dataBuffer?.dataBytes()) ?? Data()
var numBuffer = buffer.appendSampleBuffer(sampleBuffer_1, offset: 0)
numBuffer = buffer.appendSampleBuffer(sampleBuffer_2, offset: 0)

XCTAssertTrue(buffer.isReady)
if let pointer = buffer.current.int16ChannelData?[0] {
let data = Data(bytes: pointer, count: 1024 * 2 * 2)
var expectedData = Data()
expectedData.append(sampleBuffer_1Data)
expectedData.append(sampleBuffer_2Data.subdata(in: 0..<numBuffer * 2 * 2))
XCTAssertEqual(expectedData.bytes, data.bytes)
} else {
XCTFail()
}
buffer.next()
XCTAssertFalse(buffer.isReady)
XCTAssertEqual(numBuffer, 104)

var expectedData = Data()
expectedData.append(sampleBuffer_2Data.subdata(in: numBuffer * 2 * 2..<sampleBuffer_2Data.count))
numBuffer = buffer.appendSampleBuffer(sampleBuffer_2, offset: numBuffer)

if let pointer = buffer.current.int16ChannelData?[0] {
let data = Data(bytes: pointer, count: expectedData.count)
XCTAssertEqual(expectedData.bytes, data.bytes)
} else {
XCTFail()
}
}

func testMonoSamples920_921_16bit_2() {
guard
let sampleBuffer_1 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 920),
let sampleBuffer_2 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 921),
Expand All @@ -95,6 +160,31 @@ final class AudioCodecBufferTests: XCTestCase {
}
}

func testStereoSamples920_921_16bit_2() {
guard
let sampleBuffer_1 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 920, channels: 2),
let sampleBuffer_2 = SinWaveUtil.makeCMSampleBuffer(44100, numSamples: 921, channels: 2),
var asbd = sampleBuffer_1.formatDescription?.audioStreamBasicDescription,
let buffer = AudioCodecRingBuffer(&asbd, numSamples: 1024) else {
XCTFail()
return
}
let sampleBuffer_2Data = (try? sampleBuffer_2.dataBuffer?.dataBytes()) ?? Data()

appendSampleBuffer(buffer, sampleBuffer: sampleBuffer_1, offset: 0)
appendSampleBuffer(buffer, sampleBuffer: sampleBuffer_2, offset: 0)

var expectedData = Data()
expectedData.append(sampleBuffer_2Data.subdata(in: 104 * 2 * 2..<sampleBuffer_2Data.count))

if let pointer = buffer.current.int16ChannelData?[0] {
let data = Data(bytes: pointer, count: expectedData.count)
XCTAssertEqual(expectedData.bytes, data.bytes)
} else {
XCTFail()
}
}

private func appendSampleBuffer(_ buffer: AudioCodecRingBuffer, sampleBuffer: CMSampleBuffer, offset: Int = 0) {
let numSamples = buffer.appendSampleBuffer(sampleBuffer, offset: offset)
if buffer.isReady {
Expand Down
29 changes: 21 additions & 8 deletions Tests/SinWaveUtil.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AVFoundation

final class SinWaveUtil {
static func makeCMSampleBuffer(_ sampleRate: Double = 44100, numSamples: Int = 1024) -> CMSampleBuffer? {
static func makeCMSampleBuffer(_ sampleRate: Double = 44100, numSamples: Int = 1024, channels: UInt32 = 1) -> CMSampleBuffer? {
var status: OSStatus = noErr
var sampleBuffer: CMSampleBuffer?
var formatDescription: CMAudioFormatDescription? = nil
Expand All @@ -15,16 +15,23 @@ final class SinWaveUtil {
mSampleRate: sampleRate,
mFormatID: kAudioFormatLinearPCM,
mFormatFlags: 0xc,
mBytesPerPacket: 2,
mBytesPerPacket: 2 * channels,
mFramesPerPacket: 1,
mBytesPerFrame: 2,
mChannelsPerFrame: 1,
mBytesPerFrame: 2 * channels,
mChannelsPerFrame: channels,
mBitsPerChannel: 16,
mReserved: 0
)

status = CMAudioFormatDescriptionCreate(
allocator: kCFAllocatorDefault, asbd: &asbd, layoutSize: 0, layout: nil, magicCookieSize: 0, magicCookie: nil, extensions: nil, formatDescriptionOut: &formatDescription
allocator: kCFAllocatorDefault,
asbd: &asbd,
layoutSize: 0,
layout: nil,
magicCookieSize: 0,
magicCookie: nil,
extensions: nil,
formatDescriptionOut: &formatDescription
)

guard status == noErr else {
Expand Down Expand Up @@ -54,10 +61,16 @@ final class SinWaveUtil {
let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(numSamples))!
buffer.frameLength = buffer.frameCapacity
let channels = Int(format.channelCount)
for ch in (0..<channels) {
let samples = buffer.int16ChannelData![ch]
for n in 0..<Int(buffer.frameLength) {
let samples = buffer.int16ChannelData![0]
for n in 0..<Int(buffer.frameLength) {
switch channels {
case 1:
samples[n] = Int16(sinf(Float(2.0 * .pi) * 440.0 * Float(n) / Float(sampleRate)) * 16383.0)
case 2:
samples[n * 2 + 0] = Int16(sinf(Float(2.0 * .pi) * 440.0 * Float(n) / Float(sampleRate)) * 16383.0)
samples[n * 2 + 1] = Int16(sinf(Float(2.0 * .pi) * 440.0 * Float(n + 1) / Float(sampleRate)) * 16383.0)
default:
break
}
}

Expand Down