10 Replies Latest reply: Oct 18, 2010 1:55 AM by 803751 RSS

    how to play pcm raw data?

    803751
      i want to play audio which is .amr format file
      Java sound API do not support to decode amr file
      so i find amr decoder to decode .amr file , the output of decoder is PCM format
      (The decoder output information : 16-bit pulse code modulation (PCM) speech data sampled at 8 kHz.)
      and then i save the pcm data in a byte array
      this is my play pcm audio code :

      float sampleRate = 8000;
      int sampleSizeInBits = 16;
      int channel = 1;
      boolean signed = true;
      boolean bigEndian = true;

      byte[] data = getBytesFromFile(audioFile); // the byte[] data retrieve pcm format data
      InputStream audio = new ByteArrayInputStream(data);
      AudioFormat pcm = new AudioFormat(sampleRate, sampleSizeInBits,channel, signed, bigEndian);
      ain = new AudioInputStream(audio, pcm, data.length / pcm.getFrameSize());
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, pcm);
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(pcm);
      line.start();
      ......

      and then i execute the player
      but the result is noise
      i have no idea about that.....
      help me ,plz....
      sorry for my poor English :)

      Edited by: 800748 on 2010/10/7 上午 5:53
        • 1. Re: how to play pcm raw data?
          captfoss
          and then i execute the player
          but the result is noise
          What exactly are the contents of your "audioFile"? Does it contain a WAV file header? Because if it does, you're not handling that in your code and the audio system is going to try to play your fileheader. If the fileheader happens to not line up to a multiple of 16 bits, then your entire audio stream will be messed up because it'll be offset by 8 bits...
          • 2. Re: how to play pcm raw data?
            803751
            thank you for your answer , captfoss
            i hava no idea to identify my PCM file contain WAV file header
            how to know WAV file header?
            and if it does , how to deal with WAV file header?
            my decoder is use voiceage AMR-NB codec ( http://www.voiceage.com/openinit_amr.php )
            but the document does not indicate the decoder output contain WAV file header
            it only show decoder format
            ( just like : Input to the encoder is in 16-bit pulse code modulation (PCM) speech data sampled at 8 kHz. The decoder outputs the reconstructed speech data in the same format. Each input speech frame of 20 ms consists of 160 16-bit PCM words containing 14-bit left-aligned uniform samples. The encoder outputs compressed speech data in octet aligned (by using bit stuffing) AMR-NB Interface Format 2)

            i think the problem maybe in decoder
            because the output of decoder.exe is in file format
            but i not sure that the file Extension should be .pcm , .dat or others
            i save in .dat format
            and then read the .dat file into the byte array and play it.
            these are the flaws I can think of

            thank you very much
            sorry for my poor English :)
            • 3. Re: how to play pcm raw data?
              captfoss
              cecol wrote:
              how to know WAV file header?
              Save the file as a WAV file and try to play it with some audio program. If another audio program can play it, it means it had a WAV header.

              Figure that out and report back, and I'll advise you further once we know more about what you're dealing with.
              • 4. Re: how to play pcm raw data?
                803751
                Thank you for your answer,captfoss !!

                I try decode my .amr file again
                and i save the file in .wav extension,
                after that i play it with audio program , but the program cannot render the file
                so it indicate that decoder output file does not contain WAV file header.

                Without WAV file header problem , what to do next?

                Thank you very much !!
                Sorry for my poor English :)
                • 5. Re: how to play pcm raw data?
                  captfoss
                  http://www.jsresources.org/examples/SimpleAudioPlayer.java.html

                  That example plays raw PCM data by (1) reading it from a WAV file via an AudioInputStream (2) writing it to a SourceDataLine to be rendered.

                  I'd recommend you try a similar approach, but replace (1) with (1b) reading it directly from your file

                  So rather than reading from an AIS and writing to a SDL, you'd read it from your .amr file and write it to the SourceDataLine. You'll also need to modify the format parameters used to setup the SDL to match what your audio is, but, that should get around the WAV header problem...
                  • 6. Re: how to play pcm raw data?
                    803751
                    thank you for your answer ,captfoss!!
                    but the result of your recommendation is noise
                    I have tired that sample ,
                    i read my pcm raw data(or .amr file) and write into SDL ,but it only noise
                    I think there may be parameter of format issues , because i am not sure that the sampleRate ,sampleSizeInBits ...etc with .amr file
                    i tired all possable , but it only noise

                    The amr file is a hybrid speech coder which uses Algebraic Code Excited Linear Prediction (ACELP)
                    i think that it can not play with SDL , because SDL only accept PCM format

                    (What else can solve my problem....?)
                    (I am running out of time to finish my project....)

                    Thank you very much !!
                    sorry for my poor English :)
                    • 7. Re: how to play pcm raw data?
                      captfoss
                      cecol wrote:
                      The amr file is a hybrid speech coder which uses Algebraic Code Excited Linear Prediction (ACELP)
                      i think that it can not play with SDL , because SDL only accept PCM format
                      You need to use your decoder to decode the AMR file into the linear PCM bytes, and then write the PCM audio to the SDL... Sorry, I skipped a step in my previous explanation.
                      • 8. Re: how to play pcm raw data?
                        803751
                        thank you for your reply ,captfoss
                        i had tired the way you skipped,but it only noise
                        it appears this way can not be used

                        i believe that JavaSound have ability can play PCM data
                        there are something i have to adjust

                        thank you very much
                        sorry for my poor English :)
                        • 9. Re: how to play pcm raw data?
                          captfoss
                          cecol wrote:
                          thank you for your reply ,captfoss
                          i had tired the way you skipped,but it only noise
                          it appears this way can not be used
                          It can be used, you've just got one of the parameters incorrect... if you don't have absolutely every single parameter correct, you'll get noise from perfectly good sound data...
                          i believe that JavaSound have ability can play PCM data
                          WAV files are PCM data, so yes, JavaSound definately has the ability to play PCM data.

                          http://www.jsresources.org/examples/OscillatorPlayer.html

                          For example, here is a program that generates PCM data dynamically based on mathematical functions and plays the associated waveform... you know, just as a proof of concept that JavaSound can play random PCM data.
                          • 10. Re: how to play pcm raw data?
                            803751
                            Thank you for your answer,captfoss
                            I probably know how to deal with my pcm data in javasound API
                            Just parameters issue
                            I thank that i have to learn more information of pcm data

                            Thank you very much captfoss