igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
259 stars 45 forks source link

Playing music and add effects in it. #115

Closed Gouravdeep closed 3 years ago

Gouravdeep commented 4 years ago

Hi, i want to play a mp3 song from android device storage memory and add some phase shift and tempo effects in it. for this i use JavaUtilities.createSampleFromAsset but first of all it take wav file not take mp3 file. secondly it is always giving false. So, please add some examples on git how to play music from MWengine and add effects on them.

igorski commented 4 years ago

Hi @Gouravdeep

The reason MWEngine only accepts WAV files is that it is meant to process uncompressed audio samples. Working with MP3 content "live" would imply a constant need to decompress MP3 into raw audio, which is wasteful in a live audio processing environment. It is therefor better to convert this upfront and supply the raw audio to MWEngine. There are plenty of third party libraries that decompress MP3 for you. You can then feed that decoded buffer to MWEngine (see JavaUtilities.createSampleFromBuffer()).

The use case is different to that of MWEngine's goal to be honest. You can load an entire musical piece as a single sample, but the intent of MWEngine is to create music by synthesizing audio (or assembling loops from shorter samples). For a large piece of audio it would be more ideal to stream this data instead of keeping it in memory (though that can come with performance overhead as it has to do disk reads, therefor MWEngine does not support this out of the box).

You could reuse much of the example activity to achieve this, though:

// your homework : convert MP3 into two double arrays (one for left and one for right channel if the file is stereo)
File yourMp3File;
double[][] stereoBufferFromMp3File = ThirdPartyMp3ToBufferConversion( yourMp3File );

// register buffers as samples in MWEngine
double[] left = stereoBufferFromMp3File[0];
double[] right = stereoBufferFromMp3File[1];
JavaUtilities.createSampleFromBuffer("songName", left.size(), 2, left, right);

// create sample event and assign registered sample to it
SampleEvent song = new SampleEvent( _sampler );

song.setSample( SampleManager.getSample( "songName ));
song.addToSequencer();

Where sampler is the reference to the SampleInstrument. The effects can be added to its processing chain (see example Activity).

igorski commented 3 years ago

Closing as there has been no recent activity on this question, nor any similar reports.