AudioMixer

Project Url: Piasy/AudioMixer
Introduction: A cross-platform audio mixer, supports Android, iOS, macOS and Windows. Powered by WebRTC, FFmpeg and Djinni.
More: Author   ReportBugs   
Tags:

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

  • FFmpeg: 3.4.2
  • WebRTC: #23794

Development

  • Before run Android demo, push mp3 to sdcard: adb push mp3 /sdcard/
  • Generate sources: ./run_djinni.sh
  • Extract libs: ./extract_libs.sh

Caveat

  • Due to the limitation of WebRTC, frameDurationMs must be 10ms, and we must mix 10ms's audio data each time;

TODO

  • [ ] ffmpeg 4.0 audio decoder doesn't work
  • [x] iOS
  • [x] macOS
  • [ ] Windows
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools