-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecorder.java
155 lines (127 loc) · 2.98 KB
/
Recorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.gstreamer.ElementFactory;
import org.gstreamer.Gst;
import org.gstreamer.Pipeline;
import org.gstreamer.elements.PlayBin2;
/**
* The audio recorder. Recording and playing is realized with gstreamer.
*
*/
public class Recorder {
/**
* The attached GUI element.
*/
private GUI gui;
/**
* The audio pipe for recording.
*/
private Pipeline audiopipe;
/**
* Playbin element for playing.
*/
private PlayBin2 playbin;
/**
* Flag if recording is in progress.
*/
private boolean isRecord = true;
/**
* Flag if player is muted.
*/
private boolean isMute = true;
/**
* Value for the volume.
*/
private int volume = 50;
/**
* Path were the recorded file is stored.
*/
public static String recordingsPath = "src/recordings/";
/**
* Stores the name of the file for the recording.
*/
private String filename;
/**
* Constructor.
*
* @param gui
*/
public Recorder(GUI gui) {
// TODO Auto-generated constructor stub
this.gui = gui;
}
/**
* The record function. For starting and stopping the recording the same
* function is used, execution depends on the global flag isRecord.
*/
public void record() {
if (isRecord) {
isRecord = false;
// Generates a unique filename.
String currenttime = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
filename = recordingsPath + currenttime + ".ogg";
// The pipe.
String[] rec = new String[] { "alsasrc", "!", "audioconvert", "!", "audioresample", "!", "vorbisenc", "!", "oggmux", "!",
"filesink location = " + filename };
rec = Gst.init("AudioRecorder", rec);
StringBuilder sb = new StringBuilder();
for (String s : rec) {
sb.append(' ');
sb.append(s);
}
// Start recording
audiopipe = Pipeline.launch(sb.substring(1));
audiopipe.play();
} else {
// Stop recording and add file to the list.
audiopipe.stop();
gui.addNewFiletoList(filename);
isRecord = true;
}
}
/**
* Starts the audio player. Opens the selected file in the GUI list and
* plays it with playbin.
*/
public void play() {
String[] args = new String[] { recordingsPath };
Gst.init("AudioPlayer", args);
playbin = new PlayBin2("AudioPlayer");
playbin.setVideoSink(ElementFactory.make("fakesink", "videosink"));
playbin.setInputFile(new File(gui.getSelectedPlayItem()));
playbin.play();
}
/**
* Stops the audio player.
*/
public void stop() {
playbin.stop();
}
/**
* Mutes the audio player.
*/
public void mute() {
if (isMute) {
playbin.setVolume(0);
isMute = false;
} else {
playbin.setVolume(volume);
isMute = true;
}
}
/**
* Increases the volume of the audio player.
*/
public void volumeUp() {
volume = volume + 10;
playbin.setVolumePercent(volume);
}
/**
* Decreases the volume of the audio player.
*/
public void volumeDown() {
volume = volume - 10;
playbin.setVolumePercent(volume);
}
}