It took several attempts to get this code working, and I am happy with the result.
screen grab from Annie's home computer |
Alternatively you can just watch the video below.
NOTE: Adobe Flash records the microphone input on a volume of 1 to 100 (100 being maximum volume). To allow for natural ambiance in our exhibition room the interaction is programmed to ignore sounds below 15.
(we may review the figures below pending a microphone test run in the exhibition room)
1. user speaks at a volume above 15: blue bird animation plays
2. user speaks between 25 and 60: butterfly animation plays
3. user speaks between 65 and 100: water and fish animations play
4. blue bird and butterfly no longer play. user speaks above 20: the fish plays (there is a limit of 5 plays to encourage the user to move along to the next interaction. Getting a bit technical... this is also a fail safe in case the user gets bored; we need the water and fish animations to stop in order to keep the total number of running animations on screen at any one time under 6.)
5. when user speaks above 20 for the 6th time: butterfly and fish are removed from screen, water becomes still.
See this in action for yourself...
Coding the Microphone Interaction from REplay on Vimeo.
screen grabs of the output trace text
click to enlarge
Adobe Flash CS4 ActionScript 3.0 Code
I'm afraid I'm too busy at the moment to colour code and indent the script, sorry.
import flash.events.ActivityEvent;
import flash.events.StatusEvent;
import flash.media.Microphone;
var count:Number = 0;
var fishCounter:Number = count;
var repeatFish:Number = fishCounter;
var deviceArray:Array = Microphone.names;
trace("Available sound input devices:");
fish_mc.visible = false;
fishFun_mc.visible = false;
blueBirdAnimation_mc.visible = false;
redButterflyAnimation_mc.visible = false;
water_mc.visible = false;
fish_mc.stop();
fishFun_mc.stop();
blueBirdAnimation_mc.stop();
redButterflyAnimation_mc.stop();
water_mc.stop();
for (var i:int = 0; i < deviceArray.length; i++) {
trace(" " + deviceArray[i]);
}
var mic:Microphone = Microphone.getMicrophone();
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(5, 1000);
mic.addEventListener(ActivityEvent.ACTIVITY, this.onMicActivity);
mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
function onMicActivity(event:ActivityEvent):void {
if (fishCounter < 1) {
if (mic.activityLevel > 10 && mic.activityLevel <= 100) {
blueBirdAnimation_mc.visible = true;
blueBirdAnimation_mc.play();
trace("1. playing Blue Bird");
}
if (mic.activityLevel > 25 && mic.activityLevel < 60) {
redButterflyAnimation_mc.visible = true;
redButterflyAnimation_mc.play();
trace("2. playing Red Butterfly");
}
if (mic.activityLevel > 65 && mic.activityLevel <= 100) {
water_mc.visible = true;
water_mc.play();
fish_mc.visible = true;
fish_mc.play();
count++;
fishCounter = count;
trace("3. playing Fish");
}
}
if (fishCounter > 0 && fishCounter <= 5) {
if (mic.activityLevel > 20 && mic.activityLevel < 100) {
fish_mc.visible = false; fish_mc.stop();
fishFun_mc.visible = true;
fishFun_mc.play();
repeatFish++; fishCounter = repeatFish;
trace ("4. playing repeat fish, #" + fishCounter + " of 5");
}
}
if (fishCounter > 5) {
redButterflyAnimation_mc.visible = false;
fishFun_mc.visible = false;
water_mc.stop();
trace("Microphone interaction complete.");
}
}
The bare bones of this code came from Adobe help files on detecting microphone activity.
Annie.
No comments:
Post a Comment