Issue playing sound



Hi,

I am trying to port an audio playing unit in, written in C, to Java. I
am having trouble getting any sound out and the write method keeps on
belong for several seconds. Can anyone look at the code and tell me
what is being done wrong?

Andre

Code follows:


import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
*
*/
public class Sound implements Runnable {
public static int AUDIO_BUFF_SIZE = 131072 * 3;

public static final int AUDIO_SAMPLE_FREQ = 22050;

public static final int AUDIO_SAMPLE_LOWFREQ = 8000;

public static final int AUDIO_SAMPLE_BITS = 8;

public static final int AUDIO_NUM_BUFS = 0;

public static final int AUDIO_BUFSIZE_BITS = 8;

public static final int AUDIO_MIX_BUFSIZE = 128;

public static final int AUDIO_NUM_VOICES = 8;

public static final int AUDIO_TIMER_FREQ = 56;

private int masterVolume = 100;

private SourceDataLine source;

private boolean playSound = true;

private int[] audioVol = new int[AUDIO_NUM_VOICES];
private boolean[] audioDVol = new boolean[AUDIO_NUM_VOICES];
private int[] audioFreq = new int[AUDIO_NUM_VOICES];
private boolean[] audioDFreq = new boolean[AUDIO_NUM_VOICES];
private int[] audioLen = new int[AUDIO_NUM_VOICES];
private char[][] audioData = new char[AUDIO_NUM_VOICES][];
private boolean[] audioOn = new boolean[AUDIO_NUM_VOICES];

private int audioSampleFreq = AUDIO_SAMPLE_FREQ;
private int audioTimeFreq = AUDIO_TIMER_FREQ;
private float sampleRate = AUDIO_SAMPLE_FREQ;

private Thread thread;

Sound() {

try {
AudioFormat af = new AudioFormat(this.sampleRate, 8, 1, true,
true);

DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
this.source = (SourceDataLine) AudioSystem.getLine(info);
this.source.open(af);
this.source.start();

this.thread = new Thread(this, "sound");
this.thread.start();

} catch (LineUnavailableException e) {
e.printStackTrace();
this.source = null;
}
}


/** */
public void playSample(int channel, char[] data, int offset, int len,
int freq, int volume, int loop) {
if (!playSound || (channel >= AUDIO_NUM_VOICES)) {
return;
}

this.audioOn[channel] = true;

char[] c = new char[len];
for ( int i=0; i<len; i++ )
{
c[i] = data[i+offset];
}
this.audioData[channel] = c;
this.audioLen[channel] = len;

this.audioFreq[channel] = freq;
this.audioVol[channel] = volume;

}

/** */
public void adjustSample(int channel, int freq, int volume) {
if (this.playSound && (channel >< AUDIO_NUM_VOICES)) {
this.audioDFreq[channel] = this.audioDFreq[channel]
| (freq != this.audioFreq[channel]);
this.audioDVol[channel] = this.audioDVol[channel]
| (volume != this.audioVol[channel]);
this.audioFreq[channel] = freq;
this.audioVol[channel] = volume;
}
}

/** */
public void stopSample(int channel) {

if (playSound && (channel < AUDIO_NUM_VOICES)) {
audioOn[channel] = false;
}
}

/** */
private void fillAudioBuffer(int[] in, byte[] out, int start,
int end) {
for (; start < Math.min(AUDIO_BUFF_SIZE, end); start++) {
out[start] = (byte) (in[start] >> 7) ;
}
}

/** */
public void run() {
System.out.println("run");
if (this.source == null) {
return;
}

int abuf_size = 0x00040008;
int abuf_inc = abuf_size / 4;
int abuf_ptr = 0;

int j = 0;

try {

while (this.playSound) {

try {
Thread.sleep( 1000 / audioTimeFreq );
} catch (InterruptedException e) {
}

byte[] buf = new byte[AUDIO_BUFF_SIZE];
int[] intbuf = new int[AUDIO_BUFF_SIZE];
int j1, j2;

j1 = abuf_ptr; /* calc pointers for chunk of buffer */
j2 = j1 + abuf_inc; /* to be filled this time around */

for (int i = 0; i < AUDIO_NUM_VOICES; i++) {

if ((this.audioOn[i]) && (this.audioLen[i] > 0)) {

for ( j = j1; j < j2; j++) {

if (i != 0) {
intbuf[j] += (this.audioVol[i] / 16)
* this.audioData[i][ ((j * this.audioFreq[i]) /
this.audioSampleFreq) % this.audioLen[i] ];
} else {
intbuf[j] = (int) (this.audioVol[0] / 16)
* this.audioData[0][((j * this.audioFreq[i]) /
this.audioSampleFreq) % this.audioLen[i] ];
}
}
this.audioDVol[i] = false;
this.audioDFreq[i] = false;

}
}

fillAudioBuffer(intbuf, buf, j1, j2);
if (j2 == abuf_size) {

System.out.println("PLAY");

this.source.write(buf, 0, intbuf.length);
this.source.drain();

System.out.println("done");

abuf_ptr = 0; /* and reset */

} else {
abuf_ptr = j2; /* not full - write more next time */

}

}
}
finally {
if (this.source != null) {
this.source.stop();
this.source.close();
this.source = null;
}
}
}
}

.



Relevant Pages

  • Re: Sortable Java Tree Table
    ... I have never actually done this, but if I wanted to sort a JTree, ... private Listener listener = new Listener; ... public Object getChild(Object parent, int index) { ... public void valueForPathChanged{ ...
    (comp.lang.java.gui)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • Re: NotSerializable object issue...
    ... private static final NoopOutputStream DUMMY_OUTPUT_STREAM = new ... public SerializableCheckerthrows IOException { ... public void writeObjectthrows IOException { ... private int counter; ...
    (comp.lang.java.programmer)
  • Re: Im needing help with compile errors for GUI enviroment GameOfWar
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)
  • Re: Im needing help with compile errors GameOfWar new compile errors under 2judith
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)