Java Sound input on OS X. Volume control not supported? Crashes - java

For both the built-in mic and built-in input on my MacBook Pro, the following lines both return false:
line.isControlSupported(FloatControl.Type.VOLUME);
line.isControlSupported(FloatControl.Type.MASTER_GAIN);
the line is an instance of TargetDataLine and has been successfully open()'d.
Because I can't set the gain, any audio I capture is at an extremely low level and is swamped by noise. The JVM (1.6.0_24, com.apple.javajdk16.cmd, OS X 10.6.7) also frequently crashes when calling TargetDataLine.close().
Anyone had any luck with this?

Check this - here are OS X java sound demo-apps.
http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo/
Have a working os x gain control too.
Based on comments:
Check this too: http://www.jsresources.org/faq_audio.html#recording_volume , but honestly never tried it. At the page:
The obvious solution would be to get a
Control object of type VOLUME or
MASTER_GAIN for the TargetDataLine and
manipulate the volume via this object.
However, this is not possible, since
no known Java Sound implementation
supports any controls for
TargetDataLine instances.
What you can do is to use the system
mixer to control the recording volume
--- it affects hardware settings in the soundcard. One possibility is to
use the mixer application of the
operating system. The other
possibility is using Port lines from
inside a Java Sound application. See
the section Ports for details.

Related

Capturing keyboard input from specific USB port

