Search in sources :

Example 46 with AudioFormat

use of javax.sound.sampled.AudioFormat in project algs4 by kevin-wayne.

the class StdAudio method save.

/**
 * Saves the double array as an audio file (using .wav or .au format).
 *
 * @param  filename the name of the audio file
 * @param  samples the array of samples
 * @throws IllegalArgumentException if unable to save {@code filename}
 * @throws IllegalArgumentException if {@code samples} is {@code null}
 */
public static void save(String filename, double[] samples) {
    if (samples == null) {
        throw new IllegalArgumentException("samples[] is null");
    }
    // assumes 44,100 samples per second
    // use 16-bit audio, mono, signed PCM, little Endian
    AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
    byte[] data = new byte[2 * samples.length];
    for (int i = 0; i < samples.length; i++) {
        int temp = (short) (samples[i] * MAX_16_BIT);
        data[2 * i + 0] = (byte) temp;
        data[2 * i + 1] = (byte) (temp >> 8);
    }
    // now save the file
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        AudioInputStream ais = new AudioInputStream(bais, format, samples.length);
        if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
            AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
        } else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
            AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
        } else {
            throw new IllegalArgumentException("unsupported audio format: '" + filename + "'");
        }
    } catch (IOException ioe) {
        throw new IllegalArgumentException("unable to save file '" + filename + "'", ioe);
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 47 with AudioFormat

use of javax.sound.sampled.AudioFormat in project blue by kunstmusik.

the class SoundFileUtilities method getDurationInSeconds.

public static float getDurationInSeconds(String soundFileName) throws IOException, UnsupportedAudioFileException {
    File soundFile = BlueSystem.findFile(soundFileName);
    AudioFileFormat aFormat = AudioSystem.getAudioFileFormat(soundFile);
    AudioFormat format = aFormat.getFormat();
    float duration = aFormat.getByteLength() / (format.getSampleRate() * (format.getSampleSizeInBits() / 8) * format.getChannels());
    return duration;
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 48 with AudioFormat

use of javax.sound.sampled.AudioFormat in project blue by kunstmusik.

the class SoundFileInformationPanel method setSoundFile.

public void setSoundFile(File soundFile) {
    try {
        AudioFileFormat aFormat = AudioSystem.getAudioFileFormat(soundFile);
        AudioFormat format = aFormat.getFormat();
        durationText.setText(getDuration(aFormat, format));
        formatTypeText.setText(aFormat.getType().toString());
        byteLengthText.setText(Integer.toString(aFormat.getByteLength()));
        encodingTypeText.setText(format.getEncoding().toString());
        sampleRateText.setText(Float.toString(format.getSampleRate()));
        sampleSizeInBitsText.setText(Integer.toString(format.getSampleSizeInBits()));
        channelsText.setText(Integer.toString(format.getChannels()));
        isBigEndianText.setText(getBooleanString(format.isBigEndian()));
        setFtableText(soundFile, aFormat.getByteLength());
        fTableText.copy();
    } catch (java.io.IOException ioe) {
        JOptionPane.showMessageDialog(null, BlueSystem.getString("soundfile.infoPanel.error.couldNotOpenFile") + " " + soundFile.getAbsolutePath());
        clearAudioInfo();
        return;
    } catch (javax.sound.sampled.UnsupportedAudioFileException uae) {
        JOptionPane.showMessageDialog(null, BlueSystem.getString("soundfile.infoPanel.error.unsupportedAudio") + " " + uae.getLocalizedMessage());
        clearAudioInfo();
        return;
    }
}
Also used : AudioFormat(javax.sound.sampled.AudioFormat) AudioFileFormat(javax.sound.sampled.AudioFileFormat)

Example 49 with AudioFormat

use of javax.sound.sampled.AudioFormat in project openmeetings by apache.

the class AudioTone method play.

public static void play() {
    byte[] buf = new byte[1];
    AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, false);
    try (SourceDataLine sdl = AudioSystem.getSourceDataLine(af)) {
        sdl.open(af);
        sdl.start();
        for (int i = 0; i < (int) SAMPLE_RATE; ++i) {
            double angle = i / (SAMPLE_RATE / 440) * 2.0 * Math.PI;
            buf[0] = (byte) (Math.sin(angle) * 128);
            sdl.write(buf, 0, 1);
        }
    } catch (LineUnavailableException e) {
    // no-op
    }
}
Also used : LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) AudioFormat(javax.sound.sampled.AudioFormat)

Example 50 with AudioFormat

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

the class DiscordUtils method getPCMStream.

/**
 * Converts an {@link AudioInputStream} to 48000Hz 16 bit stereo signed Big Endian PCM format.
 *
 * @param stream The original stream.
 * @return The PCM encoded stream.
 */
public static AudioInputStream getPCMStream(AudioInputStream stream) {
    AudioFormat baseFormat = stream.getFormat();
    // Converts first to PCM data. If the data is already PCM data, this will not change anything.
    AudioFormat toPCM = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), // AudioConnection.OPUS_SAMPLE_RATE,
    baseFormat.getSampleSizeInBits() != -1 ? baseFormat.getSampleSizeInBits() : 16, baseFormat.getChannels(), // If we are given a frame size, use it. Otherwise, assume 16 bits (2 8bit shorts) per channel.
    baseFormat.getFrameSize() != -1 ? baseFormat.getFrameSize() : 2 * baseFormat.getChannels(), baseFormat.getFrameRate() != -1 ? baseFormat.getFrameRate() : baseFormat.getSampleRate(), baseFormat.isBigEndian());
    AudioInputStream pcmStream = AudioSystem.getAudioInputStream(toPCM, stream);
    // Then resamples to a sample rate of 48000hz and ensures that data is Big Endian.
    AudioFormat audioFormat = new AudioFormat(toPCM.getEncoding(), OpusUtil.OPUS_SAMPLE_RATE, toPCM.getSampleSizeInBits(), toPCM.getChannels(), toPCM.getFrameSize(), toPCM.getFrameRate(), true);
    return AudioSystem.getAudioInputStream(audioFormat, pcmStream);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)112 AudioInputStream (javax.sound.sampled.AudioInputStream)43 IOException (java.io.IOException)24 DataLine (javax.sound.sampled.DataLine)21 SourceDataLine (javax.sound.sampled.SourceDataLine)21 AudioFileFormat (javax.sound.sampled.AudioFileFormat)18 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)18 LineUnavailableException (javax.sound.sampled.LineUnavailableException)17 File (java.io.File)15 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 TargetDataLine (javax.sound.sampled.TargetDataLine)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 Vector (java.util.Vector)5 SequenceInputStream (java.io.SequenceInputStream)4 Clip (javax.sound.sampled.Clip)4