Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * Copyright (c) 2010 McGill University
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Jonathan Faubert
- *******************************************************************************/
- package ca.mcgill.cs.swevo.qualyzer.editors;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.util.List;
- import java.util.Map;
- //import javax.sound.sampled.AudioFormat;
- //import javax.sound.sampled.AudioInputStream;
- //import javax.sound.sampled.UnsupportedAudioFileException;
- import javazoom.jlgui.basicplayer.BasicController;
- import javazoom.jlgui.basicplayer.BasicPlayer;
- import javazoom.jlgui.basicplayer.BasicPlayerEvent;
- import javazoom.jlgui.basicplayer.BasicPlayerException;
- import javazoom.jlgui.basicplayer.BasicPlayerListener;
- //import net.sourceforge.jaad.aac.Decoder;
- //import net.sourceforge.jaad.aac.SampleBuffer;
- //import net.sourceforge.jaad.mp4.MP4Container;
- //import net.sourceforge.jaad.mp4.api.Frame;
- //import net.sourceforge.jaad.mp4.api.Movie;
- //import net.sourceforge.jaad.mp4.api.Track;
- //import net.sourceforge.jaad.mp4.api.AudioTrack;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import ca.mcgill.cs.swevo.qualyzer.Messages;
- import ca.mcgill.cs.swevo.qualyzer.QualyzerException;
- /**
- * Contains the jlgui.BasicPlayer and handles all the actions on it.
- */
- public class AudioPlayer
- {
- private static final int MICROSECONDS = 1000000;
- private Logger fLogger;
- private MyPlayer fPlayer;
- private long fMicroSecondsPos;
- private int fSecondsPos;
- private double fLength;
- private TranscriptEditor fEditor;
- private String fAudioFile;
- private boolean fIsMP3;
- private boolean fIsWAV;
- private boolean fIsMP4;
- //WAV only
- private long fMicSecondPosAfterSeek;
- private double fMicSecondsPerByte;
- private File fFile;
- // private AudioFormat fAudioFormat;
- private int fByteLength;
- private String tempLocation;
- private int fFrameLength;
- /**
- * Takes the filename of the audio file and the editor that is opening it.
- * @param audioFile
- * @param editor
- */
- public AudioPlayer(String audioFile, TranscriptEditor editor)
- {
- fLogger = LoggerFactory.getLogger(AudioPlayer.class);
- fPlayer = new MyPlayer();
- fEditor = editor;
- fAudioFile = audioFile;
- fMicroSecondsPos = 0;
- fSecondsPos = 0;
- fLength = 0;
- fFrameLength = 0;
- fByteLength = 0;
- // fAudioFormat = null;
- fIsMP3 = false;
- fIsWAV = false;
- fIsMP4 = false;
- fPlayer.addBasicPlayerListener(createBasicPlayerListener());
- fFile = new File(fAudioFile);
- System.out.println("file:" + fFile);
- // String fileExt = fAudioFile.substring(fAudioFile.lastIndexOf('.'));
- // if (fileExt.equals(".mp4") || fileExt.equals(".m4a")) {
- // outputStream(fFile);
- // }
- try
- {
- fPlayer.open(fFile);
- }
- catch(BasicPlayerException e)
- {
- fLogger.error("AudioPlayer: Could not open", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.audioOpenFailed"), e); //$NON-NLS-1$
- }
- }
- //
- // private void outputStream (File arg0) {
- // ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- // AudioFormat audioFormat = null;
- //
- // try {
- // MP4Container container = new MP4Container(new FileInputStream((File) arg0));
- // Movie movie = container.getMovie();
- // List<Track> tracks = movie.getTracks(AudioTrack.AudioCodec.AAC);
- // AudioTrack track = (AudioTrack) tracks.get(0);
- // byte[] decoderSpecificInfo = track.getDecoderSpecificInfo();
- // Frame frame = track.readNextFrame();
- // Decoder dec = new Decoder(decoderSpecificInfo);
- // SampleBuffer buf = new SampleBuffer();
- // while (track.hasMoreFrames()) {
- // frame = track.readNextFrame();
- // dec.decodeFrame(frame.getData(), buf);
- // outputStream.write(buf.getData());
- // fFrameLength =+ 1;
- // }
- // audioFormat = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
- // } catch (IOException e) {
- // // TODO Auto-generated catch block
- // e.printStackTrace();
- // }
- //
- // byte[] outputStreamByteArray = outputStream.toByteArray();
- //// ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStreamByteArray);
- //
- //// info in inputStream, outputStreamByteArray.length and AudioFormat
- // System.out.println(audioFormat);
- // fAudioFormat = audioFormat;
- // fByteLength = outputStreamByteArray.length;
- // System.out.println(outputStreamByteArray.length);
- // }
- /**
- * Handles updates as the file plays.
- * @return
- */
- private BasicPlayerListener createBasicPlayerListener()
- {
- return new BasicPlayerListener(){
- @SuppressWarnings("unchecked")
- @Override
- public void opened(Object arg0, Map arg1)
- {
- System.out.println("audio.type");
- if (!arg1.get("audio.type").equals("AAC"))
- {
- fLength = ((Integer) arg1.get("audio.length.frames")) / //$NON-NLS-1$
- ((Float) arg1.get("audio.framerate.fps")); //$NON-NLS-1$
- double lengthMicSec = fLength * MICROSECONDS;
- fMicSecondsPerByte = lengthMicSec / (Integer)arg1.get("audio.length.bytes"); //$NON-NLS-1$
- fEditor.setLength(fLength);
- }
- // else
- // {
- // fLength = (fFrameLength) / //$NON-NLS-1$
- // ((Float) fAudioFormat.getFrameRate()); //$NON-NLS-1$
- //
- //
- // double lengthMicSec = fLength * MICROSECONDS;
- // fMicSecondsPerByte = lengthMicSec / fByteLength; //$NON-NLS-1$
- // fEditor.setLength(fLength);
- // }
- if(arg1.get("audio.type").equals("WAVE")) //$NON-NLS-1$ //$NON-NLS-2$
- {
- fIsWAV = true;
- }
- else if(arg1.get("audio.type").equals("MP3")) //$NON-NLS-1$ //$NON-NLS-2$
- {
- fIsMP3 = true;
- } else if (arg1.get("audio.type").equals("AAC"))
- {
- fIsMP4 = true;
- }
- }
- @SuppressWarnings("unchecked")
- @Override
- public void progress(int arg0, long arg1, byte[] arg2, Map arg3)
- {
- // arg3.get("");
- if(fIsMP3)
- {
- fMicroSecondsPos = Long.valueOf((Long) arg3.get("mp3.position.microseconds")); //$NON-NLS-1$
- }
- else if(fIsWAV || fIsMP4)
- {
- fMicroSecondsPos = fMicSecondPosAfterSeek + arg1;
- }
- if(fSecondsPos != fMicroSecondsPos / MICROSECONDS)
- {
- fSecondsPos = (int) fMicroSecondsPos / MICROSECONDS;
- fEditor.setSeconds(fSecondsPos);
- }
- }
- @Override
- public void setController(BasicController arg0){}
- @Override
- public void stateUpdated(BasicPlayerEvent arg0){}
- };
- }
- /**
- * Handles the play button being pressed.
- */
- public void play()
- {
- try
- {
- if(fPlayer.getStatus() == BasicPlayer.PAUSED)
- {
- // fPlayer.play();
- fPlayer.resume();
- }
- else if(fPlayer.getStatus() == BasicPlayer.STOPPED || fPlayer.getStatus() == BasicPlayer.OPENED)
- {
- fPlayer.play();
- }
- }
- catch(BasicPlayerException e)
- {
- fLogger.error("AudioPlayer: Could not play audio", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.playFailed"), e); //$NON-NLS-1$
- }
- }
- /**
- * Handles the pause button being pressed.
- */
- public void pause()
- {
- try
- {
- System.out.println("Before: " + fPlayer.getStatus());
- if(fPlayer.getStatus() == BasicPlayer.PLAYING)
- {
- fPlayer.pause();
- }
- }
- catch(BasicPlayerException e)
- {
- fLogger.error("AudioPlayer: Could not pause", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.pauseFailed"), e); //$NON-NLS-1$
- }
- System.out.println("After: " + fPlayer.getStatus());
- }
- /**
- * Handles the stop button being pressed.
- */
- public void stop()
- {
- try
- {
- fPlayer.stop();
- fSecondsPos = 0;
- fMicroSecondsPos = 0;
- fMicSecondPosAfterSeek = 0;
- }
- catch(BasicPlayerException e)
- {
- fLogger.error("AudioPlayer: Could not stop.", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.stopFailed"), e); //$NON-NLS-1$
- }
- }
- /**
- * Forces the file to seek to the specified time.
- * @param selection A time in seconds to seek to.
- */
- public void jumpToTime(int selection)
- {
- long bytes = (long) (selection * MICROSECONDS / fMicSecondsPerByte);
- try
- {
- fMicSecondPosAfterSeek = selection * MICROSECONDS;
- fPlayer.seek(bytes);
- fEditor.setSeconds(selection);
- }
- catch (BasicPlayerException e)
- {
- fLogger.error("Audio Player: Could not seek", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.seekFailed"), e); //$NON-NLS-1$
- }
- }
- /**
- * Try to open a new audio file.
- * @param audioFile
- */
- public void open(String audioFile)
- {
- reset();
- fAudioFile = audioFile;
- File file = new File(fAudioFile);
- // String fileExt = fAudioFile.substring(fAudioFile.lastIndexOf('.'));
- // if (fileExt.equals(".mp4") || fileExt.equals(".m4a")) {
- // outputStream(fFile);
- // }
- try
- {
- fPlayer.open(file);
- }
- catch(BasicPlayerException e)
- {
- fLogger.error("AudioPlayer: Could not open", e); //$NON-NLS-1$
- throw new QualyzerException(Messages.getString("editors.AudioPlayer.cannotOpenAudio"), e); //$NON-NLS-1$
- }
- }
- /**
- *
- */
- private void reset()
- {
- fMicroSecondsPos = 0;
- fSecondsPos = 0;
- fLength = 0;
- fAudioFile = ""; //$NON-NLS-1$
- fIsMP3 = false;
- fIsWAV = false;
- fIsMP4 = false;
- fMicSecondPosAfterSeek = 0;
- fMicSecondsPerByte = 0;
- fFrameLength = 0;
- fByteLength = 0;
- // fAudioFormat = null;
- }
- /**
- * The audio player is closing so stop the file from playing and close the stream.
- */
- public void close()
- {
- try
- {
- fPlayer.stop();
- }
- catch (BasicPlayerException e1)
- {
- //Just keep on going.
- //System.out.println("ate stop exception");
- }
- fPlayer.closeStream();
- }
- /**
- * This gives me access to the close stream method.
- *
- */
- private class MyPlayer extends BasicPlayer
- {
- /* (non-Javadoc)
- * @see javazoom.jlgui.basicplayer.BasicPlayer#closeStream()
- */
- @Override
- public void closeStream()
- {
- super.closeStream();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement