How to decode and play gsm audio files?

1.6k views Asked by At

I have already posted a question here on how to play GSM files few weeks ago. I have faced lots of issues in order to decode and play back the stream coming from the server using jcifs.

The following code works properly with uncompressed audio wav files but it does not work with GSM files. When I try to play gsm files I get the error below.

To play GSM files I just added the Tritonus jars into the build path

Noted: I have added to the build path the following required jars:

Plase be aware that the file is a GSM file but the extension is .wav

tritonus_share-0.3.6.jar Download
tritonus_gsm-0.3.6.jar Download

import java.io.*;
import javax.sound.sampled.*;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

/**
 * Use SourceDataLine to read sound files from network storage using jcifs.
 */

public class SoundJCIFSLineTest 
{
   public static void main(String[] args) 
   {
      SourceDataLine soundLine = null;

      int BUFFER_SIZE = 64*1024;  // 64 KB

      // Set up an audio input stream piped from the sound file.
      try 
      {
         //Maked the connection fast 
         jcifs.Config.setProperty("resolveOrder", "DNS");

         String originPath="smb://192.168.1.1/audiofolder/audiofile.wav";       

         NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "username", "password");

         //This is the audio file the needs to be downloaded from the network storage   
         SmbFile audioFile = new SmbFile(originPath, auth);
         SmbFileInputStream smbFileInputStream = new SmbFileInputStream(audioFile);

         BufferedInputStream buf = new BufferedInputStream(smbFileInputStream);

         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(buf);
         AudioFormat audioFormat = audioInputStream.getFormat();

         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
         soundLine = (SourceDataLine) AudioSystem.getLine(info);
         soundLine.open(audioFormat);
         soundLine.start();

         int nBytesRead = 0;
         byte[] sampledData = new byte[BUFFER_SIZE];

         while (nBytesRead != -1) 
         {
            nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
            if (nBytesRead >= 0) 
            {
               // Writes audio data to the mixer via this source data line.
               soundLine.write(sampledData, 0, nBytesRead);
            }
         }
      } 
      catch (UnsupportedAudioFileException ex) 
      {
         ex.printStackTrace();
      } 
      catch (IOException ex) 
      {
         ex.printStackTrace();
      } 
      catch (LineUnavailableException ex) 
      {
         ex.printStackTrace();
      } 
      finally 
      {
         soundLine.drain();
         soundLine.close();
      }
   }
}

Error:

 javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1119)
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:36)
Exception in thread "main" java.lang.NullPointerException
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:71)

These are some details of the file I am trying to play

Channels       : 1
Sample Rate    : 8000
Precision      : 16-bit
Duration       : 00:04:36.92 = 2215360 samples ~ 20769 CDDA sectors
File Size      : 450k
Bit Rate       : 13.0k
Sample Encoding: GSM
0

There are 0 answers