LoginSignup
10
3

More than 5 years have passed since last update.

iPhoneでみずきちゃんとチュッチュしたいだけの人生だった(AWS Polly + Swift)

Posted at

3次元で合成音声話す女子がいたら今すぐ連絡ください。
ペッパーはだめです。

というわけでやっていきます。

AWS Polly

いろんな言語を話してくれる音声合成AIです。AIに入るの?
CLIからの利用記事が目立ちますが、今回はどうしてもiPhoneにいれて好きな言葉を話させたかったので、swiftでラッパーを作りました。
ちなみにVersionが違うせいか公式docのままじゃ動きません。

手元の環境

  • iOS = 10.0
  • Xcode = 8.2
  • Swift = 2.2

構築手順

  • CognitoIdentityPoolをつくる
    enable access to unauthenticated identities みたいなのにチェックいれとく
  • サービスクラスをつくる
import Foundation
import UIKit
import AVFoundation
import AWSCore
import AWSPolly
import AWSCognito

class AudioGuidanceService: UIViewController, NSURLSessionDownloadDelegate {
    var audioPlayer = AVAudioPlayer()

    func say(message: String) {
        let credentialsProvider = AWSCognitoCredentialsProvider(
            regionType: .USWest2,
            identityPoolId: "作成したPoolID"
        )

        // 匿名で認証する
        let configuration = AWSServiceConfiguration(region: .USWest2, credentialsProvider: credentialsProvider)
        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration

        // みずきちゃんフォーマットを組み立てる
        let input = AWSPollySynthesizeSpeechURLBuilderRequest()

        input.textType = AWSPollyTextType.Text
        input.text = message
        input.outputFormat = AWSPollyOutputFormat.Mp3
        input.voiceId = AWSPollyVoiceId.Mizuki

        let builder = AWSPollySynthesizeSpeechURLBuilder.defaultPollySynthesizeSpeechURLBuilder().getPreSignedURL(input)

        // みずきちゃん召喚!
        builder.continueWithSuccessBlock({ (awsTask: AWSTask) in
            let result = awsTask.result! as! NSURL

            let config: NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession")
            let session: NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)

            let url: NSURL = result
            let request: NSURLRequest = NSURLRequest(URL: url)
            let task:NSURLSessionDownloadTask = session.downloadTaskWithRequest(request)

            // タスクを実行.
            task.resume()

            return nil
        })
    }

    // みずきちゃん召喚後の実体に喋らせる
    func URLSession(
        session: NSURLSession,
        downloadTask: NSURLSessionDownloadTask,
        didFinishDownloadingToURL url: NSURL) {
        do {

            self.audioPlayer = try AVAudioPlayer(contentsOfURL: url)

            self.audioPlayer.prepareToPlay()
            self.audioPlayer.play()
        } catch let error as NSError {
            print(error)
        } catch {
            print("error")
        }
    }
}
  • 利用する
var audio = AudioGuidanceService()
audio.say("はろーわーるど!!")

余談

swift歴2週間なので変なところがあったらご指摘ください!

ちなみに 4 の発音が疑問形っぽくなってかわいい。

10
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
3