Skip to content

Commit

Permalink
https://github.com/rusefi/web_backend/issues/96
Browse files Browse the repository at this point in the history
  • Loading branch information
rusefillc committed Aug 15, 2020
1 parent 24ffa5d commit 5a020f4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

</manifest>
4 changes: 3 additions & 1 deletion android/app/src/main/java/com/rusefi/app/rusEFI.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class rusEFI extends Activity {

private UsbManager usbManager;
private DfuUpload dfuUpload;
private SoundBroadcast soundBroadcast = new SoundBroadcast();


@SuppressLint("SetTextI18n")
@Override
Expand Down Expand Up @@ -202,7 +204,7 @@ public void sendMessage(View view) {
if (view.getId() == R.id.button) {
handleButton();
} else if (view.getId() == R.id.buttonSound) {

soundBroadcast.start();

}
}
Expand Down
86 changes: 86 additions & 0 deletions java_console/shared_ui/src/com/rusefi/ts_plugin/AudioPlayback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.rusefi.ts_plugin;

import javax.sound.sampled.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class AudioPlayback {
private static final int sampleRate = 16000;

private static final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);

public AudioPlayback() throws IOException {
ServerSocket serverSocket = new ServerSocket(16000);

while (true) {

Socket clientSocket = serverSocket.accept();
new Thread(() -> {
try {
playSound(clientSocket.getInputStream());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}).start();
}
}

public static void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
new AudioPlayback();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}).start();
}


private static void playSound(InputStream stream) throws IOException {
while (true) {
byte[] buffer = new byte[30000];
int got = stream.read(buffer);

byte copy[] = new byte[got];
System.arraycopy(buffer, 0, copy, 0, got);
new Thread(new Runnable() {
@Override
public void run() {
toSpeaker(copy);
}
}).start();
}
}

public static void toSpeaker(byte soundbytes[]) {
try {
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

sourceDataLine.open(format);

FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
volumeControl.setValue(100.0f);

sourceDataLine.start();
sourceDataLine.open(format);

sourceDataLine.start();

System.out.println("format? :" + sourceDataLine.getFormat());

sourceDataLine.write(soundbytes, 0, soundbytes.length);
System.out.println("Playing " + soundbytes.toString());
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
System.out.println("Not working in speakers...");
e.printStackTrace();
}
}
}
3 changes: 3 additions & 0 deletions java_console/ui/src/main/java/com/rusefi/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import static com.rusefi.rusEFIVersion.CONSOLE_VERSION;
import static com.rusefi.ui.storage.PersistentConfiguration.getConfig;

/**
* @see StartupFrame
*/
public class ConsoleUI {
private static final int DEFAULT_TAB_INDEX = 0;
public String port;
Expand Down

0 comments on commit 5a020f4

Please sign in to comment.