java - 为什么我的锯齿测试音不播放?

标签 java

我不应该将其放入 for 循环中,该循环是样本长度吗?我不明白为什么它播放正弦波公式而不是锯齿波...这是从结构非常相似的蜂鸣类修改而来的,也许是中间的 for 循环生成了太多的圆形来获得锯齿形状。你怎么认为?这是我的代码:

class MakeSaw
{

  static void makeSaw(double f, int t) throws Exception
  {

    int nChannel = 1;         // number of channel : 1 or 2

    // samples per second
    float sampleRate = 16000;  // valid:8000,11025,16000,22050,44100
    int nBit = 16;             // 8 bit or 16 bit sample

    int bytesPerSample = nChannel*nBit/8;


    int bufferSize = (int) (nChannel*sampleRate*t*bytesPerSample);
    byte[] audioData = new byte[bufferSize];

    // "type cast" to ShortBuffer
    java.nio.ByteBuffer byteBuffer = java.nio.ByteBuffer.wrap(audioData);
    java.nio.ShortBuffer shortBuffer = byteBuffer.asShortBuffer();


    int sampleLength = audioData.length/bytesPerSample;

    // generate the wave
    double volume = 8192;   // 0-32767
    double PI = Math.PI;
    double sawWave=0;


        //make saw wave
        for(int i = 0; i < sampleLength; i++)
        {
            double time = i/sampleRate;
            sawWave = sawWave + Math.sin(2*PI*f*(i*t)/i);    //passing amplitude frequency and time
            short amplitude = (short) (volume*sawWave);

            for (int c=0;c<1;c++) //repeat once because audio playing on one channel
            {
                shortBuffer.put(amplitude);
            }
        }

    //end wave making//end generating sound wave sample


    boolean isSigned=true;
    boolean isBigEndian=true;

    // Define audio format
    javax.sound.sampled.AudioFormat audioFormat =
      new javax.sound.sampled.AudioFormat(sampleRate, nBit, nChannel, isSigned,isBigEndian);


    javax.sound.sampled.DataLine.Info dataLineInfo =
      new javax.sound.sampled.DataLine.Info(
         javax.sound.sampled.SourceDataLine.class, audioFormat);

    // get the SourceDataLine object
    javax.sound.sampled.SourceDataLine sourceDataLine = 
      (javax.sound.sampled.SourceDataLine)
      javax.sound.sampled.AudioSystem.getLine(dataLineInfo);

    sourceDataLine.open(audioFormat);
    sourceDataLine.start();

    // actually play the sound
    sourceDataLine.write(audioData,0,audioData.length);

    // "flush",  wait until the sound is completed
    sourceDataLine.drain();

  }


  public static void main(String[] args) throws Exception
  {

    makeSaw(400,2);
  }
}

最佳答案

您对 sawWave 的计算是错误的。所有振幅均为零。

我还没有尝试为您调试这个问题,但其中有三件事很突出

        double time = i/sampleRate;
        sawWave = sawWave + Math.sin(2*PI*f*(i*t)/i);    //passing amplitude frequency and time
  1. 您没有在 sawWave 计算中使用时间
  2. 除以 i 看起来很可疑。
  3. 为什么在生成锯齿波时要使用 sin()

关于java - 为什么我的锯齿测试音不播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805091/

相关文章:

java - 如何提取目录中的所有多部分 ZIP/RAR 存档?

java - 如何选择过去一个月的第一个星期一之前的星期日

java - 在 primefaces 中显示嵌套 bean,JSF web 应用程序

java - 自定义 UserDetailsS​​ervice 似乎没有 Autowiring

java - 如何提取文件 jre-9/lib/modules?

java - 本地 Java 函数的字节码检测

java - 要根据输入数字本身评估的两个数字的 GCD/LCM

java - 如何确保 Java 中的并行处理

java - Selenium 网络驱动程序 : stale element reference: element is not attached to the page document

java - 在 Leiningen (Clojure) for Android 中设置 JavaVersion sourceCompatibility(Java 1.7 vs 1.8)