Java Code Examples for javax.sound.sampled.AudioFormat#getSampleRate()

The following examples show how to use javax.sound.sampled.AudioFormat#getSampleRate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioFloatFormatConverter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
                                            AudioInputStream sourceStream) {
    if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
        throw new IllegalArgumentException(
                "Unsupported conversion: " + sourceStream.getFormat()
                        .toString() + " to " + targetEncoding.toString());
    }
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(Encoding.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
            channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
 
Example 2
Source File: Toolkit.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) {
    // we can't open the device for non-PCM playback, so we have
    // convert any other encodings to PCM here (at least we try!)
    AudioFormat af = ais.getFormat();

    if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) &&
        (!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) {

        try {
            AudioFormat newFormat =
                new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
                                 af.getSampleRate(),
                                 16,
                                 af.getChannels(),
                                 af.getChannels() * 2,
                                 af.getSampleRate(),
                                 Platform.isBigEndian());
            ais = AudioSystem.getAudioInputStream(newFormat, ais);
        } catch (Exception e) {
            if (Printer.err) e.printStackTrace();
            ais = null;
        }
    }

    return ais;
}
 
Example 3
Source File: AudioFloatFormatConverter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public AudioInputStream getAudioInputStream(Encoding targetEncoding,
        AudioInputStream sourceStream) {
    if (sourceStream.getFormat().getEncoding().equals(targetEncoding))
        return sourceStream;
    AudioFormat format = sourceStream.getFormat();
    int channels = format.getChannels();
    Encoding encoding = targetEncoding;
    float samplerate = format.getSampleRate();
    int bits = format.getSampleSizeInBits();
    boolean bigendian = format.isBigEndian();
    if (targetEncoding.equals(Encoding.PCM_FLOAT))
        bits = 32;
    AudioFormat targetFormat = new AudioFormat(encoding, samplerate, bits,
            channels, channels * bits / 8, samplerate, bigendian);
    return getAudioInputStream(targetFormat, sourceStream);
}
 
Example 4
Source File: Toolkit.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static AudioInputStream getPCMConvertedAudioInputStream(AudioInputStream ais) {
    // we can't open the device for non-PCM playback, so we have
    // convert any other encodings to PCM here (at least we try!)
    AudioFormat af = ais.getFormat();

    if( (!af.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) &&
        (!af.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED))) {

        try {
            AudioFormat newFormat =
                new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
                                 af.getSampleRate(),
                                 16,
                                 af.getChannels(),
                                 af.getChannels() * 2,
                                 af.getSampleRate(),
                                 Platform.isBigEndian());
            ais = AudioSystem.getAudioInputStream(newFormat, ais);
        } catch (Exception e) {
            if (Printer.err) e.printStackTrace();
            ais = null;
        }
    }

    return ais;
}
 
Example 5
Source File: AudioFloatFormatConverter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
AudioFloatInputStreamChannelMixer(AudioFloatInputStream ais,
        int targetChannels) {
    this.sourceChannels = ais.getFormat().getChannels();
    this.targetChannels = targetChannels;
    this.ais = ais;
    AudioFormat format = ais.getFormat();
    targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(),
            targetChannels, (format.getFrameSize() / sourceChannels)
                    * targetChannels, format.getFrameRate(), format
                    .isBigEndian());
}
 
Example 6
Source File: WaveFloatFileWriter.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private AudioInputStream toLittleEndian(AudioInputStream ais) {
    AudioFormat format = ais.getFormat();
    AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(), format
            .getChannels(), format.getFrameSize(), format.getFrameRate(),
            false);
    return AudioSystem.getAudioInputStream(targetFormat, ais);
}
 
Example 7
Source File: EmergencySoundbank.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static SF2Sample newSimpleFFTSample(SF2Soundbank sf2, String name,
        double[] data, double base, int fadeuptime) {

    int fftsize = data.length / 2;
    AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
    double basefreq = (base / fftsize) * format.getSampleRate() * 0.5;

    randomPhase(data);
    ifft(data);
    data = realPart(data);
    normalize(data, 0.9);
    float[] fdata = toFloat(data);
    fdata = loopExtend(fdata, fdata.length + 512);
    fadeUp(fdata, fadeuptime);
    byte[] bdata = toBytes(fdata, format);

    /*
     * Create SoundFont2 sample.
     */
    SF2Sample sample = new SF2Sample(sf2);
    sample.setName(name);
    sample.setData(bdata);
    sample.setStartLoop(256);
    sample.setEndLoop(fftsize + 256);
    sample.setSampleRate((long) format.getSampleRate());
    double orgnote = (69 + 12)
            + (12 * Math.log(basefreq / 440.0) / Math.log(2));
    sample.setOriginalPitch((int) orgnote);
    sample.setPitchCorrection((byte) (-(orgnote - (int) orgnote) * 100.0));
    sf2.addResource(sample);

    return sample;
}
 
