0xDEADBEEF

[RSS]

Generování tónů o různých frekvencích v Javě - kód

import javax.sound.sampled.*;
import javax.sound.sampled.LineEvent.Type;
import java.io.*;

class Ears {

  static ByteArrayInputStream mkTone(int sampleRate, int durationMs, int freqHz) {
    var samples = (int)(durationMs * sampleRate / 1000f);
    var t = sampleRate / freqHz / 2;
    var arr = new byte[samples];
    for (int i = 0; i < samples; i++) {
      var x = Math.sin((float)i / t * Math.PI);
      arr[i] = (byte)(x * 127);
    }
    return new ByteArrayInputStream(arr);
  }

  public static void main(String[] args) throws Exception {
    var sampleRate = 96000;
    var durationMs = 1000;
    var format = new AudioFormat(sampleRate, 8, 1, true, false);

    var freqs = new int[]{ 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 150, 200, 1000, 10000, 11000, 12000, 13000, 14000, 15000, 17000, 20000, 25000, 30000, 35000, 40000, 48000 };

    for (int f: freqs) {
      System.out.println(f+" Hz");

      var is = mkTone(sampleRate, durationMs, f);
      var audioStream = new AudioInputStream(is, format, is.available());
      var clip = AudioSystem.getClip();
      clip.open(audioStream);
      clip.loop(0);
      Thread.sleep(durationMs*3/2);
      clip.close();
    }
  }
}
píše k47 (@kaja47, k47)