Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add echo effect to wav file in android?

I have been struggling for a while now on how to modify a wav file by adding echo effect on it; My app does pitch sifting, speed and volume, but I can't add effects. I'm a total begginer at audio engineering or something like that.

My main goal is to find an algorithm and make a function that takes the byte[] samples and modifies it.

I'm using this current code right now:



sonic = new Sonic(44100, 1);
            byte samples[] = new byte[4096];
            byte modifiedSamples[] = new byte[2048];
            int bytesRead;

            if (soundFile != null) {
                sonic.setSpeed(params[0]);
                sonic.setVolume(params[1]);
                sonic.setPitch(params[2]);
                do {
                    try {
                        bytesRead = soundFile.read(samples, 0, samples.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    }

                    if (bytesRead > 0) {
                        sonic.putBytes(samples, bytesRead);
                    } else {
                        sonic.flush();
                    }

                    int available = sonic.availableBytes();

                    if (available > 0) {
                        if (modifiedSamples.length < available) {
                            modifiedSamples = new byte[available * 2];
                        }

                        sonic.receiveBytes(modifiedSamples, available);
                        if (thread.getTrack() != null && thread.getTrack().getState() != AudioTrack.STATE_UNINITIALIZED)

                            thread.WriteTrack(modifiedSamples, available);
                    }

                } while (bytesRead > 0);

As you can see I use sonic ndk to alter the pitch speed and volume by passing another byte[] array there "modifiedSamples" and I need to know if there is a way to modify this "modifiedSamples" to get the echo effect. I know this sounds like I'm asking for the function, but I don't. I just don't know anything about audio processing stuff and I would apreciate a starting point or even if what i'm trying to do is possible with my byte array.

like image 386
Heixss Avatar asked Oct 20 '22 08:10

Heixss


1 Answers

Continuing from the comments - here's an implementation that post-processes the wav file to add echo.

//Clone original Bytes
byte[] temp = bytesTemp.clone();
RandomAccessFile randomAccessFile = new RandomAccessFile(fileRecording, "rw");
//seek to skip 44 bytes
randomAccessFile.seek(44);
//Echo
int N = sampleRate / 8;
for (int n = N + 1; n < bytesTemp.length; n++) {
   bytesTemp[n] = (byte) (temp[n] + .5 * temp[n - N]);
}
randomAccessFile.write(bytesTemp);

The same process will work in real-time as well, with a few changes.

like image 142
sudo_rizwan Avatar answered Oct 23 '22 11:10

sudo_rizwan