We help IT Professionals succeed at work.

How can I mix 2 sound channels?

ForLoop5
ForLoop5 asked
on
I would like to have 2 sound channels.  each sound channel has its own volume control.  There is also a control to pan between the 2 sound channels.  I have an example attached.  I am looking to add the ability to pan between the two sounds.   mix2.fla
var balbounds:Rectangle=new Rectangle(balgroove_mc.x, balgroove_mc.y, balgroove_mc.width, 1 );
Bal_mc.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolumebb);
Bal_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);
function startdragvolumebb(myevent:MouseEvent):void{
	myevent.target.startDrag(false, balbounds);
}
Bal_mc.buttonMode=true;



var myvolumebounds:Rectangle=new Rectangle(volumegroove_mc.x, volumegroove_mc.y, 1, volumegroove_mc.height);
volume_mc.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolume);
//volume_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);
volume_mc.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
volume_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);

function startdragvolume(myevent:MouseEvent):void{
	myevent.target.startDrag(false, myvolumebounds);
}
function stopdrag(myevent:MouseEvent):void{
	myevent.target.stopDrag();
	}
	
	playbutton1.addEventListener(MouseEvent.CLICK,player1);
		function player1(event:MouseEvent){
			
var myChannel:SoundChannel;
var newSetting:SoundTransform = new SoundTransform();
var snd:Sound = new b1();
myChannel= snd.play(0,9999);

stage.addEventListener(Event.ENTER_FRAME, everyframe);
function everyframe(myevent:Event):void{
	var newvolume:Number = 1-((volume_mc.y-volumegroove_mc.y)/115);
	newSetting.volume=newvolume;
	myChannel.soundTransform=newSetting;
}
		}
		
		
		
		
		var myvolumebounds2:Rectangle=new Rectangle(volumegroove_mc2.x, volumegroove_mc2.y, 1, volumegroove_mc2.height);
volume_mc2.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolume2);
volume_mc2.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
volume_mc2.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);

function startdragvolume2(myevent:MouseEvent):void{
	myevent.target.startDrag(false, myvolumebounds2);
}

	
	playbutton2.addEventListener(MouseEvent.CLICK,player2);
		function player2(event:MouseEvent){
			
var myChannel2:SoundChannel;
var newSetting2:SoundTransform = new SoundTransform();
var snd2:Sound = new b2();
myChannel2= snd2.play(0,9999);

stage.addEventListener(Event.ENTER_FRAME, everyframe2);
function everyframe2(myevent:Event):void{
	var newvolume2:Number = 1-((volume_mc2.y-volumegroove_mc2.y)/115);
	newSetting2.volume=newvolume2;
	myChannel2.soundTransform=newSetting2;
}
		}

Open in new window

Comment
Watch Question

Top Expert 2011
Commented:
I hope I solved your puzzle

var newSetting:SoundTransform;
var newSetting2:SoundTransform;

var balbounds:Rectangle=new Rectangle(balgroove_mc.x, balgroove_mc.y, balgroove_mc.width, 1 );
Bal_mc.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolumebb);
Bal_mc.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
Bal_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);
function startdragvolumebb(myevent:MouseEvent):void{
      myevent.target.startDrag(false, balbounds);
      Bal_mc.addEventListener(MouseEvent.MOUSE_MOVE, balboundsMove);
}
Bal_mc.buttonMode=true;

function balboundsMove(event:MouseEvent):void{
      var pos:Number = event.target.x - balgroove_mc.x;
      var middle:Number = balgroove_mc.width / 2;
      var step:Number = middle / balgroove_mc.width;
      var value:Number = 1 - (pos / middle);
      if(newSetting != null)
            newSetting.pan = value;
      if(newSetting2 != null)
            newSetting2.pan = value;
}

var myvolumebounds:Rectangle=new Rectangle(volumegroove_mc.x, volumegroove_mc.y, 1, volumegroove_mc.height);
volume_mc.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolume);
//volume_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);
volume_mc.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
volume_mc.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);

function startdragvolume(myevent:MouseEvent):void{
      myevent.target.startDrag(false, myvolumebounds);
}
function stopdrag(myevent:MouseEvent):void{
      myevent.target.stopDrag();
      Bal_mc.addEventListener(MouseEvent.MOUSE_MOVE, balboundsMove);
}
      
playbutton1.addEventListener(MouseEvent.CLICK,player1);

function player1(event:MouseEvent){
                        
      var myChannel:SoundChannel;
      newSetting = new SoundTransform();
      var snd:Sound = new b1();
      myChannel= snd.play(0,9999);

      stage.addEventListener(Event.ENTER_FRAME, everyframe);
      function everyframe(myevent:Event):void{
            var newvolume:Number = 1-((volume_mc.y-volumegroove_mc.y)/115);
            newSetting.volume=newvolume;
            myChannel.soundTransform=newSetting;
      }
}
            
            
var myvolumebounds2:Rectangle=new Rectangle(volumegroove_mc2.x, volumegroove_mc2.y, 1, volumegroove_mc2.height);
volume_mc2.addEventListener(MouseEvent.MOUSE_DOWN, startdragvolume2);
volume_mc2.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
volume_mc2.addEventListener(MouseEvent.MOUSE_OUT, stopdrag);

function startdragvolume2(myevent:MouseEvent):void{
      myevent.target.startDrag(false, myvolumebounds2);
}

      
      playbutton2.addEventListener(MouseEvent.CLICK,player2);
            function player2(event:MouseEvent){
                  
var myChannel2:SoundChannel;
newSetting2 = new SoundTransform();
var snd2:Sound = new b2();
myChannel2= snd2.play(0,9999);

stage.addEventListener(Event.ENTER_FRAME, everyframe2);
function everyframe2(myevent:Event):void{
      var newvolume2:Number = 1-((volume_mc2.y-volumegroove_mc2.y)/115);
      newSetting2.volume=newvolume2;
      myChannel2.soundTransform=newSetting2;
}
            }

Author

Commented:
I may have asked the question wrong, sorry.   I would like to "fade" between the two sound channels.  so if the slider is all the way to the left it would play the sound 1 loud and sound 2 would be muted but when you slide the slider all the way to the right sound 1 would be muted and sound 2 would be loud.  
Top Expert 2011

Commented:
This is not  "pan"  that is volume control

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundTransform.html#pan

The left-to-right panning of the sound, ranging from -1 (full pan left) to 1 (full pan right). A value of 0 represents no panning (balanced center between right and left).




Author

Commented:
Thanks.  The problem I am having is that I have individual volume controls for the sounds on the left and volume control for the sounds on the right.  But I would like the middle slider when all the way to the left play the sound 1 and when all the way to the right play sound 2
Top Expert 2011

Commented:
how about "individual volume controls" the values should be change?

Author

Commented:
ok