Hello everyone,
I’m experiencing occasional crashes in my app related to the Core Haptics framework, specifically when creating haptic pattern players. The crashes are intermittent and not easily reproducible, so I’m hoping to get some guidance on what might be causing them.
It's seems it's connected to Audio Resource I'm using within AHAP file.
Setup:
I use AVAudioSession and AVAudioEngine to record and play continuous audio. After activating the audio session and setting up the audio engine, I initialize the CHHapticEngine as follows:
let engine = try CHHapticEngine(audioSession: .sharedInstance())
...
try engine?.start()
// Recreate all haptic pattern players you had created.
let pattern = createPatternFromAHAP(Pattern.thinking.rawValue)!
thinkingHapticPlayer = try? engine?.makePlayer(with: pattern)
// Repeat for other players...
AHAP file:
"Pattern":
[
... haptic events
{
"Event":
{
"Time": 0.0,
"EventType": "AudioCustom",
"EventWaveformPath": "voice.chat.thinking.mp3",
"EventParameters":
[
{ "ParameterID": "AudioVolume", "ParameterValue": 0.7 }
]
}
}
]
I’m receiving the following crash report:
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x00000001ba525c68
0
CoreHaptics
+[CHHapticEngine(CHHapticEngineInternal) doRegisterAudioResource:options:fromPattern:player:error:].cold.1 + 104
1
CoreHaptics
+[CHHapticEngine(CHHapticEngineInternal) doRegisterAudioResource:options:fromPattern:player:error:].cold.1 + 104
2
CoreHaptics
+[CHHapticEngine(CHHapticEngineInternal) doRegisterAudioResource:options:fromPattern:player:error:] + 3784
3
CoreHaptics
-[CHHapticPattern resolveExternalResources:error:] + 388
4
CoreHaptics
-[PatternPlayer initWithPlayable:engine:privileged:error:] + 560
5
CoreHaptics
-[CHHapticEngine createPlayerWithPattern:error:] + 256
6
Mind
VoiceChatHapticEngine.swift - Line 170
VoiceChatHapticEngine.createThinkingHapticPlayer() + 170
Has anyone encountered similar crashes when working with CHHapticEngine and haptic patterns that contains audioCustom event?
Thank you so much for your help.
Ondrej
Post
Replies
Boosts
Views
Activity
Hi everyone,
I’m working on a project that involves streaming audio over WebSockets, and I need to compress the audio to reduce bandwidth usage. I’m currently using AVAudioEngine to capture and process audio in PCM format (AVAudioPCMBuffer), but I want to compress the buffer into Opus (or another efficient codec) before sending it over the network.
Has anyone worked with compressing an AVAudioPCMBuffer into Opus format within a tap on the inputNode, or could you recommend the best approach for compressing the PCM buffer into a different format? I haven’t been able to find a working solution for this.
Any advice or code examples would be greatly appreciated!
Thanks in advance,
Ondřej
--
My current code without the compression:
inputNode.installTap(onBus: .zero, bufferSize: 1440, format: nil) { [weak self] buffer, time in
guard let self else {
return
}
// 1. Send data
// a) Convert the buffer into the desired format
if let outputBuffer = buffer.convert(toFormat: Self.websocketInputFormat) {
// b) Use the converted buffer
// TODO: compress it into a different format
if let data = outputBuffer.convertToData() {
self.sendAudio(data)
}
}
// 2. Get sound level
self.visualizeRecorderBuffer(buffer)
}
func convert(toFormat outputFormat: AVAudioFormat) -> AVAudioPCMBuffer? {
let outputFrameCapacity = AVAudioFrameCount(
round(Double(frameLength) * (outputFormat.sampleRate / format.sampleRate))
)
guard
let outputBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: outputFrameCapacity),
let converter = AVAudioConverter(from: format, to: outputFormat)
else {
return nil
}
converter.convert(to: outputBuffer, error: nil) { packetCount, status in
status.pointee = .haveData
return self
}
return outputBuffer
}
static private let websocketInputFormat = AVAudioFormat(
commonFormat: .pcmFormatInt16,
sampleRate: 16000,
channels: 1,
interleaved: false
)!