LoginSignup
2
2

"required condition is false: format.sampleRate == hwFormat.sampleRate"エラー解消

Last updated at Posted at 2023-05-27

はじめに

レコーダーアプリを作成にあたって

Thread 1: "required condition is false: format.sampleRate == hwFormat.sampleRate"

というエラーにかなり悩まされてとりあえず解決したのでメモ

ググっても5件しかヒットしないw
スクリーンショット 2023-05-27 17.09.38.png

GitHubのissue等も調べたのですが話題してるとこはあったのですが根本的な解決方法を見つけられなかった

デバイスごとにマイクのサンプルレートが違う

録音するためにサンプルレートを設定するのですがiPhone8だと44100、iPhone12miniは48000、iPadmini6は48000のようにデバイスごとにマイクのサンプルレートが異なってきます。

新しいデバイスは48000になってそうですが情報をみつけられませんでした(知ってる方いたら教えてください)
全てのデバイスを揃えるのは無理だし、当てずっほうでデバイスごとに振り分けるコードを書くことになるので半年間くらい放置してました。

解決方法

ChatGPT4に相談してみたらヒントを得て解決しました!!

import AVFoundation
let engine = AVAudioEngine()
let inputNode = engine.inputNode
let inputFormat = inputNode.outputFormat(forBus: 0)
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: inputFormat.sampleRate, channels: inputFormat.channelCount, interleaved: true)

こうするとデバイスのマイクの現在のサンプルレートを取得できるようです。

終わりに

間違っている点などありましたらご指摘いただけると幸いです!
ランダムでエフェクトがかかるレコーダーアプリを作りました!!

興味あったら使ってみてください

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