KillianMills

MediaPlayer.java

Oct 1st, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. import javax.sound.sampled.*;
  2. import java.io.*;
  3.  
  4. class BoundedBuffer {
  5.  
  6.     private int nextIn; // will hold the value of the tail.next of the queue/  the next free slot
  7.     private int nextOut; // will hold the value of the head of the queue/ the next slot to be taken out
  8.    
  9.     private int size; // for holding the buffer of 10 seconds of the song at a time, 441000 for 44100 bytes * 10 seconds
  10.     private int occupied = 0; // will tell us if the buffer is full or not/ the current size
  11.    
  12.     private int ins; // hold value of all values inputted, ins - outs should = 0 at the end
  13.     private int outs; // hold the value of all values outputted, outs - ins should be = 0 at the end
  14.    
  15.     private boolean dataAvailable = false; // true if data is available for the producer
  16.     private boolean roomAvailable = true; // true if data is available for the consumer
  17.    
  18.     private SourceDataLine line;
  19.    
  20.     private AudioInputStream s;
  21.     private int bytesRead;
  22.    
  23.     private byte[] audioChunk; // Buffer of length size(441000)
  24.    
  25.     //Constructor
  26.     public BoundedBuffer(SourceDataLine l, AudioInputStream stream, int sec) {
  27.  
  28.         line = l;
  29.         s = stream;
  30.         size = sec;
  31.         audioChunk = new byte[size];
  32.     }
  33.    
  34.     // Producer's Method *******************************
  35.     public synchronized void insertChunk(){ // need to change return type to boolean for the end
  36.         try{
  37.             while(!roomAvailable){ // go to sleep while there is a full buffer
  38.                 wait();
  39.             }
  40.         }
  41.         catch (InterruptedException e) { System.out.println("Error in insertChunk1"); }
  42.        
  43.         try{
  44.             roomAvailable = true;
  45.        
  46.             bytesRead = s.read(audioChunk, nextIn , size/10); // nextIn, next available space, 44100 bytes
  47.             nextIn = (nextIn + bytesRead)%size;
  48.            
  49.             occupied = occupied + bytesRead; // records the currect size of the buffer
  50.             ins++; // records the size of all chunks inputted
  51.             dataAvailable = true;
  52.            
  53.             if(size==occupied){
  54.                 roomAvailable = false;
  55.             }
  56.            
  57.             System.out.println("Chunk Inserted!");
  58.             notifyAll(); // notifys all threads synchronized
  59.         }
  60.         catch (IOException e) { System.out.println("Error in insertChunk2"); }
  61.     }
  62.    
  63.     // Consumer's Method *******************************
  64.     public synchronized void removeChunk(){ // need to change return type to boolean for the end
  65.         try{
  66.             while(!dataAvailable){ // go to sleep while there is no data on the buffer
  67.                 wait();
  68.             }
  69.         }
  70.         catch (InterruptedException e) { System.out.println("Error in removeChunk"); }
  71.        
  72.         dataAvailable = true;
  73.         line.write(audioChunk, nextOut, bytesRead);
  74.         nextOut = (nextOut + bytesRead)%size;
  75.        
  76.         occupied = occupied - bytesRead; // records the current size of the buffer
  77.         outs++; // records the size of all chunks outputted
  78.         roomAvailable = true;
  79.        
  80.         if(occupied==0){
  81.                 dataAvailable = false;
  82.         }      
  83.        
  84.         System.out.println("Chunk Removed!");
  85.         notifyAll(); // norifys all threads synchronized
  86.     }
  87. }
  88.  
  89. // Takes bytes from the audio file and places them onto the buffer
  90. class Producer extends Thread {
  91.  
  92.     //Our thread works on this
  93.     public BoundedBuffer buff;
  94.    
  95.     //Constructor
  96.     public Producer(BoundedBuffer buff){
  97.         this.buff = buff;
  98.     }
  99.  
  100.     //What our thread does
  101.     public void run(){
  102.         while (true){ // true must be changed later
  103.             buff.insertChunk();
  104.         }
  105.     }
  106. }
  107.  
  108. //Takes bytes from the buffer and places them into the media player
  109. class Consumer extends Thread {
  110.  
  111.     //Our thread works on this
  112.     public BoundedBuffer buff;
  113.  
  114.     //Constructor
  115.     public Consumer(BoundedBuffer buff){
  116.         this.buff = buff;
  117.     }
  118.    
  119.     //What our thread does
  120.     public void run(){
  121.         while (true){ // true must be changed later
  122.             buff.removeChunk();
  123.         }
  124.     }
  125. }
  126.  
  127. class MediaPlayer{
  128.  
  129.     private static AudioInputStream validate(String[] args) throws UnsupportedAudioFileException, IOException {
  130.         if (args.length != 1) {
  131.             System.out.println("Please supply an audio file");
  132.             System.exit(1);
  133.         }
  134.         return (AudioSystem.getAudioInputStream(new File(args[0])));
  135.     }
  136.  
  137.     public static void main(String[] args) throws UnsupportedAudioFileException,
  138.             IOException, LineUnavailableException {
  139.  
  140.         AudioInputStream s = validate(args);
  141.         AudioFormat format = s.getFormat();    
  142.         System.out.println("Audio format: " + format.toString());
  143.                
  144.         DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
  145.         if (!AudioSystem.isLineSupported(info)) {
  146.             System.out.println("Cannot handle that audio format");
  147.             System.exit(1);
  148.         }
  149.        
  150.         int oneSecond = (int) (format.getChannels() * format.getSampleRate() *
  151.             format.getSampleSizeInBits() /8);
  152.            
  153.         //byte[] audioChunk = new byte[oneSecond];
  154.        
  155.         SourceDataLine line;
  156.         line = (SourceDataLine) AudioSystem.getLine(info);
  157.         line.open(format);
  158.         line.start();
  159.        
  160.         //Create our BoundedBuffer
  161.         BoundedBuffer buff = new BoundedBuffer(line, s, oneSecond*10);
  162.        
  163.         //Create our Producer
  164.         Producer pthread = new Producer(buff);
  165.        
  166.         //Create our Consumer
  167.         Consumer cthread = new Consumer(buff);
  168.        
  169.         //start our threads
  170.         System.out.println("Preparing to start Producer and Consumer threads.");
  171.         pthread.start();
  172.         cthread.start();
  173.        
  174.         //line.drain();
  175.         //line.stop();
  176.         //line.close();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment