Link Search Menu Expand Document

Convert an Audio Stream to Haptics in Real Time

Real-time conversion of an audio stream into haptics requires two steps:

  1. Attach an audio source to the LofeltHaptics object.
  2. Play the audio source.

The audio that plays from the audio source is converted to haptic data and played to the device in real time.

Attaching an Audio Source

func attachAudioSource(_ audioNode: AVAudioNode, error: NSErrorPointer)

attachAudioSource() accepts audio sources of type AVAudioNode. error: NSErrorPointer indicates any failures that occurred in attempting to attach the audio source.

Realtime Audio-to-Haptics Example

This example attaches an AVAudioPlayerNode to the LofeltHaptics for real-time audio to haptics.

// Set up haptics.
let haptics = try! LofeltHaptics.init(andReturnError: ())

// Load test audio.
let fileUrl = Bundle.main.url(forResource: "my-audio-file", withExtension: "wav")!
let audioFile = try! AVAudioFile.init(forReading: fileUrl)

// Create audio engine and player.
let audioEngine = AVAudioEngine()
let audioPlayer = AVAudioPlayerNode()

// Connect player to main mixer.
audioEngine.attach(audioPlayer)
let mainMixer = audioEngine.mainMixerNode
audioEngine.connect(audioPlayer, to: mainMixer, format: audioFile.processingFormat)

// Connect player to Lofelt haptics.
try! haptics.attachAudioSource(audioPlayer)

// Start audio engine and play audio.
try! audioEngine.start()
audioPlayer.scheduleFile(audioFile, at: nil, completionHandler: nil)
audioPlayer.play()

Example Project

Included in the Lofelt Studio download, in the sdk/examples folder, is an example project for performing real-time audio-to-haptic conversion on iPhone called LofeltHapticsExampleRealtime. To run the example, open the project in Xcode, target your iPhone, and build. Press and release the button on the screen and you will hear audio play while also feeling the haptic effects. Replace the audio file with another and you will feel different haptics as the new audio file plays.

< Prev Next >


Copyright © 2020 Lofelt GmbH. All rights reserved.