1

I'm creating an audio player like garage band.

For that I have to mix 2 different audio file format at runtime into 1 file.

Example:
User pushes drum button and violin button at the same time.
Then the user pushes the disk button to save that tune.

So how can I mix them into 1 file (Asynchronously and perfect time duration)??

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Mirant
  • 308
  • 2
  • 13
  • possible duplicate of [iPhone: Mix two audio files programmatically?](http://stackoverflow.com/questions/8638731/iphone-mix-two-audio-files-programmatically) – matt Oct 10 '14 at 13:18
  • how to video file add animation gif file objective c – Ramani Hitesh Dec 14 '18 at 13:17

2 Answers2

1

This is pretty trivial with Apple's new AVAudioEngine, which is designed for exactly this kind of situation.

https://developer.apple.com/library/prerelease/iOS/documentation/AVFoundation/Reference/AVAudioEngine_Class/index.html

And watch the WWDC video about it...

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • (Curiosity killed the cat:) Is this also usable for environments other than iOS? – GitaarLAB Oct 10 '14 at 12:19
  • Here's my simple demo code, including mixdown to a file: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch14p655AVAudioEngineTest/AVAudioEngineTest/ViewController.swift – matt Oct 10 '14 at 12:21
0

Note for future readers:
when I typed this answer, the question didn't specify any environment/os/language, it just asked how to merge audio.

Basic concept: equal waves amplify each other, opposite waves attenuate each other.
So, basic idea is:

  • Convert audio-sample to PCM (if it isn't PCM already) (you might want to do this in chunks)
  • Sum each channel's sample and divide by number of channels (substituting 0 for the tracks that take less time than the longest track).
  • Pack (to wave etc.) and store binary (optionally compressed as mp3 etc).

Example: 3 mono tracks (T=track, C=channel, Sn=single Sample n's value or 0):

resultTrack_C1_Sn = ( T1_C1_Sn + T2_C1_Sn + T3_C1_Sn ) / totalTracks

When multi-channel (like stereo): repeat for each channel, interleaving results (left channel comes first.). Naturally you can do this in one 'run'.

Note (for PCM):

  • <= 8 bits sample resolution are unsigned in PCM (so 0 is 127, in other words, negative wave-swing is below 127, positive wave-swing is above 127) and you need to account for that.
  • >8 bits are signed so you can safely use the basic equation above (since -4 + -5 = -9).

That should get you started in the basic concept.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • If you have any demo or sample code or method(link) for that please tell me or send me.thank you. – Mirant Oct 10 '14 at 12:25
  • Sorry, I'm currently doing this in javascript which is not yet ready to show off. However, (apart from writing a correct wave-header, which is trivial for *basic* 16bit 2ch 41.1khz --for PCM formats with a resolution other than a multiple of 8 bits you will find the equations in the RIFF/WAVE spec and 98% of the equations in code online are wrong (they accidentally work for multiples of 8 bits), but that's stuff for a different question--) the steps outlined here are correct and should get you started. Actually, it is *exactly* what I'm doing and I verified results with some DAW's. – GitaarLAB Oct 10 '14 at 12:31