Play WAV file via GSM modem

I want to play a WAV file through a 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 that the WAV file sent via the serial port plays very fast. I do not know what's the problem. Here is my description of the WAV file:

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

Can someone help me solve the problem?

+6
source share
4 answers

I would check the following - especially # 3.

+1
source

How to sleep after "outputStream.flush ();"
maybe Thread.sleep (50)

+1
source

I think your problem is on the receiving side and reproduction. Set the audio sampling rate for it according to your audio data. Also make sure that serial port flow control is enabled (or check it if you get the correct playback speed but parts of the audio are lost). You know the sample rate of the file, so set the receiving side to the same sample rate (and other parameters).

If the end result is unavailable or cannot be changed, you need to change the sample rate at the transmitting end to match the expected recipient. The easiest way is to use an audio editor (such as SoX , which is a command line tool) to modify the audio file. You should try this first, just to make sure you can get good playback with the correct audio format.

A more flexible way is to do this in your program so that you can feed it with any sound file, and then it will convert it to fix the sampling rate and play it correctly. But this, of course, is also more complicated. Look for a library such as the one recommended in this other Elliot Frisch answer.

+1
source

I found Asterisk .

Small introduction

Asterisk is an open source platform for building communication applications. Asterisk turns a regular computer into a communication server. Asterisk supports IP-PBX, VoIP-gateways, conference servers and other user solutions. It is used by small enterprises, large enterprises, call centers, carriers, and government agencies around the world. Asterisk is free and open source.

Java binding

Here is a link for Java developers. http://www.asterisk-java.org/development/tutorial.html

0
source

Source: https://habr.com/ru/post/957338/


All Articles