Last active
December 5, 2016 11:23
-
-
Save touyou/74754e89634529ee3f28ebf728e19ec0 to your computer and use it in GitHub Desktop.
SwiftでPPAPやってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - PPAP: http://qiita.com/KIchiro/items/74d117ff894ac10b72e0 のSwift版 | |
import UIKit | |
import Foundation | |
import AVFoundation | |
import XCPlayground | |
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
// enumの一覧を使いたい | |
// http://qiita.com/su_k/items/c1c6a91324cf12151256 | |
public protocol EnumEnumerable { | |
associatedtype Case = Self | |
} | |
public extension EnumEnumerable where Case: Hashable { | |
private static var iterator: AnyIterator<Case> { | |
var n = 0 | |
return AnyIterator { | |
defer { n += 1 } | |
let next = withUnsafePointer(to: &n) { | |
UnsafeRawPointer($0).assumingMemoryBound(to: Case.self).pointee | |
} | |
return next.hashValue == n ? next : nil | |
} | |
} | |
public static func enumerate() -> EnumeratedSequence<AnySequence<Case>> { | |
return AnySequence(self.iterator).enumerated() | |
} | |
public static var cases: [Case] { | |
return Array(self.iterator) | |
} | |
public static var count: Int { | |
return self.cases.count | |
} | |
} | |
// 列挙体 | |
// http://qiita.com/hachinobu/items/392c96820588d1c03b0c | |
fileprivate enum Word: String, EnumEnumerable { | |
case pen | |
case pineapple | |
case apple | |
} | |
fileprivate enum PpapState { | |
case initial, p, pp, ppa, ppap | |
func transition(_ next: Word) -> PpapState { | |
switch (next, self) { | |
case (.pen, .initial): return .p | |
case (.pineapple, .p): return .pp | |
case (.apple, .pp): return .ppa | |
case (.pen, .ppa): return .ppap | |
default: return .initial | |
} | |
} | |
} | |
// 喋らせる | |
// http://qiita.com/takecian/items/096b07e2d0437454f670 | |
let talker = AVSpeechSynthesizer() | |
func ppapMachine() { | |
var state: PpapState = .initial | |
while state != .ppap { | |
let word = Word.cases[Int(arc4random_uniform(UInt32(Word.count)))] | |
state = state.transition(word) | |
speakWord(generateSentence(word)) | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 1)) | |
talker.stopSpeaking(at: .word) | |
} | |
speakWord("Apple pen") | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 2)) | |
talker.stopSpeaking(at: .word) | |
speakWord("Pineapple pen") | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 3.5)) | |
talker.stopSpeaking(at: .word) | |
speakWord("Uh!") | |
RunLoop.main.run(until: Date(timeIntervalSinceNow: 1)) | |
talker.stopSpeaking(at: .word) | |
speakWord("Pen pineapple apple pen!") | |
} | |
fileprivate func generateSentence(_ word: Word) -> String { | |
switch word { | |
case .apple: | |
return "I have an \(word.rawValue)" | |
default: | |
return "I have a \(word.rawValue)" | |
} | |
} | |
func speakWord(_ string: String) { | |
let utterance = AVSpeechUtterance(string: string) | |
utterance.voice = AVSpeechSynthesisVoice(language: "en-US") | |
talker.speak(utterance) | |
} | |
ppapMachine() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment