LoginSignup
masao_mobile
@masao_mobile

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

iOSアプリ 低遅延でBGMを再生する方法は? OpenAL,Alure+ios+vorbis

やりたいこと

音再生のタイミングにシビアなアプリ(音ゲー等)で、
OGG もしくは MP3ファイルを使ったBGM再生。

環境

macOS 10.15.6
xcode 11.6
Alure 1.2
Project使用言語 swift

AVAudioPlayerの問題点

MP3でAVAudioPlayerを使用する場合、再生開始時に(0.1s未満の)遅延あり。
AVAudioPlayerは音ゲーに向いていないそうでOpenALを使うべきらしい。

2020/09/15 追記

OpenAL は iOS13.0 から非推奨。将来的に使えなくなる可能性あり。
AVAudioEngine推奨。

できたこと(WAVファイル再生)

調べたところ OpenAL+Alure で低遅延再生ができるようだ。
下記の情報を参考にAlure をxcodeのプロジェクトにいれてみた。

http://flat-leon.hatenablog.com/entry/alure_ios
https://qiita.com/gomachan7/items/adddc33e0e609e81ea74

で、色々と上記サイトの方法でいくつかエラーは出たが、適宜修正して低遅延でのWAVファイル再生ができた!!
だが、重いからBGMをWAVファイルで保存するわけにもいかない。

Ogg vorbisファイルの再生の仕方がわからない。

oggを再生しようとすると、ライブラリ(Vorbisのデコーダー)がないのでアプリ実行時のxcodeログで、

Error loading libvorbisfile.3.dylib: dlopen(libvorbisfile.3.dylib, 2): image not found

のエラーになる。上記サイトには、vorbisの導入方法の記載はない。

やってみたこと (2020/9/10更新)

libvorbisfile.3.dylibを作ってみる。
https://www.xiph.org/downloads/
から、libvorbisをダウンロードして、
(libvorbis-1.3.7.tar.xz)

あとliboggもダウンロード。

ios向けにビルドする方法は…
https://blog.kishikawakatsumi.com/entry/2018/12/25/122315
上記サイトを参考にliboggをまずはビルド。

ranlib リンカーエラーでてきた(抜粋)

  CCLD     libvorbis.la
/opt/local/bin/ranlib: for object: .libs/libvorbis.a(cputyp) architecture mdct.o          1599035989  501   20    100644  66996     `
���� malformed object (unknown load command 2)
<中略>
/opt/local/bin/ranlib: for object: .libs/libvorbis.a(cputype (167) architecture sharedbook.o    1599036008  501   20    100644  79036     `
���� malformed object (unknown load command 2)
ar: internal ranlib command failed

/opt/local/bin/ranlibではなくて、/usr/bin/ranlibならうまくいくらしい。
修正して再挑戦。

./configure   --host=x86_64-apple-darwin   CC=`xcrun -find clang`   CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -mios-version-min=10.0"   CXX=`xcrun -find clang++`   CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -mios-version-min=10.0"   --prefix="$TARGETDIR_IPHONEOS" --disable-shared --enable-static

できたlibogg のstaticライブラリをlibvorbisに突っ込んで、dynamic libraryをビルド。

./configure   --host=x86_64-apple-darwin   CC=`xcrun -find clang`   CFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphoneos --show-sdk-path` -mios-version-min=10.0"   CXX=`xcrun -find clang++`   CXXFLAGS="-O3 -arch armv7 -arch armv7s -arch arm64 -isysroot `xcrun -sdk iphonesimulator --show-sdk-path` -mios-version-min=10.0"   --prefix="$TARGETDIR_IPHONEOS" --enable-shared=no --enable-static=yes

libvorbisfile.3.dylibはビルドできた。
xcodeのFrameworksに入れる。

今度は実行時エラー

dyld: Library not loaded: /lib/libvorbis.0.dylib
  Referenced from: /private/var/containers/Bundle/Application/AFA16597-4429-4959-8AED-37BD8457184F/BarrierBeat.app/BarrierBeat
  Reason: image not found

libvorbis.0.dylibをFrameworksにいれて、
Google検索したimage not found 対策をしても効果なし…

そもそも、dynamic library入れようとしてるけどalureビルド時にvorbisもビルドするのでしょうか…?

おわりに

現在は効果音はWAVでALURE、BGMはAVAudioPlayerでひとまずごまかしています。
一日悩んでみたけど解決せず、いろいろと知識不足なので久々にQiita投稿してひとまずお茶を濁してみます。もし救いの手があれば、、、と祈って投稿してみます。よろしくお願いします。

0

1Answer

FindOpenAL.cmake での指定が正しいか気になります。

FindOpenAL.cmake
  IF(APPLE)
    SET(OPENAL_LIBRARY "${PARENT_BINARY_DIR}/openal/libopenal.dylib")
0

Comments

  1. @masao_mobile

    Questioner

    拙い理解力ですみません。
    iOSにOpenALは標準で入っているのでalureだけビルドでOKと思っていましたが、iOS用にOpenALもビルドしてlibopenal.dylib作成してalureビルド時にリンクしていないといけないということでしょうか?

    なお、alureのcmake 時にこんなログが出ていました。

    ==================================================
    <略>$ cmake . -DCMAKE_TOOLCHAIN_FILE=./iOS.cmake
    -- checking for module 'openal'
    -- package 'openal' not found
    -- OpenAL include: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.6.sdk/System/Library/Frameworks/OpenAL.framework
    -- OpenAL lib: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.6.sdk/System/Library/Frameworks/OpenAL.framework
    .....
    <中略>
    ....
    ********************** Configured options **********************
    -- Building shared, static library
    -- Building and installing examples
    --
    -- SndFile support: disabled
    -- VorbisFile support: enabled
    -- FLAC support: enabled
    -- MPG123 support: disabled
    -- DUMB support: disabled
    -- ModPlug support: disabled
    -- FluidSynth support: disabled
    --
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /<略>/alure-1.2
    =============================================

    -- checking for module 'openal'
    -- package 'openal' not found
    ↑ここがまずいのでしょうか?iPhone上でAlureでWAV 再生はうまくいっているので、OKだと思っていました…

    https://snada.it/posts/openal-soft-and-alure-sandbox
    こちらの記事、ちなみにalure1.xの時点のもので、alure1.2 では FindOpenAL.cmake が用意されているみたいですね。 記事の中身がまだ理解しきれていないので、記事とFindOpenAL.cmakeをいま精査しています。

Your answer might help someone💌