Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MobileAppCenter

macrumors newbie
Original poster
Oct 23, 2008
2
0
I could use some direction regarding methods to use volume control to fade in sounds internal to an app on start, and fade out on stop.

Thanks.
 

SqueegyX

macrumors regular
Mar 24, 2008
108
1
There's like 3 different ways audio is handled on the iPhone that greatly affect how this question would be answered. How are you play the audio files you want to fade?
 

MobileAppCenter

macrumors newbie
Original poster
Oct 23, 2008
2
0
AudioPlayer

There's like 3 different ways audio is handled on the iPhone that greatly affect how this question would be answered. How are you play the audio files you want to fade?

We are using the AudioPlayer class. Of course, we are adding sounds to the queue. We set the gain to 0.0 and after initiating the Play method we want to gradually have the gain increased to some predefined gain (maybe 1.0, or maybe less depending on user input).

I hope this is what you are looking for. Thanks for your help.
 

SqueegyX

macrumors regular
Mar 24, 2008
108
1
I only have experience with Apple's SoundEngine form the Crash Landing example. But the same idea should apply.

Setup a timer to call a method every few miliseconds to adjust the volume or gain. I don't think there is any sort of core animation like API for doing this stuff.
 

Niiro13

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
I only have experience with Apple's SoundEngine form the Crash Landing example. But the same idea should apply.

Setup a timer to call a method every few miliseconds to adjust the volume or gain. I don't think there is any sort of core animation like API for doing this stuff.

I'm also trying to fade in and out from the SoundEngine but want to keep performance. Is there a difference in using a timer versus a for loop? Timers are new to me and for loops aren't, so I'd be nice to know if Timers have a significant performance boost from for loops.
 

SqueegyX

macrumors regular
Mar 24, 2008
108
1
A loop would not work like you expect. Take this psuedo code.

Code:
for (int i =0; i < 100; i++) {
  [mySound setVolume:i];
}

This will execute in an almost imperceptible amount of time, making the fade barely noticeable. You could insert some sort of sleep in there, but then you are just tieing up your main application thread doing nothing.

You can tell the timer to fire 20 times per second and step the volume down, or up, a little bit each time. A loop doesn't give you that same level of control over time.
 

firewood

macrumors G3
Jul 29, 2003
8,141
1,384
Silicon Valley
Is there a difference in using a timer versus a for loop? Timers are new to me and for loops aren't, so I'd be nice to know if Timers have a significant performance boost from for loops.

Cocoa will not function well if loops are used with delays, even if that were not a bad programming practice. Use Timers instead, and let Cocoa call your code at the time desired.

If ramping the volume down a few percent every few milliseconds (until it gets to zero) doesn't work, you could try using uncompressed raw audio files and ramping down the amplitude of the samples in software at run time before putting them into the audio queue buffers. The latter is what I tried, and it works great. Allows me to fade in/out or cross fade without a single audible glitch, and with sample accurate looping.


.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.