@@ -291,51 +291,30 @@ actor ExpoSpeechRecognizer: ObservableObject {
291
291
} else {
292
292
chunkDelayMillis = 50 // Network-based recognition
293
293
}
294
+ let chunkDelayNs = UInt64 ( chunkDelayMillis) * 1_000_000
295
+ // var playbackBuffers = [AVAudioPCMBuffer]()
294
296
295
297
Task . detached ( priority: . userInitiated) {
296
298
do {
297
299
let file = try AVAudioFile ( forReading: url)
298
- let inputFormat = file. processingFormat
299
- print ( " Input file format: \( inputFormat) " )
300
-
301
- let outputFormat = AVAudioFormat (
302
- commonFormat: . pcmFormatFloat32, sampleRate: 16000 , channels: 1 , interleaved: false ) !
303
- print ( " Output format: \( outputFormat) " )
304
-
305
- let converter = AVAudioConverter ( from: inputFormat, to: outputFormat) !
306
-
307
300
let bufferCapacity : AVAudioFrameCount = 4096
308
- let inputBuffer = AVAudioPCMBuffer ( pcmFormat: inputFormat, frameCapacity: bufferCapacity) !
309
- let outputBuffer = AVAudioPCMBuffer ( pcmFormat: outputFormat, frameCapacity: bufferCapacity) !
301
+ let inputBuffer = AVAudioPCMBuffer (
302
+ pcmFormat: file. processingFormat, frameCapacity: bufferCapacity
303
+ ) !
310
304
311
305
while file. framePosition < file. length {
312
306
let framesToRead = min (
313
307
bufferCapacity, AVAudioFrameCount ( file. length - file. framePosition)
314
308
)
315
309
try file. read ( into: inputBuffer, frameCount: framesToRead)
316
-
317
- var error : NSError ?
318
- let inputBlock : AVAudioConverterInputBlock = { _, outStatus in
319
- outStatus. pointee = . haveData
320
- return inputBuffer
321
- }
322
-
323
- converter. convert ( to: outputBuffer, error: & error, withInputFrom: inputBlock)
324
-
325
- if let error = error {
326
- throw error
327
- }
328
-
329
- request. append ( outputBuffer)
330
-
331
- // Avoid overloading the recognizer
332
- if #available( iOS 16 , * ) {
333
- try await Task . sleep ( for: . milliseconds( chunkDelayMillis) )
334
- }
310
+ request. append ( inputBuffer)
311
+ // playbackBuffers.append(inputBuffer.copy() as! AVAudioPCMBuffer)
312
+ try await Task . sleep ( nanoseconds: chunkDelayNs)
335
313
}
336
314
337
315
print ( " Audio streaming ended " )
338
316
request. endAudio ( )
317
+ // await self.playBack(playbackBuffers: playbackBuffers)
339
318
} catch {
340
319
print ( " Error feeding audio file: \( error) " )
341
320
request. endAudio ( )
@@ -851,6 +830,36 @@ actor ExpoSpeechRecognizer: ObservableObject {
851
830
}
852
831
}
853
832
}
833
+
834
+ /*
835
+ private var playbackEngine: AVAudioEngine?
836
+ private var playerNode: AVAudioPlayerNode?
837
+ /// Playback audio from an array of AVAudioPCMBuffers
838
+ /// For testing purposes only
839
+ func playBack(playbackBuffers: [AVAudioPCMBuffer]) {
840
+ guard !playbackBuffers.isEmpty else { return }
841
+
842
+ playbackEngine = AVAudioEngine()
843
+ playerNode = AVAudioPlayerNode()
844
+
845
+ guard let playbackEngine = playbackEngine, let playerNode = playerNode else { return }
846
+
847
+ playbackEngine.attach(playerNode)
848
+ let outputFormat = playbackBuffers[0].format
849
+ playbackEngine.connect(playerNode, to: playbackEngine.mainMixerNode, format: outputFormat)
850
+
851
+ for buffer in playbackBuffers {
852
+ playerNode.scheduleBuffer(buffer, completionHandler: nil)
853
+ }
854
+
855
+ do {
856
+ try playbackEngine.start()
857
+ playerNode.play()
858
+ } catch {
859
+ print("Failed to start playback engine: \(error)")
860
+ }
861
+ }
862
+ */
854
863
}
855
864
856
865
extension SFSpeechRecognizer {
0 commit comments