Adjusting back into the original lengthy version of IrrigationController; for testing...
[iot2.git] / benchmarks / Java / SpeakerController / MP3Decoder.java
1 package SpeakerController;
2
3 import javazoom.jl.player.advanced.*;
4 import javazoom.jl.player.*;
5
6 // Standard Java Packages
7 import java.util.concurrent.atomic.AtomicBoolean;
8 import java.util.List;
9 import java.util.LinkedList;
10 import java.io.FileInputStream;
11
12 /** Class MP3Decoder for the smart home application benchmark
13  *  <p>
14  *  This class decodes mp3 files into raw pcm data
15  *
16  * @author      Ali Younis <ayounis @ uci.edu>
17  * @version     1.0                
18  * @since       2016-05-01
19  */
20 public class MP3Decoder extends AudioDeviceBase {
21
22     private class PlaybackList extends PlaybackListener {
23         private MP3Decoder decoder;
24         public PlaybackList(MP3Decoder _d) {
25             decoder = _d;
26         }
27
28         public void playbackFinished(PlaybackEvent evt) {
29             decoder.decodeDone();
30         }
31
32         public void playbackStarted(PlaybackEvent evt) {
33             // do nothing
34         }
35     }
36
37     private String fileName = "";
38     private LinkedList<short[]> audioLinkedList = new LinkedList<short[]>();
39     private AtomicBoolean decodeDone = new AtomicBoolean();
40     private long dataLength = 0;
41
42     public MP3Decoder(String _fileName) {
43         fileName = _fileName;
44         decodeDone.set(false);
45
46         PlaybackList pl = new PlaybackList(this);
47
48         try {
49             AdvancedPlayer ap = new AdvancedPlayer(new FileInputStream(fileName), this);
50             ap.setPlayBackListener(pl);
51             ap.play();
52
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57
58     public List<short[]> getDecodedFrames() {
59
60         while (decodeDone.get() == false) {
61             // just block until done
62         }
63
64         return audioLinkedList;
65     }
66
67     public long getAudioFrameLength() {
68         // stereo
69         return dataLength / 2;
70     }
71
72     protected void decodeDone() {
73         decodeDone.set(true);
74     }
75
76     public int getPosition() {
77         // not used, just needed for AdvancedPlayer to work.
78         return 0;
79     }
80
81     protected void writeImpl(short[] _samples, int _offs, int _len) {
82         short[] sample = new short[_len];
83         int j = _offs;
84         for (int i = 0; i < _len; i++, j++) {
85             sample[i] = _samples[j];
86         }
87         synchronized (audioLinkedList) {
88             audioLinkedList.addLast(sample);
89         }
90
91         dataLength += (long)_len;
92     }
93
94 }
95
96
97
98
99
100
101
102
103
104