Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jmx/audio/io/RTIn.java


1   /*
2   
3   <This Java Class is part of the jMusic API version 1.0,Sun Feb 25 18:34:02  2001
4   
5   Copyright (C) 2000 Andrew Sorensen & Andrew Brown
6   
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or any
10  later version.
11  
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16  
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  
21  */
22  
23  package jmx.audio.io;
24  
25  import java.io.*;
26  import jm.music.data.Note;
27  import jm.audio.AudioObject;
28  import jm.audio.Instrument;
29  import jm.audio.AOException;
30  import javax.sound.sampled.*;
31  
32  /**
33   * Real Time audio input
34   * @author Andrew Sorensen 
35   * @version 1.0,Sun Feb 25 18:42:41  2001
36   */
37  public final class RTIn extends AudioObject{
38    //----------------------------------------------
39    // ATTRIBUTES
40    //----------------------------------------------
41    /** have we reached the end of the audio file */
42    public boolean finished = false;
43    /** the size of the holding buffer used by the TargetSource */
44    private int bufsize;
45    /** the audio input data source */
46    private TargetDataLine dline;
47    /** have we started running yet ? */
48    private boolean started = false;
49  
50    //----------------------------------------------
51    // Constructors
52    //----------------------------------------------
53    /**
54     */
55    public RTIn(Instrument inst,int sampleRate, int channels, int bufsize){
56      super(inst, sampleRate, "[RTIn]");
57      this.sampleRate = sampleRate;
58      this.channels = channels;
59      this.bufsize = bufsize;
60      this.init();
61    }
62  
63    //----------------------------------------------
64    // Public methods
65    //----------------------------------------------
66    /**
67     * @param input bogus input here to fit in.
68     */
69    public int work(float[] buffer)throws AOException{
70      if(!started){this.dline.start();started=true;}
71      int ret = 0;
72      int bc = 0; //byte counter
73      int amount = buffer.length*2;
74      byte[] data = new byte[amount];
75      dline.read(data,0,amount);
76      for(;ret<buffer.length;ret++){
77        short input=(short)((data[bc++]<<8)+(data[bc++]));
78        buffer[ret]=(float)((float)input/
79              (float)Short.MAX_VALUE);
80      }
81      return ret;
82    }
83  
84    public void init(){
85      AudioFormat af = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED
86  , (float)this.sampleRate,16, this.channels,this.channels*2, this.sampleRate,true);
87                  DataLine.Info info = new DataLine.Info(TargetDataLine.class,af);
88                  System.out.println("Setting for audio line: "+info);
89                  if(!AudioSystem.isLineSupported(info)){
90                          System.out.println(info);
91                          System.err.println("JMF Line not supported ... exiting .. sothere");
92                          System.exit(1);
93                  }
94                  try{
95                          this.dline = (TargetDataLine)AudioSystem.getLine(info);
96                          //multiply buffersize by 2 because this is bytes not shorts
97                          this.dline.open(af,bufsize*2);
98                  }catch(Exception e){e.printStackTrace();}
99    }
100 }