2021-09-13
This commit is contained in:
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Examples for Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples.new_http;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.WebSocket;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CompletionStage;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class WebSocketSample {
|
||||||
|
private static class WebSocketListener implements WebSocket.Listener {
|
||||||
|
private StringBuilder sb;
|
||||||
|
|
||||||
|
WebSocketListener() {
|
||||||
|
sb = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(WebSocket webSocket) {
|
||||||
|
log.debug("Open!");
|
||||||
|
//webSocket.request(1);
|
||||||
|
WebSocket.Listener.super.onOpen(webSocket);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
|
||||||
|
sb.append(data);
|
||||||
|
webSocket.request(1);
|
||||||
|
|
||||||
|
if (last) {
|
||||||
|
String message = sb.toString();
|
||||||
|
|
||||||
|
log.debug("Rx: {}", message);
|
||||||
|
|
||||||
|
sb = new StringBuilder();
|
||||||
|
|
||||||
|
return CompletableFuture.completedStage(null);
|
||||||
|
}
|
||||||
|
//return WebSocket.Listener.super.onText(webSocket, data, last);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(WebSocket webSocket, Throwable error) {
|
||||||
|
log.error("Error!", error);
|
||||||
|
WebSocket.Listener.super.onError(webSocket, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String... args) throws ExecutionException, InterruptedException {
|
||||||
|
HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
|
CompletableFuture<WebSocket> completableFuture = httpClient.newWebSocketBuilder()
|
||||||
|
.buildAsync(URI.create("ws://localhost:8080/websocket"), new WebSocketListener());
|
||||||
|
|
||||||
|
WebSocket webSocket = completableFuture.get();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
Thread.sleep(1500);
|
||||||
|
webSocket.sendText("Hello", false);
|
||||||
|
webSocket.sendText(" World", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
34
sound/src/main/java/kr/pe/elex/examples/Converter.java
Normal file
34
sound/src/main/java/kr/pe/elex/examples/Converter.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Examples for Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.sound.sampled.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class Converter {
|
||||||
|
public static void main(String... args) throws UnsupportedAudioFileException, IOException {
|
||||||
|
File inFile = new File("test_out.wav");
|
||||||
|
File outFile = new File("test_out.au");
|
||||||
|
|
||||||
|
AudioInputStream inputStream = AudioSystem.getAudioInputStream(inFile);
|
||||||
|
AudioFormat outFormat = new AudioFormat(8000, 8, 1, true, false);
|
||||||
|
|
||||||
|
if (AudioSystem.isConversionSupported(outFormat, inputStream.getFormat()) &&
|
||||||
|
AudioSystem.isFileTypeSupported(AudioFileFormat.Type.AU, inputStream)) {
|
||||||
|
AudioInputStream lowResInputStream = AudioSystem
|
||||||
|
.getAudioInputStream(outFormat, inputStream);
|
||||||
|
|
||||||
|
AudioSystem.write(lowResInputStream, AudioFileFormat.Type.AU, outFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,13 +32,13 @@ public class Playback {
|
|||||||
line.open();
|
line.open();
|
||||||
line.start();
|
line.start();
|
||||||
line.addLineListener(event -> {
|
line.addLineListener(event -> {
|
||||||
if (event.getType().equals(LineEvent.Type.OPEN)){
|
if (event.getType().equals(LineEvent.Type.OPEN)) {
|
||||||
|
|
||||||
} else if (event.getType().equals(LineEvent.Type.START)) {
|
} else if (event.getType().equals(LineEvent.Type.START)) {
|
||||||
|
|
||||||
}else if (event.getType().equals(LineEvent.Type.STOP)) {
|
} else if (event.getType().equals(LineEvent.Type.STOP)) {
|
||||||
|
|
||||||
}else if (event.getType().equals(LineEvent.Type.CLOSE)) {
|
} else if (event.getType().equals(LineEvent.Type.CLOSE)) {
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
63
sound/src/main/java/kr/pe/elex/examples/RecordAudio.java
Normal file
63
sound/src/main/java/kr/pe/elex/examples/RecordAudio.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Examples for Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.sound.sampled.*;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class RecordAudio {
|
||||||
|
static boolean isRunning = true;
|
||||||
|
|
||||||
|
public static void main(String... args) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
|
||||||
|
|
||||||
|
TargetDataLine line;
|
||||||
|
AudioFormat audioFormat = new AudioFormat(44100, 16, 2, true, true);
|
||||||
|
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
|
||||||
|
if (AudioSystem.isLineSupported(info)) {
|
||||||
|
line = AudioSystem.getTargetDataLine(audioFormat);
|
||||||
|
log.debug("{}", line.getLineInfo());
|
||||||
|
line.open(audioFormat);
|
||||||
|
line.start();
|
||||||
|
|
||||||
|
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
service.schedule(() -> isRunning = false, 5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[32];
|
||||||
|
|
||||||
|
while (isRunning) {
|
||||||
|
int numRead = line.read(buffer, 0, buffer.length);
|
||||||
|
os.write(buffer, 0, numRead);
|
||||||
|
|
||||||
|
log.debug("{}", line.getLevel());
|
||||||
|
}
|
||||||
|
log.debug("end of loop");
|
||||||
|
|
||||||
|
File file = new File("test_out.wav");
|
||||||
|
AudioInputStream is = new AudioInputStream(new ByteArrayInputStream(os.toByteArray()),
|
||||||
|
audioFormat, os.size()/2/2);
|
||||||
|
|
||||||
|
//AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(new File(""));
|
||||||
|
|
||||||
|
AudioSystem.write(is, AudioFileFormat.Type.WAVE, file);
|
||||||
|
|
||||||
|
os.close();
|
||||||
|
line.close();
|
||||||
|
service.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Examples for Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import com.elex_project.abraxas.Console;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.sound.sampled.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class SignalProcessing {
|
||||||
|
|
||||||
|
public static void main(String... args)
|
||||||
|
throws UnsupportedAudioFileException, IOException, LineUnavailableException {
|
||||||
|
|
||||||
|
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
|
||||||
|
Mixer mixer = AudioSystem.getMixer(mixerInfo);
|
||||||
|
Console.writeLine("MIXER: {}", mixerInfo);
|
||||||
|
for (Control control : mixer.getControls()){
|
||||||
|
Console.writeLine("CONTROL: {}", control.toString());
|
||||||
|
}
|
||||||
|
for (Line.Info info : mixer.getSourceLineInfo()) {
|
||||||
|
Console.writeLine("SOURCE LINE: {}" , info);
|
||||||
|
Line line = mixer.getLine(info);
|
||||||
|
for (Control control : line.getControls()){
|
||||||
|
Console.writeLine("CONTROL: {}", control.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (Line.Info info : mixer.getTargetLineInfo()) {
|
||||||
|
Console.writeLine("TARGET LINE: {}" , info);
|
||||||
|
Line line = mixer.getLine(info);
|
||||||
|
for (Control control : line.getControls()){
|
||||||
|
Console.writeLine("CONTROL: {}", control.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*AudioInputStream inputStream = AudioSystem.getAudioInputStream(Objects.requireNonNull(ClassLoader.
|
||||||
|
getSystemResourceAsStream("alarm_gentle.wav")));
|
||||||
|
SourceDataLine src = AudioSystem.getSourceDataLine(inputStream.getFormat());
|
||||||
|
src.open();
|
||||||
|
src.start();
|
||||||
|
|
||||||
|
Mixer mixer = AudioSystem.getMixer(AudioSystem.getMixerInfo()[1]);
|
||||||
|
Console.writeLine(mixer.getLineInfo());
|
||||||
|
Port line = (Port) mixer.getLine(Port.Info.LINE_IN);
|
||||||
|
line.open();
|
||||||
|
for (Control ctrl : line.getControls()) {
|
||||||
|
Console.writeLine(ctrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
FloatControl volumeCtrl = (FloatControl) mixer.getControl(FloatControl.Type.VOLUME);
|
||||||
|
volumeCtrl.setValue(0);
|
||||||
|
byte[] buffer = new byte[64];
|
||||||
|
int numRead = 0;
|
||||||
|
float dVol = 0.f;
|
||||||
|
while ((numRead = inputStream.read(buffer, 0, buffer.length)) > 0) {
|
||||||
|
log.debug("vol: {}", volumeCtrl.getValue());
|
||||||
|
src.write(buffer, 0, numRead);
|
||||||
|
if (volumeCtrl.getValue()<=volumeCtrl.getMinimum()) {
|
||||||
|
dVol = 0.01f;
|
||||||
|
} else if (volumeCtrl.getValue()>=volumeCtrl.getMaximum()){
|
||||||
|
dVol = -0.01f;
|
||||||
|
}
|
||||||
|
volumeCtrl.setValue(volumeCtrl.getValue()+dVol);
|
||||||
|
}
|
||||||
|
|
||||||
|
inputStream.close();
|
||||||
|
src.close();*/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
BIN
test_out.au
Normal file
BIN
test_out.au
Normal file
Binary file not shown.
BIN
test_out.wav
Normal file
BIN
test_out.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user