Skip to content

Commit 1730804

Browse files
committed
feat: use input sample rate, add debug playback function
1 parent ace6f02 commit 1730804

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

ios/ExpoSpeechRecognizer.swift

+39-30
Original file line numberDiff line numberDiff line change
@@ -291,51 +291,30 @@ actor ExpoSpeechRecognizer: ObservableObject {
291291
} else {
292292
chunkDelayMillis = 50 // Network-based recognition
293293
}
294+
let chunkDelayNs = UInt64(chunkDelayMillis) * 1_000_000
295+
// var playbackBuffers = [AVAudioPCMBuffer]()
294296

295297
Task.detached(priority: .userInitiated) {
296298
do {
297299
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-
307300
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+
)!
310304

311305
while file.framePosition < file.length {
312306
let framesToRead = min(
313307
bufferCapacity, AVAudioFrameCount(file.length - file.framePosition)
314308
)
315309
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)
335313
}
336314

337315
print("Audio streaming ended")
338316
request.endAudio()
317+
// await self.playBack(playbackBuffers: playbackBuffers)
339318
} catch {
340319
print("Error feeding audio file: \(error)")
341320
request.endAudio()
@@ -851,6 +830,36 @@ actor ExpoSpeechRecognizer: ObservableObject {
851830
}
852831
}
853832
}
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+
*/
854863
}
855864

856865
extension SFSpeechRecognizer {

0 commit comments

Comments
 (0)