Note that there are some explanatory texts on larger screens.

plurals
  1. PODistorted sounds with Dartium and Web Audio API in Dart
    primarykey
    data
    text
    <p>I'm pretty new to the HTML5 audio api: I've read some of the related articles at <a href="http://www.html5rocks.com" rel="nofollow">HTML5 Rocks</a>, but it can be a little tricky flipping between Javascript and Dart at times.</p> <p>In any case, I've been experimenting with HTML5 Audio in Dart. To produce sound effects for a simple game, I created a class as follows. I created an AudioContext, loaded sound data into SoundBuffers, and when the sound needed to be played, created an AudioBufferSourceNode via which to play the data stored in the buffers:</p> <pre><code>class Sfx { AudioContext audioContext; List&lt;Map&gt; soundList; int soundFiles; Sfx() { audioContext = new AudioContext(); soundList = new List&lt;Map&gt;(); var soundsToLoad = [ {"name": "MISSILE", "url": "SFX/missile.wav"}, {"name": "EXPLOSION", "url": "SFX/explosion.wav"} ]; soundFiles = soundsToLoad.length; for (Map sound in soundsToLoad) { initSound(sound); } } bool allSoundsLoaded() =&gt; (soundFiles == 0); void initSound(Map soundMap) { HttpRequest req = new HttpRequest(); req.open('GET', soundMap["url"], true); req.responseType = 'arraybuffer'; req.on.load.add((Event e) { audioContext.decodeAudioData( req.response, (var buffer) { // successful decode print("...${soundMap["name"]} loaded..."); soundList.add({"name": soundMap["name"], "buffer": buffer}); soundFiles--; }, (var error) { print("error loading ${soundMap["name"]}"); } ); }); req.send(); } void sfx(AudioBuffer buffer) { AudioBufferSourceNode source = audioContext.createBufferSource(); source.connect(audioContext.destination, 0, 0); source.buffer = buffer; source.start(0); } void playSound(String sound) { for (Map m in soundList) { print(m); if (m["name"] == sound) { sfx(m["buffer"]); break; } } } } </code></pre> <p>(The sound effects are in a folder "SFX". Now that I look at the code, there are probably a million better ways to organise the data, but that's besides the point right now.) I am able to play sound effects by creating an instance of Sfx and calling the method playSound. </p> <p>e.g.</p> <pre><code>#import('dart:html'); #source('sfx.dart'); Sfx sfx; void main() { sfx = new Sfx(); window.on.keyUp.add((KeyboardEvent keX) { sfx.playSound("MISSILE"); }); } </code></pre> <p>(Edit: added code to play sound when a key is hit.)</p> <p>The problem is: although with the dart2js Javascript, the sound effects play as expected in Safari, when they are played in Dartium or (with the dart2js Javascript) in Chrome, they are distorted. (In Firefox, there are even worse problems!)</p> <p>Is there anything obvious that I have neglected to do or that I need to take into account? Otherwise, are there any references or tutorials, preferably in a Dart context, that might help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload