Question

I create an audio buffer and want to edit this before I play this sound. My problem is that i get an big noise when number is more than 1. It means I can only play the buffer without noise when I dont edit the buffer (data). Background informations: data is an audiorecord buffer with following informations:

private static final String TAG = "Aufnahme";
private AudioRecord recorder = null;
private boolean isRecording = false;
private int SAMPLERATE = 44100;
private int CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private int bufferSize = AudioRecord.getMinBufferSize(SAMPLERATE, CHANNELS,
        AUDIO_FORMAT);
private Thread recordingThread = null;

And my effect class :

public class verzerrer {    
    public void distortion(short[] data) {      
        output out = new output();
        long[] y = new long[data.length];
        int number =1000;

        for(int i=1;i<data.length;i++){
            y[i]=(data[i]/number)*number;       
        }   

        for(int i=0;i<data.length;i++){
            data[i]=(short) y[i];
        }
        out.send(data);
    }   
}
Was it helpful?

Solution

You say "My problem is that i get an big noise when number is more than 1." But it looks to me like a "big noise" is exactly what you are trying to create: your effect is called "distortion" and you are performing integer division when number > 1, which will create a very large amount IM distortion.

The effect you've created looks to me to be analogous to a "bit crush" effect: throwing away the least significant data. If you are looking to create a more traditional distortion (like a guitar amp distortion), you'll need to perform "clipping", not "bit crushing". Like this:

for(int i=1;i<data.length;i++){
    if( data[i] > number )
        y[i]=number;
    else if( data[i] < - number )
        y[i]=-number;       
}

This will create harmonic distortion. The lower number is, the more distortion you will get. Here, "number" is called "threshold". you may want to use something like

number=(Short) ( Short.MAX_VALUE * ( 1-t ) ) ;

to define it. Here, t is a float value. If t is closer to 1, you get more distortion, and closer to 0 you get less.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top