Menu

Write Audio to File

Anonymous
2015-10-14
2015-10-19
  • Anonymous

    Anonymous - 2015-10-14

    Hi

    I am trying to write audio to file using the JavaSoundAudioGrabber. However, not being very sucessfull. I have the following code:

    final JavaSoundAudioGrabber xa = new JavaSoundAudioGrabber(new AudioFormat(16, 44.1, 1));
    xa.setMaxBufferSize(this.sampleChunkSize);

        // get the audio format
        audioFormatXa = new javax.sound.sampled.AudioFormat(
                (int)xa.getFormat().getSampleRateKHz() * 1000, xa
                        .getFormat().getNBits(), xa.getFormat()
                        .getNumChannels(), xa.getFormat().isSigned(), xa
                        .getFormat().isBigEndian());
    
        new Thread(xa).start();
    
        // Hanning processor on top of the main audio stream
        final HanningAudioProcessor g =
                new HanningAudioProcessor(xa, this.img.getWidth() * xa.getFormat().getNumChannels())
            {
                @Override
                public SampleChunk processSamples(final SampleChunk sample)
                {
                    AudioCapture.this.updateProcessor(sample);
                    return sample;
                }
            };
    
        System.out.println("Using audio stream: " + g.getFormat());
    
        try {
            Thread.sleep(500);
            SampleChunk s = null;       
            while ((s = g.nextSampleChunk()) != null)   {
                        this.currentAudioBuffer.append(s); 
                   // do other processing here 
             }
    

    I then write the currentAudioBuffer later using :

       final byte[] rawBytes = currentAudioBuffer.getSamplesAsByteBuffer().array();
      ByteArrayInputStream bais = new ByteArrayInputStream(rawBytes);
    
    //audioFormatXa is taken from the JavaSoundAudioGrabber instance earlier not shown above.
    
    long length = (long)(rawBytes.length  / audioFormatXa.getFrameSize());
    
    AudioInputStream audioInputStreamTemp = new AudioInputStream(bais,audioFormatXa,length);
    
    AudioSystem.write(
                    audioInputStreamTemp
                    ,AudioFileFormat.Type.WAVE
                    ,new File("testfile.wav"));
    

    However, I just get noise in the file ? any pointers would be usefull

    Thanks in advance

     
  • David Dupplaw

    David Dupplaw - 2015-10-14

    Whenever that happened to me it was usually a big-endian/little-endian problem. You're capturing 16bit samples and writing them as 8 bit value so you need to make sure the bytes are in the right order. The format should do that for you, but maybe there's a problem there? Perhaps check that the byte array contains the expected values before you write it and that the wave file contains those values in its data block?

     
  • Anonymous

    Anonymous - 2015-10-19
    Post awaiting moderation.

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.