Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  1. /*******************************************************************************
  2. * Copyright (c) 2010 McGill University
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Jonathan Faubert
  10. *******************************************************************************/
  11.  
  12. package ca.mcgill.cs.swevo.qualyzer.editors;
  13.  
  14. import java.io.ByteArrayInputStream;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.RandomAccessFile;
  22. import java.util.List;
  23. import java.util.Map;
  24.  
  25. //import javax.sound.sampled.AudioFormat;
  26. //import javax.sound.sampled.AudioInputStream;
  27. //import javax.sound.sampled.UnsupportedAudioFileException;
  28.  
  29. import javazoom.jlgui.basicplayer.BasicController;
  30. import javazoom.jlgui.basicplayer.BasicPlayer;
  31. import javazoom.jlgui.basicplayer.BasicPlayerEvent;
  32. import javazoom.jlgui.basicplayer.BasicPlayerException;
  33. import javazoom.jlgui.basicplayer.BasicPlayerListener;
  34. //import net.sourceforge.jaad.aac.Decoder;
  35. //import net.sourceforge.jaad.aac.SampleBuffer;
  36. //import net.sourceforge.jaad.mp4.MP4Container;
  37. //import net.sourceforge.jaad.mp4.api.Frame;
  38. //import net.sourceforge.jaad.mp4.api.Movie;
  39. //import net.sourceforge.jaad.mp4.api.Track;
  40. //import net.sourceforge.jaad.mp4.api.AudioTrack;
  41.  
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44.  
  45. import ca.mcgill.cs.swevo.qualyzer.Messages;
  46. import ca.mcgill.cs.swevo.qualyzer.QualyzerException;
  47.  
  48. /**
  49. * Contains the jlgui.BasicPlayer and handles all the actions on it.
  50. */
  51. public class AudioPlayer
  52. {
  53.  
  54. private static final int MICROSECONDS = 1000000;
  55.  
  56. private Logger fLogger;
  57.  
  58. private MyPlayer fPlayer;
  59. private long fMicroSecondsPos;
  60. private int fSecondsPos;
  61. private double fLength;
  62.  
  63. private TranscriptEditor fEditor;
  64.  
  65. private String fAudioFile;
  66. private boolean fIsMP3;
  67. private boolean fIsWAV;
  68. private boolean fIsMP4;
  69. //WAV only
  70. private long fMicSecondPosAfterSeek;
  71. private double fMicSecondsPerByte;
  72.  
  73. private File fFile;
  74. // private AudioFormat fAudioFormat;
  75. private int fByteLength;
  76. private String tempLocation;
  77. private int fFrameLength;
  78. /**
  79. * Takes the filename of the audio file and the editor that is opening it.
  80. * @param audioFile
  81. * @param editor
  82. */
  83. public AudioPlayer(String audioFile, TranscriptEditor editor)
  84. {
  85. fLogger = LoggerFactory.getLogger(AudioPlayer.class);
  86.  
  87. fPlayer = new MyPlayer();
  88. fEditor = editor;
  89. fAudioFile = audioFile;
  90.  
  91. fMicroSecondsPos = 0;
  92. fSecondsPos = 0;
  93. fLength = 0;
  94. fFrameLength = 0;
  95. fByteLength = 0;
  96. // fAudioFormat = null;
  97.  
  98. fIsMP3 = false;
  99. fIsWAV = false;
  100. fIsMP4 = false;
  101.  
  102. fPlayer.addBasicPlayerListener(createBasicPlayerListener());
  103.  
  104. fFile = new File(fAudioFile);
  105. System.out.println("file:" + fFile);
  106.  
  107. // String fileExt = fAudioFile.substring(fAudioFile.lastIndexOf('.'));
  108. // if (fileExt.equals(".mp4") || fileExt.equals(".m4a")) {
  109. // outputStream(fFile);
  110. // }
  111.  
  112. try
  113. {
  114. fPlayer.open(fFile);
  115. }
  116. catch(BasicPlayerException e)
  117. {
  118. fLogger.error("AudioPlayer: Could not open", e); //$NON-NLS-1$
  119. throw new QualyzerException(Messages.getString("editors.AudioPlayer.audioOpenFailed"), e); //$NON-NLS-1$
  120. }
  121. }
  122.  
  123. //
  124. // private void outputStream (File arg0) {
  125. // ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  126. // AudioFormat audioFormat = null;
  127. //
  128. // try {
  129. // MP4Container container = new MP4Container(new FileInputStream((File) arg0));
  130. // Movie movie = container.getMovie();
  131. // List<Track> tracks = movie.getTracks(AudioTrack.AudioCodec.AAC);
  132. // AudioTrack track = (AudioTrack) tracks.get(0);
  133. // byte[] decoderSpecificInfo = track.getDecoderSpecificInfo();
  134. // Frame frame = track.readNextFrame();
  135. // Decoder dec = new Decoder(decoderSpecificInfo);
  136. // SampleBuffer buf = new SampleBuffer();
  137. // while (track.hasMoreFrames()) {
  138. // frame = track.readNextFrame();
  139. // dec.decodeFrame(frame.getData(), buf);
  140. // outputStream.write(buf.getData());
  141. // fFrameLength =+ 1;
  142. // }
  143. // audioFormat = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
  144. // } catch (IOException e) {
  145. // // TODO Auto-generated catch block
  146. // e.printStackTrace();
  147. // }
  148. //
  149. // byte[] outputStreamByteArray = outputStream.toByteArray();
  150. //// ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStreamByteArray);
  151. //
  152. //// info in inputStream, outputStreamByteArray.length and AudioFormat
  153. // System.out.println(audioFormat);
  154. // fAudioFormat = audioFormat;
  155. // fByteLength = outputStreamByteArray.length;
  156. // System.out.println(outputStreamByteArray.length);
  157. // }
  158.  
  159. /**
  160. * Handles updates as the file plays.
  161. * @return
  162. */
  163. private BasicPlayerListener createBasicPlayerListener()
  164. {
  165. return new BasicPlayerListener(){
  166.  
  167. @SuppressWarnings("unchecked")
  168. @Override
  169. public void opened(Object arg0, Map arg1)
  170. {
  171. System.out.println("audio.type");
  172. if (!arg1.get("audio.type").equals("AAC"))
  173. {
  174. fLength = ((Integer) arg1.get("audio.length.frames")) / //$NON-NLS-1$
  175. ((Float) arg1.get("audio.framerate.fps")); //$NON-NLS-1$
  176.  
  177. double lengthMicSec = fLength * MICROSECONDS;
  178. fMicSecondsPerByte = lengthMicSec / (Integer)arg1.get("audio.length.bytes"); //$NON-NLS-1$
  179. fEditor.setLength(fLength);
  180. }
  181. // else
  182. // {
  183. // fLength = (fFrameLength) / //$NON-NLS-1$
  184. // ((Float) fAudioFormat.getFrameRate()); //$NON-NLS-1$
  185. //
  186. //
  187. // double lengthMicSec = fLength * MICROSECONDS;
  188. // fMicSecondsPerByte = lengthMicSec / fByteLength; //$NON-NLS-1$
  189. // fEditor.setLength(fLength);
  190. // }
  191.  
  192.  
  193. if(arg1.get("audio.type").equals("WAVE")) //$NON-NLS-1$ //$NON-NLS-2$
  194. {
  195. fIsWAV = true;
  196. }
  197. else if(arg1.get("audio.type").equals("MP3")) //$NON-NLS-1$ //$NON-NLS-2$
  198. {
  199. fIsMP3 = true;
  200. } else if (arg1.get("audio.type").equals("AAC"))
  201. {
  202. fIsMP4 = true;
  203. }
  204. }
  205. @SuppressWarnings("unchecked")
  206. @Override
  207. public void progress(int arg0, long arg1, byte[] arg2, Map arg3)
  208. {
  209. // arg3.get("");
  210. if(fIsMP3)
  211. {
  212. fMicroSecondsPos = Long.valueOf((Long) arg3.get("mp3.position.microseconds")); //$NON-NLS-1$
  213. }
  214. else if(fIsWAV || fIsMP4)
  215. {
  216. fMicroSecondsPos = fMicSecondPosAfterSeek + arg1;
  217. }
  218.  
  219. if(fSecondsPos != fMicroSecondsPos / MICROSECONDS)
  220. {
  221. fSecondsPos = (int) fMicroSecondsPos / MICROSECONDS;
  222. fEditor.setSeconds(fSecondsPos);
  223. }
  224. }
  225.  
  226. @Override
  227. public void setController(BasicController arg0){}
  228. @Override
  229. public void stateUpdated(BasicPlayerEvent arg0){}
  230. };
  231. }
  232.  
  233. /**
  234. * Handles the play button being pressed.
  235. */
  236. public void play()
  237. {
  238. try
  239. {
  240. if(fPlayer.getStatus() == BasicPlayer.PAUSED)
  241. {
  242. // fPlayer.play();
  243. fPlayer.resume();
  244. }
  245. else if(fPlayer.getStatus() == BasicPlayer.STOPPED || fPlayer.getStatus() == BasicPlayer.OPENED)
  246. {
  247. fPlayer.play();
  248. }
  249. }
  250. catch(BasicPlayerException e)
  251. {
  252. fLogger.error("AudioPlayer: Could not play audio", e); //$NON-NLS-1$
  253. throw new QualyzerException(Messages.getString("editors.AudioPlayer.playFailed"), e); //$NON-NLS-1$
  254. }
  255. }
  256.  
  257. /**
  258. * Handles the pause button being pressed.
  259. */
  260. public void pause()
  261. {
  262. try
  263. {
  264. System.out.println("Before: " + fPlayer.getStatus());
  265. if(fPlayer.getStatus() == BasicPlayer.PLAYING)
  266. {
  267. fPlayer.pause();
  268.  
  269. }
  270. }
  271. catch(BasicPlayerException e)
  272. {
  273. fLogger.error("AudioPlayer: Could not pause", e); //$NON-NLS-1$
  274. throw new QualyzerException(Messages.getString("editors.AudioPlayer.pauseFailed"), e); //$NON-NLS-1$
  275. }
  276. System.out.println("After: " + fPlayer.getStatus());
  277. }
  278.  
  279. /**
  280. * Handles the stop button being pressed.
  281. */
  282. public void stop()
  283. {
  284. try
  285. {
  286. fPlayer.stop();
  287. fSecondsPos = 0;
  288. fMicroSecondsPos = 0;
  289. fMicSecondPosAfterSeek = 0;
  290. }
  291. catch(BasicPlayerException e)
  292. {
  293. fLogger.error("AudioPlayer: Could not stop.", e); //$NON-NLS-1$
  294. throw new QualyzerException(Messages.getString("editors.AudioPlayer.stopFailed"), e); //$NON-NLS-1$
  295. }
  296. }
  297.  
  298. /**
  299. * Forces the file to seek to the specified time.
  300. * @param selection A time in seconds to seek to.
  301. */
  302. public void jumpToTime(int selection)
  303. {
  304. long bytes = (long) (selection * MICROSECONDS / fMicSecondsPerByte);
  305. try
  306. {
  307. fMicSecondPosAfterSeek = selection * MICROSECONDS;
  308. fPlayer.seek(bytes);
  309. fEditor.setSeconds(selection);
  310. }
  311. catch (BasicPlayerException e)
  312. {
  313. fLogger.error("Audio Player: Could not seek", e); //$NON-NLS-1$
  314. throw new QualyzerException(Messages.getString("editors.AudioPlayer.seekFailed"), e); //$NON-NLS-1$
  315. }
  316.  
  317. }
  318.  
  319. /**
  320. * Try to open a new audio file.
  321. * @param audioFile
  322. */
  323. public void open(String audioFile)
  324. {
  325. reset();
  326. fAudioFile = audioFile;
  327. File file = new File(fAudioFile);
  328. // String fileExt = fAudioFile.substring(fAudioFile.lastIndexOf('.'));
  329. // if (fileExt.equals(".mp4") || fileExt.equals(".m4a")) {
  330. // outputStream(fFile);
  331. // }
  332. try
  333. {
  334. fPlayer.open(file);
  335. }
  336. catch(BasicPlayerException e)
  337. {
  338. fLogger.error("AudioPlayer: Could not open", e); //$NON-NLS-1$
  339. throw new QualyzerException(Messages.getString("editors.AudioPlayer.cannotOpenAudio"), e); //$NON-NLS-1$
  340. }
  341. }
  342.  
  343. /**
  344. *
  345. */
  346. private void reset()
  347. {
  348. fMicroSecondsPos = 0;
  349. fSecondsPos = 0;
  350. fLength = 0;
  351. fAudioFile = ""; //$NON-NLS-1$
  352. fIsMP3 = false;
  353. fIsWAV = false;
  354. fIsMP4 = false;
  355. fMicSecondPosAfterSeek = 0;
  356. fMicSecondsPerByte = 0;
  357. fFrameLength = 0;
  358. fByteLength = 0;
  359. // fAudioFormat = null;
  360. }
  361.  
  362. /**
  363. * The audio player is closing so stop the file from playing and close the stream.
  364. */
  365. public void close()
  366. {
  367.  
  368. try
  369. {
  370. fPlayer.stop();
  371. }
  372. catch (BasicPlayerException e1)
  373. {
  374. //Just keep on going.
  375. //System.out.println("ate stop exception");
  376. }
  377.  
  378. fPlayer.closeStream();
  379. }
  380.  
  381. /**
  382. * This gives me access to the close stream method.
  383. *
  384. */
  385. private class MyPlayer extends BasicPlayer
  386. {
  387. /* (non-Javadoc)
  388. * @see javazoom.jlgui.basicplayer.BasicPlayer#closeStream()
  389. */
  390. @Override
  391. public void closeStream()
  392. {
  393. super.closeStream();
  394. }
  395. }
  396.  
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement