Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaeyo committed Dec 2, 2016
0 parents commit 86fad6b
Show file tree
Hide file tree
Showing 41 changed files with 3,852 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
#Pods/
46 changes: 46 additions & 0 deletions AutoModeSample/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}

func applicationWillResignActive(_ application: UIApplication) {
}

func applicationDidEnterBackground(_ application: UIApplication) {
}

func applicationWillEnterForeground(_ application: UIApplication) {
}

func applicationDidBecomeActive(_ application: UIApplication) {
}

func applicationWillTerminate(_ application: UIApplication) {
}


}

48 changes: 48 additions & 0 deletions AutoModeSample/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
199 changes: 199 additions & 0 deletions AutoModeSample/AutoViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright 2016 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import UIKit
import NaverSpeech
import Common

/*네이버 개발자 센터(https://developers.naver.com)에서 앱 등록을 한 후 발급받은 client id가 필요합니다.*/
let ClientID = ""

/////////////////////////////////////////////////////////////////////
//음성인식기를 auto mode로 동작 시키는 sample app
/////////////////////////////////////////////////////////////////////
class AutoViewController: UIViewController {

// MARK: - init
required init?(coder aDecoder: NSCoder) {
/*
* NSKRecognizer를 초기화 하는데 필요한 NSKRecognizerConfiguration을 생성합니다.
* configuration의 EPD(End Point Detection)type의 default값은 auto 이므로 여기에서 따로 값을 setting하지 않아도 됩니다.
*/
let configuration = NSKRecognizerConfiguration(clientID: ClientID)
configuration?.canQuestionDetected = true
self.speechRecognizer = NSKRecognizer(configuration: configuration)
super.init(coder: aDecoder)

self.speechRecognizer.delegate = self
}

// MARK: - override
override func viewDidLoad() {
super.viewDidLoad()
self.setupLanguagePicker()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

if self.isViewLoaded && self.view.window == nil {
self.view = nil
}
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

let x = languagePickerButton.frame.minX
let y = languagePickerButton.frame.maxY
self.pickerView.frame = CGRect.init(x: x, y: y, width: languagePickerButton.bounds.size.width, height: self.pickerView.bounds.size.height)
}

// MARK: - action
@IBAction func languagePickerButtonTapped(_ sender: Any) {
self.pickerView.isHidden = false
}

/*
* Auto mode는 화자의 발화가 일정시간 이상 지속되지 않으면 자동으로 끝점을 인식하여 음성인식이 종료됩니다.
* 이 sample app에서는 button으로 음성인식을 시작하고 끝내게 됩니다.
* 인식기가 동작 중일 때 button에 대한 tap action이 들어오면 인식기를 중지 시킵니다.
* 인식기가 동작 중이지 않을 때 button에 대한 tap action이 들어오면 인식기에 언어 코드를 넣어서 인식기를 시작시킵니다.
*/
@IBAction func recognitionButtonTapped(_ sender: Any) {
if self.speechRecognizer.isRunning {
self.speechRecognizer.stop()
} else {
self.speechRecognizer.start(with: self.languages.selectedLanguage)
self.recognitionButton.isEnabled = false
self.statusLabel.text = "Connecting......"
}
}

// MARK: - property
@IBOutlet weak var languagePickerButton: UIButton!
@IBOutlet weak var recognitionResultLabel: UILabel!
@IBOutlet weak var recognitionButton: UIButton!
@IBOutlet weak var statusLabel: UILabel!

fileprivate let speechRecognizer: NSKRecognizer
fileprivate let languages = Languages()
fileprivate let pickerView = UIPickerView()
}

/*
* NSKRecognizerDelegate protocol 구현부
*/
extension AutoViewController: NSKRecognizerDelegate {

public func recognizerDidEnterReady(_ aRecognizer: NSKRecognizer!) {
print("Event occurred: Ready")

self.statusLabel.text = "Connected"
self.recognitionResultLabel.text = "Recognizing......"
self.setRecognitionButtonTitle(withText: "Stop", color: .red)
self.recognitionButton.isEnabled = true
}

public func recognizerDidDetectEndPoint(_ aRecognizer: NSKRecognizer!) {
print("Event occurred: End point detected")
}

public func recognizerDidEnterInactive(_ aRecognizer: NSKRecognizer!) {
print("Event occurred: Inactive")

self.setRecognitionButtonTitle(withText: "Record", color: .blue)
self.recognitionButton.isEnabled = true
self.statusLabel.text = ""
}

public func recognizer(_ aRecognizer: NSKRecognizer!, didRecordSpeechData aSpeechData: Data!) {
print("Record speech data, data size: \(aSpeechData.count)")

}

public func recognizer(_ aRecognizer: NSKRecognizer!, didReceivePartialResult aResult: String!) {
print("Partial result: \(aResult)")

self.recognitionResultLabel.text = aResult
}

public func recognizer(_ aRecognizer: NSKRecognizer!, didReceiveError aError: Error!) {
print("Error: \(aError)")

self.setRecognitionButtonTitle(withText: "Record", color: .blue)
self.recognitionButton.isEnabled = true
}

public func recognizer(_ aRecognizer: NSKRecognizer!, didReceive aResult: NSKRecognizedResult!) {
print("Final result: \(aResult)")

if let result = aResult.results.first as? String {
self.recognitionResultLabel.text = "Result: " + result
}
}
}


extension AutoViewController: UIPickerViewDelegate, UIPickerViewDataSource {

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.languages.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return languages.languageString(at: row)
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
languages.selectLanguage(at: row)
languagePickerButton.setTitle(languages.selectedLanguageString, for: .normal)
self.pickerView.isHidden = true

/*
* 음성인식 중 언어를 변경하게 되면 음성인식을 즉시 중지(cancel()) 합니다.
* 음성인식이 즉시 중지되면 별도의 delegate method가 호출되지 않습니다.
*/
if self.speechRecognizer.isRunning {
self.speechRecognizer.cancel()
self.recognitionResultLabel.text = "Canceled"
self.setRecognitionButtonTitle(withText: "Record", color: .blue)
self.recognitionButton.isEnabled = true
}
}
}


fileprivate extension AutoViewController {
func setupLanguagePicker() {
self.view.addSubview(self.pickerView)
self.pickerView.dataSource = self
self.pickerView.delegate = self
self.pickerView.showsSelectionIndicator = true
self.pickerView.backgroundColor = UIColor.white
self.pickerView.isHidden = true
}

func setRecognitionButtonTitle(withText text: String, color: UIColor) {
self.recognitionButton.setTitle(text, for: .normal)
self.recognitionButton.setTitleColor(color, for: .normal)
}
}

27 changes: 27 additions & 0 deletions AutoModeSample/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
Loading

0 comments on commit 86fad6b

Please sign in to comment.