Example 8
Source File: AudioFloatFormatConverter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
                                                // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 9
Source File: AudioFloatFormatConverter.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
                                                // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 10
Source File: Toolkit.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
        return false;
    }
    if ((format.getFrameRate() <= 0)
        || (format.getSampleRate() <= 0)
        || (format.getSampleSizeInBits() <= 0)
        || (format.getFrameSize() <= 0)
        || (format.getChannels() <= 0)) {
        return false;
    }
    return true;
}
 
Example 11
Source File: WaveFloatFileWriter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private AudioInputStream toLittleEndian(AudioInputStream ais) {
    AudioFormat format = ais.getFormat();
    AudioFormat targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(), format
            .getChannels(), format.getFrameSize(), format.getFrameRate(),
            false);
    return AudioSystem.getAudioInputStream(targetFormat, ais);
}
 
Example 12
Source File: SoftMixingDataLine.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public AudioFloatInputStreamResampler(AudioFloatInputStream ais,
        AudioFormat format) {
    this.ais = ais;
    AudioFormat sourceFormat = ais.getFormat();
    targetFormat = new AudioFormat(sourceFormat.getEncoding(), format
            .getSampleRate(), sourceFormat.getSampleSizeInBits(),
            sourceFormat.getChannels(), sourceFormat.getFrameSize(),
            format.getSampleRate(), sourceFormat.isBigEndian());
    nrofchannels = targetFormat.getChannels();
    Object interpolation = format.getProperty("interpolation");
    if (interpolation != null && (interpolation instanceof String)) {
        String resamplerType = (String) interpolation;
        if (resamplerType.equalsIgnoreCase("point"))
            this.resampler = new SoftPointResampler();
        if (resamplerType.equalsIgnoreCase("linear"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("linear1"))
            this.resampler = new SoftLinearResampler();
        if (resamplerType.equalsIgnoreCase("linear2"))
            this.resampler = new SoftLinearResampler2();
        if (resamplerType.equalsIgnoreCase("cubic"))
            this.resampler = new SoftCubicResampler();
        if (resamplerType.equalsIgnoreCase("lanczos"))
            this.resampler = new SoftLanczosResampler();
        if (resamplerType.equalsIgnoreCase("sinc"))
            this.resampler = new SoftSincResampler();
    }
    if (resampler == null)
        resampler = new SoftLinearResampler2(); // new
    // SoftLinearResampler2();
    pitch[0] = sourceFormat.getSampleRate() / format.getSampleRate();
    pad = resampler.getPadding();
    pad2 = pad * 2;
    ibuffer = new float[nrofchannels][buffer_len + pad2];
    ibuffer2 = new float[nrofchannels * buffer_len];
    ibuffer_index = buffer_len + pad;
    ibuffer_len = buffer_len;
}
 
Example 13
Source File: Toolkit.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void isFullySpecifiedAudioFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.ULAW)
        && !format.getEncoding().equals(AudioFormat.Encoding.ALAW)) {
        // we don't know how to verify possibly non-linear encodings
        return;
    }
    if (format.getFrameRate() <= 0) {
        throw new IllegalArgumentException("invalid frame rate: "
                                           +((format.getFrameRate()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getFrameRate())));
    }
    if (format.getSampleRate() <= 0) {
        throw new IllegalArgumentException("invalid sample rate: "
                                           +((format.getSampleRate()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getSampleRate())));
    }
    if (format.getSampleSizeInBits() <= 0) {
        throw new IllegalArgumentException("invalid sample size in bits: "
                                           +((format.getSampleSizeInBits()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getSampleSizeInBits())));
    }
    if (format.getFrameSize() <= 0) {
        throw new IllegalArgumentException("invalid frame size: "
                                           +((format.getFrameSize()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getFrameSize())));
    }
    if (format.getChannels() <= 0) {
        throw new IllegalArgumentException("invalid number of channels: "
                                           +((format.getChannels()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getChannels())));
    }
}
 
