|
-
Jul 16th, 2008, 08:55 AM
#1
Thread Starter
PowerPoster
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.
-
Jul 16th, 2008, 04:15 PM
#2
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
-
Jul 16th, 2008, 06:14 PM
#3
Thread Starter
PowerPoster
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)
-
Jul 17th, 2008, 12:28 PM
#4
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.
-
Jul 17th, 2008, 01:46 PM
#5
Thread Starter
PowerPoster
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..
-
Jul 18th, 2008, 07:34 AM
#6
Fanatic Member
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.
-
Jul 18th, 2008, 07:51 AM
#7
Thread Starter
PowerPoster
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
-
Jul 18th, 2008, 08:13 AM
#8
Fanatic Member
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.
-
Jul 18th, 2008, 12:27 PM
#9
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.
-
Jul 18th, 2008, 01:41 PM
#10
Fanatic Member
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.
-
Jul 20th, 2008, 03:52 AM
#11
Thread Starter
PowerPoster
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
-
Jul 21st, 2008, 07:28 AM
#12
Fanatic Member
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.
-
Jul 21st, 2008, 07:42 AM
#13
Thread Starter
PowerPoster
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
-
Jul 21st, 2008, 08:04 AM
#14
Fanatic Member
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.
-
Jul 21st, 2008, 03:41 PM
#15
Thread Starter
PowerPoster
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.
-
Jul 21st, 2008, 04:17 PM
#16
Thread Starter
PowerPoster
Re: Fading smoothness (timers)
this seems to work!
timerInterval = timerLength / this.trackBar1.Value;
almost... maybe at some points 400ms off
-
Jul 22nd, 2008, 07:22 AM
#17
Fanatic Member
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.
-
Jul 22nd, 2008, 09:14 AM
#18
Thread Starter
PowerPoster
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
-
Jul 22nd, 2008, 09:18 AM
#19
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|