Skip to Navigation

Muffinlabs!

Blogs

sfxr for flixel

10
May 2010

Also Posted here

I've got a game in progress using flixel, and I used sfxr to generate the sounds, but I ran into a bunch of mp3-related issues during the embedding process, and I decided I would go ahead and implement sfxr directly into my game to see how that works. So, I wrote a very basic class 'SfxrSound' which handles this. I've added it as a zip which includes the original as3sfxr code, and pasted it below.

Using it is pretty basic. For starters, just unzip [url=http://muffinlabs.com/sites/default/files/sfxr-for-flixel.zip]the code[/url] into your app. I've got it setup to work with embedded sfs files, although there is an (untested) method for loading settings directly as well. To use it you'll want to do something like this:

[Embed(source = 'explosion.sfs', mimeType="application/octet-stream")] public static var ExplosionSound:Class;

var explosion:SfxrSound;
explosion = new SfxrSound();
explosion.loadEmbedded(ExplosionSound);

// meanwhile, something blows up
explosion.play()

Right now, the sound is generated when it is loaded and cached so its ready to be played on demand, because there's a chunk of lag the 1st time you generate it. From my experience, it is wise to instantiate the sound well before you need it, and it might be worthwhile to have some sort of sound manager class if you want to call the same sound from a bunch of different objects.

The code inherits from FlxSound and could probably be made to work with the proximity/translation methods with a little tweaking.

I'm experiencing a certain amount of latency which sounds like it's an FP10 issue. There might not be a decent workaround, so I'm not sure this is a good option if you need your sounds to play with any precision.

Anyway, it's rough and might not work for you, but hopefully it's helpful.

/**
 * very basic integration of SFXR AS3 lib into Flixel, to generate sounds on the fly
 * coded by Colin Mitchell (<a href="mailto:colin@muffinlabs.com">colin@muffinlabs.com</a>)
 * licensed under WTFPL <a href="http://sam.zoy.org/wtfpl/<br />
" title="http://sam.zoy.org/wtfpl/<br />
">http://sam.zoy.org/wtfpl/<br />
</a> *
 * usage:
 *  embedded sfs files
 *      [Embed(source = 'explosion.sfs', mimeType="application/octet-stream")] public static var ExplosionSound:Class;
 *       var explosion:SfxrSound;
 *       explosion = new SfxrSound();
 *       explosion.loadEmbedded(ExplosionSound);
 */
package sfxr {
  import org.flixel.*;
  import flash.events.Event;
  import flash.utils.Endian;
  import flash.utils.ByteArray;
   
  public class SfxrSound extends FlxSound {
        private var _synth:SfxrSynth;

        public function SfxrSound():void {
          super();
          _synth = new SfxrSynth();
        }

        override public function loadEmbedded(EmbeddedSound:Class, Looped:Boolean=false):FlxSound {
          var tmpData:ByteArray = new EmbeddedSound() as ByteArray;

          stop();
          init();

          setSettingsFile(tmpData);
          initSound();
          return this;
        }

        private function initSound(Looped:Boolean=false):void {
          _synth.cacheSound();
          _looped = Looped;

          updateTransform();
          active = true;

          updateSound();
        }

        public function loadSettings(string:String, looped:Boolean=false):Boolean {
          if ( _synth.setSettingsString(string) == false ) {
                return false;
          }

          initSound(looped);
          return true;
        }

        /**
         * Call this function to play the sound.
         */
        override public function play():void {
          _synth.playCached();
        }

        private function updateTransform():void {
          _transform.volume = FlxG.getMuteValue()*FlxG.volume*_volume*_volumeAdjust;
          if(_channel != null)
                _channel.soundTransform = _transform;
        }


        /**
         * Reads parameters from a ByteArray file
         * Compatible with the original Sfxr files
         * @param       file    ByteArray of settings data
         */
        public function setSettingsFile(file:ByteArray):void {
          _synth.deleteCache();
          file.position = 0;
          file.endian = Endian.LITTLE_ENDIAN;
                       
          var version:int = file.readInt();
         
          if(version != 100 && version != 101 && version != 102) return;
                       
          _synth.waveType = file.readInt();
          _synth.masterVolume = (version == 102) ? file.readFloat() : 0.5;
                       
          _synth.startFrequency = file.readFloat();
          _synth.minFrequency = file.readFloat();
          _synth.slide = file.readFloat();
          _synth.deltaSlide = (version >= 101) ? file.readFloat() : 0.0;
                       
          _synth.squareDuty = file.readFloat();
          _synth.dutySweep = file.readFloat();
                       
          _synth.vibratoDepth = file.readFloat();
          _synth.vibratoSpeed = file.readFloat();
          var unusedVibratoDelay:Number = file.readFloat();
                       
          _synth.attackTime = file.readFloat();
          _synth.sustainTime = file.readFloat();
          _synth.decayTime = file.readFloat();
          _synth.sustainPunch = file.readFloat();
         
          var unusedFilterOn:Boolean = file.readBoolean();
          _synth.lpFilterResonance = file.readFloat();
          _synth.lpFilterCutoff = file.readFloat();
          _synth.lpFilterCutoffSweep = file.readFloat();
          _synth.hpFilterCutoff = file.readFloat();
          _synth.hpFilterCutoffSweep = file.readFloat();
         
          _synth.phaserOffset = file.readFloat();
          _synth.phaserSweep = file.readFloat();
                       
          _synth.repeatSpeed = file.readFloat();
         
          _synth.changeSpeed = (version >= 101) ? file.readFloat() : 0.0;
          _synth.changeAmount = (version >= 101) ? file.readFloat() : 0.0;
         
          _synth.validate();
        }
         
  } // SfxrSound
} // package

friedman

25
Apr 2010

Honestly is there a bigger idiot in punditry than Thomas Friedman?

“We, the Green Tea Party, believe that the most effective way to advance America’s national security and economic vitality would be to impose a $10 “Patriot Fee” on every barrel of imported oil, with all proceeds going to pay down our national debt.”

via Balloon Juice

Art, Games

20
Apr 2010

Scott McCloud | Journal » Archive » Wrong Question?

If you’re asking if videogames are art, I think you’re asking the wrong question. I don’t think art is an either/or proposition. Any medium can accommodate it, and there can be at least a little art in nearly everything we do.

Once in a while, someone makes a work in their chosen medium so driven by aesthetic concerns and so removed from any other consideration that we trot out the A-word, but even then it’s a matter of degrees, and for most creative endeavors you can find a full spectrum from the sublime to the mundane.

This all started with Roger Ebert's post stating that "Video games can never be art" -- which, as much as I respect Ebert, is a completely baseless statement.

From Cultivated Play: Farmville:

Indeed, when one measures Farmville against Roger Caillois’ six criteria for defining games, Farmville fails to satisfy each and every one. Caillois stated that games must be free from obligation, separate from 'real life,' uncertain in outcome, an unproductive activity, governed by rules, and make-believe.

and...

The most important thing to recognize here is that, whether we like it or not, seventy-three million people are playing Farmville: a boring, repetitive, and potentially dangerous activity that barely qualifies as a game. Seventy-three million people are obligated to a company that holds no reciprocal ethical obligation toward those people.

It's interesting to compare Farmville to something like EVOKE, which isn't much more than a PR campaign for the World Bank. See INVOKE for more on that.

Syndicate content