Skip to content
Snippets Groups Projects
Commit a1340889 authored by tobias's avatar tobias
Browse files

Add NativeAudioMac project to branch

parent 1f9b0a6d
No related branches found
No related tags found
No related merge requests found
Showing
with 543 additions and 0 deletions
package de.tobias.playpad.audio;
import javafx.beans.property.DoubleProperty;
public interface Peakable {
public enum Channel {
LEFT,
RIGHT;
}
public DoubleProperty audioLevelProperty(Channel channel);
public double getAudioLevel(Channel channel);
}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/PlayWallCore"/>
<classpathentry combineaccessrules="false" kind="src" path="/libUtils"/>
<classpathentry kind="output" path="bin"/>
</classpath>
/bin/
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PlayWallNative</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
File added
package de.tobias.playpad;
public class NativeAudio {
public static native void initialize();
public static native void play(int id);
public static native void pause(int id);
public static native void stop(int id);
public static native void setLoop(int id, boolean loop);
public static native double getVolume(int id);
public static native void setVolume(int id, double volume);
public static native boolean load(int id, String path);
public static native void dispose(int id);
public static native double getDuration(int id);
public static native double getPosition(int id);
public static void onPeakMeter(int id, float left, float right) {
if (delegate != null) {
delegate.onPeakMeter(id, left, right);
}
}
public static void onPositionChanged(int id, double position) {
if (delegate != null) {
delegate.onPositionChanged(id, position);
}
}
public static void onFinish(int id) {
if (delegate != null) {
delegate.onFinish(id);
}
}
private static NativeAudioDelegate delegate;
public static void setDelegate(NativeAudioDelegate delegate) {
NativeAudio.delegate = delegate;
}
public interface NativeAudioDelegate {
public void onFinish(int id);
public void onPeakMeter(int id, float left, float right);
public void onPositionChanged(int id, double position);
}
}
package de.tobias.playpad;
import java.nio.file.Path;
import de.tobias.playpad.audio.AudioHandler;
import de.tobias.playpad.audio.Peakable;
import de.tobias.playpad.pad.PadStatus;
import de.tobias.playpad.pad.conntent.PadContent;
import de.tobias.utils.util.Worker;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.util.Duration;
public class NativeAudioMacHandler extends AudioHandler implements Peakable {
private static int counter = 0;
private final int id;
ObjectProperty<Duration> positionProperty;
private ObjectProperty<Duration> durationProperty;
private boolean isLoaded;
private DoubleProperty leftPeak;
private DoubleProperty rightPeak;
public NativeAudioMacHandler(PadContent content) {
super(content);
id = counter++;
positionProperty = new SimpleObjectProperty<>();
durationProperty = new SimpleObjectProperty<>();
leftPeak = new SimpleDoubleProperty();
rightPeak = new SimpleDoubleProperty();
}
protected int getId() {
return id;
}
@Override
public void play() {
NativeAudio.setLoop(id, getContent().getPad().getPadSettings().isLoop());
NativeAudio.play(id);
}
@Override
public void pause() {
NativeAudio.pause(id);
}
@Override
public void stop() {
NativeAudio.stop(id);
}
@Override
public Duration getPosition() {
return positionProperty.get();
}
@Override
public ReadOnlyObjectProperty<Duration> positionProperty() {
return positionProperty;
}
@Override
public Duration getDuration() {
return durationProperty.get();
}
@Override
public ReadOnlyObjectProperty<Duration> durationProperty() {
return durationProperty;
}
@Override
public void setVolume(double volume) {
NativeAudio.setVolume(id, volume);
}
@Override
public boolean isMediaLoaded() {
return isLoaded;
}
@Override
public void loadMedia(Path[] paths) {
Platform.runLater(() ->
{
if (getContent().getPad().isPadVisible()) {
getContent().getPad().getController().getView().showBusyView(true);
}
});
Worker.runLater(() ->
{
isLoaded = NativeAudio.load(id, paths[0].toString());
if (isLoaded) {
Platform.runLater(() ->
{
durationProperty.set(Duration.seconds(NativeAudio.getDuration(id)));
getContent().getPad().setStatus(PadStatus.READY);
if (getContent().getPad().isPadVisible()) {
getContent().getPad().getController().getView().showBusyView(false);
}
});
}
});
}
@Override
public void unloadMedia() {
NativeAudio.dispose(id);
}
@Override
public DoubleProperty audioLevelProperty(Channel channel) {
if (channel == Channel.LEFT) {
return leftPeak;
} else if (channel == Channel.RIGHT) {
return rightPeak;
}
return null;
}
@Override
public double getAudioLevel(Channel channel) {
return audioLevelProperty(channel).get();
}
}
package de.tobias.playpad;
import java.util.HashMap;
import de.tobias.playpad.NativeAudio.NativeAudioDelegate;
import de.tobias.playpad.audio.AudioCapability;
import de.tobias.playpad.audio.AudioHandler;
import de.tobias.playpad.audio.AudioHandlerConnect;
import de.tobias.playpad.audio.Peakable.Channel;
import de.tobias.playpad.pad.PadStatus;
import de.tobias.playpad.pad.conntent.PadContent;
import de.tobias.playpad.viewcontroller.AudioHandlerViewController;
import javafx.util.Duration;
public class NativeAudioMacHandlerConnect extends AudioHandlerConnect implements NativeAudioDelegate {
private static final HashMap<Integer, NativeAudioMacHandler> handlers = new HashMap<>();
public NativeAudioMacHandlerConnect() {
NativeAudio.initialize();
NativeAudio.setDelegate(this);
}
@Override
public AudioHandler createAudioHandler(PadContent content) {
NativeAudioMacHandler nativeAudioMacHandler = new NativeAudioMacHandler(content);
handlers.put(nativeAudioMacHandler.getId(), nativeAudioMacHandler);
return nativeAudioMacHandler;
}
@Override
public AudioHandlerViewController getAudioHandlerSettingsViewController() {
return null;
}
@Override
public String getType() {
return "Native";
}
@Override
public void onFinish(int id) {
NativeAudioMacHandler nativeAudioMacHandler = handlers.get(id);
if (nativeAudioMacHandler != null) {
PadContent content = nativeAudioMacHandler.getContent();
if (content != null) {
content.getPad().setStatus(PadStatus.STOP);
}
}
}
@Override
public void onPositionChanged(int id, double position) {
NativeAudioMacHandler nativeAudioMacHandler = handlers.get(id);
if (nativeAudioMacHandler != null) {
nativeAudioMacHandler.positionProperty.set(Duration.seconds(position));
}
}
@Override
public void onPeakMeter(int id, float left, float right) {
NativeAudioMacHandler nativeAudioMacHandler = handlers.get(id);
if (nativeAudioMacHandler != null) {
nativeAudioMacHandler.audioLevelProperty(Channel.LEFT).set(left);
nativeAudioMacHandler.audioLevelProperty(Channel.RIGHT).set(right);
}
}
@Override
public boolean isFeatureAvaiable(AudioCapability audioCapability) {
return false;
}
@Override
public AudioHandlerViewController getAudioFeatureSettings(AudioCapability audioCapablility) {
return null;
}
}
package de.tobias.playpad;
public class Waveform {
public static native float[] createWaveform(String path);
}
package de.tobias.playpad.view;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class WaveformView extends Path {
public WaveformView(float[] data) {
getElements().add(new MoveTo(0, 0));
double width2 = data.length / 1200.0;
int width = data.length / 10000;
System.out.println(data.length);
System.out.println(width);
int i = 0;
for (i = 0; i < data.length; i += width) {
if (i < data.length) {
LineTo lineTo = new LineTo(i / width2, data[i] * 50.0);
MoveTo moveTo = new MoveTo(i / width2, data[i] * 50.0);
getElements().addAll(lineTo, moveTo);
}
}
for (; i >= 0; i -= width) {
if (i >= 0 && i < data.length) {
LineTo lineTo = new LineTo(i / width2, -data[i] * 50.0);
MoveTo moveTo = new MoveTo(i / width2, -data[i] * 50.0);
getElements().addAll(lineTo, moveTo);
}
}
getElements().add(new LineTo(0, 0));
getElements().add(new MoveTo(0, 0));
setFill(Color.BLACK);
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class de_tobias_playpad_NativeAudio */
#ifndef _Included_de_tobias_playpad_NativeAudio
#define _Included_de_tobias_playpad_NativeAudio
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: de_tobias_playpad_NativeAudio
* Method: play
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_NativeAudio_play
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: pause
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_NativeAudio_pause
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: stop
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_NativeAudio_stop
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: getVolume
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL Java_de_tobias_playpad_NativeAudio_getVolume
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: setVolume
* Signature: (ID)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_NativeAudio_setVolume
(JNIEnv *, jclass, jint, jdouble);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: load
* Signature: (ILjava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_de_tobias_playpad_NativeAudio_load
(JNIEnv *, jclass, jint, jstring);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: dispose
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_NativeAudio_dispose
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: getDuration
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL Java_de_tobias_playpad_NativeAudio_getDuration
(JNIEnv *, jclass, jint);
/*
* Class: de_tobias_playpad_NativeAudio
* Method: getPosition
* Signature: (I)D
*/
JNIEXPORT jdouble JNICALL Java_de_tobias_playpad_NativeAudio_getPosition
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class de_tobias_playpad_Waveform */
#ifndef _Included_de_tobias_playpad_Waveform
#define _Included_de_tobias_playpad_Waveform
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: de_tobias_playpad_Waveform
* Method: initialize
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_Waveform_initialize
(JNIEnv *, jclass);
/*
* Class: de_tobias_playpad_Waveform
* Method: createWaveform
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_de_tobias_playpad_Waveform_createWaveform
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
package de.tobias.playpad;
public class NativeAudioTest {
public static void main(String[] args) {
System.load("/Users/tobias/Documents/Programmieren/Java/git/PlayWall/PlayWallNative/libNativeAudio.dylib");
NativeAudio.load(0, "/Users/tobias/Downloads/03%20Hymn%20For%20The%20Weekend.mp3.wav");
System.out.println(NativeAudio.getDuration(0));
NativeAudio.play(0);
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package de.tobias.playpad;
import de.tobias.playpad.view.WaveformView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class WaveformTest extends Application {
public static void main(String[] args) {
System.load("/Users/tobias/Documents/Programmieren/Java/git/PlayWall/PlayWallNative/libNativeAudio.dylib");
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
float[] data = Waveform.createWaveform("/Users/tobias/Music/iTunes/iTunes Media/Music/Coldplay/Mylo Xyloto/04 Charlie Brown.mp3");
float[] data2 = Waveform.createWaveform("/Users/tobias/Downloads/TNT-Loop.wav");
WaveformView view = new WaveformView(data);
WaveformView view2 = new WaveformView(data2);
WritableImage image = new WritableImage(1200, 150);
view.snapshot(null, image);
WritableImage image2 = new WritableImage(1200, 150);
view2.snapshot(null, image2);
VBox root = new VBox(new ImageView(image), new ImageView(image2));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment