开发者

Volume fade at the end of the audio file

开发者 https://www.devze.com 2023-01-24 06:56 出处:网络
HI, I am doing an iphone applica开发者_如何转开发tion that works with audio files. Application also contains an audio converter, so I can potentially have a file of any audio format in my /Documents

HI,

I am doing an iphone applica开发者_如何转开发tion that works with audio files. Application also contains an audio converter, so I can potentially have a file of any audio format in my /Documents folder.

What I want is to implement a smooth audio file ending by reducing volume level at the end and fading it in at the beginning of the file.

How can I do that? (just point a direction. a framework, function, method)

I can use all Apple audio frameworks, both high level and low level ones. I can even access audio buffers while file is being converted.

Thanks in advance.

NOTE: I do not need audio Fade in/out feature while PLAYING it. I need this feature already written in audio file.


Because you are dealing with different formats, your best bet here is to apply the fade at the sample level when you are converting the data.

If you are doing the conversion offline, then you already know the number of samples in the file, so you can process X - B samples normally (where X is the total number of samples and B is the fade duration):

int i = 0;
for(i = 0; i < X - B; i++) {
  sampleToBeConverted[i] = inputSample[i]
}

Then, for the last B samples, you'd do something like this:

for(int j = 0; j < B; j++, i++) {
  sampleToBeConverted[i] = inputSample[i] * (1.0 - (j / B));
}

That would be for a linear fade slope, so if you want something fancier, well, then go knock yourself out. ;) Basically, the formula for doing a linear fade is y = m * x + b (remember algebra class?), where your slope in this case is 1 / B, and the y offset is 1.0, which is the maximum volume gain you want to apply (ie, no gain).

If you are doing the conversion in realtime, then you will need to keep B samples in a ringbuffer, and then apply the above algorithm when you reach the end of processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号