Post

Replies

Boosts

Views

Activity

Reply to save audio file in iOS 18 instead of iOS 12
Here is the main method I use: func synthesize(utteranceString: String, avAudioPCMBufferFormatSettings: [String:Any], usingVoiceIdentifier voiceIdentifier: String, utteranceRate: Float, pitchMultiplier: Float, andSaveAudioFileTo saveToURL: URL, completionHandler: ((_ pcmBuffer: AVAudioPCMBuffer, _ audioFile: AVAudioFile?)->Void)? = nil) { print("#", #function, "voiceIdentifier: \(voiceIdentifier)", terminator: "\n") let utterance = AVSpeechUtterance(string: utteranceString) utterance.rate = utteranceRate utterance.pitchMultiplier = pitchMultiplier let voice = AVSpeechSynthesisVoice(identifier: voiceIdentifier) // TODO: Why is this generating a nil? utterance.voice = voice synthesizer.write(utterance) { (buffer: AVAudioBuffer) in print("in closure") // TODO: Tailor code in this closure to adjust to current device running this app project. guard let pcmBuffer = buffer as? AVAudioPCMBuffer else { fatalError("unknown buffer unknown buffer type: \(buffer)") } if pcmBuffer.frameLength == 0 { print("done because buffer frame length equals 0") } else { // append buffer to file if self.audioFile == nil { print("self.audioFile == nil") do { self.audioFile = try AVAudioFile( forWriting: saveToURL, settings: pcmBuffer.format.settings, commonFormat: .pcmFormatInt16, interleaved: false) } catch { print("try AVAudioFile( catch error: \(error.localizedDescription)") completionHandler?(pcmBuffer, nil) } } do { try self.audioFile!.write(from: pcmBuffer) print("!!! 2") completionHandler?(pcmBuffer, self.audioFile) } catch { print("try self.audioFile!.write(from: pcmBuffer) catch error: \(error.localizedDescription)") completionHandler?(pcmBuffer,nil) } } } }
Topic: Media Technologies SubTopic: Audio Tags:
Mar ’25