Click here to Skip to main content
15,885,914 members
Home / Discussions / Android
   

Android

 
AnswerRe: Method not returning expected value Pin
Jochen Arndt23-Feb-17 3:45
professionalJochen Arndt23-Feb-17 3:45 
GeneralRe: Method not returning expected value Pin
Richard MacCutchan23-Feb-17 5:45
mveRichard MacCutchan23-Feb-17 5:45 
AnswerRe: Method not returning expected value Pin
Davidw196924-Feb-17 16:52
Davidw196924-Feb-17 16:52 
AnswerRe: Method not returning expected value Pin
Richard MacCutchan23-Feb-17 3:58
mveRichard MacCutchan23-Feb-17 3:58 
GeneralRe: Method not returning expected value Pin
Davidw196924-Feb-17 16:53
Davidw196924-Feb-17 16:53 
QuestionAndroid and C# Web Services Pin
Member 1155786814-Feb-17 22:41
Member 1155786814-Feb-17 22:41 
SuggestionRe: Android and C# Web Services Pin
Richard Deeming15-Feb-17 2:39
mveRichard Deeming15-Feb-17 2:39 
QuestionAndroid: High Pass filter Pin
Himanshu Bhutani13-Feb-17 1:36
Himanshu Bhutani13-Feb-17 1:36 
So I am trying o make an application which records some audio and then filters all the high-frequency sounds from it and then in turn play only that high- frequency sounds. I am doing so using a filter class i found online.
Now the problem I'm facing is, when i press the play button, nothing is played. Now i can't tell if it is filtering out everything and hence playing nothing or that the output file never gets the filtered part.

I am posting my code and the filter class code and would really appreciate if someone could help me out with it. Thanks.

MAIN CODE

package abc.com.please;

import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;


public class MainActivity extends AppCompatActivity {

    private static MediaRecorder mediaRecorder = new MediaRecorder();
    private static MediaPlayer mediaPlayer;

    private static String audioFilePath;
    private static Button stopButton;
    private static Button playButton;
    private static Button recordButton;
    EditText box1;
    EditText    box2;
    private boolean isRecording = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recordButton = (Button) findViewById(R.id.recordButton);
        playButton = (Button) findViewById(R.id.playButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        box1=(EditText) findViewById(R.id.editText);
        box2=(EditText) findViewById(R.id.editText2);

        if (!hasMicrophone())
        {
            stopButton.setEnabled(false);
            playButton.setEnabled(false);
            recordButton.setEnabled(false);
        } else {
            playButton.setEnabled(false);
            stopButton.setEnabled(false);
        }

        audioFilePath =
                Environment.getExternalStorageDirectory().getAbsolutePath()
                        + "/myaudio.3gp";


        recordButton.setOnClickListener(new View.OnClickListener(){

            public void onClick (View v)
            {

                isRecording = true;
                stopButton.setEnabled(true);
                playButton.setEnabled(false);
                recordButton.setEnabled(false);

                try {
                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mediaRecorder.setOutputFile(audioFilePath);
                    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                }catch (Exception e) {
                    e.printStackTrace();

                }



            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick (View view)
            {

                stopButton.setEnabled(false);
                playButton.setEnabled(true);
                if (isRecording)
                {
                    recordButton.setEnabled(false);
                    isRecording = false;
                    mediaRecorder.stop();
                    mediaRecorder.release();
                    recordButton.setEnabled(true);
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Not recording",Toast.LENGTH_SHORT).show();
                    recordButton.setEnabled(true);
                }
            }});

        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view)

            {
               
                
                try{
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    BufferedInputStream in = new BufferedInputStream(new FileInputStream(audioFilePath.toString()));
                    int read;
                    byte[] buff = new byte[1024];
                    while ((read = in.read(buff)) > 0)
                    {
                        out.write(buff, 0, read);
                    }
                    out.flush();
                    byte[] audioBytes = out.toByteArray();
                    final FloatBuffer fb = ByteBuffer.wrap(audioBytes).asFloatBuffer();
                    final float[] dst = new float[fb.capacity()];
                    fb.get(dst);
                    Filter filter = new Filter(15000,44100, Filter.PassType.Highpass,1);
                    for (int i = 0; i <dst.length; i++)
                    {
                        filter.Update(dst[i]);
                        dst[i] = filter.getValue();
                    }

                    byte byteArray[] = new byte[dst.length*4];
                    ByteBuffer byteBuf = ByteBuffer.wrap(byteArray);
                    FloatBuffer floatBuf = byteBuf.asFloatBuffer();
                    floatBuf.put (dst);

                    File someFile = new File(audioFilePath);
                    String filepath= someFile.toString();
                    FileOutputStream fos = new FileOutputStream(someFile);
                    fos.write(byteArray);
                    fos.flush();
                    fos.close();
                    playButton.setEnabled(false);
                    recordButton.setEnabled(false);
                    stopButton.setEnabled(true);
                   
                    try {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setDataSource(filepath);
                        mediaPlayer.prepare();
                        mediaPlayer.start();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                 

                }catch(java.io.IOException e){
                    e.printStackTrace();
                }




            }
        });
    }

    protected boolean hasMicrophone() {
        PackageManager pmanager = this.getPackageManager();
        return pmanager.hasSystemFeature(
                PackageManager.FEATURE_MICROPHONE);
    }









}




