Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.98 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. flat zeros being output every sample when trying to get data from a microphone
  2. // AudioFormat parameters
  3. public  static final int     SAMPLE_RATE = 22050;
  4. private static final int     SAMPLE_SIZE = 16;
  5. private static final int     CHANNELS = 1;
  6. private static final boolean SIGNED = true;
  7. private static final boolean BIG_ENDIAN = true;
  8. // Chunk of audio processed at one time
  9. public static final int BUFFER_SIZE = 1000;
  10. public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
  11.        
  12. public class VoiceProvider implements ISynthProvider{
  13.     private TargetDataLine line;
  14.     public VoiceProvider(){
  15.         DataLine.Info info = new DataLine.Info(TargetDataLine.class,
  16.             Player.format); // format is an AudioFormat object
  17.         if (!AudioSystem.isLineSupported(info)) {
  18.             System.out.println("o_0 line not supported!");
  19.  
  20.         }
  21.  
  22.         // Obtain and open the line.
  23.         try {
  24.             line = (TargetDataLine) AudioSystem.getLine(info);
  25.             line.start();
  26.             line.open(Player.format);
  27.  
  28.         } catch (LineUnavailableException ex) {
  29.             // Handle the error ...
  30.         }
  31.  
  32.     }
  33.     @Override
  34.     public double getSample() {
  35.         byte[] a = {0,0};
  36.         if (line.isOpen()){
  37.             //System.out.println("0_0");
  38.             line.read(a, 0, 2);
  39.         }else{
  40.             //System.out.println("o_0");
  41.         }
  42.         System.out.println(Arrays.toString(a));
  43.         return readShort(a,0);
  44.  
  45.     }
  46.     public static short readShort(byte[] data, int offset) {
  47.     return (short) (((data[offset] << 8)) | ((data[offset + 1] & 0xff)));
  48.     }
  49.        
  50. try {
  51.         line = (TargetDataLine) AudioSystem.getLine(info);
  52.         line.start();
  53.         line.open(Player.format);
  54.  
  55.     } catch (LineUnavailableException ex) {
  56.         // Handle the error ...
  57.     }
  58.        
  59. try {
  60.         line = (TargetDataLine) AudioSystem.getLine(info);
  61.         line.open(Player.format);
  62.         line.start();
  63.  
  64.     } catch (LineUnavailableException ex) {
  65.         // Handle the error ...
  66.     }