<commit_before>/**
 * Copyright IBM Corporation 2016
 *
 * 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 Foundation
import AVFoundation
import RestKit

/** Stream microphone audio to Speech to Text. */
class SpeechToTextAudioStreamer: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {

    private var settings: TranscriptionSettings
    private var failure: (NSError -> Void)?
    private let success: [TranscriptionResult] -> Void
    private var socket: SpeechToTextWebSocket?
    private var captureSession: AVCaptureSession?
    private let domain = "com.ibm.watson.developer-cloud.WatsonDeveloperCloud"

    /**
     Create a `SpeechToTextAudioStreamer` to stream microphone audio to Speech to Text.

     - parameter authStrategy: An `AuthenticationStrategy` that defines how to authenticate
        with the Watson Developer Cloud's Speech to Text service. The `AuthenticationStrategy`
        is used internally to obtain tokens, refresh expired tokens, and maintain information
        about authentication state.
     - parameter settings: The configuration for this transcription request.
     - parameter failure: A function executed whenever an error occurs.
     - parameter success: A function executed with all transcription results whenever
        a final or interim transcription is received.
     
     - returns: A `SpeechToTextAudioStreamer` object that can stream microphone audio to
        Speech to Text.
    */
    init?(
        restToken: RestToken,
        settings: TranscriptionSettings,
        failure: (NSError -> Void)? = nil,
        success: [TranscriptionResult] -> Void)
    {
        self.settings = settings
        self.success = success
        self.failure = failure

        super.init()

        self.failure = { (error: NSError) in
            self.stopStreaming()
            failure?(error)
        }

        guard let socket = SpeechToTextWebSocket(
            restToken: restToken,
            settings: settings,
            failure: failure,
            success: success) else
        {
            // A bug in the Swift compiler requires us to set all properties before returning nil
            // This bug is fixed in Swift 2.2, so we can set socket as non-optional
            return nil
        }

        self.socket = socket
    }

    /**
     Start streaming microphone audio to Speech to Text.

     - returns: `true` if the `AVCaptureSession` could be configured to stream microphone audio
        to the Speech to Text service; false, otherwise.
     */
    func startStreaming() -> Bool {
<commit_msg>Remove unnecessary comments in SpeechToTextAudioStreamer<commit_after>/**
 * Copyright IBM Corporation 2016
 *
 * 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 Foundation
import AVFoundation
import RestKit

/** Stream microphone audio to Speech to Text. */
class SpeechToTextAudioStreamer: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {

    private var settings: TranscriptionSettings
    private var failure: (NSError -> Void)?
    private let success: [TranscriptionResult] -> Void
    private var socket: SpeechToTextWebSocket?
    private var captureSession: AVCaptureSession?
    private let domain = "com.ibm.watson.developer-cloud.WatsonDeveloperCloud"

    /**
     Create a `SpeechToTextAudioStreamer` to stream microphone audio to Speech to Text.

     - parameter authStrategy: An `AuthenticationStrategy` that defines how to authenticate
        with the Watson Developer Cloud's Speech to Text service. The `AuthenticationStrategy`
        is used internally to obtain tokens, refresh expired tokens, and maintain information
        about authentication state.
     - parameter settings: The configuration for this transcription request.
     - parameter failure: A function executed whenever an error occurs.
     - parameter success: A function executed with all transcription results whenever
        a final or interim transcription is received.
     
     - returns: A `SpeechToTextAudioStreamer` object that can stream microphone audio to
        Speech to Text.
    */
    init?(
        restToken: RestToken,
        settings: TranscriptionSettings,
        failure: (NSError -> Void)? = nil,
        success: [TranscriptionResult] -> Void)
    {
        self.settings = settings
        self.success = success
        self.failure = failure

        super.init()

        self.failure = { (error: NSError) in
            self.stopStreaming()
            failure?(error)
        }

        guard let socket = SpeechToTextWebSocket(
            restToken: restToken,
            settings: settings,
            failure: failure,
            success: success) else
        {
            return nil
        }

        self.socket = socket
    }

    /**
     Start streaming microphone audio to Speech to Text.

     - returns: `true` if the `AVCaptureSession` could be configured to stream microphone audio
        to the Speech to Text service; false, otherwise.
     */
    func startStreaming() -> Bool {

{'fraction_non_alphanumeric': 0.0639986770299322, 'fraction_numerical': 0.0029766826525549858, 'mean_word_length': 2.794228356336261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '974985', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1405, 'n_words': 617}