-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceProcessor.swift
155 lines (124 loc) · 5.28 KB
/
VoiceProcessor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Copyright 2020 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 AVFoundation
@objc(PvVoiceProcessor)
class PvVoiceProcessor: RCTEventEmitter {
private let audioInputEngine: AudioInputEngine = AudioInputEngine()
private let BUFFER_EMITTER_KEY = "buffer_sent"
private var isListening = false
public override init() {
super.init()
}
override func supportedEvents() -> [String]! {
return [self.BUFFER_EMITTER_KEY]
}
override static func requiresMainQueueSetup() -> Bool {
return true
}
@objc override func constantsToExport() -> [AnyHashable : Any] {
return [
"BUFFER_EMITTER_KEY": self.BUFFER_EMITTER_KEY
]
}
@objc(start:sampleRate:resolver:rejecter:)
func start(frameLength: Int, sampleRate: Int,
resolver resolve:RCTPromiseResolveBlock, rejecter reject:RCTPromiseRejectBlock) -> Void {
guard !isListening else {
NSLog("Audio engine already running.");
return
}
audioInputEngine.audioInput = { [weak self] audio in
guard let `self` = self else {
return
}
let buffer = UnsafeBufferPointer(start: audio, count: frameLength);
self.sendEvent(withName: self.BUFFER_EMITTER_KEY, body: Array(buffer))
}
let audioSession = AVAudioSession.sharedInstance()
if audioSession.recordPermission == .denied {
NSLog("Recording permissions denied")
return;
}
do{
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, options: [.mixWithOthers, .defaultToSpeaker, .allowBluetooth])
try audioInputEngine.start(frameLength:frameLength, sampleRate:sampleRate)
}
catch{
NSLog("Unable to start audio engine");
return
}
isListening = true
resolve(true)
}
@objc(stop:rejecter:)
func stop(resolver resolve:RCTPromiseResolveBlock, rejecter reject:RCTPromiseRejectBlock) -> Void {
guard isListening else {
return
}
self.audioInputEngine.stop()
isListening = false
resolve(true)
}
private class AudioInputEngine {
private let numBuffers = 3
private var audioQueue: AudioQueueRef?
var audioInput: ((UnsafePointer<Int16>) -> Void)?
func start(frameLength:Int, sampleRate:Int) throws {
var format = AudioStreamBasicDescription(
mSampleRate: Float64(sampleRate),
mFormatID: kAudioFormatLinearPCM,
mFormatFlags: kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
mBytesPerPacket: 2,
mFramesPerPacket: 1,
mBytesPerFrame: 2,
mChannelsPerFrame: 1,
mBitsPerChannel: 16,
mReserved: 0)
let userData = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
AudioQueueNewInput(&format, createAudioQueueCallback(), userData, nil, nil, 0, &audioQueue)
guard let queue = audioQueue else {
return
}
let bufferSize = UInt32(frameLength) * 2
for _ in 0..<numBuffers {
var bufferRef: AudioQueueBufferRef? = nil
AudioQueueAllocateBuffer(queue, bufferSize, &bufferRef)
if let buffer = bufferRef {
AudioQueueEnqueueBuffer(queue, buffer, 0, nil)
}
}
AudioQueueStart(queue, nil)
}
func stop() {
guard let audioQueue = audioQueue else {
return
}
AudioQueueFlush(audioQueue)
AudioQueueStop(audioQueue, true)
AudioQueueDispose(audioQueue, true)
audioInput = nil
}
private func createAudioQueueCallback() -> AudioQueueInputCallback {
return { userData, queue, bufferRef, startTimeRef, numPackets, packetDescriptions in
// `self` is passed in as userData in the audio queue callback.
guard let userData = userData else {
return
}
let `self` = Unmanaged<AudioInputEngine>.fromOpaque(userData).takeUnretainedValue()
let pcm = bufferRef.pointee.mAudioData.assumingMemoryBound(to: Int16.self)
if let audioInput = self.audioInput {
audioInput(pcm)
}
AudioQueueEnqueueBuffer(queue, bufferRef, 0, nil)
}
}
}
}