Piasy / AudioMixer

A cross-platform audio mixer, supports Android, iOS, macOS and Windows. Powered by WebRTC, FFmpeg and Djinni.
MIT License
190 stars 70 forks source link

AudioMixer

A cross-platform audio mixer, supports Android, iOS, macOS and Windows. Powered by WebRTC, FFmpeg and Djinni.

Install

Android

allprojects {
    repositories {
        mavenCentral()
    }
}

compile 'com.github.piasy:AudioMixer:1.0.3'

iOS & macOS

Due to the file size limitation, publish with CocoaPods is difficult, so please download the prebuilt AudioMixer.framework directly.

Usage

Android

Initialize:

AudioMixer.globalInitialize();

Create mixer:

AudioMixer mixer = new AudioMixer(new MixerConfig(
        new ArrayList<>(Arrays.asList(
                new MixerSource(MixerSource.TYPE_FILE, 1, 1,
                        "/sdcard/mp3/morning.mp3", 0, 0),
                new MixerSource(MixerSource.TYPE_RECORD, 2, 1, "", sampleRate,
                        channelNum)
        )),
        sampleRate, channelNum, frameDurationMs
));

Do mix with recorded data:

mixer.addRecordedData(2, buf, size);

Do mix with file only:

AudioBuffer buffer = mixer.mix();

Use the mixed audio data:

if (buffer.getSize() > 0) {
    audioTrack.write(buffer.getBuffer(), 0, buffer.getSize());
}

For more detailed info, please refer to the source code.

iOS & macOS

Initialize:

#import <AudioMixer/AudioMixer.h>

[PYAAudioMixer globalInitializeFFmpeg];

Create mixer:

#import <AudioMixer/AudioMixer.h>

NSArray* mixerSources = @[
    [PYAMixerSource
        mixerSourceWithType:PYAMixerSourceTypeFile
                       ssrc:1
                     volume:1
                       path:[self pathForFileName:@"morning.mp3"]
                 sampleRate:0
                 channelNum:0],
    [PYAMixerSource mixerSourceWithType:PYAMixerSourceTypeRecord
                                   ssrc:2
                                 volume:1
                                   path:@""
                             sampleRate:_sampleRate
                             channelNum:_channelNum],
];
PYAMixerConfig* config = [PYAMixerConfig mixerConfigWithSources:mixerSources
                                               outputSampleRate:_sampleRate
                                               outputChannelNum:_channelNum
                                                frameDurationMs:10];
_mixer = [[PYAAudioMixer alloc] initWithConfig:config];

Do mix with recorded data:

[_mixer addRecordedData:2 data:_buffer size:mixerInputSize];
_mixedBuffer = [_mixer mix];

Do mix with file only:

_mixedBuffer = [_mixer mix];

Use the mixed audio data:

if (_mixedBuffer.size > 0) {
  write([_recordAndMixDumper fileDescriptor], _mixedBuffer.data,
        _mixedBuffer.size);
}

For more detailed info, please refer to the source code.

Dependencies

Development

Caveat

TODO