Search in sources :

Example 56 with AudioFormat

use of javax.sound.sampled.AudioFormat in project competitive-programming by ttvi-cse.

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 57 with AudioFormat

use of javax.sound.sampled.AudioFormat in project aws-doc-sdk-examples by awsdocs.

the class Microphone method get.

public static TargetDataLine get() throws Exception {
    AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
    DataLine.Info datalineInfo = new DataLine.Info(TargetDataLine.class, format);
    TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(datalineInfo);
    dataLine.open(format);
    return dataLine;
}
Also used : TargetDataLine(javax.sound.sampled.TargetDataLine) DataLine(javax.sound.sampled.DataLine) AudioFormat(javax.sound.sampled.AudioFormat) TargetDataLine(javax.sound.sampled.TargetDataLine)

Example 58 with AudioFormat

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

the class ToneGenerator method writeWav.

/**
     * Writes the temporary file with the generated audio.
     *
     * @param inputStream input stream with the waveform
     * @param length      length of the waveform
     * @return name of the generated temporary file
     * @throws IOException
     */
private static String writeWav(InputStream inputStream, int length) throws IOException {
    AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, 8, 1, 1, SAMPLE_RATE, false);
    File file = File.createTempFile("wav", ".");
    AudioSystem.write(new AudioInputStream(inputStream, format, length), AudioFileFormat.Type.WAVE, file);
    return file.getAbsolutePath();
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 59 with AudioFormat

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

the class AudioReceiver method main.

// ******************************* MAIN *******************************
/** The main method. */
public static void main(String[] args) {
    int port = 0;
    int sample_rate = 8000;
    int sample_size = 1;
    boolean linear_signed = false;
    boolean pcmu = false;
    boolean big_endian = false;
    String filename = null;
    boolean sound = true;
    boolean help = true;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-h")) {
            break;
        }
        if (i == 0) {
            port = Integer.parseInt(args[i]);
            help = false;
            continue;
        }
        if (args[i].equals("-F") && args.length > (i + 1)) {
            sound = false;
            filename = args[++i];
            continue;
        }
        if (args[i].equals("-S") && args.length > (i + 2)) {
            sample_rate = Integer.parseInt(args[++i]);
            sample_size = Integer.parseInt(args[++i]);
            continue;
        }
        if (args[i].equals("-Z")) {
            linear_signed = true;
            continue;
        }
        if (args[i].equals("-U")) {
            pcmu = true;
            continue;
        }
        if (args[i].equals("-E")) {
            big_endian = true;
            continue;
        }
        // else, do:
        System.out.println("unrecognized param '" + args[i] + "'\n");
        help = true;
    }
    if (help) {
        System.out.println("usage:\n  java AudioReceiver <local_port> [options]");
        System.out.println("   options:");
        System.out.println("   -h               this help");
        System.out.println("   -F <audio_file>  records to audio file");
        System.out.println("   -S <rate> <size> sample rate [B/s], and size [B]");
        System.out.println("   -Z               uses PCM linear signed format (linear unsigned is used as default)");
        System.out.println("   -U               uses PCMU format");
        System.out.println("   -E               uses big endian format");
        System.exit(0);
    }
    AudioFormat.Encoding codec;
    if (pcmu)
        codec = AudioFormat.Encoding.ULAW;
    else if (linear_signed)
        codec = AudioFormat.Encoding.PCM_SIGNED;
    else
        codec = AudioFormat.Encoding.PCM_UNSIGNED;
    try {
        RtpStreamReceiver receiver;
        AudioOutput audio_output = null;
        if (sound)
            AudioOutput.initAudioLine();
        if (sound) {
            AudioFormat format = new AudioFormat(codec, sample_rate, 8 * sample_size, 1, sample_size, sample_rate, big_endian);
            audio_output = new AudioOutput(format);
            receiver = new RtpStreamReceiver(audio_output.getOuputStream(), port);
        } else //if (filename!=null)
        {
            File file = new File(filename);
            /*
            AudioFileFormat format=AudioSystem.getAudioFileFormat(file);
            System.out.println("File audio format: "+format);
            OutputStream output_stream=new OutputStream() { public void write(int b) {} };
            receiver=new RtpStreamReceiver(output_stream,port);
            */
            FileOutputStream output_stream = new FileOutputStream(file);
            receiver = new RtpStreamReceiver(output_stream, port);
        }
        receiver.start();
        if (sound)
            audio_output.play();
        System.out.println("Press 'Return' to stop");
        System.in.read();
        receiver.halt();
        if (sound)
            audio_output.stop();
        if (sound)
            AudioOutput.closeAudioLine();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 60 with AudioFormat

use of javax.sound.sampled.AudioFormat in project playn by threerings.

the class BigClip method open.

@Override
public void open(AudioInputStream stream) throws IOException, LineUnavailableException {
    AudioInputStream is1;
    format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
        is1 = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, stream);
    } else {
        is1 = stream;
    }
    format = is1.getFormat();
    InputStream is2 = is1;
    byte[] buf = new byte[1 << 16];
    int numRead = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    numRead = is2.read(buf);
    while (numRead > -1) {
        baos.write(buf, 0, numRead);
        numRead = is2.read(buf, 0, buf.length);
    }
    is2.close();
    audioData = baos.toByteArray();
    AudioFormat afTemp;
    if (format.getChannels() < 2) {
        int frameSize = format.getSampleSizeInBits() * 2 / 8;
        afTemp = new AudioFormat(format.getEncoding(), format.getSampleRate(), format.getSampleSizeInBits(), 2, frameSize, format.getFrameRate(), format.isBigEndian());
    } else {
        afTemp = format;
    }
    setLoopPoints(0, audioData.length);
    dataLine = AudioSystem.getSourceDataLine(afTemp);
    dataLine.open();
    inputStream = new ByteArrayInputStream(audioData);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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