2013-07-26 23 views
0

我做了一個小應用程序,可以從我的電腦上捕捉聲音。系統的簡單過程是當我播放一個音樂文件時,它會捕捉聲音,當我播放捕捉到的東西時,它會播放。完全一切正常。現在我想要做的就是打印播放。假設我捕捉到「嗨,早上好」這樣的聲音,現在當我按下回放時,它應該不得不用文本打印錄製的內容。捕獲和回放編碼如下。如何在音頻文件中打印該詞

private void captureAudio() { 
    try { 
     final AudioFormat format = getFormat(); 
     DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); 
     final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); 
     line.open(format); 

     line.start(); 
     Runnable runner = new Runnable() { 
      int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); 
      byte buffer[] = new byte[bufferSize]; 

      public void run() { 

       out = new ByteArrayOutputStream(); 
       running = true; 
       try { 
        while (running) { 
         int count = line.read(buffer, 0, buffer.length); 
         if (count > 0) { 
          out.write(buffer, 0, count); 
         }        
        } 
        out.close(); 
       } catch (IOException e) { 
        System.err.println("I/O problems: " + e); 
        System.exit(-1); 
       } 
      } 
     }; 
     Thread captureThread = new Thread(runner); 
     captureThread.start(); 
    } catch (LineUnavailableException e) { 
     System.err.println("Line unavailable: " + e); 
     System.exit(-2); 
    } 
} 


private void playAudio() { 
    try { 
     byte audio[] = out.toByteArray(); 

     InputStream input = new ByteArrayInputStream(audio); 
     final AudioFormat format = getFormat(); 
     final AudioInputStream ais = new AudioInputStream(input, format, audio.length/format.getFrameSize()); 
     DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 
     final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); 
     line.open(format); 
     line.start(); 
     Runnable runner = new Runnable() { 
      int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); 
      byte buffer[] = new byte[bufferSize]; 

      public void run() { 

       try { 
        int count; 
        while ((count = ais.read(buffer, 0, buffer.length)) != -1) { 
         if (count > 0) { 
          line.write(buffer, 0, (char)count); 
          System.out.print((char)count); 

         } 
        } 
        line.drain(); 
        line.close(); 
       } catch (IOException e) { 
        System.err.println("I/O problems: " + e); 
        System.exit(-3); 
       } 
      } 
     }; 
     Thread playThread = new Thread(runner); 
     playThread.start(); 
    } catch (LineUnavailableException e) { 
     System.err.println("Line unavailable: " + e); 
     System.exit(-4); 
    } 
} 

請有人建議/幫助我克服這一點。
謝謝。

+0

1)你想要什麼是語音識別,但我懷疑將這些信息放入生成的音頻文件中是可能的。 2)在將它們應用於問題之前,請*閱讀*標籤彈出窗口,這與'音頻流式傳輸'無關。 –

+0

大概看看[API](http://docs.oracle.com/cd/E17802_01/products/products/java-media/speech/forDevelopers/jsapi-doc/)和[Sphinx-4](http:///cmusphinx.sourceforge.net/sphinx4/)。 – NINCOMPOOP

+0

@AndrewThompson - 感謝評論老兄。我已經做了語音識別部分,並且100%正常工作。但在這裏的事情是沒有講話的一部分。它關於播放音頻。要識別語音,應該有一些輸入到目標數據行。這就是我目前所困擾的一點。任何想法? – maXfenda

回答

1

看看CMU sphinx!爲了能夠將捕捉到的audion轉換爲語音,您可以使用Sphinx api。但請注意,由於系統仍處於開發階段,因此語音識別的準確性不足以幫助您。在android中,您可以使用google的語音識別,其中有一個公平的accuracy。但仍然不會將您捕獲的語音轉換爲語音以期望的精度。所以最好有一個固定的歌詞文本,以播放音頻的速度顯示。

希望幫助!

+0

雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的基本部分,並提供參考鏈接。否則,發表評論而不是答案。 – jlordo

+0

@jlordo我曾與獅身人面像,我知道它的優點和缺點非常well.Anyways我肯定會包括一些更多的信息和links.Thanx反正! – rahulserver

+0

我經歷了CMU獅身人面像,並做了一些部分。請檢查我的上述評論。那裏我清楚地提到了我的問題。謝謝@jlordo對不起兄弟此刻我沒有參考鏈接,但老實說,我從互聯網上得到這個代碼,並根據我的需要做一些修改。我非常感謝你的建議。 – maXfenda

相關問題