FILTER CLASS

package abc.com.please;

/**
 * Created by HBHUTAN on 2/9/2017.
 */

public class Filter {

    private float resonance;

    private float frequency;
    private int sampleRate;
    private PassType passType;


    public float value;

    private float c, a1, a2, a3, b1, b2;

    /// <summary>
/// Array of input values, latest are in front
/// </summary>
    private float[] inputHistory = new float[2];

    /// <summary>
/// Array of output values, latest are in front
/// </summary>
    private float[] outputHistory = new float[3];

    public Filter(float frequency, int sampleRate, PassType passType, float resonance)
    {
        this.resonance = resonance;
        this.frequency = frequency;
        this.sampleRate = sampleRate;
        this.passType = passType;

        switch (passType)
        {
            case Lowpass:
                c = 1.0f / (float)Math.tan(Math.PI * frequency / sampleRate);
                a1 = 1.0f / (1.0f + resonance * c + c * c);
                a2 = 2f * a1;
                a3 = a1;
                b1 = 2.0f * (1.0f - c * c) * a1;
                b2 = (1.0f - resonance * c + c * c) * a1;
                break;
            case Highpass:
                c = (float)Math.tan(Math.PI * frequency / sampleRate);
                a1 = 1.0f / (1.0f + resonance * c + c * c);
                a2 = -2f * a1;
                a3 = a1;
                b1 = 2.0f * (c * c - 1.0f) * a1;
                b2 = (1.0f - resonance * c + c * c) * a1;
                break;
        }
    }

    public enum PassType
    {
        Highpass,
        Lowpass,
    }

    public void Update(float newInput)
    {
        float newOutput = a1 * newInput + a2 * this.inputHistory[0] + a3 * this.inputHistory[1] - b1 * this.outputHistory[0] - b2 * this.outputHistory[1];

        this.inputHistory[1] = this.inputHistory[0];
        this.inputHistory[0] = newInput;

        this.outputHistory[2] = this.outputHistory[1];
        this.outputHistory[1] = this.outputHistory[0];
        this.outputHistory[0] = newOutput;
    }


    public float getValue()
    {
        return this.outputHistory[0];
    }

}

QuestionRe: Android: High Pass filter Pin
David Crow13-Feb-17 2:41
David Crow13-Feb-17 2:41 
AnswerRe: Android: High Pass filter Pin
Nick_314159265415-Apr-17 3:55
Nick_314159265415-Apr-17 3:55 
QuestionAndroid Day of Week Calculator Pin
Pavlex49-Feb-17 9:50
Pavlex49-Feb-17 9:50 
AnswerRe: Android Day of Week Calculator Pin
David Crow9-Feb-17 10:02
David Crow9-Feb-17 10:02 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 10:09
Pavlex49-Feb-17 10:09 
GeneralRe: Android Day of Week Calculator Pin
David Crow9-Feb-17 10:12
David Crow9-Feb-17 10:12 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 10:15
Pavlex49-Feb-17 10:15 
SuggestionRe: Android Day of Week Calculator Pin
David Crow9-Feb-17 10:23
David Crow9-Feb-17 10:23 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 10:32
Pavlex49-Feb-17 10:32 
GeneralRe: Android Day of Week Calculator Pin
David Crow9-Feb-17 10:36
David Crow9-Feb-17 10:36 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 10:38
Pavlex49-Feb-17 10:38 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan9-Feb-17 21:07
mveRichard MacCutchan9-Feb-17 21:07 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 21:39
Pavlex49-Feb-17 21:39 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan9-Feb-17 22:20
mveRichard MacCutchan9-Feb-17 22:20 
GeneralRe: Android Day of Week Calculator Pin
David Crow10-Feb-17 2:08
David Crow10-Feb-17 2:08 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan10-Feb-17 2:46
mveRichard MacCutchan10-Feb-17 2:46 
GeneralMobile and o/s application development Pin
JimmiJames337-Feb-17 5:42
professionalJimmiJames337-Feb-17 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.