PLATFORM_API_MacOSX_MidiOut.c: 162->Invalid message: - java

Hello everyone I have been digging the javax.sound.midi Api and I came across with the issue that I think is not related with the library itself but something related with mac. Well I have this simple code to send a Timing clock midi message. And receive this red message"PLATFORM_API_MacOSX_MidiOut.c: 162->Invalid message: message status=0xF8 while sending short message". Anyone knows work around this? thank in advance
I had a look at this source code https://github.com/JetBrains/jdk8u_jdk/blob/master/src/macosx/native/com/sun/media/sound/PLATFORM_API_MacOSX_MidiOut.c
And it seem the range of midi message is a bit constrained.
public class TestMidiLibrary {
public static void main(String[] args) {
MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
MidiDevice device = null;
try {
device = MidiSystem.getMidiDevice(info[6]);
device.open();
} catch (MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ShortMessage msg = new ShortMessage();
try {
msg.setMessage(ShortMessage.TIMING_CLOCK);
long timeStamp = -1;
Receiver r = device.getReceiver();
r.send(msg,timeStamp);
} catch (InvalidMidiDataException | MidiUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Related

Why was I able to send more than the apparent maximum amount of data via Bluetooth 4.2?

My research showed (see pg. 5) that the maximum amount of data that can be sent via Bluetooth 4.2 is 257 bytes.
However, I was able to send 990 bytes between my Python script and Java application. Why was I able to send so much data? Is the information I found about the maximum data wrong, or did something else happen?
Python Bluetooth script:
#Parameters config
sdr=RtlSdr()
sdr.fc=100e6
sdr.gain=48
sdr.rs=1.024e6
#Bluetooth connection
server_sock=BluetoothSocket(RFCOMM)
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port=server_sock.getsockname()[1]
uuid="94f39d29-7d6d-437d-973b-fba39e49d4ee"
client_sock,client_info=server_sock.accept()
while (1):
samples= sdr.read_samples(256*1024)
result=psd(samples, NFFT=70, Fc=sdr.fc/1e6, Fs=sdr.rs/1e6)
tab_freq=(result[1])
value_freq=str(tab_freq)[1:-1]
value_freq2=[format(float(v),".4f")[:6] for v in value_freq.split()]
value_freq3="\n".join(value_pxx2)
#SAME FOR POWER VALUE
#THEN I SEND DATA BY BLUETOOTH
client_sock.send(value_freq3)
Java Bluetooth code:
private class ThreadConnected extends Thread {
private final BluetoothSocket connectedBluetoothSocket;
private final InputStream connectedInputStream;
private final OutputStream connectedOutputStream;
boolean running;
public ThreadConnected(BluetoothSocket socket) {
connectedBluetoothSocket = socket;
InputStream in = null;
OutputStream out = null;
running = true;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connectedInputStream = in;
connectedOutputStream = out;
}
#Override
public void run() {
byte[] buffer = new byte[1048576]; // 20 bits
int bytes;
String strRx = "";
while (running) {
try {
bytes = connectedInputStream.read(buffer);
final String strReceived_freq = new String(buffer,0, bytes/2);
final String strReceived_pxx = new String(buffer,(bytes/2)+1, bytes);
//final int samples_sdr=new Integer(buffer,0,bytes);
final String strByteCnt = String.valueOf(bytes) + " bytes received.\n";
runOnUiThread(new Runnable(){
#Override
public void run() {
Pxx_value.setText(strReceived_pxx+"\n"); // get data PXX
freq_value.setText(strReceived_freq+"\n"); // get data freq
// plot value
/* for (int i=0; i<nb_points; i++)
{
freq[i]=Double.parseDouble(strReceived_freq);
pxx[i]=Double.parseDouble(strReceived_pxx);
series.appendData(new DataPoint(freq[i],pxx[i]), true,500);
}*/
}});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
cancel();
final String msgConnectionLost = "Connection lost:\n" + e.getMessage();
runOnUiThread(new Runnable(){
#Override
public void run() {
}});
}
}
}
public void write(byte[] buffer) {
try {
connectedOutputStream.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void cancel() {
running = false;
try {
connectedBluetoothSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The document you link to shows the LE (low energy) Link Layer packet format, as specified in Vol 6, Part B, Section 2.1 of the Bluetooth Core Specification.
You are using RFCOMM, which is a Bluetooth Classic (aka BR/EDR) profile. The Link Layer packet format for BR/EDR is specified in Vol 2, Part B, Section 6 and shows that the payload can be up to 2790 bytes long.
In any case the API you are using doesn't give you access to the Link Layer. You are writing on an RFCOMM channel (which is designed to work more or less like a serial port) and you can write as many bytes as you want. Your library and the underlying platform will take care of placing your data into the appropriate number of L2CAP packets, which will then be further encapsulated in link layer packets. The main limitation you will run into are the buffer sizes in your implementation. In this case your socket based API will return the number of bytes that were written in the call to send, and you will be able to attempt retransmission later.

How to ship android app with R script in it and make sure it executes

I am building one Android machine learning app where in a java class I need to make a call to a R script. But before that I need to make sure that R is up and running. How do I do that? How to get the Rconnection established in android code?
public String getSum() {
RConnection connection = null;
try {
/* Create a connection to Rserve instance running on default port
* 6311
*/
connection = new RConnection();
connection.eval("library(randomForest)");
/* Note four slashes (\\\\) in the path */
connection.eval("source('MyScript.R')");
REXP sum =connection.eval("predictSAL()");
this.sum = sum.asString();
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
} catch (REngineException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sum;
}

Bluetooth data loss between Android and Arduino

Straight to the point - my application due to some mysterious reason looses part of a data (String) when receiving an InputStream. I am talking about Bluetooth connection here. Most of the time, I am receiving correct string but once in while it is shortened. Weirdest thing in here is that I am printing every InputStream into my ScrollView and I can tell that the whole string is there... Nevertheless here is the code:
#Override
public void run() {
InputStream inputStream;
try {
inputStream = mBTSocket.getInputStream();
while (!bStop) {
byte[] buffer = new byte[256];
int bytes;
if (inputStream.available() > 0) {
bytes = inputStream.read(buffer);
final String strInput = new String(buffer, 0, bytes);;
mTxtReceive.post(new Runnable() {
#Override
public void run() {
mTxtReceive.append(strInput);
int txtLength = mTxtReceive.getEditableText().length();
if (txtLength > mMaxChars) {
mTxtReceive.getEditableText().delete(0, txtLength - mMaxChars);
}
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(View.FOCUS_DOWN);
}
});
}
});
runOnUiThread(new Runnable() {
#Override
public void run() {
weather = strInput.split(",");
mTxtHumidity.setText(weather[0]);
mTxtTemperatureDHT.setText(weather[1]);
mTxtPressure.setText(weather[2]);
mTxtLux.setText(weather[3]);
mTxtRainMM.setText(weather[4]);
mTxtRainDaily.setText(weather[5]);
mTxtWSKPH.setText(weather[6]);
mTxtWGKPH.setText(weather[7]);
mTxtWSAVG2.setText(weather[8]);
mTxtWGAVG10.setText(weather[9]);
}
});
}
Thread.sleep(500);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem in here is null exception on array weather as according to my app I am sometimes accessing items out of boundry (weather length is 6/7/8 most of the time once it errors). App crashes in 10% of the time.
Any reason behind it?
EDIT: while receiving InputStream sometimes instead of receiving 56 bytes I get 33 and 22 separetely
Answer to be found here: Android InputStream dropping first two bytes (modified BluetoothChat)
Adding
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
solves only for non-dynamic data exchange between Android and Arduino due to sleep method. It turns out that even sleep(50) works in this condition. For some reason after this short sleep buffer is never divided nor lost. If it is bad coding please explain before downvoting.

Asmack set up for Android

I want to create a messaging application for Android, so I've been reading up about XMPP and Asmack.
Asmack sounds pretty much exactly what I want, especially after reading this other question - Android and XMPP: Currently available solutions.
However, I'm fairly new to Android programming and also Github.
I have found the src for Asmack at https://github.com/Flowdalic/asmack, and also downloaded the jar from http://asmack.freakempire.de/4.0.4/, which I have included in my project. However I am unsure how the folders in the github asmack folder are supposed to be used. After hours trawling google trying to find some sort of step by step guide on how to set aSmack up, and not finding anything useful, I'm losing the will to live!
I'm using the following example code I found on the Smack github page, and I dont get any errors:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XMPPTCPConnection connection = new XMPPTCPConnection("jabber.org");
try {
connection.connect();
connection.login("mtucker", "password");
} catch (SmackException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Chat chat = ChatManager.getInstanceFor(connection)
.createChat("jsmith#jivesoftware.com", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
try {
chat.sendMessage("Howdy!");
} catch (NotConnectedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
However I haven't used the 'build environment' from aSmack github repository - how is this meant to be included/used?
Thanks for any help!
You need to add single Jar file (last version now is 4.0.6) to your library and also don't forget to set proper permissions in manifest file.

Java event is killed when a function for audio playing is called

I am facing a strange programming problem. It has exhausted me but with no solution found!
My program is mainly dependent on an event (Java Listener) that is fired from an external hardware for receiving an audio message from that hardware.Inside the eventHandler i do the following
pass the received message to a static method "decode" from another class which returns data
then I open FileOutputStream, write these data "audio" to a file,and close the FileOutputStream.
I call a static method "play" from another class to play the audio file.
The problem is: whenever the method "Play" is called for the first time, it executes correctly but it causes the event to stop raising and the program to terminate but without exceptions. When I comment out the play method, everything becomes okay!
Do you have some idea about a method causing program termination ?
public void messageReceived(int to,Message message)
{
speexAudioMsg msg = (speexAudioMsg)message;
try{
byte[] output = jspeexDecoder.decode(msg.get_frame());
os = new FileOutputStream(file);
os.write(output);
os.close();
Player.play();
}
catch (IOException ex) {ex.printStackTrace();}
}
You are probably using the event thread to play the music. Try calling Player.play() in a new thread.
new Thread(new Runnable() { public void run() {Player.play()}}).start();
here is an example:
static String url = "http://www.stackoverload.com";
public static void threadTest() {
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
URL url2 = new URL(url);
url2.openStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).run();

Categories

Resources