音频输入编程

音频输入编程

问题描述:

即时通讯播放的内容超出了我通常编程的范围,所以我想在浪费更多时间之前我会问。音频输入编程

所以基本上我有一个按钮(通常在计算机开/关开关上找到),并使用我不太了解的电子电路知识,我将它连接到标准音频电缆(当然连接到我的电脑音频插孔)。通过Audacity,我发现当我点击按钮时,会生成一个音频波形并将其拾取。

这是非常基本的东西,我离开了我的Arduino回到我父母的地方,以便在绝望中我决定用我的声卡作为微控制器。

无论如何,我相信你们都知道我要去哪里。 我该如何编程一个运行于命令提示符(用于测试)的小应用程序,以及作为后台进程来监视和处理来自输入的信息以执行某些任务(如锁定我的计算机或打开Firefox等..)?

我与Java上班我的计算机学位的一部分(我们仍在学习很基本的东西......因此这个问题),并用PHP的工作每天都在工作,所以我不介意学习一门新语言,如果我必须。

我最终搞清楚了这一张给自己。基本上就像听输入线一样简单,然后运行一个方法,如果音频的幅度得到一定的数量。通过测试,我还发现,当按下按钮时,幅度的测量值为负值,释放按钮时记录正幅值。这意味着您可以为这些单独事件编写代码,并且如果您足够聪明,还可以使用间隔计时器为按钮被按下时添加事件。

反正这里是代码(不幸的是我还没有发表评论它的时间):

AudioInputButton.java

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.TargetDataLine; 
import javax.sound.sampled.Mixer.Info; 


public class AudioInputButton { 
    private int line; 
    private AudioFormat format; 
    private Info[] lines; 
    private TargetDataLine inputLine; 
    private DataLine.Info inInfo; 
    private int bufferSize; 

    public AudioInputButton(int line){ 
    this.line = line; 
    this.setup(); 
    } 

    public AudioInputButton(){ 
    this.line = 2; 
    this.setup(); 
    } 

    private void setup(){ 
    format = new AudioFormat(8000, 8, 1, true, true); 
    lines = AudioSystem.getMixerInfo();  
    inInfo = new DataLine.Info(TargetDataLine.class, format); 
    bufferSize = (int) format.getSampleRate() * format.getFrameSize(); 
    } 

    public void printLineInfo(){ 
    for (int i = 0; i < lines.length; i++){ 
     System.out.println(i+": "+lines[i].getName()+"\n"+lines[i].getDescription()); 
    } 
    } 

    public void startListening(){ 
    try{ 
     inputLine = (TargetDataLine)AudioSystem.getMixer(lines[line]).getLine(inInfo); 
     inputLine.open(format, bufferSize); 
     inputLine.start(); 

     byte[] buffer = new byte[bufferSize]; 

     System.out.println("Listening on line " +line+", " + lines[line].getName() + "..."); 

     while(true){ 
     inputLine.read(buffer,0,buffer.length); 
     int sample = listen(buffer); 
     if(sample > 0){ 
      onClick(); 
     } 
     } 
    }catch (LineUnavailableException e){ 
     System.out.println("Line " + line + " is unavailable."); 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    } 

    public int listen(byte[] eightBitByteArray) 
    { 
    int index = 0; 
    int ret = 0; 
    boolean down = false; 
    boolean up = false; 
    for (int audioByte = 0; audioByte < eightBitByteArray.length;) 
    { 
     int low = (int) eightBitByteArray[audioByte]; 
     audioByte++; 
     int high = (int) eightBitByteArray[audioByte]; 
     audioByte++; 
     int sample = (high << 8) + (low & 0x00ff); 
     if(sample < -1100){ 
     if(!down){ 
      onDown(); 
      ret = sample; 
      down = true; 
     } 
     }else if(sample > 1100){ 
     if(!up){ 

      onUp(); 
      ret = sample; 
      down = false; 
      up = true; 
     } 
     } 
     index++; 
    } 
    return ret; 
    } 

    private void onClick(){ 
    System.out.println("Click!"); 
    } 

    private void onDown(){ 
    System.out.println("Down!"); 
    } 

    private void onUp(){ 
    System.out.println("Up"); 
    } 
} 

AudioInputButtonTester.java

public class AudioInputButtonTester { 
    public static void main (String [] args){ 
    AudioInputButton myButton = new AudioInputButton(2); 
    myButton.startListening(); 
    } 
} 

我打算将各种按钮状态变成事件,以便您可以在测试者类中为他们编写代码,但是它的凌晨2:30,如果你真的想要分离你的逻辑,创建一个新的按钮类来扩展AudioInputButton和你自己的代码。

我觉得,这个任务可以标准化或使用COM port简化。

查阅这些例子 http://www.java2s.com/Code/Java/Development-Class/COM-Port.htm http://java.sun.com/developer/Books/javaprogramming/cookbook/11.pdf

如果你想使用的音频输入插孔(做你的任务麦克风输入端口),您必须检查声音幅度的输入电平并根据亮度等级完成您的任务。对于这一点,你必须让多线程的想法和记录的声音在JAVA

+0

通过我的急躁,我终于找到了一种通过良好的旧Java来实现它的方法。我只是为了真正的乐趣而尝试这一点。 –