• What and why

    • 컴퓨터 음성합성 API
    • 사용용도
      • 안내 음성
      • 보이지 않는 인터페이스
      • 교육용 앱
      • 그외 등을...
    • 접근성용으로도 좋다.
      • 다만 보이스오버의 대체제는 아니다.
        • 보이스오버와 오디오가 겹칠 수 있다.
        • 점자 디바이스와 호환되지 않는다.
        • 이런 용도로는 UIAccessibility를 쓰라.
  • AVSpeechSynthesis basics

    • synthesizer 생성

      • 말이 끝날 때까지 retain되고 있어야 한다. dealloc되면 말이 끊긴다.
      let synthesizer = AVSpeechSynthesizer()
      
    • utterance를 만들고, 이를 synthesizer에 dispatch한다.

      let utterance = AVSpeechUtterance(string: "Hello")
      synthesizer.speak(utterance)
      
    • 내부적으로 AVAudioSession이 자동으로 활성화된다.

      • 다른 audio와 섞기 위해서는 다음과 같이 한다.

        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
        	with: .mixOthers)
        
      • 다른 오디오 볼륨을 일시적으로 줄이기 위해서는 다음과 같이.

        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,
        	with: .duckOthers)
        
      • 이 때 shared audio session을 쓰기 때문에, 다른 오디오가 끊기는 것을 막지않기 위해 inactive는 하지 않는다. → 하고 싶으면 스스로 한다.

    • Delegate를 통해서 음성 중간중간을 후킹할 수 있다.

      • 발화 시작 및 종료
      • 특정 범위의 캐릭터가 발화되기 직전
      • 발화 일시 정지 / 재개
    synthesizer.delegate = self
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance)
    {
    	print("Speech started")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
    	print("Speech finished")
    }
    
    func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
    	guard let rangeInString = Range(characterRange, in: utterance.speechString()) else { return }
    	print("Will speak: \\(utterance.speechString[rangeInString])")
    }
    
  • Choosing the right voice

    • 지원하는 언어당 음성이 하나씩 빌트인 되어 있다.
    • 시리 음성은 이 API로는 사용할 수 없다.
    • 유저는 좀 더 고해상도의 음성을 다운로드 할 수도 있다.
    let utterance = AVSpeechUtterance(string: "Hello")
    utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
    
    let allVoices = AVSpeechSynthesisVoice.speechVoices()
    utterance.voide = AVSpeechSynthesisVoice(identifier: allVoices[0].identifier)
    
    • 지원 언어

    스크린샷 2021-08-17 오후 7.10.24.png

  • Rate, Pitch, and volume

    • Rate: 속도
      • 0~1사이 값
        • [0,.5] 까지는 0~1배속
        • [.5,1] 까지는 1~4배속
    let utterance = AVSpeechUtterance(string: "Hello")
    utterance.rate = 0.75
    utterance.rate = 0.25
    utterance.rate = AVSpeechUtteranceDefaultSpeechRate
    utterance.rate = AVSpeechUtteranceMaximumSpeechRate
    
    • Pitch
      • 0~1사이의 값
    • Volume
      • 0~1 사이의 값
      • 시스템 볼륨에 영향을 주지 않는다.
  • Attributed strings

    • Attribute를 통해서 speech를 커스텀할 수 있다.

    • IPA(International Phonetic Alphabet) Notation 사용

      • 특별한 이름, 고유 명사, 비즈니스 이름 등에 사용
      • 지원 언어 제한 - en-US, en-AU, en-GB, de-DE, es-ES, es-MX, fr-CA, fr-FR, it-IT, ja-JP
      • IPA를 어떻게 입력하는가?
        • Settings>General>Accessibility>Speech>Pronunciations 통해서 발음을 얻어온다. → 받아쓰기로. 근데 잘 안된다.
      let attributedString = NSMutableAttributedString(string: "Hello iPhone")
      attributedString.addAttribute(.accessibilitySpeechIPANotation, value: "(발음기호)", range: NSRange(location: 6, length: 6)))