Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.sound.sampled.*;
- import java.io.*;
- class BoundedBuffer {
- private int nextIn; // will hold the value of the tail.next of the queue/ the next free slot
- private int nextOut; // will hold the value of the head of the queue/ the next slot to be taken out
- private int size; // for holding the buffer of 10 seconds of the song at a time, 441000 for 44100 bytes * 10 seconds
- private int occupied = 0; // will tell us if the buffer is full or not/ the current size
- private int ins; // hold value of all values inputted, ins - outs should = 0 at the end
- private int outs; // hold the value of all values outputted, outs - ins should be = 0 at the end
- private boolean dataAvailable = false; // true if data is available for the producer
- private boolean roomAvailable = true; // true if data is available for the consumer
- private SourceDataLine line;
- private AudioInputStream s;
- private int bytesRead;
- private byte[] audioChunk; // Buffer of length size(441000)
- //Constructor
- public BoundedBuffer(SourceDataLine l, AudioInputStream stream, int sec) {
- line = l;
- s = stream;
- size = sec;
- audioChunk = new byte[size];
- }
- // Producer's Method *******************************
- public synchronized void insertChunk(){ // need to change return type to boolean for the end
- try{
- while(!roomAvailable){ // go to sleep while there is a full buffer
- wait();
- }
- }
- catch (InterruptedException e) { System.out.println("Error in insertChunk1"); }
- try{
- roomAvailable = true;
- bytesRead = s.read(audioChunk, nextIn , size/10); // nextIn, next available space, 44100 bytes
- nextIn = (nextIn + bytesRead)%size;
- occupied = occupied + bytesRead; // records the currect size of the buffer
- ins++; // records the size of all chunks inputted
- dataAvailable = true;
- if(size==occupied){
- roomAvailable = false;
- }
- System.out.println("Chunk Inserted!");
- notifyAll(); // notifys all threads synchronized
- }
- catch (IOException e) { System.out.println("Error in insertChunk2"); }
- }
- // Consumer's Method *******************************
- public synchronized void removeChunk(){ // need to change return type to boolean for the end
- try{
- while(!dataAvailable){ // go to sleep while there is no data on the buffer
- wait();
- }
- }
- catch (InterruptedException e) { System.out.println("Error in removeChunk"); }
- dataAvailable = true;
- line.write(audioChunk, nextOut, bytesRead);
- nextOut = (nextOut + bytesRead)%size;
- occupied = occupied - bytesRead; // records the current size of the buffer
- outs++; // records the size of all chunks outputted
- roomAvailable = true;
- if(occupied==0){
- dataAvailable = false;
- }
- System.out.println("Chunk Removed!");
- notifyAll(); // norifys all threads synchronized
- }
- }
- // Takes bytes from the audio file and places them onto the buffer
- class Producer extends Thread {
- //Our thread works on this
- public BoundedBuffer buff;
- //Constructor
- public Producer(BoundedBuffer buff){
- this.buff = buff;
- }
- //What our thread does
- public void run(){
- while (true){ // true must be changed later
- buff.insertChunk();
- }
- }
- }
- //Takes bytes from the buffer and places them into the media player
- class Consumer extends Thread {
- //Our thread works on this
- public BoundedBuffer buff;
- //Constructor
- public Consumer(BoundedBuffer buff){
- this.buff = buff;
- }
- //What our thread does
- public void run(){
- while (true){ // true must be changed later
- buff.removeChunk();
- }
- }
- }
- class MediaPlayer{
- private static AudioInputStream validate(String[] args) throws UnsupportedAudioFileException, IOException {
- if (args.length != 1) {
- System.out.println("Please supply an audio file");
- System.exit(1);
- }
- return (AudioSystem.getAudioInputStream(new File(args[0])));
- }
- public static void main(String[] args) throws UnsupportedAudioFileException,
- IOException, LineUnavailableException {
- AudioInputStream s = validate(args);
- AudioFormat format = s.getFormat();
- System.out.println("Audio format: " + format.toString());
- DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
- if (!AudioSystem.isLineSupported(info)) {
- System.out.println("Cannot handle that audio format");
- System.exit(1);
- }
- int oneSecond = (int) (format.getChannels() * format.getSampleRate() *
- format.getSampleSizeInBits() /8);
- //byte[] audioChunk = new byte[oneSecond];
- SourceDataLine line;
- line = (SourceDataLine) AudioSystem.getLine(info);
- line.open(format);
- line.start();
- //Create our BoundedBuffer
- BoundedBuffer buff = new BoundedBuffer(line, s, oneSecond*10);
- //Create our Producer
- Producer pthread = new Producer(buff);
- //Create our Consumer
- Consumer cthread = new Consumer(buff);
- //start our threads
- System.out.println("Preparing to start Producer and Consumer threads.");
- pthread.start();
- cthread.start();
- //line.drain();
- //line.stop();
- //line.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment