LoginSignup
5
5

More than 5 years have passed since last update.

Haxe de Web Audio API

Posted at

Haxeの勉強がてらにWeb Audio APIで遊んでみました。

基本的な情報

基本はg200kgさんのページに詳しく載ってます。
http://www.g200kg.com/jp/docs/webaudio/

Haxeで使う場合

Web Audio APIのライブラリが 一応 用意されています。

import js.html.audio.*;

exportから書く必要は無さそうで一安心です。

一応?

Haxeのドキュメントを信じて正直に

var audioctx = new js.html.audio.AudioContext();

とか書いて、ChromeやSafariで開くと

Uncaught ReferenceError: AudioContext is not defined 

とか

[Error] ReferenceError: Can't find variable: AudioContext

と、AudioContextの取得失敗のエラーが発生して、途方に暮れる罠が仕掛けられてます。

なので、動作させるために、次のようにしてあげます。

var audioctx : AudioContext;

audioctx = if (untyped __js__("typeof(webkitAudioContext)") != "undefined") {
    untyped __js__("new webkitAudioContext()");
} else {
    new js.html.audio.AudioContext();
}

後、プロパティなどの定数値は各クラスが持ってる事が多いので、ドキュメントを読んだ方が良いです。

私は読まずに作って、enum定義と実装が終わった後に気づいて、泣きながら直しました。

実際動くの?

動かしてみるとこんな感じ
http://wokowa.github.io/webaudio-haxe-practice/
ChromeとSafariで動作確認しました(俺調べ)
Customが選択出来ないのは仕様でs(ry

コードはこちら
https://github.com/wokowa/webaudio-haxe-practice
なお、enumのWaveTypeが残ってるのは、Firefoxでも動かそうとして諦めた物の産物です。

今後の課題

各ブラウザごとにビミョーに実装方法が異なるので、その差異の吸収が今後の課題かも。

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