Articles Archive
Articles Search
Director Wiki
 

Adjusting volume with a slider

June 29, 1998
by Pat McClellan

Dear Multimedia Handyman,

I hope you don't mind but the user groups have not beeen able to help. You are my last hope. I want to be able to adjust volume through a slider mechanism. How do I do this?

Jared Fusedale

Jared,

It's a scary thought that I'm anybody's "last hope". Luckily, in this case I think I can help you out. Let's start with some definitions.

soundLevel is the volume level of the sound that is played through the computer's speaker. Setting range from a minimum of 0 (no sound) to a maximum of 7. soundLevel affects all channels of your programs sound.

volume of sound determines the volume of the sound channel which you specify (sound channels 1 to 8 -- with 1 and 2 being the channels appearing in the score.) In addition to being able to specifically affect a single sound channel, you also have a much greater range of dymamic control: from 0 (mute) to a maximum of 255. You can also use "the volume of member" for a SWA file and "the volume of sprite" for a digital video member.

Since "volume..." provides so much more control, I'll create a slider behavior which controls this. The properties we'll want to be able to set include whichChannel and the min and max of the slider's range. For example, if the slider's range happens to be 256 pixels, then there is a one-to-one correspondence between the slider's position and the volume level. However, it's not very likely that your slider will be exactly 256 pixels, so we'll build in the math necessary to make the conversion. Let's work out the math specifically.

The range of the slider needs to coincide with the range of the volume. So, we'll divide 256 (volume range) by the height of the slider trough. This gives us the amount that the volume will change for each pixel. Let's call that our deltaFactor. Now, multiply the change in locV of our slider knob by the deltaFactor to get volume. Here are the formulas -- NOT LINGO.

deltaFactor = 256/the height of the trough sliderChange = the bottom of trough - the locV of the slider knob volume = sliderChange * deltaFactor

For my demo movie, I created 2 tiny sound files (just over 100 bytes each) by using just a tiny bit of waveform and setting it to loop. (Thanks to Joe Sparks for the idea.) I'll set each of the tones up on a separate volume slider -- each using the same behavior -- but with different ranges. In this behavior, I'll make the assumption that the slider trough member is in the sprite channel just below the slider knob. For example, in my demo movie, slider 1's trough sprite is in channel 1, with the slider knob in channel 2. For the second slider, the sprites are in channels 3 and 4. This will allow us to automatically "grab" the range of the slider -- one less thing for you to worry about.

Now, let's write that behavior:


-- Volume Slider Behavior
-- assumes that the slider trough is in the
-- sprite channel just below the slider knob.
property pSoundChannel
property pRange
property pSliderBottom
property pDeltaFactor
property pMySprite
property pFlag
on getPropertyDescriptionList me
  set pdlist to [¬
    #pSoundChannel: [#comment:"Which Sound Channel",¬
                     #format:#integer,¬
                     #default:1,¬
                     #range:[#min:1,#max:8]] ]
  return pdlist
end getPropertyDescriptionList
on beginSprite me
  set pMySprite = the spriteNum of me
  set myTroughSprite = pMySprite - 1
  set pRange = the height of sprite myTroughSprite
  put pRange
  set pSliderBottom = the bottom of sprite ¬
    myTroughSprite
  set pDeltaFactor = 256/pRange
  adjustVolume me
  set pFlag = FALSE
end
on mouseDown me
  set pFlag = TRUE
end
on mouseUp me
  set pFlag = FALSE
end
on mouseUpOutside me 
  set pFlag = FALSE
end mouseUpOutside
on enterFrame me
  if pFlag = TRUE then
    if the mouseV <= pSliderBottom AND¬
       the mouseV >= pSliderBottom - pRange then
      set the locV of sprite pMySprite = the mouseV
      adjustVolume me
    end if
  end if
end
on adjustVolume me
  set myLocV = the locV of sprite pMySprite
  set sliderChange = pSliderBottom - myLocV 
  set the volume of sound pSoundChannel = ¬
     sliderChange * pDeltaFactor
end

The only tricky part to this behavior stems from the fact that the the slider in my example goes from low to high, while the locV of the slider knob moves counter -- from high to low. This leads to some counter-intuitive math formulas. For example, for the tasks in the enterFrame handler to be executed, the mouseV must be less than the bottom of the slider trough and greater than the top of the trough. If you create a slider which is upside down, you'll need to change the signs in the formulas to accomodate.

Good Luck with your program.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.