convert Frequency to Byte array - Android java.lang

Android examples for java.lang:Byte Array

Description

convert Frequency to Byte array

Demo Code

public class Main {

  public static byte[] convertFreq2Bytes(float freq, int samplingRate) {
    byte[] bytes = new byte[samplingRate];

    for (int i = 0; i < samplingRate; i++) {
      // double phase = calcPhase(i, freq, samplingRate);
      // bytes[i] = (byte) (128 * (Math.sin(phase)));
      bytes[i] = generateSample(i, freq, samplingRate);
    }//  ww  w .  j a  v  a  2s.co  m

    return bytes;
  }

  private static byte generateSample(int step, float freq, int samplingRate) {
    double phase4 = calcPhase(step, freq * 0.08f, samplingRate);
    return (byte) (60 * (Math.sin(phase4)));
  }

  private static double calcPhase(int step, float freq, int samplingRate) {
    return Math.PI * step * freq / samplingRate;
  }
}

Related Tutorials