8 Replies Latest reply: Oct 18, 2010 4:43 PM by captfoss RSS

    Introduction to Mixer objects

    804900
      Hello,

      I am a relative beginner to Java Sound, so I appologise for the inanity of the question.

      I have a WAV file I want to manipulate, and am wanting to use a Mixer object to do so. Various tutorials I have looked at talk of InputStreams from devices, and have seen no talk of input from a file. Is this possible? if so, I would be grateful for a link to relavent tutorials, or for example code.

      The alternative to this is to use DSP, but as this is far more complicated, I would like to explore Mixer avenues first.

      Many thanks,
      Fiona
        • 1. Re: Introduction to Mixer objects
          captfoss
          801897 wrote:
          I have a WAV file I want to manipulate, and am wanting to use a Mixer object to do so. Various tutorials I have looked at talk of InputStreams from devices, and have seen no talk of input from a file. Is this possible? if so, I would be grateful for a link to relavent tutorials, or for example code.
          AudioInputStream ais = AudioSystem.getAudioInputStream(new File("test.wav"));
          • 2. Re: Introduction to Mixer objects
            804900
            I now have the Mixer working, and am trying to get the wav file in, alter it then save it.

            I can read it in, but it is automatically sent to the speaker, then the output is unrecognisable (27 minutes longer than input).

            The code is an altered version of some example code, and i am having trouble figuring out what still needs to be changed:
                        File soundFile = file;
                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                        AudioFormat audioFormat = audioInputStream.getFormat();
                        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            
                        Mixer.Info[] mixerInfo =
                                AudioSystem.getMixerInfo();
                        System.out.println("Available mixers:");
                        for (int cnt = 0; cnt < mixerInfo.length;
                                cnt++) {
                            System.out.println(mixerInfo[cnt].getName());
                        }
            
                        Mixer mixer = AudioSystem.getMixer(mixerInfo[6]);
            
                        soundLine = (SourceDataLine) mixer.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);//need to make sure doesnt print to speaker
                            }
                        }
            
                        Control[] controls = soundLine.getControls();
                        System.out.println("getting controls");
                        for (int i = 0; i < controls.length; i++) {
                            System.out.println(controls.getType());
            }



            // FloatControl speedControl = (FloatControl) soundLine.getControl(FloatControl.Type.SAMPLE_RATE);
            // FloatControl volumeControl = (FloatControl) soundLine.getControl(FloatControl.Type.MASTER_GAIN);


            AudioFileFormat.Type fileType = null;
            File audioFile = null;
            fileType = AudioFileFormat.Type.WAVE;
            audioFile = new File("exampleOutput.wav");

            DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
            targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);


            targetDataLine.open(audioFormat);
            targetDataLine.start();
            AudioSystem.write(
            new AudioInputStream(targetDataLine),
            fileType,
            audioFile);



            Thanks again,
            Fiona

            Edited by: Fillis52 on 16-Oct-2010 08:57
            Appologies. am new to forums, and have obviously missed something.
            • 3. Re: Introduction to Mixer objects
              793415
              Please use the code tags, as described in the top post of the forum that you apparently did not read.
              • 4. Re: Introduction to Mixer objects
                804900
                Now that the code is properly formatted, are people willing to help?

                I know that most people use mixers to input data then send it directly to the speakers, but any help in inputting data to the mixer without sending it to the speakers would be really helpful

                Many thanks,
                Fiona
                • 5. Re: Introduction to Mixer objects
                  captfoss
                  Fillis52 wrote:
                  Now that the code is properly formatted, are people willing to help?
                  No one here gets paid to help you... so let's lose the attitude, ok? If someone trying to help you
                  I know that most people use mixers to input data then send it directly to the speakers, but any help in inputting data to the mixer without sending it to the speakers would be really helpful
                  It depends on the mixer you're using, honestly...

                  http://www.vsj.co.uk/java/display.asp?id=370

                  The first thing to note is, if you have a SourceDataLine and a TargetDataLine from the same mixer object...whatever is written to (the set of) SourceDataLine will be mixed and sent to the TargetDataLine... JavaSound should always have at least one software mixer than will allow you to mix without rendering the sound...

                  So, for instance, you could write a program that mixes audio files using JavaSound
                  http://www.jsresources.org/examples/AudioConcat.html

                  Anyway, the second thing to keep in mind is that you can always just grab the Speaker port and turn it off...
                  Port.Info recPortInfo = new Port.Info(Port.class, "SPEAKER", true);
                  Port recPort = (Port)AudioSystem.getLine(recPortInfo);
                   
                  recPort.open();
                  Control [] controls = recPort.getControls();
                  for(int i=0; i<controls.length; i++) {
                    if(controls[i] instanceof CompoundControl) {
                      Control[] members =((CompoundControl)controls).getMemberControls();
                  for(int j=0; j<members.length; j++) {
                  if(members[j].getType().toString().equals(“Select”)) {
                  ((BooleanControl)members[j]).setValue(false);
                  }
                  }
                  }
                  else {
                  if(controls[i].getType().toString().equals(“Select”)) {
                  ((BooleanControl)controls[i]).setValue(false);
                  }
                  }
                  }

                  recPort.close();



                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                  • 6. Re: Introduction to Mixer objects
                    804900
                    Thanks for the links, they are very helpful.

                    Also, sorry about the attitude, I just didn't appriciate being reprimanded for not knowing the quirks of the new website.

                    The end result I am looking for is to alter the frequency and sample rate of an audio file, but with maintaining the original length. I believe that DFT will be more successful than the Mixer and various controls. Although I have heard that they are a lot more complicated, so I can see that being a catastrophe.


                    thanks again,
                    Fiona
                    • 7. Re: Introduction to Mixer objects
                      captfoss
                      Fillis52 wrote:
                      Thanks for the links, they are very helpful.

                      Also, sorry about the attitude, I just didn't appriciate being reprimanded for not knowing the quirks of the new website.
                      Andrew Thompson wrote:Please use the code tags, as described in the top post of the forum that you apparently did not read.
                      I'd hardly consider that a reprimand... regardless, the older members don't appriciate having to read unformatted code so you and Andrew are, at worst, even ;-)
                      The end result I am looking for is to alter the frequency and sample rate of an audio file, but with maintaining the original length. I believe that DFT will be more successful than the Mixer and various controls. Although I have heard that they are a lot more complicated, so I can see that being a catastrophe.
                      I actually have a link for that as well...

                      http://jvalentino2.tripod.com/dft/index.html

                      I'm not 100% sure what DFTs do, but, there's a tutorial on them none the less...

                      And for altering the frequency, you'll want to a FFT (fast fourier transform) rather than a DFT... just because of the speed differential. Honestly, implementing an FFT shouldn't be too hard, as long as you're a decent coder and you've got some sort of a background in scientific java computing...
                      • 8. Re: Introduction to Mixer objects
                        captfoss
                        If all you're wanting to do is modify the raw audio data, you probably don't need to deal with Mixers at all... you can just read from an AudioInputStream, modify the data, and then write it back to the disk directly (you'll need to create an OutputStream linked to an InputStream linked to a 2nd AudioInputStream in order to write it back to disk, but, that's trivial if you use PipedStreams...)