behrad-kzm / BEKSpeechToText

A Swift library to convert speech to text using Google API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BEKSpeechToText

A Swift Library to Convert Speech to Text Using Google's Cloud Machine Learning APIs.

Using the configuration you can change the language from the default (Persian) to all google's supported languages.

Important Note: Current cofiguration is set for Persian language,

CI Status License Platform

Example

1- Google API Key:

First of all, set your Google API key to BEKSpeech instance like below:

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let googleAPIKey = "Your-Google-API-Key"
        BEKSpeech().setAPIKey(string: googleAPIKey)
        return true
    }

2- Add key to Info.plist:

Before you start recording the user's voice you need to grant access to the device's microphone, so add Privacy - Microphone Usage Description into your Info.plist file.

3- Create an instance of BEKSpeech:

Create an instance of BEKSpeech inside your view controller.

  private let speechToTextManager = BEKSpeech()

4- Set BEKSpeech delegate: BEKSpeech will receive you the responses through 2 functions of speech(didReceiveContectText: String) for the result and speech(didReceiveError:Error) if there was an error occurred.

set the instance's delegate to your view controller and make your view controller to confirm it.

  speechToTextManager.delegate = self

5- Request recording permission:

  speechToTextManager.requestPermission { (allowed) in
       if allowed {
           //Do Something
       }else {
           //Do Somthing else
       }
  }

6- Record voice:

    @IBAction func startRecording(_ sender: Any) {
        speechToTextManager.startRecording()
    }
    
    @IBAction func stopRecording(_ sender: Any) {
        speechToTextManager.stopRecording()
    }

Configuration

BEKSpeech can modify easily with the configuration property. Important Note: Current cofiguration is set for "Persian" language, if you want to change the language to you custom defined language create a config that confirms BEKSpeechConfigType so you can create your configuration and change language, audio quality, etc.

Custom configuration:

import AVFoundation
import BEKSpeechToText
struct MyConfiguration: BEKSpeechConfigType {
    let audioFormatString = "flac"
    let keyRate = 16000
    let audioFormat = kAudioFormatFLAC
    let contentType = "audio/x-flac"
    let language = "fa-IR"
    let quality = AVAudioQuality.high
}

Then set your custom config to the BEKSpeech configuration property:

speechToTextManager.configuration = MyConfiguration()

Installation


     pod 'BEKSpeechToText', :git => 'https://github.com/behrad-kzm/BEKSpeechToText.git'

Author

Behrad Kazemi, Behradkzm@gmail.com, bekapps.com Visit Web Site

License

BEKCurveTabbar is available under the MIT license. See the LICENSE file for more info.

About

A Swift library to convert speech to text using Google API.

License:MIT License


Languages

Language:Swift 86.7%Language:Ruby 13.3%