Skip to content

Commit 08071d5

Browse files
committed
added read from mic and change brigthness from sound level
1 parent 03629aa commit 08071d5

File tree

3 files changed

+125
-33
lines changed

3 files changed

+125
-33
lines changed

.idea/modules.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HueTestApp.iml PhilipsHueApp.iml

File renamed without changes.

src/com/CMarker/Controller.java

+124-32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.philips.lighting.hue.sdk.utilities.PHUtilities;
88
import com.philips.lighting.model.*;
99

10+
import javax.sound.sampled.*;
1011
import java.util.List;
1112
import java.util.Random;
1213
import java.util.Scanner;
@@ -20,6 +21,7 @@
2021
public class Controller {
2122

2223
//TODO make bridge a field!
24+
private PHBridge connectedBridge;
2325
private PHHueSDK phHueSDK;
2426
private Controller instance;
2527

@@ -54,7 +56,7 @@ private void listAllLights() {
5456

5557
if (choice <= allLights.size()) {
5658

57-
selectedLightMenu((allLights.get(choice - 1)), bridge);
59+
selectedLightMenu((allLights.get(choice - 1)));
5860

5961
} else {
6062

@@ -64,13 +66,13 @@ private void listAllLights() {
6466
}
6567

6668

67-
private void selectedLightMenu(PHLight light, PHBridge bridge) {
69+
private void selectedLightMenu(PHLight light) {
6870

6971
System.out.println("WHAT DO YOU WANT TO DO?");
7072
System.out.println("1: Change brightness");
7173
System.out.println("2: Change power state");
7274

73-
if (light.getLightType() == CT_COLOR_LIGHT || light.getLightType() == COLOR_LIGHT ){
75+
if (light.getLightType() == CT_COLOR_LIGHT || light.getLightType() == COLOR_LIGHT) {
7476
System.out.println("3: Change color of the light");
7577
}
7678

@@ -83,15 +85,19 @@ private void selectedLightMenu(PHLight light, PHBridge bridge) {
8385
switch (choice) {
8486

8587
case 1:
86-
changeBrightness(light, bridge);
88+
changeBrightness(light);
8789
break;
8890

89-
case 2:
90-
changePowerState(light, bridge);
91+
case 2:
92+
changePowerState(light);
9193
break;
9294

9395
case 3:
94-
changeColor(light, bridge);
96+
changeColor(light);
97+
break;
98+
99+
case 4:
100+
changeBrightnessBasedOnMicIn(light);
95101
break;
96102

97103
case 9:
@@ -100,19 +106,21 @@ private void selectedLightMenu(PHLight light, PHBridge bridge) {
100106

101107
default:
102108
System.out.println("ERROR! Enter a valid number");
103-
selectedLightMenu(light,bridge);
109+
selectedLightMenu(light);
104110
}
105111

106112

107113
}
108114

109-
private void changeColor(PHLight light, PHBridge bridge){
115+
private void changeColor(PHLight light) {
110116

111-
if (light.getLightType() != CT_COLOR_LIGHT || light.getLightType() != COLOR_LIGHT ){
117+
System.out.println(light.getLightType());
118+
119+
/*if (light.getLightType().toString().equals("CT_COLOR_LIGHT") || light.getLightType().toString().equals("COLOR_LIGHT")){
112120
System.out.println("This bulb does not support colors!");
113121
selectedLightMenu(light,bridge);
114122
return;
115-
}
123+
}*/
116124

117125
Scanner scanner = new Scanner(System.in);
118126
int r = 0;
@@ -192,52 +200,49 @@ private void changeColor(PHLight light, PHBridge bridge){
192200

193201
case 9:
194202
//BACK
195-
selectedLightMenu(light,bridge);
203+
selectedLightMenu(light);
196204
break;
197205

198206
default:
199207
System.out.println("ERROR! Enter a valid number");
200-
changeColor(light,bridge);
208+
changeColor(light);
201209
}
202210

203211
//Setting the colors
204212
PHLightState lightState = new PHLightState();
213+
205214
float[] xy = PHUtilities.calculateXYFromRGB(r, g, b, PHLight.PHLightColorMode.COLORMODE_XY.getValue());
206-
System.out.println(xy[0] + " " + xy[1]);
207215
lightState.setX(xy[0]);
208216
lightState.setY(xy[1]);
209217

210-
bridge.updateLightState(light, lightState);
218+
phHueSDK.getSelectedBridge().updateLightState(light, lightState);
211219

212-
selectedLightMenu(light, bridge);
220+
selectedLightMenu(light);
213221
}
214222

215223
/**
216224
* This method checks the last known light state and sets the opposite power state
217225
*
218226
* @param light light to be changed
219-
* @param bridge connected bridge
220227
*/
221-
private void changePowerState(PHLight light, PHBridge bridge) {
228+
private void changePowerState(PHLight light) {
222229
PHLightState lightState = light.getLastKnownLightState();
223230

224-
if (lightState.isOn()){
231+
if (lightState.isOn()) {
225232
lightState.setOn(false);
226233
System.out.println("Turned " + light.getName() + " OFF");
227234
} else {
228235
lightState.setOn(true);
229236
System.out.println("Turned " + light.getName() + " ON");
230237
}
231238

232-
bridge.updateLightState(light, lightState);
239+
phHueSDK.getSelectedBridge().updateLightState(light, lightState);
233240

234-
selectedLightMenu(light, bridge);
241+
selectedLightMenu(light);
235242
}
236243

237244

238-
public void changeBrightness(PHLight light, PHBridge bridge) {
239-
240-
System.out.println(light);
245+
public void changeBrightness(PHLight light) {
241246

242247
Scanner scanner = new Scanner(System.in);
243248
System.out.println("ENTER A NEW BRIGHTNESS VALUE: ");
@@ -249,32 +254,111 @@ public void changeBrightness(PHLight light, PHBridge bridge) {
249254

250255
PHLightState lightState = new PHLightState();
251256
lightState.setBrightness(updatedBrightness);
252-
bridge.updateLightState(light, lightState);
257+
phHueSDK.getSelectedBridge().updateLightState(light, lightState);
253258

254259
Thread.sleep(1000);
255260

256261
} catch (InterruptedException e) {
257262
e.printStackTrace();
258263
}
259264

260-
selectedLightMenu(light, bridge);
265+
selectedLightMenu(light);
266+
267+
}
268+
269+
private void changeBrightnessBasedOnMicIn(PHLight light) {
270+
271+
boolean stopRecording = false;
272+
int counter = 0;
273+
int level = 0;
274+
byte tempBuffer[] = new byte[6000];
275+
276+
try {
277+
278+
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
279+
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
280+
281+
PHLightState lightState = new PHLightState();
282+
283+
if (!AudioSystem.isLineSupported(info)) {
284+
System.out.println("This line is not supported");
285+
return;
286+
}
287+
288+
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine((info));
289+
targetDataLine.open();
290+
291+
System.out.println("Starting to record...");
292+
targetDataLine.start();
293+
294+
while (counter <= 200) {
295+
296+
if (targetDataLine.read(tempBuffer, 0, tempBuffer.length) > 0) {
297+
level = calculateRMSLevel(tempBuffer);
298+
299+
lightState.setBrightness(level);
300+
phHueSDK.getSelectedBridge().updateLightState(light, lightState);
301+
302+
System.out.println(level);
303+
counter++;
304+
}
305+
}
306+
307+
targetDataLine.stop();
308+
targetDataLine.close();
309+
System.out.println("Recording stopped");
310+
311+
312+
} catch (LineUnavailableException e) {
313+
e.printStackTrace();
314+
}
315+
}
316+
317+
318+
/**
319+
* Algorithm is copyed from https://stackoverflow.com/a/32622121
320+
*
321+
* @param audioData data segment to analyze
322+
* @return procent of input audio level
323+
*/
324+
public int calculateRMSLevel(byte[] audioData)
325+
{
326+
long lSum = 0;
327+
for(int i=0; i < audioData.length; i++)
328+
lSum = lSum + audioData[i];
329+
330+
double dAvg = lSum / audioData.length;
331+
double sumMeanSquare = 0d;
261332

333+
for(int j=0; j < audioData.length; j++)
334+
sumMeanSquare += Math.pow(audioData[j] - dAvg, 2d);
335+
336+
double averageMeanSquare = sumMeanSquare / audioData.length;
337+
338+
return (int)(Math.pow(averageMeanSquare,0.5d) + 0.5);
262339
}
263340

341+
264342
private PHSDKListener listener = new PHSDKListener() {
265343
@Override
266344
public void onCacheUpdated(List<Integer> list, PHBridge phBridge) {
267-
345+
268346
}
269347

270348
@Override
271349
public void onBridgeConnected(PHBridge phBridge, String s) {
272350

273351
phHueSDK.setSelectedBridge(phBridge);
352+
phHueSDK.enableHeartbeat(phBridge, PHHueSDK.HB_INTERVAL);
274353

275354
System.out.println("BRIDGE CONNECTED");
276355

277-
phHueSDK.enableHeartbeat(phBridge, PHHueSDK.HB_INTERVAL);
356+
try {
357+
Thread.sleep(1000);
358+
359+
} catch (InterruptedException e) {
360+
e.printStackTrace();
361+
}
278362

279363
listAllLights();
280364

@@ -321,22 +405,22 @@ public void onAccessPointsFound(List<PHAccessPoint> list) {
321405

322406
@Override
323407
public void onError(int i, String s) {
324-
408+
325409
}
326410

327411
@Override
328412
public void onConnectionResumed(PHBridge phBridge) {
329-
413+
330414
}
331415

332416
@Override
333417
public void onConnectionLost(PHAccessPoint phAccessPoint) {
334-
418+
335419
}
336420

337421
@Override
338422
public void onParsingErrors(List<PHHueParsingError> list) {
339-
423+
340424
}
341425
};
342426

@@ -347,4 +431,12 @@ public PHSDKListener getListener() {
347431
public void setListener(PHSDKListener listener) {
348432
this.listener = listener;
349433
}
434+
435+
public PHBridge getConnectedBridge() {
436+
return connectedBridge;
437+
}
438+
439+
public void setConnectedBridge(PHBridge connectedBridge) {
440+
this.connectedBridge = connectedBridge;
441+
}
350442
}

0 commit comments

Comments
 (0)