6

I want to play a WAV file over GSM modem. Here is my sample code

private final int BUFFER_SIZE = 8;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
public void playSound(String filename) throws IOException{

    String strFilename = filename;

    try {
        soundFile = new File(strFilename);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    try {
        audioStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e){
        e.printStackTrace();
        System.exit(1);
    }

    audioFormat = audioStream.getFormat();

    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);

    int nBytesRead = 0;
    byte[] abData = new byte[BUFFER_SIZE];
    while (nBytesRead != -1) {
        try {
            nBytesRead = audioStream.read(abData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nBytesRead >= 0) {
            outputStream.write(abData, 0, nBytesRead);
            outputStream.flush();
        }
    }
}

But the problem is the WAV file sending through serial port is playing very fast. I don't know what's the problem . Here is my WAV file description:

ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame, Audio Sample Rate 8Khz.

Can anyone help me to solve the issue?

Shantanu Banerjee
  • 1,417
  • 6
  • 31
  • 51

3 Answers3

1

I would check the following - especially #3.

  1. Synchronization
  2. AudioFormat
  3. JSSRC Resampling Library
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

How about sleeping a while after "outputStream.flush();"
might be Thread.sleep(50)

goodsong
  • 46
  • 2
  • is there any calculation for sleep time ?? – Shantanu Banerjee Nov 14 '13 at 07:45
  • This is definitely not the way to do this. Even if you could get a steady sample rate at sending end this way (which you can't, unless you have a real time OS...), the receiving end expects certain sample rate and at best you'd get really bad sound quality if you limited the sample rate this way at sending end. – hyde Nov 18 '13 at 06:44
1

I think your problem is at the receiving end and doing playback. Set audio sample rate there to match your audio data. Also make sure serial port flow control is enabled (or look into it if you get correct playback speed, but parts of audio are lost). You know the sample rate of the file, so set the receiving end to have same sample rate (and other parameters).

If receiving end is out of reach or can't be changed, you need to change the sample rate at the transmitting end to match what receiver expects. Easiest is to use some audio editor (such as SoX, which is command line tool) to change the audio file. You should try this first, just to check that you can get good playback with right audio format.

More flexible way is to do it in your program, so you can feed it any audio file, and then it will convert it to correct sample rate and play it correctly. But this is of course more complex, too. Look for a library, such as the one recommended in that other answer by Elliott Frisch.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • *"... you need to change the sample rate at the transmitting end to match what receiver expects"* - I thought you put the modem in Voice Mode and then play the file into the modem. The playback into the modem has to match the way the sending modem is setup. I don't believe the called party's equipment has anything to do with it. – jww Feb 15 '19 at 21:49