Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
REAL AUDIO FOR EXPLOSIONS
Browse files Browse the repository at this point in the history
this was a huge deal.

0. finding the sound on opengameart. thank ZEUS for that website

1. i had to figure out how to make ffmpeg convert a 24KB file into
something much smaller to fit into 13KB. even 10KB was probably too big
because i want to add extra code. it was always too big using Ogg
Vorbis, so i tried Ogg Opus, but that doesn't even play, so then i fell
back to mp3.

2. converting mp3s to data-uris is actually not fun. i might write a bin
to do it if i have to do this a lot.

3. if you have hecka Audio streams, you're screwed. if the game plays a
total of 1000 explosion sounds, you can't make 1000 audio streams. so
you have to reuse them. i tried to max it out (in a reasonable way,
making a sound a few times a second) and it made 9 streams max. i was
happy with that.

4. because it's mp3, this doesn't work on Firefox (unless you're on
Windows, according to Wikipedia) but it should work elsewhere.

phew. i hate sound
  • Loading branch information
EvanHahn committed Aug 18, 2013
1 parent 4d4afbb commit 63182d3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ build scripts? grunt? browserify? i don't need your witchcraft:
cd server
npm install
npm start

open http://localhost:8000 # uncompressed build
open http://localhost:8000/prod # hella compressed build

Expand All @@ -22,5 +22,6 @@ i stole a few files:
- `src/vendor/extend.js` taken from MIT-licensed [Underscore.js](http://underscorejs.org)
- `src/vendor/requestanimationframe.js` taken from [right here](https://gist.github.com/paulirish/1579671) and it's MIT
- `src/vendor/miniclass.js` was [written by me](https://github.com/EvanHahn/MiniClass) so i can do whatever the hell i want, _mom_
- `src/assets/boom.ogg` made by [Amon](http://www.amon.co), licensed under a Creative Commons Attribution-Share Alike 3.0 license

the rest of the stuff is licensed under that [unlicense](http://unlicense.org/) in LICENSE.txt.
1 change: 1 addition & 0 deletions server/files.json
Expand Up @@ -9,6 +9,7 @@
"../src/lib/canvas_extensions.js",
"../src/lib/sounds.js",
"../src/lib/entity.js",
"../src/assets/boom-data.js",
"../src/config.js",
"../src/shield.js",
"../src/particle.js",
Expand Down
1 change: 1 addition & 0 deletions src/assets/boom-data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added src/assets/boom-original.ogg
Binary file not shown.
Binary file added src/assets/boom.mp3
Binary file not shown.
6 changes: 6 additions & 0 deletions src/assets/convert.sh
@@ -0,0 +1,6 @@
#!/bin/bash

# to ogg opus
# ffmpeg -i boom-original.ogg -to 00:00:0.600 -y -acodec opus -ar 8k -ac 1 boom.ogg

ffmpeg -i boom-original.ogg -to 00:00:0.600 -y -ar 12k -ac 1 boom.mp3
28 changes: 27 additions & 1 deletion src/lib/sounds.js
Expand Up @@ -16,6 +16,9 @@ Sound.play = (function() {
triangle: 3
};

// make "real audio" contexts too
var audios = [];

// make our master context
// sadly, there can only be one
var context;
Expand All @@ -25,13 +28,36 @@ Sound.play = (function() {
return function playSound(options) {

// bail, bail!
if (Sound.muted || (!Sound.isSupported))
if (Sound.muted)
return;

// this whole thing is in a try/catch because not everything
// supports this stuff
try {

// if it's a string, let's just run that file
if (typeof options === 'string') {

// find an available stream
var stream = audios.filter(function(audio) {
return audio.paused == true;
})[0];

// add a stream if we don't have one
if (!stream) {
stream = new Audio;
audios.push(stream);
}

// play it
stream.volume = 1;
stream.src = options;
stream.play();

return;

}

// make me an oscillator
var oscillator = context.createOscillator();
oscillator.connect(context.destination);
Expand Down
1 change: 1 addition & 0 deletions src/particle.js
Expand Up @@ -44,6 +44,7 @@ var Particle = Entity.extend({
setTimeout(function() {
setBackgroundColor(COLOR_BACKGROUND);
}, 100);
Sound.play(BOOM_DATA);
}

// if I hit the shield...
Expand Down

0 comments on commit 63182d3

Please sign in to comment.