Results 1 to 19 of 19

Thread: Fading smoothness (timers)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Fading smoothness (timers)

    whoever developed the app I am working on really should be put behind bars.

    anyway
    problem is, when we press a button, it should fade out the volume. this is all working with some timing issues.

    now, it uses a timer (before, they were using Thread.Sleep()! Bad practice) to fade out the volume, thats all good
    The volume interval is set as an application setting, so can be 6 seconds to fade out or 10 seconds or whatever

    however, say if the volume is 50% and we press the button to fade, it doesnt take the full interval defined to fade out, but instead fades out earlier than the defined interval

    what is the best way of tackling this so that the volume is decreased nicely/smoothly regardless of the interval but as long as it completes fully within that interval?


    Now, there are 2 timers.

    1) which is set for the fade interval (6 seconds)
    2) the main one which is given the interval calculated based on the fade length * 1000

    in the 2nd timer, the real fader, it just decreases the volume by 1 - thats all but again, regardess of the volume I want it to decrease the volume to 0 and take the entire length of the time defined to do this

    does this make sense?
    Last edited by Techno; Jul 16th, 2008 at 09:01 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Fading smoothness (timers)

    You must have a volume 'unit' in your application. When the user requests fade, calculate the current units. Divide by 6 seconds. Then in the timer's tick event, deduct the result each time

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    hmmm unit.. volume unit? Can you explain a bit more?

    I have:

    total seconds to fade
    current volume (represented by a slider control in .NET)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Fading smoothness (timers)

    So you must then have the .Value property of the Slider. That can represent your current volume (as it does).

    So if the current value is 92 and you want it to fade in 4 seconds,

    92/4 = 23

    In each tick event, deduct 23 from the slider's current value while you reduce the volume using whatever mechanism you currently have.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    sure i understand. but now im confused with myself haha.

    so, the timer is set to fade in at a predefined level from the DB. say 6 seconds.

    Now, regardless of the position of the volume value, I want to fade it out for the full length of the seconds. so if the volume is at 50%, i want to take it 6 seconds to get to 0%. if its volume is 75%, same thing..

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    So if you were to follow what medhak is saying...

    50% / 6 seconds = 8 1/3% per second
    75% / 6 seconds = 12.5% per second

    So the formula would be:

    Value / time

    That correlates to medhak saying deduct 23 each second. So here let's say you started at 75% and wanted it to fade in 6 seconds. We would do the calculation and get 12.5 then the intervals would look like:

    75%, 62.5%, 50%, 37.5%, 25%, 12.5%, 0%
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    thanks. almost there except it doesnt fully fade down... leaves a few % of the volume to go. hmmm.

    im doing...

    int fadeVolumeInterval = this.fadeLength * 1000 / this.volumeController.Value;

    fadeLength is in seconds.... so we * 1000 to get milliseconds for the timer interval property

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    How much is left? You might want to check to see if there is a rounding error.
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Fading smoothness (timers)

    If you're getting a 'little bit remaining', it'll be a rounding problem as SteveF says. I'd suggest rounding up to the nearest whole number if you can in addition to checking, each time, whether the remaining value in the trackbar is less than the amount you are planning to deduct each time.

    So what I'm saying is if you are deducting 12.5 each time and you happen to have 5 left, then because 5 is less than 12.5, you should make it a special case and drop down to 0.

  10. #10
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    Try what medhak said... For the 75% in 6 seconds it should work exactly though. And then try the 50% in 6 seconds to get the rounding error. Then use his method and see if it works.
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    yeh thanks.
    I just realised something (after having a long rest!)
    the thing here is that its not going to fade smoothly and has to be done smoothly which is why I have a 2nd timer which does the fade (minus 1 from the current value of the volume).

    so it works out the timer interval based on the length of fade (6000 ms)

    now, how do we implement this so that the fading is still smooth, but I guess we need to really work on the timer interval rather than how much %age to fade by

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  12. #12
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    I'm not really understanding your explaination but let me take a shot at it... You have a second timer, Timer2, that will do each fade by subtracting just one from the volume each time. So perhaps every interval of the first timer we set up, it will fire Timer2. The interval for Timer2 will always be one second, so the only parameter it will need is the % to fade each time. So if we used the example we just had you would fade 12.5% over 1 second.

    If this is way off could you explain in a little more detail what's going on in your program?
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    sure. you are almost there my friend!

    right, it WILL decrease by 1% everytime, which is what I want (so we can have a SMOOTH fade)
    The thing that needs to be adjusted is the fade timer (2nd timer that decreases the value) interval.

    So we need to work out some formula where it will set the interval of the timer based on:

    current value of volume
    total fade length

    thats all

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  14. #14
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    Well like I said... You wouldnt need to incorperate the value of the volume if I understand correctly... Since you will just be decrementing by 1 each time. So if you pass in the total fade length as 12.5 and divide that by 1 second it will give you the number you are looking for. Did you try doing that? Sorry I might just not be understanding correctly...
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    sure, ill give that a shot if I understand. So to set the fade timer interval we would do this:

    current %age of volume * 1000 (milliseconds) = timer interval ??

    doesnt seem right since the volume is always decreased by 1% every tick.


    totalFadeLength = 6000; (6 secs)
    currentVolume = 50;
    timerInterval = ??
    Last edited by Techno; Jul 21st, 2008 at 03:48 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    this seems to work!


    timerInterval = timerLength / this.trackBar1.Value;

    almost... maybe at some points 400ms off

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  17. #17
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    First Timer
    totalFadeLength = 6000; (6 secs)
    currentVolume = 50;
    % per second = 50/6 = 8 1/3

    Second Timer
    So then in 1000 miliseconds you want to fade 8 1/3% going 1 percent at a time
    timerInterval = 1000/8.33333 = 120 seconds

    That would mean every 120 milliseconds for 1 second you decrease by 1%
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Fading smoothness (timers)

    Thanks. its funny works fine in a new project but not this dreaded project. timers flying all over the place...stupid people

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  19. #19
    Fanatic Member
    Join Date
    Jun 2003
    Location
    Worcester, MA
    Posts
    782

    Re: Fading smoothness (timers)

    Haha no problem. Mark it as RESOLVED please Thanks!
    C#.net, VB, C++, Java, VS 2005/2008
    Dont' forget to rate posts that are helpful to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width