1 Reply Latest reply on Feb 22, 2007 9:06 AM by raredesign07

    Only react to low frequencies

    raredesign07

      // SpectrumAnalyzer.as
      package {
      import flash.display.*;
      import flash.media.*;
      import flash.net.*;
      import flash.utils.ByteArray;
      import flash.events.*;
      class SpectrumAnalyzer extends Sprite {
      // Settings
      private var lineThickness:Number = 2;
      private var lineColor:Number = 0x993300;
      private var circleSize:Number = 75;
      private var scaleOnBeat:Number = 1.1; // 120%
      private var reactToBeat:Number = 30;
      //
      private var music:Sound = new Sound;
      private var ba:ByteArray = new ByteArray();
      private var __width:uint;
      private var __height:uint;
      function SpectrumAnalyzer(mp3:String, _width:uint, _height:uint) {
      __width = _width;
      __height = _height;
      x = __width/2;
      y = __height/2
      music.load(new URLRequest(mp3));
      music.play(0, 999);
      addEventListener(Event.ENTER_FRAME, processSound);
      }
      private function processSound(ev:Event):void {
      SoundMixer.computeSpectrum(ba, true, 0);
      graphics.clear();
      graphics.moveTo(0, -circleSize);
      graphics.lineStyle(lineThickness, lineColor);
      var vol:Number = 0;
      for (var i:uint = 0; i <512; i++) {
      var lev:Number = ba.readFloat();
      vol += lev;
      var a:uint = i;
      var j:Number = i;
      if (i <256) a += 256;
      if (i == 256) graphics.moveTo(0, -circleSize);
      graphics.lineTo(-Math.sin(i/256*Math.PI)*circleSize*(lev+1), Math.cos(a/256*Math.PI)*circleSize*(lev+1));

      }
      if (vol> reactToBeat) {
      scaleX = scaleY = scaleOnBeat;
      }
      else {
      scaleX = scaleY = 1;
      }

      }
      }
      }
      I am thinking that
      private var reactToBeat:Number = 30;is completely dependant on song.mp3. If the song was processed at a different volume level, then you may need to lower the value. (e.g. 30 would be lowered to 19)

      I ultimately want to have this work with any song, because I will be making a playlist and have song.mp3 replaced with the var.

      To start, however, I would at least like to get this spectrum to only respond to low frequencies.

      I think for start, I would need to change:
      SoundMixer.computeSpectrum(ba, true, 0);to
      SoundMixer.computeSpectrum(ba, false, 0);so that it reads it as a frenquency spectrum rather than a complete wav file (according to the as3 ref doc)
      With that, the first 256 values should be low freqs and 257 to 512 would be high freqs.

      How can I utilize that to only scaleOnBeat to the low frequencies?
      Also, I didn't know if it was best to use a select case for a certain range of values.

      Thanks
        • 1. Re: Only react to low frequencies
          raredesign07 Level 1
          After thinking about it a little bit, if the computeSpectrum was set to false, that would give me the left channel of 0-256, but so would the class of "leftpeak"...so is there a way to use that?
          Also, vol is currently dependant on the 512 total values, so perhaps setting computeSpectrum to false, and then changing all 512 references to 256 and all 256 values to 128. That would cut it down to only work with the left channel...right?
          I am also trying to get away from the fixed value of 30 for reactOnBeat, because what if I want to change the volume?
          Yes, I could always increase the vol by the difference reactOnBeat is from 30, but isn't there a dB averager or some other class that would tell me more information about the sound file?