Skip to content

Commit

Permalink
LLM Voice Assistant for iOS (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisMik authored Jun 5, 2024
1 parent 36b07b4 commit 1c1765d
Show file tree
Hide file tree
Showing 37 changed files with 1,591 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/swift-codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Swift Codestyle

on:
workflow_dispatch:
push:
branches: [ main ]
paths:
- '**/*.swift'
pull_request:
branches: [ main, 'v[0-9]+.[0-9]+' ]
paths:
- '**/*.swift'

jobs:
check-switch-codestyle:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Check swift codestyle
uses: norio-nomura/action-swiftlint@3.2.1
with:
args: lint --config res/.lint/swift/.swiftlint.yml --strict
1 change: 1 addition & 0 deletions recipes/llm-voice-assistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Hands-free voice assistant powered by a large language model (LLM), all voice re

- [Python](python)
- [Web](web)
- [iOS](ios)
22 changes: 22 additions & 0 deletions recipes/llm-voice-assistant/ios/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Exclude the build directory
build/*

# Exclude temp nibs and swap files
*~.nib
*.swp

# Exclude OS X folder attributes
.DS_Store

# Exclude user-specific XCode 3 and 4 files
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
*.xcworkspace
project.xcworkspace
contents.xcworkspacedata
xcuserdata
Pods

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02A11944268D39A700A2AC99"
BuildableName = "LLMVoiceAssistantDemo.app"
BlueprintName = "LLMVoiceAssistantDemo"
ReferencedContainer = "container:LLMVoiceAssistantDemo.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C7969FC426F53FBB0013DD41"
BuildableName = "LLMVoiceAssistantDemoUITests.xctest"
BlueprintName = "LLMVoiceAssistantDemoUITests"
ReferencedContainer = "container:LLMVoiceAssistantDemo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02A11944268D39A700A2AC99"
BuildableName = "LLMVoiceAssistantDemo.app"
BlueprintName = "LLMVoiceAssistantDemo"
ReferencedContainer = "container:LLMVoiceAssistantDemo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "APP_ID"
value = "G+s1i6gv9HGe0e0y0WPWwFmC+ShUrjTIce2XJWFbeBiOcJ1B/2wCuQ=="
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "02A11944268D39A700A2AC99"
BuildableName = "LLMVoiceAssistantDemo.app"
BlueprintName = "LLMVoiceAssistantDemo"
ReferencedContainer = "container:LLMVoiceAssistantDemo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"images" : [
{
"size" : "20x20",
"idiom": "iphone",
"filename" : "pv_circle_512-20@2x.png",
"scale": "2x"
},
{
"size" : "20x20",
"idiom": "iphone",
"filename" : "pv_circle_512-20@3x.png",
"scale": "3x"
},
{
"size" : "20x20",
"idiom": "ipad",
"filename" : "pv_circle_512-20.png",
"scale": "1x"
},
{
"size" : "20x20",
"idiom": "ipad",
"filename" : "pv_circle_512-20@2x.png",
"scale": "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "pv_circle_512-29@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "pv_circle_512-29@3x.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "pv_circle_512-40@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "pv_circle_512-40@3x.png",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "pv_circle_512-60@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "pv_circle_512-60@3x.png",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "pv_circle_512-29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "pv_circle_512-29@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "pv_circle_512-40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "pv_circle_512-40@2x.png",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "pv_circle_512-76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "pv_circle_512-76@2x.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "pv_circle_512-83.5@2x.png",
"scale" : "2x"
},
{
"size" : "1024x1024",
"idiom" : "ios-marketing",
"filename" : "pv_circle_512-1024.png",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright 2024 Picovoice Inc.
// You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
// file accompanying this source.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//

import Foundation
import AVFoundation

class AudioPlayerStream {
private let engine = AVAudioEngine()
private let playerNode = AVAudioPlayerNode()
private let mixerNode = AVAudioMixerNode()

private var pcmBuffers = [AVAudioPCMBuffer]()
public var isPlaying = false

init(sampleRate: Double) throws {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback, mode: .default)
try audioSession.setActive(true)

let format = AVAudioFormat(
commonFormat: .pcmFormatFloat32,
sampleRate: sampleRate,
channels: AVAudioChannelCount(1),
interleaved: false)

engine.attach(mixerNode)
engine.connect(mixerNode, to: engine.outputNode, format: format)

engine.attach(playerNode)
engine.connect(playerNode, to: mixerNode, format: format)

try engine.start()
}

func playStreamPCM(_ pcmData: [Int16], completion: @escaping (Bool) -> Void) {
let audioBuffer = AVAudioPCMBuffer(
pcmFormat: playerNode.outputFormat(forBus: 0), frameCapacity: AVAudioFrameCount(pcmData.count))!

audioBuffer.frameLength = audioBuffer.frameCapacity
let buf = audioBuffer.floatChannelData![0]
for (index, sample) in pcmData.enumerated() {
var convertedSample = Float32(sample) / Float32(Int16.max)
if convertedSample > 1 {
convertedSample = 1
}
if convertedSample < -1 {
convertedSample = -1
}
buf[index] = convertedSample
}

pcmBuffers.append(audioBuffer)
if !isPlaying {
playNextPCMBuffer(completion: completion)
} else {
completion(true)
}
}

private func playNextPCMBuffer(completion: @escaping (Bool) -> Void) {
guard let pcmData = pcmBuffers.first else {
isPlaying = false
completion(false)
return
}
pcmBuffers.removeFirst()

playerNode.scheduleBuffer(pcmData) { [weak self] in
self?.playNextPCMBuffer(completion: completion)
}

playerNode.play()
isPlaying = true
completion(true)
}

func stopStreamPCM() {
playerNode.stop()
engine.stop()
}
}
Loading

0 comments on commit 1c1765d

Please sign in to comment.