Search in sources :

Example 21 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class SF2Sample method getData.

public Object getData() {
    AudioFormat format = getFormat();
    /*
        if (sampleFile != null) {
            FileInputStream fis;
            try {
                fis = new FileInputStream(sampleFile);
                RIFFReader riff = new RIFFReader(fis);
                if (!riff.getFormat().equals("RIFF")) {
                    throw new RIFFInvalidDataException(
                        "Input stream is not a valid RIFF stream!");
                }
                if (!riff.getType().equals("sfbk")) {
                    throw new RIFFInvalidDataException(
                        "Input stream is not a valid SoundFont!");
                }
                while (riff.hasNextChunk()) {
                    RIFFReader chunk = riff.nextChunk();
                    if (chunk.getFormat().equals("LIST")) {
                        if (chunk.getType().equals("sdta")) {
                            while(chunk.hasNextChunk()) {
                                RIFFReader chunkchunk = chunk.nextChunk();
                                if(chunkchunk.getFormat().equals("smpl")) {
                                    chunkchunk.skip(sampleOffset);
                                    return new AudioInputStream(chunkchunk,
                                            format, sampleLen);
                                }
                            }
                        }
                    }
                }
                return null;
            } catch (Exception e) {
                return new Throwable(e.toString());
            }
        }
        */
    InputStream is = data.getInputStream();
    if (is == null)
        return null;
    return new AudioInputStream(is, format, data.capacity());
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Example 22 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class SoftAudioBuffer method swap.

public void swap(SoftAudioBuffer swap) {
    int bak_size = size;
    float[] bak_buffer = buffer;
    boolean bak_empty = empty;
    AudioFormat bak_format = format;
    AudioFloatConverter bak_converter = converter;
    byte[] bak_converter_buffer = converter_buffer;
    size = swap.size;
    buffer = swap.buffer;
    empty = swap.empty;
    format = swap.format;
    converter = swap.converter;
    converter_buffer = swap.converter_buffer;
    swap.size = bak_size;
    swap.buffer = bak_buffer;
    swap.empty = bak_empty;
    swap.format = bak_format;
    swap.converter = bak_converter;
    swap.converter_buffer = bak_converter_buffer;
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat)

Example 23 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class UlawCodec method getAudioInputStream.

/**
     */
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
    AudioFormat sourceFormat = sourceStream.getFormat();
    AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
    if (sourceEncoding.equals(targetEncoding)) {
        return sourceStream;
    } else {
        AudioFormat targetFormat = null;
        if (!isConversionSupported(targetEncoding, sourceStream.getFormat())) {
            throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
        }
        if (AudioFormat.Encoding.ULAW.equals(sourceEncoding) && AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)) {
            targetFormat = new AudioFormat(targetEncoding, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), 2 * sourceFormat.getChannels(), sourceFormat.getSampleRate(), sourceFormat.isBigEndian());
        } else if (AudioFormat.Encoding.PCM_SIGNED.equals(sourceEncoding) && AudioFormat.Encoding.ULAW.equals(targetEncoding)) {
            targetFormat = new AudioFormat(targetEncoding, sourceFormat.getSampleRate(), 8, sourceFormat.getChannels(), sourceFormat.getChannels(), sourceFormat.getSampleRate(), false);
        } else {
            throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
        }
        return getAudioInputStream(targetFormat, sourceStream);
    }
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat)

Example 24 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class UlawCodec method getConvertedStream.

// OLD CODE
/**
     * Opens the codec with the specified parameters.
     * @param stream stream from which data to be processed should be read
     * @param outputFormat desired data format of the stream after processing
     * @return stream from which processed data may be read
     * @throws IllegalArgumentException if the format combination supplied is
     * not supported.
     */
/*  public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) { */
private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
    AudioInputStream cs = null;
    AudioFormat inputFormat = stream.getFormat();
    if (inputFormat.matches(outputFormat)) {
        cs = stream;
    } else {
        cs = (AudioInputStream) (new UlawCodecStream(stream, outputFormat));
    }
    return cs;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Example 25 with AudioFormat

use of javax.sound.sampled.AudioFormat in project JMRI by JMRI.

the class SoundUtil method bufferFromFile.

public static byte[] bufferFromFile(String filename, float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) throws java.io.IOException, javax.sound.sampled.UnsupportedAudioFileException {
    File sourceFile = new File(filename);
    // Get the type of the source file. We need this information 
    // later to write the audio data to a file of the same type. 
    AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(sourceFile);
    //AudioFileFormat.Type targetFileType = fileFormat.getType(); 
    AudioFormat audioFormat = fileFormat.getFormat();
    // get desired output format
    AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    // get a conversion stream
    // (Errors not checked yet)
    AudioInputStream stream = AudioSystem.getAudioInputStream(sourceFile);
    AudioInputStream inputAIS = AudioSystem.getAudioInputStream(format, stream);
    // Read the audio data into a memory buffer. 
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();
    byte[] abBuffer = new byte[nBufferSize];
    while (true) {
        if (log.isDebugEnabled()) {
            log.debug("trying to read (bytes): " + abBuffer.length);
        }
        int nBytesRead = inputAIS.read(abBuffer);
        if (log.isDebugEnabled()) {
            log.debug("read (bytes): " + nBytesRead);
        }
        if (nBytesRead == -1) {
            break;
        }
        baos.write(abBuffer, 0, nBytesRead);
    }
    // Create byte array
    byte[] abAudioData = baos.toByteArray();
    return abAudioData;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)74 AudioInputStream (javax.sound.sampled.AudioInputStream)32 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)11 AudioFileFormat (javax.sound.sampled.AudioFileFormat)10 SourceDataLine (javax.sound.sampled.SourceDataLine)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataInputStream (java.io.DataInputStream)6 File (java.io.File)6 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 Vector (java.util.Vector)5 DataLine (javax.sound.sampled.DataLine)5 LineUnavailableException (javax.sound.sampled.LineUnavailableException)5 SequenceInputStream (java.io.SequenceInputStream)4 AudioMetaData (ddf.minim.AudioMetaData)3 DataOutputStream (java.io.DataOutputStream)3