Repository navigation
Expand file tree
/
Copy pathSound.java
More file actions
143 lines (124 loc) · 4.18 KB
/
Copy pathSound.java
File metadata and controls
143 lines (124 loc) · 4.18 KB
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
package ev3dev.actuators;
import ev3dev.hardware.EV3DevDevice;
import ev3dev.hardware.EV3DevPlatform;
import ev3dev.utils.Shell;
import ev3dev.utils.Sysfs;
import lejos.utility.Delay;
import org.slf4j.Logger;
import java.io.File;
/**
* Class that provides access methods for the local audio device
*
* The class is implemented as Singleton.
*
* Note: Only tested with EV3Brick
*
* @author Juan Antonio Breña Moral
*
*/
public class Sound extends EV3DevDevice {
private static final Logger log = org.slf4j.LoggerFactory.getLogger(Sound.class);
private final static String SOUND_PATH = "/sys/devices/platform/snd-legoev3/";
private final static String CMD_BEEP = "beep";
private final static String CMD_APLAY ="aplay";
private final static String VOLUME = "volume";
private final static String VOLUME_PATH = SOUND_PATH + VOLUME;
private final static String DISABLED_FEATURE_MESSAGE = "This feature is disabpled for this platform.";
private static Sound Instance;
/**
* Return a Instance of Sound.
*
* @return A Sound instance
*/
public static Sound getInstance() {
if (Instance == null) {
Instance = new Sound();
}
return Instance;
}
// Prevent duplicate objects
private Sound() {
}
/**
* Beeps once.
*/
public void beep() {
if(this.getPlatform().equals(EV3DevPlatform.EV3BRICK)){
log.debug(CMD_BEEP);
Shell.execute(CMD_BEEP);
Delay.msDelay(100);
} else {
log.debug(DISABLED_FEATURE_MESSAGE);
}
}
/**
* Beeps twice.
*/
public void twoBeeps() {
if(this.getPlatform().equals(EV3DevPlatform.EV3BRICK)){
beep();
beep();
} else {
log.debug(DISABLED_FEATURE_MESSAGE);
}
}
/**
* Plays a tone, given its frequency and duration.
* @param frequency The frequency of the tone in Hertz (Hz).
* @param duration The duration of the tone, in milliseconds.
* @param volume The volume of the playback 100 corresponds to 100%
*/
public void playTone(final int frequency, final int duration, final int volume) {
if(this.getPlatform().equals(EV3DevPlatform.EV3BRICK)){
this.setVolume(volume);
this.playTone(frequency, duration);
} else {
log.debug(DISABLED_FEATURE_MESSAGE);
}
}
/**
* Plays a tone, given its frequency and duration.
* @param frequency The frequency of the tone in Hertz (Hz).
* @param duration The duration of the tone, in milliseconds.
*/
public void playTone(final int frequency, final int duration) {
if(this.getPlatform().equals(EV3DevPlatform.EV3BRICK)) {
final String cmdTone = CMD_BEEP + " -f " + frequency + " -l " + duration;
Shell.execute(cmdTone);
} else {
log.debug(DISABLED_FEATURE_MESSAGE);
}
}
/**
* Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.
* @param file the 8-bit or 16-bit PWM (WAV) sample file
* @param volume the volume percentage 0 - 100
*/
public void playSample(final File file, final int volume) {
this.setVolume(volume);
Shell.execute(CMD_APLAY + " " + file.toString());
}
/**
* Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.
* @param file the 8-bit or 16-bit PWM (WAV) sample file
*/
public void playSample(final File file) {
Shell.execute(CMD_APLAY + " " + file.toString());
}
/**
* Set the master volume level
* @param volume 0-100
*/
public void setVolume(final int volume) {
//TODO Review to move to this.setIntegerAttribute();
Sysfs.writeString(VOLUME_PATH,"" + volume);
}
/**
* Get the current master volume level
* @return the current master volume 0-100
*/
public int getVolume() {
//TODO Review to move to this.getIntegerAttribute()
return Sysfs.readInteger(VOLUME_PATH);
}
}