13 Replies Latest reply: Nov 8, 2010 1:27 PM by captfoss RSS

    Writing an AudioInputStream to an array

    810675
      I've been searching for quite some time for a solution to my problem. Without any luck :(

      Here's the deal:

      I want to play a soundfile, but at the same time I want to save the file in an array (an array of amplitudes or levels). Afterwards I want to take out some of the samples (resampling to a lower sampling rate). The signal has to be used to draw a line on a screen (like a spectrum).

      Does anyone have an idea of how to do this? Is it possible to load the wave file directly into an array?

      Edited by: 807672 on Nov 6, 2010 12:31 PM
        • 1. Re: Writing an AudioInputStream to an array
          DrClap
          An array? An array of what? If you want to write it to an array of bytes, then what you do is copy the contents of the AudioInputStream to a ByteArrayOutputStream. When you're finished, the ByteArrayOutputStream has a method whereby you can extract an array of bytes from it.
          • 2. Re: Writing an AudioInputStream to an array
            793415
            807672 wrote:
            ..I want to play a soundfile, but at the same time I want to save the file in an array. Afterwards I want to take out some of the samples (resampling to a lower sampling rate). The signal has to be used to draw a line on a screen (like a spectrum).
            I developed BigClip (http://pscode.org/javadoc/org/pscode/xui/sound/bigclip/BigClip.html (a)) to work with byte arrays. I would have used Clip but for the fact that is will not support large amounts of data as might be found in a 10+ minute, 16 bit, stereo, 44.1 KHz MP3. My main interest was to get an oscilloscope style trace of the sound as it was being played, but you apparently need a Fast Fourier Transform or similar for spectrum analysis.

            a) The line..
            public class BigClip
            ..at the top links to the source, though I have added a number of tweaks to the functionality since then, including double speed fast forward/rewind.
            • 3. Re: Writing an AudioInputStream to an array
              810675
              Perhaps I didn't state the question clearly...

              What I want is the amplitude at certain times (for instance, the amplitude at every 8th sample).
              • 4. Re: Writing an AudioInputStream to an array
                Kayaman
                807672 wrote:
                Perhaps I didn't state the question clearly...

                What I want is the amplitude at certain times (for instance, the amplitude at every 8th sample).
                Well, you were given an answer by jverd. It's just working with streams, nothing complicated.
                • 5. Re: Writing an AudioInputStream to an array
                  810675
                  I think there's something fundamental that I don't understand...

                  Running the getLevel() method on the DataLine always gives me 0.0. Why's that?

                  At the moment I am just trying to get the level for each frame.

                  Can you guide me to a text talking about streams? I am a bit lost here.

                  Edited by: 807672 on Nov 6, 2010 12:27 PM
                  • 6. Re: Writing an AudioInputStream to an array
                    793415
                    807672 wrote:
                    ..Running the getLevel() method on the DataLine always gives me 0.0. Why's that?
                    Are you sure it is not a value that is less than 0.0?
                    • 7. Re: Writing an AudioInputStream to an array
                      810675
                      Yes I am sure. No matter when I do a

                      System.out.println("level: " + line.getLevel());

                      I get 0.0.
                      • 8. Re: Writing an AudioInputStream to an array
                        793415
                        807672 wrote:
                        ..I get 0.0.
                        Huh. I think it is time you posted an SSCCE (http://pscode.org/sscce.html).
                        • 9. Re: Writing an AudioInputStream to an array
                          810675
                          import java.io.IOException;
                          import javax.sound.sampled.AudioFormat;
                          import javax.sound.sampled.AudioInputStream;
                          import javax.sound.sampled.AudioSystem;
                          import javax.sound.sampled.DataLine;
                          import javax.sound.sampled.LineUnavailableException;
                          import javax.sound.sampled.SourceDataLine;
                          import javax.sound.sampled.UnsupportedAudioFileException;
                          import java.net.URL;
                          
                          public class test {
                               public static void main(String[] args) {
                                    try {
                                         URL url = new URL("http://pscode.org/media/leftright.wav");
                                         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
                                         AudioFormat     audioFormat = audioInputStream.getFormat();
                                         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
                                         SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                                         line.open(audioFormat);
                                         line.start();
                                                                       
                                         System.out.println("level: " + line.getLevel());
                          
                                         int     nBytesRead = 0;
                                         byte[]     abData = new byte[128000];
                                         while (nBytesRead != -1) {
                                              nBytesRead = audioInputStream.read(abData, 0, abData.length);
                                              
                                              if (nBytesRead >= 0)
                                                   line.write(abData, 0, nBytesRead);
                                              
                                         }
                                         line.drain();
                                         line.close();
                                         line = null;
                                         System.exit(0);
                                    } catch(IOException e) {
                                         e.printStackTrace();
                                    } catch (UnsupportedAudioFileException e) {
                                         e.printStackTrace();
                                    } catch (LineUnavailableException e) {
                                         e.printStackTrace();
                                    }
                               }
                          }
                          Edited by: 807672 on Nov 7, 2010 2:28 PM

                          Edited by: 807672 on Nov 7, 2010 2:43 PM
                          • 10. Re: Writing an AudioInputStream to an array
                            793415
                            Good work, but please edit your post in 3 ways:
                            <li> Do not try to use formatting like bold within a code sample, use a code comment instead.
                            <li> Use code format tags, as mentioned on the 'sticky post' at the top of the forum. Those comments are there for a reason.
                            <li> Another tip for doing a media based SSCCE is to hot-link to a resource available on the internet. This makes it easier for those trying to help, in that they do not have to (find a sound file on their disks &) alter the source to point to it. I provide some sound samples at http://pscode.org/media/#sound. The leftright.wav is fairly small and should do for this. Access by URL, rather than by File.

                            Do those 3 things, and I will look into it further.
                            • 11. Re: Writing an AudioInputStream to an array
                              DarrylBurke
                              Moderator action: Moved from Java Programming.

                              db
                              • 12. Re: Writing an AudioInputStream to an array
                                810675
                                Andrew Thompson wrote:
                                Good work, but please edit your post in 3 ways:
                                <li> Do not try to use formatting like bold within a code sample, use a code comment instead.
                                <li> Use code format tags, as mentioned on the 'sticky post' at the top of the forum. Those comments are there for a reason.
                                <li> Another tip for doing a media based SSCCE is to hot-link to a resource available on the internet. This makes it easier for those trying to help, in that they do not have to (find a sound file on their disks &) alter the source to point to it. I provide some sound samples at http://pscode.org/media/#sound. The leftright.wav is fairly small and should do for this. Access by URL, rather than by File.

                                Do those 3 things, and I will look into it further.
                                Thanks for pointing these things out. I will remember from now on!
                                • 13. Re: Writing an AudioInputStream to an array
                                  captfoss
                                  You just read data from the AIS, and then consider every frame-length bits (8,16,24,32, etc) to be a numeric value. You convert the appropriate number of bytes into an integer using information from the audio format (big endian, signed, etc) and then those are your amplitude values.

                                  Want every 8th sample? Pretty easy to figure out how to get it from there ;-)