如何在Java中获取.wav文件的频率?

时间:2019-07-18 08:16:39

标签: java audio

我已经使用AudioFormat-Object在Java中创建了一个噪声发生器。该生成器以我指定的频率生成文件。它运作完美。现在,我只想使用.wav文件查找指定的频率。 这是我的发电机:

     public static void play(int hz, int msecs, double vol) throws LineUnavailableException {
         if (hz <= 0)
             throw new IllegalArgumentException("Frequency <= 0 hz");

         if (msecs <= 0)
             throw new IllegalArgumentException("Duration <= 0 msecs");

         if (vol > 1.0 || vol < 0.0)
             throw new IllegalArgumentException("Volume out of range 0.0 - 1.0");

         byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000];

         for (int i=0; i<buf.length; i++) {
             double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
             buf[i] = (byte)(Math.sin(angle) * 127.0 * vol);
         }

         for (int i=0; i < SAMPLE_RATE / 100.0 && i < buf.length / 2; i++) {
             buf[i] = (byte)(buf[i] * i / (SAMPLE_RATE / 100.0));
             buf[buf.length-1-i] =
              (byte)(buf[buf.length-1-i] * i / (SAMPLE_RATE / 100.0));
         }

         AudioFormat af = new AudioFormat(SAMPLE_RATE,8,1,true,false);

         SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
         sdl.open(af);
         sdl.start();
         sdl.write(buf,0,buf.length);
         sdl.drain();
         sdl.close();

         AudioInputStream ais = new AudioInputStream(
                        new ByteArrayInputStream(buf), af, 
                        buf.length / af.getFrameSize()
                      );

                      try {
         AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new
                          File("test.wav")
                        );
                      } 
                      catch(Exception e) {
                        e.printStackTrace();
                      } 
     }

那有可能吗? 预先感谢!

0 个答案:

没有答案