Added Java analyzer using NativeBass

This commit is contained in:
Norbi Peti 2017-11-11 00:39:06 +01:00
parent 29d91a744d
commit 4be1eb7d28
7 changed files with 215 additions and 16 deletions

23
pom.xml
View file

@ -15,6 +15,19 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
@ -30,12 +43,22 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>local-repo</id>
<url>file:///${basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jouvieje</groupId>
<artifactId>nativebass</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>

Binary file not shown.

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>jouvieje</groupId>
<artifactId>nativebass</artifactId>
<version>1.1.2</version>
<description>POM was created from install:install-file</description>
</project>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>jouvieje</groupId>
<artifactId>nativebass</artifactId>
<versioning>
<release>1.1.2</release>
<versions>
<version>1.1.2</version>
</versions>
<lastUpdated>20171110213049</lastUpdated>
</versioning>
</metadata>

View file

@ -0,0 +1,160 @@
package io.github.norbipeti.audiospectrum;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import static jouvieje.bass.Bass.*;
import static jouvieje.bass.defines.BASS_MUSIC.BASS_MUSIC_RAMP;
import static jouvieje.bass.defines.BASS_SAMPLE.BASS_SAMPLE_LOOP;
import static jouvieje.bass.defines.BASS_DATA.BASS_DATA_FFT2048;
import static jouvieje.bass.utils.BufferUtils.newByteBuffer;
import static jouvieje.bass.utils.BufferUtils.SIZEOF_FLOAT;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.Timer;
import java.util.TimerTask;
import jouvieje.bass.BassInit;
import jouvieje.bass.exceptions.BassException;
import jouvieje.bass.structures.HMUSIC;
import jouvieje.bass.structures.HSTREAM;
public class Analyzer //Based on NativeBass example 'Spectrum'
{
/* display error messages */
private final void error(String text, CommandSender sender)
{
System.out.println(text + " - " + BASS_ErrorGetCode());
}
private final void printfExit(String format, Object... args)
{
String s = String.format(format, args);
System.out.println(s);
stop();
Bukkit.getPluginManager().disablePlugin(PluginMain.getPlugin(PluginMain.class));
}
private boolean init = false;
private boolean deinit = false;
private int chan;
public void init()
{
/*
* NativeBass Init
*/
try
{
BassInit.loadLibraries();
} catch (BassException e)
{
printfExit("NativeBass error! %s\n", e.getMessage());
return;
}
/*
* Checking NativeBass version
*/
if (BassInit.NATIVEBASS_LIBRARY_VERSION() != BassInit.NATIVEBASS_JAR_VERSION())
{
printfExit("Error! NativeBass library version (%08x) is different to jar version (%08x)\n",
BassInit.NATIVEBASS_LIBRARY_VERSION(), BassInit.NATIVEBASS_JAR_VERSION());
return;
}
/* ================================================== */
init = true;
}
private ByteBuffer buffer;
private TimerTask tt;
public FloatBuffer run(CommandSender sender, String file)
{
if (!init)
{
return null;
}
// check the correct BASS was loaded
if (((BASS_GetVersion() & 0xFFFF0000) >> 16) != BassInit.BASSVERSION())
{
printfExit("An incorrect version of BASS.DLL was loaded");
return null;
}
// initialize BASS
if (!BASS_Init(-1, 44100, 0, null, null))
{
error("Can't initialize device", sender);
stop();
return null;
}
if (!playFile(sender, file))
{
// start a file playing
BASS_Free();
stop();
return null;
}
final int size = 1024 * SIZEOF_FLOAT;
if (buffer == null || buffer.capacity() < size)
buffer = newByteBuffer(size);
// setup update timer (50hz)
timer.scheduleAtFixedRate(tt = new TimerTask()
{
@Override
public void run()
{
BASS_ChannelGetData(chan, buffer, BASS_DATA_FFT2048); //Get the FFT data
}
}, 50, 50);
FloatBuffer floats = buffer.asFloatBuffer();
return floats;
}
public boolean isRunning()
{
return deinit;
}
public void stop()
{
if (!init || deinit)
{
return;
}
deinit = true;
tt.cancel();
BASS_Free();
}
private boolean playFile(CommandSender sender, String file)
{
if (!new File(file).exists())
{
sender.sendMessage("§cFile not found: " + file);
}
HSTREAM stream = null;
HMUSIC music = null;
if ((stream = BASS_StreamCreateFile(false, file, 0, 0, BASS_SAMPLE_LOOP)) == null
&& (music = BASS_MusicLoad(false, file, 0, 0, BASS_MUSIC_RAMP | BASS_SAMPLE_LOOP, 0)) == null)
{
error("Can't play file", sender);
return false; // Can't load the file
}
chan = (stream != null) ? stream.asInt() : ((music != null) ? music.asInt() : 0);
BASS_ChannelPlay(chan, false);
return true;
}
private Timer timer = new Timer();
}

View file

@ -7,18 +7,19 @@ import org.bukkit.map.*;
public class BarsRenderer extends BarsRendererBase
{
private boolean single = false;
private boolean single = true;
public BarsRenderer(int[] bars)
{
super(bars);
//System.out.println("black: " + MapPalette.matchColor(Color.black));
//System.out.println("BLACK: " + MapPalette.matchColor(Color.BLACK));
}
@SuppressWarnings("deprecation")
@Override
public void render(MapView mv, MapCanvas mc, Player pl)
{ //Width: 8, empty space: 8, count per map: 8
//if (firstrender ? !(firstrender = !firstrender) : firstrender)
if (firstrender < 4 ? firstrender++ < 4 : false) //Only increment if true
for (int i = 0; i < 128; i++)
for (int j = 0; j < 128; j++)
@ -35,7 +36,6 @@ public class BarsRenderer extends BarsRendererBase
return;
}
int offsetx = mv.getId() % 2 * 8, offsety = mv.getId() < 2 ? -128 : 0;
//System.out.println("OX: " + offsetx + " OY: " + offsety + " ID: " + mv.getId());
for (int i = 0; i < 8 && i < count - offsetx; i++)
for (int j = 0; j < 128; j++)
for (int k = 0; k < 8; k++)

View file

@ -12,7 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
public class PluginMain extends JavaPlugin
{
private Thread thread;
//private Thread thread;
private boolean running = false;
private volatile int[] bars = new int[16];
private BarsRenderer br;
@ -31,16 +31,13 @@ public class PluginMain extends JavaPlugin
map.getRenderers().clear();
map.addRenderer(br);
}
thread = new Thread()
{
@Override
public void run()
{
PluginMain.this.run(5896);
}
};
running = true;
thread.start();
//thread = new Thread(() -> PluginMain.this.run(5896));
//running = true;
//thread.start();
Analyzer an = new Analyzer();
Bukkit.getConsoleSender().sendMessage("§bInitializing analyzer...");
an.init(); //TODO: Add command to play music, test
Bukkit.getConsoleSender().sendMessage("§bDone!");
}
// Fired when plugin is disabled
@ -65,8 +62,6 @@ public class PluginMain extends JavaPlugin
while (running)
{
serverSocket.receive(receivePacket);
//bars[Byte.toUnsignedInt(packet[0])] = Byte.toUnsignedInt(packet[1]);
//System.out.println("Index: " + Byte.toUnsignedInt(packet[0]) + " Value: " + bars[Byte.toUnsignedInt(packet[0])]);
for (int i = 0; i < packet.length && i < bars.length; i++)
bars[i] = Byte.toUnsignedInt(packet[i]);
}