LocalConnection & Flash Music Data

From Director Online Wiki
Jump to: navigation, search

Screenshot

Error creating thumbnail: Unable to save thumbnail to destination

Description

This small demo shows how to live analyze music data in Flash (AS3) and sending the data to Director via LocalConnection to animate based on this data. In this example we use the left channel peak values to animate and change colors in a small & simple Shockwave3D scene.

The demo consists of two individual files (dcr/swf) embedded into one HTML web page (see Demo URL).

Demo URL

http://www.directorforum.de/_realtimeFFT/realtimeFFT.html

Lingo Code

The Shockwave3D scene has been built with 3DPI Xtra and saved to W3D.

-- movie script
global gLocalConn
global gMyID
global gExtID
 
on startMovie
  gMyID = "DirFile"
  gExtID = "FlashFile"
  gLocalConn = newObject("LocalConnection")
end
 
on stopMovie
  gLocalConn.close()
  gLocalConn = VOID
  clearAsObjects()
end
-- Shockwave3D sprite behavior
global gLocalConn
global gMyID
global gExtID
global gFFTData
 
property pSprite
property pMember
 
on beginSprite me
  pSprite = sprite(me.spriteNum)
  pMember = pSprite.member
  pSprite.targetFrameRate = 999.0000
  gFFTData = ""
  setCallback(gLocalConn,"allowDomain",#allowDomain, me)
  setCallback(gLocalConn,"onStatus",#onStatus, me)
  setCallback(gLocalConn,"incomingMessage",#incomingMessage, me)
  tConnResult = gLocalConn.connect(gMyID)
  if not(tConnResult) then
    gLocalConn = VOID
  end if
end
 
on endSprite me
  pMember.resetWorld()
  pMember.shader("discofloor1").emissive = color(0,0,0)
  pMember.shader("discofloor2").emissive = color(0,0,0)
  pMember.shader("discofloor3").emissive = color(0,0,0)
  pMember.shader("discofloor4").emissive = color(0,0,0)
  pMember.shader("discofloor5").emissive = color(0,0,0)
  pMember.shader("discofloor6").emissive = color(0,0,0)
  pMember.shader("discofloor7").emissive = color(0,0,0)
  pMember.shader("discofloor8").emissive = color(0,0,0)
end
 
on allowDomain (me, aLocalConnObj, aDomain)
  -- for our demo purposes always return true
  return TRUE
end
 
on incomingMessage (me, aLocalConnObj, FFTData)
  animate(FFTData)
end
 
on onStatus me, aLocalConnObj, aInfoObject
end
 
on animate FFTData
  theModel = pMember.model("ball")
  theModel.transform.position = vector( 0.0000, (FFTData * 100.0), 0.0000 )
  baseColor = integer(255.0 * (1.0 + log(FFTData)))
  pMember.shader("pegel").emissive = color(baseColor, (255 - baseColor), 0)
  pMember.shader("discofloor1").emissive = color(0, baseColor, (255 - baseColor))
  pMember.shader("discofloor2").emissive = color(baseColor, 0, (255 - baseColor))
  pMember.shader("discofloor3").emissive = color((255 - baseColor), baseColor, 0)
  pMember.shader("discofloor4").emissive = color((255 - baseColor), 0, baseColor)
  pMember.shader("discofloor5").emissive = color((255 - baseColor), 128, (255 - baseColor))
  pMember.shader("discofloor6").emissive = color((255 - baseColor), (255 - baseColor), 255)
  pMember.shader("discofloor7").emissive = color(baseColor, baseColor, 0)
  pMember.shader("discofloor8").emissive = color(baseColor, 0, baseColor)
end

Flash AS3 Code

var sound:Sound = new Sound(new URLRequest("music.mp3"));
var sc:SoundChannel;
sc = sound.play(0,1000);
 
var leftVolume:Number = new Number();
 
var gMyName:String = "FlashFile";
var gOtherName:String = "DirFile";
var gLocalConn:LocalConnection = new LocalConnection();
var tConnResult = gLocalConn.connect(gMyName);
 
addEventListener(Event.ENTER_FRAME, loop);
 
function loop(e:Event):void {
	// compute peak values (left channel) float values from 0 to 1
	leftVolume = sc.leftPeak;
	gLocalConn.send("DirFile", "incomingMessage", leftVolume);
}

Files