49

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);
   }
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51
Dominik Hensler
  • 635
  • 4
  • 9
  • 2
    Can you please provide, in the post, the output of the program in both scenarios, when Wine is running, & when Wine is not running. – Khaled.K Dec 06 '15 at 11:05
  • I think using DataLine::available might help with blocking. – IceGlow Jul 25 '16 at 15:37
  • 2
    Check config of Wine to see how it access the audio. http://askubuntu.com/questions/77210/how-to-change-the-default-audio-in-wine-to-alsa-only – IceGlow Jul 25 '16 at 15:39

1 Answers1

1

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);
Mohammed Shareef C
  • 3,829
  • 25
  • 35