excitedcosmos/ecwavinghaptics
1. **结合音频处理和触觉反馈**,提供实时的音频可感知化体验
特征和特性
- 结合音频处理和触觉反馈,提供实时的音频可感知化体验
- 支持自定义音频文件和频率范围
- 提供循环播放选项
- 实现了后台暂停和前台恢复功能
- 使用 Core Haptics 框架提供触觉反馈
- 利用 Accelerate 框架进行高效的 FFT 计算
注意事项
- 需要 iOS 13.0 或更高版本,因为使用了 Core Haptics 框架
- 触觉反馈依赖于设备硬件支持,部分设备可能不支持
- 音频处理可能会增加设备的电池消耗
- 大文件或高采样率的音频可能会影响性能
使用方法
Swift Package Manager
.package(url: "https://github.com/excitedcosmos/ECWavingHaptics.git", from: "1.0.0")或者
https://github.com/excitedcosmos/ECWavingHaptics.git创建 ECWavingHaptics 实例
guard let url = Bundle.main.url(forResource: "Sound-File-Name", withExtension: "mp3") else {
print("Unknown Audio File")
return
}
let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: false)!
let haptics = ECWavingHaptics(
audioFileURL: url,
audioFormat: audioFormat,
minFrequency: 150,
maxFrequency: 220,
isLooping: true,
playCallback: { print("Start Playing") },
stopCallback: { print("Stopped") },
errorCallback: { error in print("Error:\(error)") }
)参数说明:
audioFileURL: 音频文件的 URLaudioFormat: 音频格式,包括采样率和通道数minFrequency: 最小频率,单位为 HzmaxFrequency: 最大频率,单位为 HzisLooping: 是否循环播放playCallback: 开始播放时的回调函数stopCallback: 停止播放时的回调函数errorCallback: 发生错误时的回调函数
开始音频处理和触觉反馈
haptics.startAudioProcessing()停止音频处理和触觉反馈
haptics.stopAudioProcessing()在适当的时机释放资源
deinit {
haptics.stopAudioProcessing()
}ECWavingHaptics Class
Features and Characteristics
- Combines audio processing and haptic feedback to provide real-time audio sensorization experience
- Supports custom audio files and frequency ranges
- Offers looping playback option
- Implements background pause and foreground resume functionality
- Uses Core Haptics framework for haptic feedback
- Utilizes Accelerate framework for efficient FFT calculations
Notes
- Requires iOS 13.0 or higher due to the use of Core Haptics framework
- Haptic feedback depends on device hardware support, some devices may not support it
- Audio processing may increase device battery consumption
- Large files or high sample rate audio may affect performance
Usage
- Create an
ECWavingHapticsinstance:
```swift guard let url = Bundle.main.url(forResource: "Sound-File-Name", withExtension: "mp3") else { print("Unknown Audio File") return } let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 2, interleaved: false)!
let haptics = ECWavingHaptics( audioFileURL: url, audioFormat: audioFormat, minFrequency: 150, maxFrequency: 220, isLooping: true, playCallback: { print("Start Playing") }, stopCallback: { print("Stopped") }, errorCallback: { error in print("Error:\(error)") } ) ```
Parameter description:
- audioFileURL: URL of the audio file - audioFormat: Audio format, including sample rate and number of channels - minFrequency: Minimum frequency in Hz - maxFrequency: Maximum frequency in Hz - isLooping: Whether to loop playback - playCallback: Callback function when playback starts - stopCallback: Callback function when playback stops - errorCallback: Callback function when an error occurs
- Start audio processing and haptic feedback:
``swift haptics.startAudioProcessing() ``
- Stop audio processing and haptic feedback:
``swift haptics.stopAudioProcessing() ``
- Release resources at the appropriate time:
``swift deinit { haptics.stopAudioProcessing() } ``
Package Metadata
Repository: excitedcosmos/ecwavinghaptics
Default branch: main
README: README.md