Example 14
Source File: Upsample.java    From jipes with GNU Lesser General Public License v2.1 5 votes vote down vote up
private AudioFormat getProcessedAudioFormat(final AudioBuffer buffer) {
    final AudioFormat sourceAudioFormat = buffer.getAudioFormat();
    return new AudioFormat(sourceAudioFormat.getSampleRate() * factor,
            sourceAudioFormat.getSampleSizeInBits(),
            sourceAudioFormat.getChannels(),
            AudioFormat.Encoding.PCM_SIGNED.equals(sourceAudioFormat.getEncoding()),
            sourceAudioFormat.isBigEndian());
}
 
Example 15
Source File: Test.java    From DTMF-Decoder with MIT License 5 votes vote down vote up
public static void testPlay(String filename)
{
  try
  {
    File file = new File(filename);
    // Get AudioInputStream from given file.	
    AudioInputStream in= AudioSystem.getAudioInputStream(file);
    AudioInputStream din = null;
    if (in != null)
    {
        AudioFormat baseFormat = in.getFormat();
        AudioFormat  decodedFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);
         // Get AudioInputStream that will be decoded by underlying VorbisSPI
        din = AudioSystem.getAudioInputStream(decodedFormat, in);
        // Play now !
        rawplay(decodedFormat, din);
        in.close();		
    }
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}
 
Example 16
Source File: Toolkit.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static boolean isFullySpecifiedPCMFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {
        return false;
    }
    if ((format.getFrameRate() <= 0)
        || (format.getSampleRate() <= 0)
        || (format.getSampleSizeInBits() <= 0)
        || (format.getFrameSize() <= 0)
        || (format.getChannels() <= 0)) {
        return false;
    }
    return true;
}
 
Example 17
Source File: AudioFloatFormatConverter.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
AudioFloatInputStreamChannelMixer(AudioFloatInputStream ais,
        int targetChannels) {
    this.sourceChannels = ais.getFormat().getChannels();
    this.targetChannels = targetChannels;
    this.ais = ais;
    AudioFormat format = ais.getFormat();
    targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(),
            targetChannels, (format.getFrameSize() / sourceChannels)
                    * targetChannels, format.getFrameRate(), format
                    .isBigEndian());
}
 
Example 18
Source File: AudioFloatFormatConverter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
AudioFloatInputStreamChannelMixer(AudioFloatInputStream ais,
        int targetChannels) {
    this.sourceChannels = ais.getFormat().getChannels();
    this.targetChannels = targetChannels;
    this.ais = ais;
    AudioFormat format = ais.getFormat();
    targetFormat = new AudioFormat(format.getEncoding(), format
            .getSampleRate(), format.getSampleSizeInBits(),
            targetChannels, (format.getFrameSize() / sourceChannels)
                    * targetChannels, format.getFrameRate(), format
                    .isBigEndian());
}
 
Example 19
Source File: Toolkit.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void isFullySpecifiedAudioFormat(AudioFormat format) {
    if (!format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)
        && !format.getEncoding().equals(AudioFormat.Encoding.ULAW)
        && !format.getEncoding().equals(AudioFormat.Encoding.ALAW)) {
        // we don't know how to verify possibly non-linear encodings
        return;
    }
    if (format.getFrameRate() <= 0) {
        throw new IllegalArgumentException("invalid frame rate: "
                                           +((format.getFrameRate()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getFrameRate())));
    }
    if (format.getSampleRate() <= 0) {
        throw new IllegalArgumentException("invalid sample rate: "
                                           +((format.getSampleRate()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getSampleRate())));
    }
    if (format.getSampleSizeInBits() <= 0) {
        throw new IllegalArgumentException("invalid sample size in bits: "
                                           +((format.getSampleSizeInBits()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getSampleSizeInBits())));
    }
    if (format.getFrameSize() <= 0) {
        throw new IllegalArgumentException("invalid frame size: "
                                           +((format.getFrameSize()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getFrameSize())));
    }
    if (format.getChannels() <= 0) {
        throw new IllegalArgumentException("invalid number of channels: "
                                           +((format.getChannels()==-1)?
                                             "NOT_SPECIFIED":String.valueOf(format.getChannels())));
    }
}
 
Example 20
Source File: AudioSignalSource.java    From jipes with GNU Lesser General Public License v2.1 4 votes vote down vote up
private AudioFormat toProcessedAudioFormat(final AudioFormat sourceAudioFormat) {
    return new AudioFormat(sourceAudioFormat.getSampleRate(), FLOAT_SAMPLE_SIZE_IN_BITS, sourceAudioFormat.getChannels(),
            AudioFormat.Encoding.PCM_SIGNED.equals(sourceAudioFormat.getEncoding()),
            sourceAudioFormat.isBigEndian());
}