Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing sound from Wine with TargetDataLine

I have written a small Java application for testing purposes that captures sound from a mixer on ubuntu 12.04.

The code works fine, I can capture sound from all applications except for anything running under Wine.

Whenever I start my program, after having started Wine, the call to targetDataLine.read() will block forever

When Wine is not running in the background, it correctly outputs 0 when there is no input, or the number of bytes read if there is input, as expected.

If I start my program before starting Wine, the sound driver will not be available within wine.

I have tried using both the mixers provided by Alsa as well as the default device, same result.

I could imagine that wine somehow locks Alsa (for whatever reason), but why would a simple call to TargetDataLine.read() cause sound to fail in Wine? mixerInfo[0] is default on my system btw, and the application is of course always running outside of Wine using oracle's latest JRE (7).

private void readSound ()
{
    byte tempBuffer[] = new byte[10000];
    int cnt = 0;
    Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();

    System.out.println("Available mixers:");
    for (int p = 0; p < mixerInfo.length; p++)
        System.out.println(mixerInfo[p].getName());

    format = getAudioFormat();
    DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format);
    Mixer mixer = AudioSystem.getMixer(mixerInfo[0]);

    try
    {
         targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
         targetDataLine.open(format);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    targetDataLine.start();

    while (true)
    {
        i++;
        cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
        System.out.println("read " + cnt + " bytes:" + tempBuffer[i]);            
        calculateLevel(tempBuffer, 0, 200);
        targetDataLine.flush();
        System.out.println(level);
   }
}
like image 226
Dominik Hensler Avatar asked Jul 18 '12 21:07

Dominik Hensler


Video Answer


1 Answers

Make use of AudioSystem.write() method. It is much easier

targetDataLine.open(format);
targetDataLine.start();
AudioInputStream ais=new AudioInputStream(targetDataLine);
AudioFileFormat.Type fileformat=AudioFileFormat.Type.WAVE;
/*
Other Audio file formats supported:
AudioFileFormat.Type.AU
AudioFileFormat.Type.AIFF
AudioFileFormat.Type.AIFC
AudioFileFormat.Type.SND
*/
File audoutputfile=new File('myfile');
//adjust extension according to AudioFileFormat
AudioSystem.write(ais,fileformat, audoutputfile);
like image 180
Mohammed Shareef C Avatar answered Oct 17 '22 08:10

Mohammed Shareef C