- Notifications
You must be signed in to change notification settings - Fork6
Swift client for Hume AI APIs
License
HumeAI/hume-swift-sdk
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
API reference documentation is availablehere.
Adding to your Xcode project
- Open project settings > Package Dependencies
- Click the + button to add a package dependency
- Enter SDK URL (
https://github.com/HumeAI/hume-swift-sdk.git) - Set the version rule (we recommend pinning to a specific version)
- Click "Add Package"
- Add the
Privacy - Microphone Usage Descriptionentry to yourInfo.plist - (Optional) If you plan to support background audio, select the "Audio, Airplay, and Picture and Picture" option in the "Background Modes" section of your project capabilities.
Adding to yourPackage.swift
dependencies: [ .package(url: "https://github.com/HumeAI/hume-swift-sdk.git", from: "x.x.x") ]The SDK provides aVoiceProvider abstraction that manages active socket connection against the/chat endpoint. This abstraction handles and coordinates the audio stack.
Capabilities
- Pipes output audio from
audio_outputevents into SoundPlayer to play back in realtime. VoiceProvider.connect(...)opens and connects to the/chatsocket, waits for thechat_metadataevent to be received, and starts the microphone.VoiceProvider.disconnect()closes the socket, stops the microphone, and stops all playback.
Example
import Humelettoken=tryawait myAccessTokenClient.fetchAccessToken()humeClient=HumeClient(options:.accessToken(token: token))letvoiceProvider=VoiceProviderFactory.getVoiceProvider(client: humeClient)voiceProvider.delegate= myDelegate// Request permission to record audio. Be sure to add `Privacy - Microphone Usage Description`// to your Info.plistifMicrophonePermission.current==.undetermined{letgranted=awaitMicrophonePermission.requestPermissions()guard grantedelse{print("user declined mic permsissions")return}}elseifMicrophonePermission.current==.denied{print("user previously declined mic permissions") // ask user to update in settingsreturn}letsessionSettings=SessionSettings( systemPrompt:"my optional system prompt", variables:["myCustomVariable": myValue,"datetime":Date().formattedForSessionSettings()])tryawait voiceProvider.connect( configId: myConfigId, configVersion:nil, sessionSettings: sessionSettings)// Sending user text input manuallyawaitself.voiceProvider.sendUserInput(message:"Hey, how are you?")
ImplementVoiceProviderDelegate methods to be notified of events, errors, meter data, state, etc.
Example
import Humelettoken=tryawait myAccessTokenClient.fetchAccessToken()humeClient=HumeClient(options:.accessToken(token: token))letttsClient= humeClient.ttsletpostedUtterances:[PostedUtterance]=[PostedUtterance( description: voiceDescription, speed: speed, trailingSilence: trailingSilence, text: text, voice:.postedUtteranceVoiceWithId(PostedUtteranceVoiceWithId(id:"<config ID>", provider:.humeAi)))]letfmt=.wav(FormatWav()let request=PostedTts( context:nil, numGenerations:1, splitUtterances:nil, stripHeaders:nil, utterances: postedUtterances, instantMode:true, format: fmt)let stream= tts.synthesizeFileStreaming(request: request)fortryawaitdata in stream{ // convert data to SoundClipguardlet soundClip=SoundClip.from(data)else{print("warn: failed to create sound clip")return} // play SoundClip with ttsPlayertryawait ttsPlayer.play(soundClip: soundClip, format: fmt) _data.append(data)}
This SDK is in beta, and there may be breaking changes between versions without a majorversion update. Therefore, we recommend pinning the package version to a specific version.This way, you can install the same version each time without breaking changes.
- Audio interruptions (e.g. phone calls) are not yet handled.
- Manually starting/stopping
AVAudioSessionwill likely break an active voice session. Leave all audio handling toAudioHub. If you need to add your own output audio nodes, see `AudioHub.addNode(_:) - Input metering is not yet implemented.
About
Swift client for Hume AI APIs
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors5
Uh oh!
There was an error while loading.Please reload this page.
