0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Swift]リアルタイム音声認識+ファイル出力

Last updated at Posted at 2023-10-28

はじめに

リアルタイム音声認識(Speech-To-Text)を用いて、話した内容がテキストファイルとして出力されるアプリです。

1.Speech-To-Textの実装
2.画面構成
下記記事を参考にさせていただきました、ありがとうございます。
また本記事は前提として下記記事を実装した状態で動作します。

完成品

1.録音ボタン(赤いボタン)を押すと録音、音声認識が開始
2.停止するとファイルアプリ内にテキストファイルとして出力
peter.png

peterTXT.png

実装1.フォルダへアクセス

音声認識した結果を自身のドキュメント内に保存するために、FileManagerを用います。
画像の「ファイル」というios標準実装のアプリ内に保存されるようにします。ないという方は、ストアでインストールできます。
fileApp.png

次に、作成するアプリにファイルへアクセスする権限を付与します。info.plistに「Supports opening documents in place」と「Application supports iTunes file sharing」を追加します。
filemanager_use.png

ここまでできたらファイル作成のコードを記載します。

/// ファイル名用の変数を用意
let date = NSDate()
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let dateStr = formatter.string(from: date as Date)

/// DocumentsフォルダURL取得
guard let dirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
              fatalError("フォルダURL取得エラー")
          }
/// 対象のファイルURL取得
let fileURL = dirURL.appendingPathComponent((dateStr)+".txt")
do {
    try self.textView.text.write(to: fileURL, atomically: false, encoding: .utf8)
    }catch {
        print("Error: \(error)")
    }

実装2.音声認識した結果をテキストファイルに書込

実装1のコードを「@IBAction func recordButtonTapped」内に入れます。

@IBAction func recordButtonTapped(_ sender: Any) {
    if isRecording {
      UIView.animate(withDuration: 0.2) {
        self.showStartButton()
          /// ファイル名用の変数を用意
          let date = NSDate()
          let formatter = DateFormatter()
          formatter.dateFormat = "yyyyMMddHHmmss"
          let dateStr = formatter.string(from: date as Date)
          /// DocumentsフォルダURL取得
          guard let dirURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
              fatalError("フォルダURL取得エラー")
          }
          /// 対象のファイルURL取得
          let fileURL = dirURL.appendingPathComponent((dateStr)+".txt")
          do {
              try self.textView.text.write(to: fileURL, atomically: false, encoding: .utf8)
          }catch {
              print("Error: \(error)")
          }
      }
      stopLiveTranscription()
    } else {
      UIView.animate(withDuration: 0.2) {
        self.showStopButton()
      }
      try! startLiveTranscription()
    }
    isRecording = !isRecording
  }

以上でリアルタイム音声認識+ファイル出力が行えます。

今後

表題にもありますが議事録などを撮る際に使えそうかなと思い実装しました。話者特定などもSoundAnalysisというものを使うことで実装できそうなのでそちらも追加していき改良していこうかなと思います。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?