i'm stuck at capturing keyboard events.
I'm working with a barcode scanner which is detected by system as traditional keyboard (it is it's only mode, my client has a lot of budget scanners which can't emulate virtual com port).
My goal is to be able to detect at which port is this scanner plugged in, capture any input from that port, and prevent it from being handled by the OS (i don't want the barcode numbers popping up on focused input).
All this must be performed by a background service, which is never an active window, AND the solution must work both on linux and on windows.
I've been able to capture the input using jnativehook, but failed to prevent it from being handled by OS.
As far as i know, it is impossible to achieve this using pure java (JVM gives access to keyboard events only when application window is focused), so how should i handle this issue? I'm okay with using jni and c++ if that's necessary, but i don't know where to start.
JNativeHook does have the ability to discard events on Windows and OSX (not Linux), however, it does not have the ability to determine which USB port the scanner is connected to. The reason it cannot consume events on Linux is because the XRecord API makes a copy of the events and doesn't sit directly on the input event loop. If the devices you are working with can utilize HID, I would take a look into some of the JNI HID library wrappers like gary-rowe/hid4java or signal11/hidapi. The assertion that "it is impossible to achieve this using pure java" is correct. The only way to do it would be though native code, and HID is the most appropriate native method to accomplish your goals. Other input methods like input hooking (used by JNativeHook) will not be able to provide you with the source USB port, nor can they consume events on some platforms (Linux). There is another option out there like melloware/jintellitype that use a different mechanism for capturing input. I don't think it can grab input based on the port, however, the non-portable Linux equivalent of this library uses the XGrab API which will only consume events, but again, I don't know if it will be practical for your input source as I think it can only bind one key per call and that key must use a modifier.
Hope this helps you move forward with your project.

Java block keyboard presses

Is there a way how to block keyboard input in java. I would like to catch the input in the java code, but stop it from being send to OS.
Example: i have notepad opened and i can write just fine, but when i press a combination of keys java app catches that input, and now i should not be able to write with my keyboard. Is this kind of behaviour possible?
I know how to capture key presses but the keyboard blocking part is a mystery to me.
I tried googling it but i did not find any solution.
You have to understand: java applications run with the JVM. A JVM isn't the operating system.
Therefore your ways to access resources belonging to the operating system are very limited.
In other words: there is no generic, cross plattform way of having a Java application being able to "intersect" arbitrary console user input for arbitrary other applications.
Imagine you are a person sitting in a bus - just a guy like everybody else. You have no authority to turn to fellow passengers and ask them for their passport or such things. You are just one guy in the bus, like everybody else. Same here: a Java application is lacking the means to control other processes.
As you are specifically asking about the Windows platform: there might be some options using JNI and specific native calls. See here for example.
So, to be precise: it is not possible in general, but depending on your operating system there might be ways, for example using JNI.
Yes, you can do this - I used this library:
https://github.com/tulskiy/jkeymaster
to successfully to register a "global" keyboard shortcut to open a window in my program that was running in the task tray.
You can't "stop" it from being sent to the OS, but you can register a keyboard shortcut, open your window, and give it focus, so that all other keystrokes go into that text box.

How to check if monitor is on or off

I'm trying to know if is possible to understand if monitor is on or is off.
This is what i've tried:
GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = g.getScreenDevices();
int monitor_count = 0;
for(GraphicsDevice device : devices){
if(device.getType() == GraphicsDevice.TYPE_RASTER_SCREEN)
monitor_count++;
}
if(monitor_count==0){
System.out.println("Monitor is OFF");
}else{
System.out.println("Monitor is ON");
}
But even if i close monitor ( or disconnect directly from power ) it continue count me one monitor.
How can i know if monitor is OFF?
This is certainly not possible in cross platform Java, and to be honest isn't really possible in a reliable sense even if we resort to native code.
The (non-reliable) way to do this natively for Windows would be to use GetDevicePowerState - find it in kernel32.dll. However, from experiments I did using this function a while back I can say it definitely doesn't work with every monitor, and obviously even if this was reliable it would be a Windows-only solution.
If you do want to go down this route bearing in mind the above limitations, then use MonitorFromPoint to grab the handle to the primary monitor (pass in a 0,0 as the point and use the MONITOR_DEFAULTTOPRIMARY flag.)
Some information may be in operation system hands, especially for laptops - OS may get some notification if lid is open or closed. Most certainly not possible for VGA connector, maybe HDMI or DVI monitors report something back to the OS.
You should search for some OS specific functions, maybe something related to Power Management.
The only way to do it would be to have some sort of power usage monitoring device connected between the monitor and the outlet. Apart from that, I think even the computer can't tell if the monitor is on or off; only whether the signal lead is connected.
GraphicsEnvironment can only tell you the desktop arrangement that the user has configured at the OS level. It doesn't care whether there's a real monitor displaying it, or if it's a remote desktop connection, or if there's nothing.
The whole idea is flawed.
There is no way to tell for sure if a monitor is connected to lets say a VGA port, since it works one-way (output only). Although there is a way how the monitor can tell the computer about its capabilities added to VGA, but its completely optional.
Its different with HDMI (the devices actually need to talk to each other), but that doesn't necessarily mean that a monitor is connected to the port, even if the graphics card end believes that. It could very well be a recording device or anything other than a monitor.
The only cases where one could tell reliably if the display was on is when the display is built-in and controlled by the computer itself (e.g. laptop). Still the information is device specific and may not be available through any OS-calls.
Even if you manage to get an indication from the OS (as suggested through windows API), you can never rely on it to be correct. It will be the OS best guess and that will still be systematically wrong in some configurations.

Getting system info i.e graphics card, cpu, ram etc using java

How would I get system info using java in a java applet. I nid to use this in my gaming website to find whether the clients's system can handle the requirements of some pc games
I will really appreciate the help.
I think that the most reasonable and light-weight way to do that would be to detect the underlying OS, and parse the output of some command line commands. (for example, uname and the contents of the file /proc/meminfo on Linux)
Java graphics performance for games has been shown to come down to the drivers that the card uses, on occasions, so I doubt any of this information will be of much use to discover if the game will run OK.
It would be more useful to run a test on a fixed size rendering surface and count how many FPS can be drawn.

Java 6 Hardware Acceleration on Mac OS X

I have a java application I wrote that loads up a TTF font and uses the drawString method from Graphics2D. This gets called every 50ms with the x and y positions changing each time to make the text move. When I run the program on Windows, I get 0-1% CPU usage, but on Mac I get about 75% usage. This Windows machine does have a better CPU but there's no way there should be that big of a difference. I think it has to do with Hardware Acceleration and I want to know how enable it. I found some Mac specific Java properties, but none of them lowered my CPU usage. Any ideas how to increase Java 2D performance on OS X? Thanks.
EDIT1: I thought that these properties would help but they didn't.
System.setProperty("sun.java2d.opengl", "true");
System.setProperty("apple.awt.graphics.UseQuartz","true"); System.setProperty("apple.awt.graphics.EnableQ2DX","true");
EDIT2: You can download the project source and byte code here: http://drop.io/ExampleScreenSaver
EDIT3: Since drop.io no longer exists, I uploaded the project to Google Code. Now you can browse the code without downloading it. I still have no idea how to approach this problem so any help would be appreciated.
Run the profiler in "jvisualvm" to identify where the time goes.
Apple JVM font handling is specific to MacOS X. For instance, if a glyph is not found for a character in the current font, the JVM looks for another font to display the character. Also, antialiasing is enabled by default (you should check that you have it enabled on Windows if you want to compare).
I am not surprised Apple's implementation is using more CPU.
OpenGL can be enabled when calling java e.g.
java -Dsun.java2d.opengl=true MyJavaGame
Too enable opengl from inside your java application
System.setProperty("sun.java2d.opengl", "true");
Not sure how this pans out on the iOS / Linux / Android and may require more looking into.

Categories

Resources